Example #1
0
        public SilkwormSegment(Curve curve, int mainSegmentCount, int subSegmentCount, double maxAngleRadians, double maxChordLengthRatio, double maxAspectRatio, double tolerance, double minEdgeLength, double maxEdgeLength, bool keepStartPoint)
        {
            if (curve != null)
            {
                //See if curve can be represented as a polyline already
                if (curve.TryGetPolyline(out Pline))
                {
                    PolylineCurve plinecurve = new PolylineCurve(Pline);
                    //Segments = plinecurve.DuplicateSegments().ToList();
                    Segments = DuplicateSegments(plinecurve);
                }

                //Try to see if conversion will work
                if (curve.ToPolyline(mainSegmentCount, subSegmentCount, maxAngleRadians, maxChordLengthRatio, maxAspectRatio, tolerance, minEdgeLength, maxEdgeLength, keepStartPoint) != null)
                {
                    //Convert
                    PolylineCurve plinec = curve.ToPolyline(mainSegmentCount, subSegmentCount, maxAngleRadians, maxChordLengthRatio, maxAspectRatio, tolerance, minEdgeLength, maxEdgeLength, keepStartPoint);

                    if (plinec.TryGetPolyline(out Pline))
                    {
                        PolylineCurve plinecurve = new PolylineCurve(Pline);
                        //Segments = plinecurve.DuplicateSegments().ToList();
                        Segments = DuplicateSegments(plinecurve);
                    }
                }

            }
        }
Example #2
0
        public SilkwormSegment(Curve curve)
        {
            if (curve != null)
                {
                    //See if curve can be represented as a polyline already
                    if (curve.TryGetPolyline(out Pline))
                    {
                        PolylineCurve plinecurve = new PolylineCurve(Pline);
                        //Segments = plinecurve.DuplicateSegments().ToList();
                        Segments = DuplicateSegments(plinecurve);
                    }

                    //Try to see if conversion will work
                    if (curve.ToPolyline(0, 0, 0.05, 0.1, 0, 0, 0.1, 0, true) != null)
                    {
                    //Convert
                       PolylineCurve plinec = curve.ToPolyline(0, 0, 0.05, 0.1, 0, 0, 0.1, 0, true);

                    if (plinec.TryGetPolyline(out Pline))

                    {
                        PolylineCurve plinecurve = new PolylineCurve(Pline);
                        //Segments = plinecurve.DuplicateSegments().ToList();
                        Segments = DuplicateSegments(plinecurve);

                    }
                    }

            }
        }
Example #3
0
        private void SetCurveType(Rg.Curve curve)
        {
            Rg.Circle   R = new Rg.Circle();
            Rg.Arc      A = new Rg.Arc();
            Rg.Ellipse  S = new Rg.Ellipse();
            Rg.Polyline P = new Rg.Polyline();

            if (curve.TryGetCircle(out R))
            {
                curveType = CurveTypes.Circle;
            }
            else if (curve.TryGetArc(out A))
            {
                curveType = CurveTypes.Arc;
            }
            else if (curve.TryGetEllipse(out S))
            {
                curveType = CurveTypes.Ellipse;
            }
            else if (curve.IsLinear())
            {
                curveType = CurveTypes.Line;
            }
            else if (curve.TryGetPolyline(out P))
            {
                curveType = CurveTypes.Polyline;
            }
            else
            {
                curveType = CurveTypes.Spline;
            }
        }
Example #4
0
        /***************************************************/

        public static BHG.ICurve FromRhino(this RHG.Curve rCurve)
        {
            if (rCurve == null)
            {
                return(null);
            }

            Type curveType = rCurve.GetType();

            if (rCurve.IsLinear() && rCurve.SpanCount < 2)
            {
                return(new BHG.Line {
                    Start = rCurve.PointAtStart.FromRhino(), End = rCurve.PointAtEnd.FromRhino(), Infinite = false
                });
            }
            if (rCurve.IsCircle())
            {
                RHG.Circle circle = new RHG.Circle();
                rCurve.TryGetCircle(out circle);
                return(circle.FromRhino());
            }
            else if (rCurve.IsArc() || typeof(RHG.ArcCurve).IsAssignableFrom(curveType))
            {
                RHG.Arc arc = new RHG.Arc();
                rCurve.TryGetArc(out arc);
                return(arc.FromRhino());
            }
            else if (rCurve.IsPolyline() || typeof(RHG.PolylineCurve).IsAssignableFrom(curveType))
            {
                RHG.Polyline polyline = new RHG.Polyline();
                rCurve.TryGetPolyline(out polyline);
                return(polyline.FromRhino());
            }
            else if (rCurve.IsClosed && rCurve.IsEllipse())
            {
                RHG.Ellipse ellipse = new RHG.Ellipse();
                rCurve.TryGetEllipse(out ellipse);
                return(ellipse.FromRhino());
            }
            else if (rCurve is RHG.NurbsCurve)
            {
                return(((RHG.NurbsCurve)rCurve).FromRhino());
            }
            else if (rCurve is RHG.PolyCurve)
            {
                return(((RHG.PolyCurve)rCurve).FromRhino());  //The test of IsPolyline above is very important to make sure we can cast to PolyCurve here
            }
            else
            {
                return((rCurve.ToNurbsCurve()).FromRhino());
            }
        }
Example #5
0
        public static TurtleMesh ExtractTMesh(Curve c)
        {
            var m = new TurtleMesh();
            Polyline pl;
            c.TryGetPolyline(out pl);

            TurtleFace f = new TurtleFace(pl.Count - 1);

            for (int j = 0; j < pl.Count - 1; j++)
            {
                var v = pl[j];
                f.Add(m.VertexCount);
                m.AddVertex(new TurtleVertex((float)v.X, (float)v.Y, (float)v.Z));
            }
            m.AddFace(f);
            return m;
        }
Example #6
0
            public static Polyline2D FromCurve(Rhino.Geometry.Curve curve, Plane plane)
            {
                Polyline polyline;

                if (!curve.TryGetPolyline(out polyline))
                {
                    return(null);
                }

                var points = polyline.Select(o => o.Map2D(plane));

                if (curve.ClosedCurveOrientation(plane.ZAxis) == CurveOrientation.Clockwise)
                {
                    points = points.Reverse();
                }

                var result = new Polyline2D(points)
                {
                    Closed = curve.IsClosed
                };

                return(result);
            }
Example #7
0
 /// <summary>
 /// Convert a Rhino curve to a Nucleus one
 /// </summary>
 /// <param name="curve"></param>
 /// <returns></returns>
 public static Curve Convert(RC.Curve curve)
 {
     if (curve == null)
     {
         return(null);
     }
     if (curve is RC.LineCurve)
     {
         return(Convert((RC.LineCurve)curve));
     }
     else if (curve.IsLinear())
     {
         return(Convert(new RC.Line(curve.PointAtStart, curve.PointAtEnd)));
     }
     else if (curve.IsPolyline())
     {
         RC.Polyline pLine;
         if (curve.TryGetPolyline(out pLine))
         {
             return(Convert(pLine));
         }
     }
     else if (curve is RC.PolyCurve)
     {
         return(Convert((RC.PolyCurve)curve));
     }
     else if (curve.IsArc())
     {
         RC.Arc arc;
         if (curve.TryGetArc(out arc))
         {
             return(Convert(arc));
         }
     }
     throw new NotImplementedException();
 }
Example #8
0
 protected bool CurveSegments(List<Curve> L, Curve crv, bool recursive)
 {
     if (crv == null) { return false; }
     PolyCurve polycurve = crv as PolyCurve;
     if (polycurve != null)
     {
         if (recursive) { polycurve.RemoveNesting(); }
         Curve[] segments = polycurve.Explode();
         if (segments == null) { return false; }
         if (segments.Length == 0) { return false; }
         if (recursive)
         {
             foreach (Curve S in segments)
             {
                 CurveSegments(L, S, true);
             }
         }
         else
         {
             foreach (Curve S in segments)
             {
                 L.Add(S.DuplicateShallow() as Curve);
             }
         }
         return true;
     }
     PolylineCurve polyline = crv as PolylineCurve;
     if (polyline != null)
     {
         if (recursive)
         {
             for (int i = 0; i < (polyline.PointCount - 1); i++)
             {
                 L.Add(new LineCurve(polyline.Point(i), polyline.Point(i + 1)));
             }
         }
         else
         {
             L.Add(polyline.DuplicateCurve());
         }
         return true;
     }
     Polyline p;
     if (crv.TryGetPolyline(out p))
     {
         if (recursive)
         {
             for (int i = 0; i < (p.Count - 1); i++)
             {
                 L.Add(new LineCurve(p[i], p[i + 1]));
             }
         }
         else
         {
             L.Add(new PolylineCurve(p));
         }
         return true;
     }
     //Maybe it's a LineCurve?
     LineCurve line = crv as LineCurve;
     if (line != null)
     {
         L.Add(line.DuplicateCurve());
         return true;
     }
     //It might still be an ArcCurve...
     ArcCurve arc = crv as ArcCurve;
     if (arc != null)
     {
         L.Add(arc.DuplicateCurve());
         return true;
     }
     //Nothing else worked, lets assume it's a nurbs curve and go from there...
     NurbsCurve nurbs = crv.ToNurbsCurve();
     if (nurbs == null) { return false; }
     double t0 = nurbs.Domain.Min;
     double t1 = nurbs.Domain.Max;
     int LN = L.Count;
     do
     {
       double t;
       if (!nurbs.GetNextDiscontinuity(Continuity.C1_locus_continuous, t0, t1, out t)) { break; }
         Interval trim = new Interval(t0, t);
         if (trim.Length < 1e-10)
         {
             t0 = t;
             continue;
         }
         Curve M = nurbs.DuplicateCurve();
         M = M.Trim(trim);
         if (M.IsValid) { L.Add(M); }
         t0 = t;
     } while (true);
     if (L.Count == LN) { L.Add(nurbs); }
     return true;
 }
Example #9
0
        private void computeContourCurve()
        {
            if (targetPRhObjID == Guid.Empty)
            {
                return;
            }
            ObjRef targetpObjRef = new ObjRef(targetPRhObjID);

            snapPointsList = new List <Point3d>();
            Double tolerance = 0;

            if (drawnType != DrawnType.In3D)
            {
                Brep targetBrep = (Brep)(targetpObjRef.Object().Geometry);
                //TODO- topLeftP won't be on the face in the 3D case. so probably use orgin
                float minD     = 1000000f;
                int   minIndex = -1;
                for (int i = 0; i < targetBrep.Faces.Count; i++)
                {
                    //cast BrepFace to Brep for ClosestPoint(P) menthod
                    double dist = targetBrep.Faces[i].DuplicateFace(false).ClosestPoint(projectP).DistanceTo(projectP);
                    if (dist < minD)
                    {
                        minD     = (float)dist;
                        minIndex = i;
                    }
                }

                Surface s = targetBrep.Faces[minIndex];
                //surface might not be a perfect planar surface
                while (tolerance < toleranceMax)
                {
                    if (s.TryGetPlane(out curvePlane, tolerance))
                    {
                        break;
                    }
                    tolerance++;
                }

                if (tolerance >= toleranceMax)
                {
                    double u, v;
                    if (s.ClosestPoint(projectP, out u, out v))
                    {
                        Rhino.Geometry.Vector3d normal = s.NormalAt(u, v);
                        //TODO fix the issue when normal is inward
                        curvePlane = new Plane(projectP, normal);
                    }
                }
                else
                {
                    //testing finding the edge curve
                    Rhino.Geometry.Curve[] edgeCurves = (targetBrep.Faces[minIndex].DuplicateFace(false)).DuplicateEdgeCurves(true);
                    double tol = mScene.rhinoDoc.ModelAbsoluteTolerance * 2.1;
                    edgeCurves = Rhino.Geometry.Curve.JoinCurves(edgeCurves, tol);

                    // TODO: Check if null
                    contourCurve = edgeCurves[0];

                    //detect whether it's rect or circle then generate a snap pointList
                    Circle circle;
                    Rhino.Geometry.Polyline polyline;
                    if (contourCurve.TryGetCircle(out circle))
                    {
                        snapPointsList.Add(circle.Center);
                        snapPointsList.Add(circle.PointAt(0));
                        snapPointsList.Add(circle.PointAt(Math.PI / 2));
                        snapPointsList.Add(circle.PointAt(Math.PI));
                        snapPointsList.Add(circle.PointAt(Math.PI * 1.5));
                    }
                    else if (contourCurve.TryGetPolyline(out polyline))
                    {
                        Rectangle3d rect = Rectangle3d.CreateFromPolyline(polyline);
                        snapPointsList.Add(rect.Center);
                        snapPointsList.Add(rect.Corner(0));
                        snapPointsList.Add(rect.Corner(1));
                        snapPointsList.Add(rect.Corner(2));
                        snapPointsList.Add(rect.Corner(3));
                    }
                    else
                    {
                        double u = 0;
                        double v = 0;
                        s.ClosestPoint(s.GetBoundingBox(true).Center, out u, out v);
                        snapPointsList.Add(s.PointAt(u, v));
                    }
                }
            }
        }