Exemple #1
0
 internal LineConverter(PointConverter ptConv) :
     base(
         (ln) =>
 {
     return(new ppc.Line(ptConv.ToPipe <dg.Point, ppg.Vec>(ln.StartPoint),
                         ptConv.ToPipe <dg.Point, ppg.Vec>(ln.EndPoint)));
 },
         (pln) =>
 {
     return(dg.Line.ByStartPointEndPoint(ptConv.FromPipe <dg.Point, ppg.Vec>(pln.StartPoint),
                                         ptConv.FromPipe <dg.Point, ppg.Vec>(pln.EndPoint)));
 }
         )
 { }
Exemple #2
0
 internal ArcConverter(PointConverter ptConv, VectorConverter vecConv) :
     base(
         (dgarc) => {
     ppg.Plane pl = new ppg.Plane(ptConv.ToPipe <dg.Point, ppg.Vec>(dgarc.CenterPoint));
     return(new ppc.Arc(pl, dgarc.Radius, dgarc.StartAngle, dgarc.StartAngle + dgarc.SweepAngle));
 },
         (pparc) => {
     pparc.TransformToPlane(new ppg.Plane(pparc.Plane.Origin, pparc.Plane.Z));
     return(dg.Arc.ByCenterPointRadiusAngle(ptConv.FromPipe <dg.Point, ppg.Vec>(pparc.Plane.Origin), pparc.Radius,
                                            PipeDataUtil.RadiansToDegrees(pparc.StartAngle), PipeDataUtil.RadiansToDegrees(pparc.EndAngle),
                                            vecConv.FromPipe <dg.Vector, ppg.Vec>(pparc.Plane.Z)));
 }
         )
 { }
Exemple #3
0
        internal GeometryConverter() :
            base()
        {
            ptConv = new PointConverter();
            AddConverter(ptConv);
            var vecConv = new VectorConverter();
            var curConv = new CurveConverter(ptConv, vecConv);

            AddConverter(curConv);
            var surfConv = new SurfaceConverter(vecConv, ptConv, curConv);

            AddConverter(surfConv);

            //solids - only one way mapping
            AddConverter(new PipeConverter <dg.Solid, pps.PolySurface>(
                             (ds) => {
                List <List <int> > adjacency = new List <List <int> >();
                var faces = ds.Faces.Select((f) => {
                    var surf = (pps.NurbsSurface)surfConv.ToPipe <dg.Surface, pps.Surface>(f.SurfaceGeometry().ToNurbsSurface());
                    surf.OuterTrims.Clear();
                    try
                    {
                        var closedTrim = dg.PolyCurve.ByJoinedCurves(f.Edges.Select((e) => e.CurveGeometry));
                        surf.OuterTrims.Add(curConv.ToPipe <dg.Curve, ppc.Curve>(closedTrim));
                    }
                    catch (Exception e)
                    {
                        //do nothing
                        //surf.OuterTrims.AddRange(ds.Edges.Select((edge) => curConv.ToPipe<dg.Curve, ppc.Curve>(edge.CurveGeometry)));
                    }
                    adjacency.Add(f.Edges.SelectMany((e) => e.AdjacentFaces.ToList()).Distinct().Select((af) =>
                                                                                                        ds.Faces.ToList().IndexOf(af)).ToList());
                    return((pps.Surface)surf);
                }).ToList();
                return(new pps.PolySurface(faces, adjacency));
            },
                             null// null because of oneway mapping
                             ));
        }
Exemple #4
0
        internal SurfaceConverter(VectorConverter vecConv, PointConverter ptConv, CurveConverter curveConv)
        {
            //NURBS surface

            /*
             * Nurbs Surfaces from the pipe cannot be converted to dynamo nurbs because they could have trims.
             * In Dynamo trimmed surfaces are instances of the Surface class. So it has to be another converter.
             * This converter has to be a one way mapping, hence one of the conversion delegates is null
             */
            var nurbsConv = new PipeConverter <dg.NurbsSurface, pps.NurbsSurface>(
                (dns) => {
                var nurbs          = new pps.NurbsSurface(dns.NumControlPointsU, dns.NumControlPointsV, dns.DegreeU, dns.DegreeV);
                dg.Point[][] pts   = dns.ControlPoints();
                double[][] weights = dns.Weights();

                for (int u = 0; u < dns.NumControlPointsU; u++)
                {
                    for (int v = 0; v < dns.NumControlPointsV; v++)
                    {
                        nurbs.SetControlPoint(ptConv.ToPipe <dg.Point, pp.Vec>(pts[u][v]), u, v);
                        nurbs.SetWeight(weights[u][v], u, v);
                    }
                }

                nurbs.UKnots        = dns.UKnots().ToList();
                nurbs.VKnots        = dns.VKnots().ToList();
                nurbs.SurfaceNormal = vecConv.ToPipe <dg.Vector, pp.Vec>(dns.NormalAtParameter(0.5, 0.5));

                return(nurbs);
            },
                null
                );

            AddConverter(nurbsConv);

            /*
             * ThePipe Extrusions can be mapped to surfaces in dynamo but all surfaces should not be mapped to extrusions.
             * So this mapping has to be one way, hence the first conversion delegate is null.
             */
            AddConverter(new PipeConverter <dg.Surface, pps.Extrusion>(
                             null, //null because of one way mapping
                             (pe) => {
                var extrVec = pp.Vec.Multiply(pe.Direction, pe.Height);
                var path    = curveConv.FromPipe <dg.Curve, ppc.Curve>(new ppc.Line(pe.ProfileCurve.StartPoint,
                                                                                    pp.Vec.Sum(pe.ProfileCurve.StartPoint, extrVec)));
                var profile = curveConv.FromPipe <dg.Curve, ppc.Curve>(pe.ProfileCurve);

                var extr = dg.Surface.BySweep(profile, path);
                if (!profile.IsClosed)
                {
                    var cutPt    = profile.PointAtSegmentLength(1e-4);
                    profile      = profile.TrimByParameter(0, profile.ParameterAtPoint(cutPt));
                    var profile2 = dg.PolyCurve.ByJoinedCurves(new List <dg.Curve>()
                    {
                        profile
                    }).CloseWithLine();
                    if (!profile2.IsClosed)
                    {
                        return(extr);
                    }
                    profile = profile2;
                }

                try
                {
                    var cap1 = dg.Surface.ByPatch(profile);
                    var cap2 = dg.Surface.ByPatch((dg.Curve)profile.Translate(vecConv.FromPipe <dg.Vector, pp.Vec>(extrVec)));
                    if (pe.CappedAtStart)
                    {
                        extr = dg.PolySurface.ByJoinedSurfaces(new List <dg.Surface>()
                        {
                            extr, cap1
                        });
                    }
                    if (pe.CappedAtEnd)
                    {
                        extr = dg.PolySurface.ByJoinedSurfaces(new List <dg.Surface>()
                        {
                            extr, cap2
                        });
                    }
                }
                catch (Exception e)
                {
                    //do nothing
                }

                if (extr.NormalAtParameter(0.5, 0.5).Dot(vecConv.FromPipe <dg.Vector, pp.Vec>(pe.SurfaceNormal)) < 0)
                {
                    extr.FlipNormalDirection();
                }

                return(extr);
            }
                             ));

            /*
             * Surface is not an abstract class in dynamo, so there needs to be concrete conversion logic for them.
             * They are being mapped to Pipe's NurbsSurface class.
             */
            AddConverter(new PipeConverter <dg.Surface, pps.NurbsSurface>(
                             (ds) => {
                var nurbs = nurbsConv.ToPipe <dg.NurbsSurface, pps.NurbsSurface>(ds.ToNurbsSurface());
                nurbs.OuterTrims.Clear();
                try
                {
                    var closedTrim = dg.PolyCurve.ByJoinedCurves(ds.Edges.Select((e) => e.CurveGeometry));
                    nurbs.OuterTrims.Add(curveConv.ToPipe <dg.Curve, ppc.Curve>(closedTrim));
                }
                catch (Exception e)
                {
                    //do nothing
                    //nurbs.OuterTrims.AddRange(ds.Edges.Select((edge) => curveConv.ToPipe<dg.Curve, ppc.Curve>(edge.CurveGeometry)));
                }
                nurbs.SurfaceNormal = vecConv.ToPipe <dg.Vector, pp.Vec>(ds.NormalAtParameter(0.5, 0.5));
                return(nurbs);
            },
                             (pns) => {
                if (pns.IsClosedInU || pns.IsClosedInV)
                {
                    throw new PipeDataModel.Exceptions.PipeConversionException(pns.GetType(), typeof(dg.Surface), "Closed Nurbs surfaces are not " +
                                                                               "supported in dynamo, please try converting this geometry to split-open surfaces or meshes before sending it through the pipe.");
                }

                List <List <dg.Point> > pts   = new List <List <dg.Point> >();
                List <List <double> > weights = new List <List <double> >();
                for (int u = 0; u < pns.UCount; u++)
                {
                    List <dg.Point> ptRow = new List <dg.Point>();
                    List <double> wRow    = new List <double>();
                    for (int v = 0; v < pns.VCount; v++)
                    {
                        ptRow.Add(ptConv.FromPipe <dg.Point, pp.Vec>(pns.GetControlPointAt(u, v)));
                        wRow.Add(pns.GetWeightAt(u, v));
                    }
                    pts.Add(ptRow);
                    weights.Add(wRow);
                }

                dg.Surface nurbs;
                try
                {
                    nurbs = dg.NurbsSurface.ByControlPointsWeightsKnots(pts.Select((r) => r.ToArray()).ToArray(),
                                                                        weights.Select((r) => r.ToArray()).ToArray(), pns.UKnots.ToArray(), pns.VKnots.ToArray(), pns.UDegree, pns.VDegree);
                }
                catch (Exception e)
                {
                    nurbs = dg.NurbsSurface.ByControlPoints(pts.Select((r) => r.ToArray()).ToArray(), pns.UDegree, pns.VDegree);
                }

                if (pns.OuterTrims.Count > 0)
                {
                    var trims = pns.OuterTrims.Select((t) =>
                                                      ((dg.PolyCurve)curveConv.FromPipe <dg.Curve, ppc.Curve>(t.AsPolyCurve()))?.CloseWithLine()).ToList();
                    try
                    {
                        nurbs = nurbs.TrimWithEdgeLoops(trims);
                    }
                    catch (Exception e)
                    {
                        //do nothing
                    }
                }

                if (nurbs.NormalAtParameter(0.5, 0.5).Dot(vecConv.FromPipe <dg.Vector, pp.Vec>(pns.SurfaceNormal)) < 0)
                {
                    nurbs.FlipNormalDirection();
                }
                return(nurbs);
            }
                             ));

            //Polysurfaces
            AddConverter(new PipeConverter <dg.PolySurface, pps.PolySurface>(
                             (dps) => {
                List <List <int> > adjacency = new List <List <int> >();
                var faces = dps.Faces.Select((f) => {
                    var dgSurf = f.SurfaceGeometry().ToNurbsSurface();
                    var surf   = nurbsConv.ToPipe <dg.NurbsSurface, pps.NurbsSurface>(dgSurf);
                    // add edges as trim curves
                    surf.OuterTrims.Clear();
                    try
                    {
                        var closedTrim = dg.PolyCurve.ByJoinedCurves(f.Edges.Select((e) => e.CurveGeometry));
                        surf.OuterTrims.Add(curveConv.ToPipe <dg.Curve, ppc.Curve>(closedTrim));
                    }
                    catch (Exception e)
                    {
                        //do nothing
                        //surf.OuterTrims.AddRange(f.Edges.Select((edge) => curveConv.ToPipe<dg.Curve, ppc.Curve>(edge.CurveGeometry)));
                    }
                    adjacency.Add(f.Edges.SelectMany((e) => e.AdjacentFaces.ToList()).Distinct().Select((af) =>
                                                                                                        dps.Faces.ToList().IndexOf(af)).ToList());
                    return((pps.Surface)surf);
                }).ToList();
                var polySurf = new pps.PolySurface(faces, adjacency);
                return(polySurf);
            },
                             (ps) => {
                return(dg.PolySurface.ByJoinedSurfaces(ps.Surfaces.Select((s) => {
                    var surf = FromPipe <dg.Surface, pps.Surface>(s);
                    //if (typeof(pps.NurbsSurface).IsAssignableFrom(s.GetType())
                    //    && ((pps.NurbsSurface)s).OuterTrims.Count > 0)
                    //{
                    //    surf = surf.TrimWithEdgeLoops(((pps.NurbsSurface)s).OuterTrims.Select((c) =>
                    //        (dg.PolyCurve)curveConv.FromPipe<dg.Curve, ppc.Curve>(c.AsPolyCurve())));
                    //}
                    return surf;
                })));
            }
                             ));
        }
Exemple #5
0
        internal CurveConverter(PointConverter ptConv, VectorConverter vecConv)
        {
            var lineConv = new LineConverter(ptConv);

            AddConverter(lineConv);
            var arcConv = new ArcConverter(ptConv, vecConv);

            AddConverter(arcConv);

            //to convert nurbs curves
            var nurbsConv = new PipeConverter <dg.NurbsCurve, ppc.NurbsCurve>(
                (dc) => {
                ////just to smooth out anything weird about this curve
                //dc = dc.ToNurbsCurve();

                ppc.NurbsCurve cur;
                List <dg.Point> pts = dc.ControlPoints().ToList();
                List <double> knots = dc.Knots().ToList();

                var startParam = dc.StartParameter();
                var endParam   = dc.EndParameter();
                knots          = knots.Select((k) => (k - startParam) / (endParam - startParam)).ToList();
                //making sure all the weights are not zeros by setting them to 1 if they are
                double tolerance      = 1e-4;
                List <double> weights = dc.Weights().ToList();
                if (weights.Any((w) => w <= tolerance))
                {
                    weights = weights.Select((w) => 1.0).ToList();
                }
                cur = new ppc.NurbsCurve(pts.Select((pt) => ptConv.ToPipe <dg.Point, ppg.Vec>(pt)).ToList(),
                                         dc.Degree, weights, knots, dc.IsClosed);

                return(cur);
            },
                (pnc) => {
                dg.NurbsCurve cur;
                try
                {
                    cur = dg.NurbsCurve.ByControlPointsWeightsKnots(
                        pnc.ControlPoints.Select((pt) => ptConv.FromPipe <dg.Point, ppg.Vec>(pt)),
                        pnc.Weights.ToArray(), pnc.Knots.ToArray(), pnc.Degree);
                    if (pnc.IsClosed != cur.IsClosed)
                    {
                        cur = dg.NurbsCurve.ByControlPoints(
                            pnc.ControlPoints.Select((pt) => ptConv.FromPipe <dg.Point, ppg.Vec>(pt)), pnc.Degree,
                            pnc.IsClosed);
                    }
                }
                catch (Exception e)
                {
                    cur = dg.NurbsCurve.ByControlPoints(
                        pnc.ControlPoints.Select((pt) => ptConv.FromPipe <dg.Point, ppg.Vec>(pt)), pnc.Degree,
                        pnc.IsClosed);
                }
                return(cur);
            }
                );

            AddConverter(nurbsConv);

            //generic curves - one way conversion, hence one of the conversion delegates is null
            AddConverter(new PipeConverter <dg.Curve, ppc.Curve>(
                             (dc) => {
                return(nurbsConv.ToPipe <dg.NurbsCurve, ppc.NurbsCurve>(dc.ToNurbsCurve()));
            },
                             null
                             ));

            //conversion for polycurves
            var polyCurveConv = new PipeConverter <dg.PolyCurve, ppc.PolyCurve>(
                (dpc) => {
                dg.Curve[] curs = dpc.Curves();
                return(new ppc.PolyCurve(curs.Select((c) => ToPipe <dg.Curve, ppc.Curve>(c)).ToList()));
            },
                (ppcrv) => {
                try
                {
                    List <dg.Curve> curves = ppcrv.Segments.Select((c) => FromPipe <dg.Curve, ppc.Curve>(c)).ToList();
                    return(dg.PolyCurve.ByJoinedCurves(curves));
                }
                catch (Exception e)
                {
                    return(null);
                }
            }
                );

            AddConverter(polyCurveConv);
            //one way conversion for incoming polylines
            var plineConv = new PipeConverter <dg.PolyCurve, ppc.Polyline>(
                null,
                (ppl) => {
                return(polyCurveConv.FromPipe <dg.PolyCurve, ppc.PolyCurve>(ppl.AsPolyCurve()));
            }
                );
        }