// Rh Capture public static SpeckleObject ToSpeckle(this PolylineCurve poly) { Polyline polyline; if (poly.TryGetPolyline(out polyline)) { if (polyline.Count == 2) { return(new SpeckleLine(polyline.ToFlatArray(), null, poly.UserDictionary.ToSpeckle(root: poly))); } var myPoly = new SpecklePolyline(polyline.ToFlatArray()); myPoly.Closed = polyline.IsClosed; if (myPoly.Closed) { myPoly.Value.RemoveRange(myPoly.Value.Count - 3, 3); } myPoly.Domain = poly.Domain.ToSpeckle(); myPoly.Properties = poly.UserDictionary.ToSpeckle(root: poly); myPoly.GenerateHash(); return(myPoly); } return(null); }
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); } } } }
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); } } } }
// Rh Capture public Base PolylineToSpeckle(PolylineCurve poly) { RH.Polyline polyline; if (poly.TryGetPolyline(out polyline)) { if (polyline.Count == 2) { return(new Line(PointsToFlatArray(polyline), ModelUnits)); } var myPoly = new Polyline(PointsToFlatArray(polyline), ModelUnits); myPoly.closed = polyline.IsClosed; if (myPoly.closed) { myPoly.value.RemoveRange(myPoly.value.Count - 3, 3); } myPoly.domain = IntervalToSpeckle(poly.Domain); return(myPoly); } return(null); }
/// <summary> /// Creates a Geometry object from a List of Rhino Curves. /// </summary> /// <param name="c">The c.</param> /// <param name="scale">The scale.</param> /// <param name="rebox">if set to <c>true</c> [rebox].</param> /// <returns></returns> public static Geometry pathGeomFromCrvs(List <Curve> c, double scale, bool rebox) { string crvString = ""; //adapt curve location/positioning rebaseGeometry(c.OfType <GeometryBase>(), scale, rebox); //for all the curves foreach (Curve crv in c) { //try to convert to a polylineCurve and then get the polyline from that PolylineCurve p = new PolylineCurve(); Rhino.Geometry.Polyline pl = new Rhino.Geometry.Polyline(); if (!crv.TryGetPolyline(out pl)) { p = crv.ToPolyline(0, 0, 0.1, 2.0, 0, 0, crv.GetLength() / 50, 0, true); p.TryGetPolyline(out pl); } crvString += "M "; //Start structuring the notation syntax into a string - M starts a shape foreach (Point3d pt in pl) //for all the points in the polyline { // Add each vertex of the polyline. Closed polylines naturally have duplicate vertices at beginning and end // so no special accounting is necessary. crvString += String.Format("{0:0.000},{1:0.000} ", pt.X, pt.Y); } } return(Geometry.Parse(crvString)); //parses the curve string into a geometry object }
// Rh Capture public static SpecklePolyline ToSpeckle(this PolylineCurve poly) { Polyline polyline; if (poly.TryGetPolyline(out polyline)) { var myPoly = new SpecklePolyline(polyline.ToFlatArray()); myPoly.Properties = poly.UserDictionary.ToSpeckle(); return(myPoly); } return(null); }
public override bool CastFrom(object source) { if (source is GH_tasPath) { Value = new Path((source as GH_tasPath).Value); return(true); } else if (source is Path) { Value = new Path(source as Path); return(true); } else if (source is GH_Curve) { Polyline poly; Curve c = (source as GH_Curve).Value; if (c.IsPolyline()) { c.TryGetPolyline(out poly); } else { PolylineCurve polyc = (source as Curve).ToPolyline(0, 0, 0.1, 1.0, 0, 0.2, 0.1, 0, true); if (!polyc.TryGetPolyline(out poly)) { return(false); } } List <Plane> planes = new List <Plane>(); foreach (Point3d p in poly) { planes.Add(new Plane(p, Vector3d.ZAxis)); } Value = new Path(planes); return(true); } else if (source is List <GH_Plane> ) { List <Plane> planes = (source as List <GH_Plane>).Select(x => x.Value).ToList(); Value = new Path(planes); return(true); } return(false); }
public List <Polyline> SplitSegmentizeCurveByCurves(Curve c, List <Curve> crvs, double intTol, double segTol, out List <Curve> segCrvs, out List <Point3d> associatedPts, List <Point3d> pts = null) { List <Polyline> pls = new List <Polyline>(); List <Curve> segcrvs = new List <Curve>(); List <Point3d> assPts = new List <Point3d>(); foreach (Curve spc in SplitCurveByCurves(c, crvs, intTol, out assPts, pts)) { PolylineCurve pc = spc.ToPolyline(0, 0, 0, 0, 0, segTol, 0, 0, true); segcrvs.Add(spc); Polyline pl; pc.TryGetPolyline(out pl); pls.Add(pl); } segCrvs = segcrvs; associatedPts = assPts; return(pls); }
/// <summary> /// Converts the curve to polyline. /// </summary> /// <param name="c">The c.</param> /// <param name="pl">The pl.</param> /// <returns></returns> public static bool ConvertCurveToPolyline(Curve c, out Polyline pl) { if (c.TryGetPolyline(out pl)) { return(pl.IsValid && !(pl.Length < RhinoMath.ZeroTolerance)); } // default options.. should perhaps do something with the document tolerance. PolylineCurve polylineCurve = c.ToPolyline(0, 0, 0.1, 0, 0, 0, 0, 0, true); if (polylineCurve == null) { //Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert brep edge to polyline"); return(false); } if (!polylineCurve.TryGetPolyline(out pl)) { //Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert brep edge to polyline - weird shizzle"); return(false); } return(pl.IsValid && !(pl.Length < RhinoMath.ZeroTolerance)); }
public static bool ConvertCurveToPolyline(Curve c, out Polyline pl) { pl = new Polyline(); if (!c.TryGetPolyline(out pl)) { PolylineCurve polylineCurve = c.ToPolyline(0, 0, 0.1, 0, 0, 0, 0, 0, true); if (polylineCurve == null) { //Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert brep edge to polyline"); return(false); } if (!polylineCurve.TryGetPolyline(out pl)) { //Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert brep edge to polyline - weird shizzle"); return(false); } } if (pl.Length == 0 || !pl.IsValid) { return(false); } return(true); }
// Rh Capture public Base PolylineToSpeckle(PolylineCurve poly, string units = null) { var u = units ?? ModelUnits; RH.Polyline polyline; if (poly.TryGetPolyline(out polyline)) { var intervalToSpeckle = IntervalToSpeckle(poly.Domain); if (polyline.Count == 2) { var polylineToSpeckle = new Line(PointsToFlatArray(polyline), u) { domain = intervalToSpeckle }; polylineToSpeckle.length = polyline.Length; var box = new RH.Box(poly.GetBoundingBox(true)); polylineToSpeckle.bbox = BoxToSpeckle(box, u); return(polylineToSpeckle); } var myPoly = new Polyline(PointsToFlatArray(polyline), u); myPoly.closed = polyline.IsClosed; if (myPoly.closed) { myPoly.value.RemoveRange(myPoly.value.Count - 3, 3); } myPoly.domain = intervalToSpeckle; myPoly.bbox = BoxToSpeckle(new RH.Box(poly.GetBoundingBox(true)), u); myPoly.length = poly.GetLength(); return(myPoly); } return(null); }
/// <summary> /// This one does many things, which essentially boil down to translating the geometry + performance measures into a spk instance. /// This instance subsequently gets json-ed out to a file /// </summary> public static System.Dynamic.ExpandoObject translateGeometry(List <System.Object> inputObjects, List <string> guids, String name, IGH_Component component) { dynamic myInstance = new System.Dynamic.ExpandoObject(); myInstance.metadata = new System.Dynamic.ExpandoObject(); myInstance.metadata.verion = "1.0"; myInstance.metadata.type = "Object"; myInstance.metadata.generator = "Instance Export"; // super important - name will link it to the correct group in three js myInstance.metadata.name = name; myInstance.metadata.properties = new List <String>(); foreach (IGH_Param param in component.Params.Input[1].Sources) { var myprop = getPanelNameAndVal(param); if (myprop != null) { myInstance.metadata.properties.Add(myprop); } } myInstance.geometries = new List <System.Object>(); int k = 0; foreach (System.Object myObj in inputObjects) { if (myObj != null) { string myguid = "000"; if (name != "staticGeo") { myguid = guids[k]; } k++; if (myObj is GH_Mesh) { GH_Mesh tempers = (GH_Mesh)myObj; SuperMesh mesh = new SuperMesh(tempers, myguid); myInstance.geometries.Add(mesh); } else if ((myObj is GH_Curve)) { GH_Curve tempers = (GH_Curve)myObj; Curve myCrv = tempers.Value; if (myCrv.Degree == 1) { Polyline tempP; myCrv.TryGetPolyline(out tempP); SuperPolyline polyline = new SuperPolyline(tempP, false, myguid); myInstance.geometries.Add(polyline); } else if ((myCrv.Degree == 2) || (myCrv.Degree == 3)) { bool isClosed = myCrv.IsClosed; int mainSegmentCount = 0, subSegmentCount = 1; double maxAngleRadians = 0, maxChordLengthRatio = 0, maxAspectRatio = 0, tolerance = 0.1, minEdgeLength = 0, maxEdgeLength = 0; bool keepStartPoint = true; PolylineCurve p = myCrv.ToPolyline(mainSegmentCount, subSegmentCount, maxAngleRadians, maxChordLengthRatio, maxAspectRatio, tolerance, minEdgeLength, maxEdgeLength, keepStartPoint); Polyline pp; p.TryGetPolyline(out pp); myInstance.geometries.Add(new SuperPolyline(pp, isClosed, myguid)); } } else if (myObj is Point3d) { GH_Point tempers = (GH_Point)myObj; SuperPoint point = new SuperPoint(tempers, myguid); myInstance.geometries.Add(point); } else if ((myObj is GH_Brep) || (myObj is GH_Surface)) { Mesh[] myMeshes; Brep myFutureBrep = null; GH_Convert.ToBrep(myObj, ref myFutureBrep, GH_Conversion.Primary); if (myFutureBrep != null) { myMeshes = Mesh.CreateFromBrep(myFutureBrep, MeshingParameters.Smooth); if (myMeshes == null || myMeshes.Length == 0) { // TODO throw an error } Mesh brep_mesh = new Mesh(); foreach (Mesh tempmesh in myMeshes) { brep_mesh.Append(tempmesh); } GH_Mesh temporal = new GH_Mesh(brep_mesh); SuperMesh mesh = new SuperMesh(temporal, myguid); myInstance.geometries.Add(mesh); } } else if (myObj is GH_Circle) { GH_Circle myCircle = myObj as GH_Circle; //NurbsCurve mycrv = myCircle.Value.ToNurbsCurve(); NurbsCurve mycrv = NurbsCurve.CreateFromCircle(myCircle.Value); int mainSegmentCount = 30, subSegmentCount = 1; double maxAngleRadians = 0, maxChordLengthRatio = 0, maxAspectRatio = 0, tolerance = 0.1, minEdgeLength = 0, maxEdgeLength = 0; bool keepStartPoint = true; if (mycrv != null) { PolylineCurve p = mycrv.ToPolyline(mainSegmentCount, subSegmentCount, maxAngleRadians, maxChordLengthRatio, maxAspectRatio, tolerance, minEdgeLength, maxEdgeLength, keepStartPoint); Polyline pp; p.TryGetPolyline(out pp); if (pp != null) { myInstance.geometries.Add(new SuperPolyline(pp, true, myguid)); } else { myInstance.geometries.Add("Circle error"); } } else { myInstance.geometries.Add("Circle error 2"); } } else if (myObj is GH_Line) { GH_Line myLine = myObj as GH_Line; myInstance.geometries.Add(new SuperPolyline(myLine, myguid)); } else { myInstance.geometries.Add("error - undefined type"); } } } return(myInstance); }