private Topologic.Wire ByPolyCurve(PolyCurve ghPolyCurve) { Curve[] ghCurves = ghPolyCurve.Explode(); List <Topologic.Edge> edges = new List <Topologic.Edge>(); foreach (Curve ghCurve in ghCurves) { Topologic.Topology topology = ByCurve(ghCurve); Topologic.Edge edge = topology as Topologic.Edge; if (edge != null) { edges.Add(edge); continue; } Topologic.Wire wire = topology as Topologic.Wire; if (wire != null) { edges.AddRange(wire.Edges); continue; } } return(Topologic.Wire.ByEdges(edges)); }
public static Wire ToTopologic(this PolyCurve polyCurve) { Curve[] curves = polyCurve.Explode(); List <Edge> edges = new List <Edge>(); foreach (Curve curve in curves) { Topology topology = curve.ToTopologic(); Edge edge = topology as Edge; if (edge != null) { edges.Add(edge); continue; } Wire wire = topology as Wire; if (wire != null) { edges.AddRange(wire.Edges); continue; } } return(Wire.ByEdges(edges)); }
// Polycurve // Rh Capture/Gh Capture public static SpecklePolycurve ToSpeckle(this PolyCurve p) { SpecklePolycurve myPoly = new SpecklePolycurve(); p.RemoveNesting(); var segments = p.Explode(); myPoly.Segments = segments.Select(s => { return(s.ToSpeckle()); }).ToArray(); myPoly.Properties = p.UserDictionary.ToSpeckle(); myPoly.SetHashes(myPoly.Segments.Select(obj => obj.Hash).ToArray()); return(myPoly); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { GetPolyCurveObject go = new GetPolyCurveObject(); go.SetCommandPrompt("Select polycurve to explode"); go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve; go.SubObjectSelect = false; go.Get(); if (go.CommandResult() != Result.Success) { return(go.CommandResult()); } Curve curve = go.Object(0).Curve(); if (null == curve) { return(Result.Failure); } PolyCurve poly_curve = curve as PolyCurve; if (null == poly_curve) { return(Result.Failure); } Curve[] segments = poly_curve.Explode(); foreach (Curve segment in segments) { doc.Objects.AddCurve(segment); } doc.Objects.Delete(go.Object(0), false); doc.Views.Redraw(); return(Result.Success); }
// Blocks and groups // TODO // Proper explosion of polycurves: // (C) The Rutten David https://www.grasshopper3d.com/forum/topics/explode-closed-planar-curve-using-rhinocommon public static 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, recursive); } } else { foreach (Curve S in segments) { L.Add(S.DuplicateShallow() as Curve); } } 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; double t; int LN = L.Count; do { 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); }
// =============================================================================================== // splits curve at kinks (discontinuity) // =============================================================================================== public static bool CurveDiscontinuity(List <Curve> L, Curve crv, int continuity, 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) { return(CurveDiscontinuity(L, S, continuity, recursive)); } } 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()); } 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; double t; int LN = L.Count; do { if (!nurbs.GetNextDiscontinuity((Continuity)continuity, 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); }
internal static IfcIndexedPolyCurve Convert(DatabaseIfc db, PolyCurve polycurve, bool twoD) { double tol = db.Tolerance; Curve[] segments = polycurve.Explode(); PolyCurve pc = new PolyCurve(); foreach (Curve s in segments) { if (s.IsLinear(tol)) { pc.Append(new Line(s.PointAtStart, s.PointAtEnd)); } else { Arc a = Arc.Unset; if (s.TryGetArc(out a, tol)) { pc.Append(a); } else { return(null); } } } List <IfcSegmentIndexSelect> segs = new List <IfcSegmentIndexSelect>(); IfcCartesianPointList cpl = null; bool closed = pc.PointAtStart.DistanceTo(pc.PointAtEnd) < tol; if (twoD) { Point2d pt = new Point2d(pc.PointAtStart.X, pc.PointAtStart.Y); int pcounter = 1; List <Point2d> pts = new List <Point2d>(); pts.Add(pt); IfcLineIndex li = null; for (int icounter = 0; icounter < pc.SegmentCount; icounter++) { Curve c = pc.SegmentCurve(icounter); if (c.IsLinear(tol) && c.PointAtStart.DistanceTo(c.PointAtEnd) < tol) { continue; } if (c.IsLinear(tol)) { if (closed && icounter + 1 == segments.Length) { if (li != null) { li.mIndices.Add(1); } else { li = new IfcLineIndex(pcounter, 1); } } else { pts.Add(new Point2d(c.PointAtEnd.X, c.PointAtEnd.Y)); if (li != null) { li.mIndices.Add(++pcounter); } else { li = new IfcLineIndex(pcounter++, pcounter); } } } else { if (li != null) { segs.Add(li); li = null; } Point3d tp = c.PointAt(c.Domain.Mid); pts.Add(new Point2d(tp.X, tp.Y)); if (closed && icounter + 1 == segments.Length) { segs.Add(new IfcArcIndex(pcounter++, pcounter++, 1)); } else { pts.Add(new Point2d(c.PointAtEnd.X, c.PointAtEnd.Y)); segs.Add(new IfcArcIndex(pcounter++, pcounter++, pcounter)); } } } if (li != null) { segs.Add(li); } cpl = new IfcCartesianPointList2D(db, pts.ToArray()); } else { Point3d pt = pc.PointAtStart; int pcounter = 1; List <Point3d> pts = new List <Point3d>(); pts.Add(pt); List <IfcSegmentIndexSelect> sis = new List <IfcSegmentIndexSelect>(segments.Length); IfcLineIndex li = null; for (int icounter = 0; icounter < pc.SegmentCount; icounter++) { Curve c = pc.SegmentCurve(icounter); if (c.IsLinear(tol) && c.PointAtStart.DistanceTo(c.PointAtEnd) < tol) { continue; } if (c.IsLinear(tol)) { if (closed && icounter + 1 == segments.Length) { if (li != null) { li.mIndices.Add(0); } else { li = new IfcLineIndex(pcounter, 0); } } else { pts.Add(c.PointAtEnd); if (li != null) { li.mIndices.Add(++pcounter); } else { li = new IfcLineIndex(pcounter++, pcounter); } } } else { if (li != null) { segs.Add(li); li = null; } pts.Add(c.PointAt(c.Domain.Mid)); if (closed && icounter + 1 == segments.Length) { segs.Add(new IfcArcIndex(pcounter++, pcounter, 0)); } else { pts.Add(c.PointAtEnd); segs.Add(new IfcArcIndex(pcounter++, pcounter++, pcounter)); } } } if (li != null) { segs.Add(li); } cpl = new IfcCartesianPointList3D(db, pts.ToArray()); } return(new IfcIndexedPolyCurve(cpl, segs) { }); }