public static Edge ToTopologic(this Rhino.Geometry.NurbsCurve nurbsCurve) { if (nurbsCurve == null) { return(null); } int degree = nurbsCurve.Degree; bool isPeriodic = nurbsCurve.IsPeriodic; bool isRational = nurbsCurve.IsRational; List <double> knots = nurbsCurve.Knots.ToList(); knots.Insert(0, knots[0]); knots.Add(knots.Last()); NurbsCurvePointList ghControlPoints = nurbsCurve.Points; List <Vertex> controlPoints = new List <Vertex>(); List <double> weights = new List <double>(); for (int i = 0; i < ghControlPoints.Count; ++i) { controlPoints.Add(ghControlPoints[i].Location.ToTopologic()); weights.Add(ghControlPoints[i].Weight); } return(Edge.ByNurbsParameters(controlPoints, weights, knots, isRational, isPeriodic, degree)); }
public static RHG.NurbsCurve ToRhino5(this BHG.NurbsCurve bCurve) { if (bCurve == null) { return(null); } List <double> knots = bCurve.Knots; List <double> weights = bCurve.Weights; List <BHG.Point> ctrlPts = bCurve.ControlPoints; RHG.NurbsCurve rCurve = new RHG.NurbsCurve(3, false, bCurve.Degree() + 1, ctrlPts.Count); for (int i = 0; i < knots.Count; i++) { rCurve.Knots[i] = knots[i]; } for (int i = 0; i < ctrlPts.Count; i++) { BHG.Point pt = ctrlPts[i]; rCurve.Points.SetPoint(i, pt.X, pt.Y, pt.Z, weights[i]); } return(rCurve); }
private Topologic.Edge ByNurbsCurve(Rhino.Geometry.NurbsCurve ghNurbsCurve) { int degree = ghNurbsCurve.Degree; bool isClosed = ghNurbsCurve.IsClosed; bool isPeriodic = ghNurbsCurve.IsPeriodic; bool isRational = ghNurbsCurve.IsRational; NurbsCurveKnotList ghKnots = ghNurbsCurve.Knots; List <double> knots = ghKnots.ToList(); // OCCT-compatible knots.Insert(0, knots[0]); knots.Add(knots.Last()); NurbsCurvePointList ghControlPoints = ghNurbsCurve.Points; List <Topologic.Vertex> controlPoints = new List <Topologic.Vertex>(); List <double> weights = new List <double>(); for (int i = 0; i < ghControlPoints.Count; ++i) { controlPoints.Add(ByPoint(ghControlPoints[i].Location)); weights.Add(ghControlPoints[i].Weight); } return(Topologic.Edge.ByNurbsParameters(controlPoints, weights, knots, isRational, isPeriodic, degree)); }
/***************************************************/ public static BHG.ICurve FromRhino(this RHG.NurbsCurve rCurve) { if (rCurve == null) { return(null); } if (rCurve.IsPolyline()) { RHG.Polyline polyline; rCurve.TryGetPolyline(out polyline); return(polyline.FromRhino()); } if (rCurve.IsClosed && rCurve.IsEllipse()) { RHG.Ellipse ellipse = new RHG.Ellipse(); rCurve.TryGetEllipse(out ellipse); return(ellipse.FromRhino()); } IEnumerable <RHG.ControlPoint> rPoints = rCurve.Points; List <double> knots = rCurve.Knots.ToList(); return(new BHG.NurbsCurve { ControlPoints = rPoints.Select(x => x.FromRhino()).ToList(), Weights = rPoints.Select(x => x.Weight).ToList(), Knots = knots }); }
/***************************************************/ public static bool IsEqual(this BHG.NurbsCurve bhCurve, RHG.NurbsCurve rhCurve, double tolerance = BHG.Tolerance.Distance) { if (bhCurve == null & rhCurve == null) { return(true); } List <BHG.Point> bhPoints = bhCurve.ControlPoints; List <double> bhWeights = bhCurve.Weights; List <double> bhKnots = bhCurve.Knots; RHG.Collections.NurbsCurvePointList rhPoints = rhCurve.Points; bool pointsEqual = true; bool weightsEqual = true; bool knotsEqual = true; for (int i = 0; i < bhPoints.Count; i++) { pointsEqual &= bhPoints[i].IsEqual(rhPoints[i], tolerance); } for (int i = 0; i < bhWeights.Count; i++) { weightsEqual &= Math.Abs(bhWeights[i] - rhPoints[i].Weight) < tolerance; } for (int i = 0; i < bhKnots.Count; i++) { knotsEqual &= Math.Abs(bhKnots[i] - rhCurve.Knots[i]) < tolerance; } return(pointsEqual && weightsEqual && knotsEqual); }
public void simplifyCurve(ref List <Vector3> curvePoints) { //clear list first simplifiedCurvePoints.Clear(); reducePoints.Clear(); float pointReductionTubeWidth = 0.002f; //0.002 reducePoints = DouglasPeucker(ref curvePoints, 0, curvePoints.Count - 1, pointReductionTubeWidth); //Rhino.RhinoApp.WriteLine("reduce points from" + curvePoints.Count + " to " + reducePoints.Count); //TODO- the curve isn't correct while drawing for (int i = 0; i < reducePoints.Count; i++) { simplifiedCurvePoints.Add(UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, reducePoints[i]))); } if (simplifiedCurvePoints.Count >= 2) //TODO: might need 8 for closecurve check { int order = 3; if (isClosed) { while (order >= 1) { simplifiedCurve = Rhino.Geometry.NurbsCurve.Create(true, order, simplifiedCurvePoints.ToArray()); if (simplifiedCurve != null) { break; } order--; } } else { //null check while (order >= 1) { simplifiedCurve = Rhino.Geometry.NurbsCurve.Create(false, order, simplifiedCurvePoints.ToArray()); if (simplifiedCurve != null) { break; } order--; } } //reduced control points if (simplifiedCurve.Points.Count > 5) { simplifiedCurve = simplifiedCurve.Rebuild(5, simplifiedCurve.Degree, false); } else { simplifiedCurve = simplifiedCurve.Rebuild(simplifiedCurve.Points.Count, simplifiedCurve.Degree, false); } } }
public static Edge ToTopologic(this BrepEdge brepEdge) { if (brepEdge == null) { return(null); } Rhino.Geometry.NurbsCurve nurbsCurve = brepEdge.ToNurbsCurve(); return(ToTopologic(nurbsCurve)); }
public static Edge ToTopologic(this ArcCurve arcCurve) { if (arcCurve == null) { return(null); } Rhino.Geometry.NurbsCurve nurbsCurve = arcCurve.ToNurbsCurve(); return(ToTopologic(nurbsCurve)); }
private Topologic.Topology ByCurve(Curve ghCurve) { LineCurve ghLine = ghCurve as LineCurve; if (ghLine != null) { return(ByLine(ghLine.Line)); } Rhino.Geometry.NurbsCurve ghNurbsCurve = ghCurve as Rhino.Geometry.NurbsCurve; if (ghNurbsCurve != null) { return(ByNurbsCurve(ghNurbsCurve)); } ArcCurve ghArcCurve = ghCurve as ArcCurve; if (ghArcCurve != null) { return(ByArcCurve(ghArcCurve)); } BrepEdge ghBrepEdge = ghCurve as BrepEdge; if (ghBrepEdge != null) { return(ByBrepEdge(ghBrepEdge)); } //BrepTrim ghBrepTrim = ghCurve as BrepTrim; //if (ghBrepTrim != null) //{ // return ByBrepTrim(ghBrepTrim); //} PolylineCurve ghPolylineCurve = ghCurve as PolylineCurve; if (ghPolylineCurve != null) { return(ByPolylineCurve(ghPolylineCurve)); } PolyCurve ghPolyCurve = ghCurve as PolyCurve; if (ghPolyCurve != null) { return(ByPolyCurve(ghPolyCurve)); } throw new Exception("This type of curve is not yet supported."); }
public static Topology ToTopologic(this Curve curve) { if (curve == null) { return(null); } LineCurve lineCurve = curve as LineCurve; if (lineCurve != null) { return(lineCurve.Line.ToTopologic()); } Rhino.Geometry.NurbsCurve nurbsCurve = curve as Rhino.Geometry.NurbsCurve; if (nurbsCurve != null) { return(nurbsCurve.ToTopologic()); } ArcCurve arcCurve = curve as ArcCurve; if (arcCurve != null) { return(arcCurve.ToTopologic()); } BrepEdge brepEdge = curve as BrepEdge; if (brepEdge != null) { return(brepEdge.ToTopologic()); } PolylineCurve ghPolylineCurve = curve as PolylineCurve; if (ghPolylineCurve != null) { return(ghPolylineCurve.ToTopologic()); } PolyCurve ghPolyCurve = curve as PolyCurve; if (ghPolyCurve != null) { return(ghPolyCurve.ToTopologic()); } return(null); }
public static string ToSVG(this Rg.Curve input) { Rg.NurbsCurve nurbs = input.ToNurbsCurve(); nurbs.MakePiecewiseBezier(true); Rhino.Geometry.BezierCurve[] bezier = Rg.BezierCurve.CreateCubicBeziers(nurbs, 0, 0); string output = "M " + input.PointAtStart.ToSVG(); for (int i = 0; i < bezier.Count(); i++) { output += " C " + bezier[i].GetControlVertex3d(1).ToSVG() + bezier[i].GetControlVertex3d(2).ToSVG() + bezier[i].GetControlVertex3d(3).ToSVG(); } output += " "; return(output); }
//TODO- check if the x,y axis of the plane will change whenever we call tryGetPlane //railPlaneSN-addRhinoObjSceneNode(draw on referece), curveOnObjRef-addRhinoObj(!=In3D) //drawPoint, strokeSN-addSceneNode, renderObjSN-updateSceneNode(Revolve or Curve2) public void resetVariables() { stroke_g = new Geometry.GeometryStroke(ref mScene); currentState = State.READY; reducePoints = new List <Vector3>(); targetPRhObjID = Guid.Empty; drawPoint = null; snapPointSN = null; projectP = new Point3d(); rhinoCurvePoints = new List <Point3d>(); rhinoCurve = null; proj_plane = new Plane(); simplifiedCurvePoints = new List <Point3d>(); simplifiedCurve = null; editCurve = null; //for extrude //curveOnObjRef = null; backgroundStart = false; displacement = 0; dynamicBrep = null; modelName = "tprint"; //dynamicRender = "none"; // need to save same as drawType and shapeType snapPointsList = new List <Point3d>(); rayCastingObjs = new List <ObjRef>(); toleranceMax = 100000; snapDistance = 40; isSnap = false; shouldSnap = false; moveControlerOrigin = new Point3d(); movePlaneRef = null; planeNormal = new Rhino.Geometry.Vector3d(); curvePlane = new Plane(); lastTranslate = 0.0f; d = null; localListCurve = mScene.iCurveList; oldCurveOnObjID = ""; oldPlaneOrigin = ""; oldPlaneNormal = ""; }
internal static Curve ToNurbsSpline(RG.NurbsCurve value, double factor) { var degree = value.Degree; var knots = ToDoubleArray(value.Knots, degree); var controlPoints = ToXYZArray(value.Points, factor); if (value.IsRational) { var weights = value.Points.ConvertAll(x => x.Weight); return(NurbSpline.CreateCurve(value.Degree, knots, controlPoints, weights)); } else { return(NurbSpline.CreateCurve(value.Degree, knots, controlPoints)); } }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; List <RG.Point3d> pts = new List <RG.Point3d> { new RG.Point3d(0, 0, 0), new RG.Point3d(5, 10, 0), new RG.Point3d(15, 0, 0), new RG.Point3d(20, 0, 0) }; RG.PolylineCurve pc = new RG.PolylineCurve(pts); RG.NurbsCurve nurb = pc.ToNurbsCurve(); nurb.IncreaseDegree(3); var knots = ToDoubleArray(nurb.Knots, nurb.Degree); var controlPoints = ToXYZArray(nurb.Points, 1); var weights = nurb.Points.ConvertAll(x => x.Weight); XYZ normal = new XYZ(0, 0, 1); XYZ origin = new XYZ(0, 0, 0); Plane rvtPlane = Plane.CreateByNormalAndOrigin(normal, origin); //var plane = sketchPlane.GetPlane().ToPlane(); Curve rvtN = NurbSpline.CreateCurve(nurb.Degree, knots, controlPoints); using (Transaction t = new Transaction(doc, "a")) { t.Start(); SketchPlane sketchPlane = SketchPlane.Create(doc, rvtPlane); ModelCurve mc = doc.Create.NewModelCurve(rvtN, sketchPlane); TaskDialog.Show("r", mc.Id.ToString()); t.Commit(); } return(Result.Succeeded); }
private Rhino.Geometry.Curve ToNurbsCurve(Topologic.NurbsCurve nurbsCurve) { // Based on https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Geometry_NurbsCurve_Knots.htm bool isPeriodic = nurbsCurve.IsPeriodic; bool isRational = nurbsCurve.IsRational; int degree = nurbsCurve.Degree; List <Topologic.Vertex> controlVertices = nurbsCurve.ControlVertices; List <Point3d> ghControlPoints = new List <Point3d>(); Rhino.Geometry.NurbsCurve ghNurbsCurve = new Rhino.Geometry.NurbsCurve(3, isRational, degree + 1, controlVertices.Count); int i = 0; foreach (Topologic.Vertex controlVertex in controlVertices) { Point3d ghControlPoint = ToPoint(controlVertex); ghControlPoints.Add(ghControlPoint); ghNurbsCurve.Points.SetPoint(i, ghControlPoint); ++i; } List <double> knots = nurbsCurve.Knots; knots = knots.GetRange(1, knots.Count - 2); i = 0; foreach (double knot in knots) { ghNurbsCurve.Knots[i] = knot; ++i; } double t0 = nurbsCurve.FirstParameter; double t1 = nurbsCurve.LastParameter; Rhino.Geometry.Curve ghTrimmedNurbsCurve = ghNurbsCurve.Trim(t0, t1); String log = ""; if (ghTrimmedNurbsCurve.IsValidWithLog(out log)) { return(ghTrimmedNurbsCurve); } throw new Exception("A valid curve cannot be created from this Edge."); }
public static Rhino.Commands.Result AddNurbsCircle(Rhino.RhinoDoc doc) { // The easy way to get a NURBS curve from a circle is with // the following two lines of code. // // Rhino.Geometry.Circle c = new Rhino.Geometry.Circle(20); // Rhino.Geometry.NurbsCurve nc = c.ToNurbsCurve(); // // This sample demonstrates creating a NURBS curve from scratch. const int dimension = 3; const bool isRational = true; const int order = 3; const int cv_count = 9; Rhino.Geometry.NurbsCurve nc = new Rhino.Geometry.NurbsCurve(dimension, isRational, order, cv_count); nc.Points.SetPoint(0, 1.0, 0.0, 0.0, 1.0); nc.Points.SetPoint(1, 0.707107, 0.707107, 0.0, 0.707107); nc.Points.SetPoint(2, 0.0, 1.0, 0.0, 1.0); nc.Points.SetPoint(3, -0.707107, 0.707107, 0.0, 0.707107); nc.Points.SetPoint(4, -1.0, 0.0, 0.0, 1.0); nc.Points.SetPoint(5, -0.707107, -0.707107, 0.0, 0.707107); nc.Points.SetPoint(6, 0.0, -1.0, 0.0, 1.0); nc.Points.SetPoint(7, 0.707107, -0.707107, 0.0, 0.707107); nc.Points.SetPoint(8, 1.0, 0.0, 0.0, 1.0); nc.Knots[0] = 0.0; nc.Knots[1] = 0.0; nc.Knots[2] = 0.5 * Math.PI; nc.Knots[3] = 0.5 * Math.PI; nc.Knots[4] = Math.PI; nc.Knots[5] = Math.PI; nc.Knots[6] = 1.5 * Math.PI; nc.Knots[7] = 1.5 * Math.PI; nc.Knots[8] = 2.0 * Math.PI; nc.Knots[9] = 2.0 * Math.PI; if (nc.IsValid) { doc.Objects.AddCurve(nc); doc.Views.Redraw(); return(Rhino.Commands.Result.Success); } return(Rhino.Commands.Result.Failure); }
public static Rhino.Commands.Result AddNurbsCurve(Rhino.RhinoDoc doc) { Rhino.Collections.Point3dList points = new Rhino.Collections.Point3dList(5); points.Add(0, 0, 0); points.Add(0, 2, 0); points.Add(2, 3, 0); points.Add(4, 2, 0); points.Add(4, 0, 0); Rhino.Geometry.NurbsCurve nc = Rhino.Geometry.NurbsCurve.Create(false, 3, points); Rhino.Commands.Result rc = Rhino.Commands.Result.Failure; if (nc != null && nc.IsValid) { if (doc.Objects.AddCurve(nc) != Guid.Empty) { doc.Views.Redraw(); rc = Rhino.Commands.Result.Success; } } return(rc); }
public void renderCurve() { //reduce the points in the curve first simplifyCurve(ref ((Geometry.GeometryStroke)(stroke_g)).mPoints); foreach (OpenTK.Vector3 point in reducePoints) { // -y_rhino = z_gl, z_rhino = y_gl and unit conversion // OpenTK.Vector3 p = Util.transformPoint(Util.mGLToRhino, point*1000); //curvePoints.Add(new Point3d(p.X, p.Y, p.Z)); curvePoints.Add(UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, point))); allPoints.Add(new Point(UtilOld.openTkToRhinoPoint(UtilOld.vrToPlatformPoint(ref mScene, point)))); } //Rhino CreateInterpolatedCurve and CreatePlanarBreps if (curvePoints.Count >= 2) { curve = Rhino.Geometry.NurbsCurve.Create(true, 3, curvePoints.ToArray()); curvelist.Add(curve); } }
public static Rhino.Commands.Result CreateSpiral(Rhino.RhinoDoc doc) { var axisStart = new Rhino.Geometry.Point3d(0, 0, 0); var axisDir = new Rhino.Geometry.Vector3d(1, 0, 0); var radiusPoint = new Rhino.Geometry.Point3d(0, 1, 0); Rhino.Geometry.NurbsCurve curve0 = GetSpirial0(); if (null != curve0) { doc.Objects.AddCurve(curve0); } Rhino.Geometry.NurbsCurve curve1 = GetSpirial1(); if (null != curve1) { doc.Objects.AddCurve(curve1); } doc.Views.Redraw(); return(Rhino.Commands.Result.Success); }
public static Rhino.Commands.Result InsertKnot(Rhino.RhinoDoc doc) { const ObjectType filter = Rhino.DocObjects.ObjectType.Curve; Rhino.DocObjects.ObjRef objref; Result rc = Rhino.Input.RhinoGet.GetOneObject("Select curve for knot insertion", false, filter, out objref); if (rc != Rhino.Commands.Result.Success) { return(rc); } Rhino.Geometry.Curve curve = objref.Curve(); if (null == curve) { return(Rhino.Commands.Result.Failure); } Rhino.Geometry.NurbsCurve nurb = curve.ToNurbsCurve(); if (null == nurb) { return(Rhino.Commands.Result.Failure); } Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Point on curve to add knot"); gp.Constrain(nurb, false); gp.Get(); if (gp.CommandResult() == Rhino.Commands.Result.Success) { double t; Rhino.Geometry.Curve crv = gp.PointOnCurve(out t); if (crv != null && nurb.Knots.InsertKnot(t)) { doc.Objects.Replace(objref, nurb); doc.Views.Redraw(); } } return(Rhino.Commands.Result.Success); }
public static Parasite_NurbsCurve ToParasiteType(Rhino.Geometry.NurbsCurve nurbsCurve, Dictionary <string, string> properties = null) { Parasite_Point3d [] controlPoints = nurbsCurve.Points.Select(a => ToParasiteType(a.Location)).ToArray(); double[] weights = nurbsCurve.Points.Select(a => a.Weight).ToArray(); //making sure all the weights are not zeros by setting them to 1 if they are double tolerance = 1e-4; if (weights.Any((x) => x <= tolerance)) { weights = weights.Select((x) => 1.0).ToArray(); } double[] knots = nurbsCurve.Knots.Select(a => a).ToArray(); double[] interiorKnotMultiplicity = new double[nurbsCurve.Knots.Count]; for (int i = 0; i < interiorKnotMultiplicity.Length; i++) { interiorKnotMultiplicity[i] = nurbsCurve.Knots.KnotMultiplicity(i); } return(new Parasite_NurbsCurve(controlPoints, weights, knots, interiorKnotMultiplicity, nurbsCurve.Degree)); }
/// <summary> /// Returns a Windows Media Bezier Spline Path Geometry from a Rhinocommon Curve /// </summary> /// <param name="input">Rhinocommon Curve</param> /// <returns>System Windows Media Bezier Curve Path Geometry </returns> public static Sm.PathGeometry ToGeometry(this Rg.Curve input) { Rg.NurbsCurve nurbsCurve = input.ToNurbsCurve(); nurbsCurve.MakePiecewiseBezier(true); Rg.BezierCurve[] bezier = Rg.BezierCurve.CreateCubicBeziers(nurbsCurve, 0, 0); Sm.PathFigure figure = new Sm.PathFigure(); Sm.PathGeometry geometry = new Sm.PathGeometry(); Sm.PathFigureCollection figureCollection = new Sm.PathFigureCollection(); Sm.PathSegmentCollection segmentCollection = new Sm.PathSegmentCollection(); figure.StartPoint = bezier[0].GetControlVertex3d(0).ToWindowsPoint(); for (int i = 0; i < bezier.Count(); i++) { Sm.BezierSegment segment = new Sm.BezierSegment(bezier[i].GetControlVertex3d(1).ToWindowsPoint(), bezier[i].GetControlVertex3d(2).ToWindowsPoint(), bezier[i].GetControlVertex3d(3).ToWindowsPoint(), true); segmentCollection.Add(segment); } figure.Segments = segmentCollection; figureCollection.Add(figure); geometry.Figures = figureCollection; return(geometry); }
private Topologic.Edge ByArcCurve(ArcCurve ghArcCurve) { Rhino.Geometry.NurbsCurve ghNurbsCurve = ghArcCurve.ToNurbsCurve(); return(ByNurbsCurve(ghNurbsCurve)); }
private Topologic.Edge ByBrepEdge(BrepEdge ghBrepEdge) { Rhino.Geometry.NurbsCurve ghNurbsCurve = ghBrepEdge.ToNurbsCurve(); return(ByNurbsCurve(ghNurbsCurve)); }
public CurveConverter(Point3dConverter ptConv, ArcConverter arcConv, LineConverter lineConv) { //to convert ArcCurves AddConverter(new PipeConverter <rh.ArcCurve, pp.Arc>( (rhArc) => { return(arcConv.ToPipe <rh.Arc, pp.Arc>(rhArc.Arc)); }, (ppArc) => { return(new rh.ArcCurve(arcConv.FromPipe <rh.Arc, pp.Arc>(ppArc))); } )); //to convert LineCurves AddConverter(new PipeConverter <rh.LineCurve, pp.Line>( (rhLine) => { return(lineConv.ToPipe <rh.Line, pp.Line>(rhLine.Line)); }, (ppLine) => { return(new rh.LineCurve(lineConv.FromPipe <rh.Line, pp.Line>(ppLine))); } )); //to convert polyline curves AddConverter(new PipeConverter <rh.PolylineCurve, pp.Polyline>( (rhpl) => { List <ppg.Vec> ptList = new List <ppg.Vec>(); int ptCount = rhpl.PointCount; for (int i = 0; i < ptCount; i++) { ptList.Add(ptConv.ToPipe <rh.Point3d, ppg.Vec>(rhpl.Point(i))); } return(new pp.Polyline(ptList)); }, (ppl) => { List <rh.Point3d> ptList = new List <rh.Point3d>(); foreach (var pt in ppl.Points) { ptList.Add(ptConv.FromPipe <rh.Point3d, ppg.Vec>(pt)); } return(new rh.PolylineCurve(ptList)); } )); //to convert nurbs curves AddConverter(new PipeConverter <rh.NurbsCurve, pp.NurbsCurve>( (rhc) => { pp.NurbsCurve curve; /* * if the curve is closed, the internal NurbsCurve datastructure stores too many points in the * array, in order to loop around to the next knot, we want to take a smaller list in that case */ //rebuilding the curve just in case.. if there is something weird about the curve var rhc2 = rhc.Rebuild(rhc.Points.Count, rhc.Degree, true); rhc = rhc2 ?? rhc; int controlPtsNum = rhc.IsClosed ? rhc.Points.Count - (rhc.IsPeriodic ? rhc.Degree : 1) : rhc.Points.Count; List <ppg.Vec> ptList = rhc.Points.Take(controlPtsNum).Select( (pt) => ptConv.ToPipe <rh.Point3d, ppg.Vec>(pt.Location)).ToList(); //normalizing the knots to be between 0 and 1 List <double> knotList = rhc.Knots.Select((k) => (k - rhc.Domain.Min) / (rhc.Domain.Length)).ToList(); curve = new pp.NurbsCurve(ptList, rhc.Degree, rhc.Points.Take(controlPtsNum).Select((pt) => pt.Weight).ToList(), knotList, rhc.IsClosed); curve.IsPeriodic = rhc.IsPeriodic; return(curve); }, (ppc) => { List <rh.Point3d> ptList = ppc.ControlPoints.Select( (pt) => ptConv.FromPipe <rh.Point3d, ppg.Vec>(pt)).ToList(); /* * If the curve is closed, then rhino expects the first point to appear at the end of the * control point list again, so we add it. */ if (ppc.IsClosed) { ptList.Add(ptList.First()); } rh.NurbsCurve curve = rh.NurbsCurve.Create(ppc.IsPeriodic, ppc.Degree, ptList); if (ppc.IsClosed && ppc.ControlPoints.Count > 3 && !curve.IsClosed) { curve.MakeClosed(1e-7); } if (!ppc.IsRational) { for (int i = 0; i < curve.Points.Count; i++) { var pt = curve.Points.ElementAt(i); var newPt = new rh.Point4d(pt.Location.X, pt.Location.Y, pt.Location.Z, ppc.Weights[i % ppc.Weights.Count]); curve.Points.SetPoint(i, newPt); } } //setting knots after scaling them to the domain if (ppc.Knots.Count == curve.Knots.Count) { for (int i = 0; i < ppc.Knots.Count; i++) { curve.Knots[i] = ppc.Knots[i] * (curve.Domain.Length) + curve.Domain.Min; } } string msg; if (!curve.IsValidWithLog(out msg)) { System.Diagnostics.Debug.WriteLine(msg); if (curve.IsPeriodic) { curve.Knots.CreatePeriodicKnots(1.0 / (curve.Points.Count)); } else { curve.Knots.CreateUniformKnots(1.0 / (curve.Points.Count)); } if (!curve.IsValid) { throw new InvalidOperationException("Cannot create a valid curve with " + "received data because: \n" + msg); } } return(curve); } )); //to convert polycurves AddConverter(new PipeConverter <rh.PolyCurve, pp.PolyCurve>( (rhc) => { List <pp.Curve> curves = new List <pp.Curve>(); for (int i = 0; i < rhc.SegmentCount; i++) { curves.Add(ToPipe <rh.Curve, pp.Curve>(rhc.SegmentCurve(i))); } return(new pp.PolyCurve(curves)); }, (ppc) => { var curve = new rh.PolyCurve(); foreach (var segment in ppc.Segments) { curve.Append(FromPipe <rh.Curve, pp.Curve>(segment)); } return(curve); } )); }
public static Response Grasshopper(NancyContext ctx) { // load grasshopper file var archive = new GH_Archive(); // TODO: stream to string var body = ctx.Request.Body.ToString(); string json = string.Empty; using (var reader = new StreamReader(ctx.Request.Body)) { json = reader.ReadToEnd(); } //GrasshopperInput input = Newtonsoft.Json.JsonConvert.DeserializeObject<GrasshopperInput>(json); //JsonSerializerSettings settings = new JsonSerializerSettings(); //settings.ContractResolver = new DictionaryAsArrayResolver(); Schema input = JsonConvert.DeserializeObject <Schema>(json); string grasshopperXml = string.Empty; if (input.Algo != null) { // If request contains markup byte[] byteArray = Convert.FromBase64String(input.Algo); grasshopperXml = System.Text.Encoding.UTF8.GetString(byteArray); } else { // If request contains pointer string pointer = input.Pointer; grasshopperXml = GetGhxFromPointer(pointer); } if (!archive.Deserialize_Xml(grasshopperXml)) { throw new Exception(); } var definition = new GH_Document(); if (!archive.ExtractObject(definition, "Definition")) { throw new Exception(); } // Set input params foreach (var obj in definition.Objects) { var group = obj as GH_Group; if (group == null) { continue; } if (group.NickName.Contains("RH_IN")) { // It is a RestHopper input group! GHTypeCodes code = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]); var param = group.Objects()[0]; //GH_Param<IGH_Goo> goo = obj as GH_Param<IGH_Goo>; // SetData foreach (Resthopper.IO.DataTree <ResthopperObject> tree in input.Values) { string paramname = tree.ParamName; if (group.NickName == paramname) { switch (code) { case GHTypeCodes.Boolean: //PopulateParam<GH_Boolean>(goo, tree); Param_Boolean boolParam = param as Param_Boolean; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Boolean> objectList = new List <GH_Boolean>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; bool boolean = JsonConvert.DeserializeObject <bool>(restobj.Data); GH_Boolean data = new GH_Boolean(boolean); boolParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Point: //PopulateParam<GH_Point>(goo, tree); Param_Point ptParam = param as Param_Point; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Point> objectList = new List <GH_Point>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Point3d rPt = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data); GH_Point data = new GH_Point(rPt); ptParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Vector: //PopulateParam<GH_Vector>(goo, tree); Param_Vector vectorParam = param as Param_Vector; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Vector> objectList = new List <GH_Vector>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data); GH_Vector data = new GH_Vector(rhVector); vectorParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Integer: //PopulateParam<GH_Integer>(goo, tree); Param_Integer integerParam = param as Param_Integer; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Integer> objectList = new List <GH_Integer>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; int rhinoInt = JsonConvert.DeserializeObject <int>(restobj.Data); GH_Integer data = new GH_Integer(rhinoInt); integerParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Number: //PopulateParam<GH_Number>(goo, tree); Param_Number numberParam = param as Param_Number; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Number> objectList = new List <GH_Number>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; double rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data); GH_Number data = new GH_Number(rhNumber); numberParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Text: //PopulateParam<GH_String>(goo, tree); Param_String stringParam = param as Param_String; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_String> objectList = new List <GH_String>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; string rhString = JsonConvert.DeserializeObject <string>(restobj.Data); GH_String data = new GH_String(rhString); stringParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Line: //PopulateParam<GH_Line>(goo, tree); Param_Line lineParam = param as Param_Line; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Line> objectList = new List <GH_Line>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Line rhLine = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data); GH_Line data = new GH_Line(rhLine); lineParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Curve: //PopulateParam<GH_Curve>(goo, tree); Param_Curve curveParam = param as Param_Curve; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Curve> objectList = new List <GH_Curve>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; GH_Curve ghCurve; try { Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data); Rhino.Geometry.Curve c = new Rhino.Geometry.PolylineCurve(data); ghCurve = new GH_Curve(c); } catch { Rhino.Geometry.NurbsCurve data = JsonConvert.DeserializeObject <Rhino.Geometry.NurbsCurve>(restobj.Data); Rhino.Geometry.Curve c = new Rhino.Geometry.NurbsCurve(data); ghCurve = new GH_Curve(c); } curveParam.AddVolatileData(path, i, ghCurve); } } break; case GHTypeCodes.Circle: //PopulateParam<GH_Circle>(goo, tree); Param_Circle circleParam = param as Param_Circle; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Circle> objectList = new List <GH_Circle>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data); GH_Circle data = new GH_Circle(rhCircle); circleParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.PLane: //PopulateParam<GH_Plane>(goo, tree); Param_Plane planeParam = param as Param_Plane; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Plane> objectList = new List <GH_Plane>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data); GH_Plane data = new GH_Plane(rhPlane); planeParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Rectangle: //PopulateParam<GH_Rectangle>(goo, tree); Param_Rectangle rectangleParam = param as Param_Rectangle; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Rectangle> objectList = new List <GH_Rectangle>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data); GH_Rectangle data = new GH_Rectangle(rhRectangle); rectangleParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Box: //PopulateParam<GH_Box>(goo, tree); Param_Box boxParam = param as Param_Box; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Box> objectList = new List <GH_Box>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Box rhBox = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data); GH_Box data = new GH_Box(rhBox); boxParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Surface: //PopulateParam<GH_Surface>(goo, tree); Param_Surface surfaceParam = param as Param_Surface; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Surface> objectList = new List <GH_Surface>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data); GH_Surface data = new GH_Surface(rhSurface); surfaceParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Brep: //PopulateParam<GH_Brep>(goo, tree); Param_Brep brepParam = param as Param_Brep; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Brep> objectList = new List <GH_Brep>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Brep rhBrep = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data); GH_Brep data = new GH_Brep(rhBrep); brepParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Mesh: //PopulateParam<GH_Mesh>(goo, tree); Param_Mesh meshParam = param as Param_Mesh; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Mesh> objectList = new List <GH_Mesh>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; Rhino.Geometry.Mesh rhMesh = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data); GH_Mesh data = new GH_Mesh(rhMesh); meshParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Slider: //PopulateParam<GH_Number>(goo, tree); GH_NumberSlider sliderParam = param as GH_NumberSlider; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Number> objectList = new List <GH_Number>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; double rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data); GH_Number data = new GH_Number(rhNumber); sliderParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.BooleanToggle: //PopulateParam<GH_Boolean>(goo, tree); GH_BooleanToggle toggleParam = param as GH_BooleanToggle; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Boolean> objectList = new List <GH_Boolean>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; bool rhBoolean = JsonConvert.DeserializeObject <bool>(restobj.Data); GH_Boolean data = new GH_Boolean(rhBoolean); toggleParam.AddVolatileData(path, i, data); } } break; case GHTypeCodes.Panel: //PopulateParam<GH_String>(goo, tree); GH_Panel panelParam = param as GH_Panel; foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree) { GH_Path path = new GH_Path(GhPath.FromString(entree.Key)); List <GH_Panel> objectList = new List <GH_Panel>(); for (int i = 0; i < entree.Value.Count; i++) { ResthopperObject restobj = entree.Value[i]; string rhString = JsonConvert.DeserializeObject <string>(restobj.Data); GH_String data = new GH_String(rhString); panelParam.AddVolatileData(path, i, data); } } break; } } } } } Schema OutputSchema = new Schema(); OutputSchema.Algo = Utils.Base64Encode(string.Empty); // Parse output params foreach (var obj in definition.Objects) { var group = obj as GH_Group; if (group == null) { continue; } if (group.NickName.Contains("RH_OUT")) { // It is a RestHopper output group! GHTypeCodes code = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]); var param = group.Objects()[0] as IGH_Param; if (param == null) { continue; } try { param.CollectData(); param.ComputeData(); } catch (Exception) { param.Phase = GH_SolutionPhase.Failed; // TODO: throw something better throw; } // Get data Resthopper.IO.DataTree <ResthopperObject> OutputTree = new Resthopper.IO.DataTree <ResthopperObject>(); OutputTree.ParamName = group.NickName; var volatileData = param.VolatileData; for (int p = 0; p < volatileData.PathCount; p++) { List <ResthopperObject> ResthopperObjectList = new List <ResthopperObject>(); foreach (var goo in volatileData.get_Branch(p)) { if (goo == null) { continue; } else if (goo.GetType() == typeof(GH_Boolean)) { GH_Boolean ghValue = goo as GH_Boolean; bool rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <bool>(rhValue)); } else if (goo.GetType() == typeof(GH_Point)) { GH_Point ghValue = goo as GH_Point; Point3d rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Point3d>(rhValue)); } else if (goo.GetType() == typeof(GH_Vector)) { GH_Vector ghValue = goo as GH_Vector; Vector3d rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Vector3d>(rhValue)); } else if (goo.GetType() == typeof(GH_Integer)) { GH_Integer ghValue = goo as GH_Integer; int rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <int>(rhValue)); } else if (goo.GetType() == typeof(GH_Number)) { GH_Number ghValue = goo as GH_Number; double rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <double>(rhValue)); } else if (goo.GetType() == typeof(GH_String)) { GH_String ghValue = goo as GH_String; string rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <string>(rhValue)); } else if (goo.GetType() == typeof(GH_Line)) { GH_Line ghValue = goo as GH_Line; Line rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Line>(rhValue)); } else if (goo.GetType() == typeof(GH_Curve)) { GH_Curve ghValue = goo as GH_Curve; Curve rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Curve>(rhValue)); } else if (goo.GetType() == typeof(GH_Circle)) { GH_Circle ghValue = goo as GH_Circle; Circle rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Circle>(rhValue)); } else if (goo.GetType() == typeof(GH_Plane)) { GH_Plane ghValue = goo as GH_Plane; Plane rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Plane>(rhValue)); } else if (goo.GetType() == typeof(GH_Rectangle)) { GH_Rectangle ghValue = goo as GH_Rectangle; Rectangle3d rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Rectangle3d>(rhValue)); } else if (goo.GetType() == typeof(GH_Box)) { GH_Box ghValue = goo as GH_Box; Box rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Box>(rhValue)); } else if (goo.GetType() == typeof(GH_Surface)) { GH_Surface ghValue = goo as GH_Surface; Brep rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue)); } else if (goo.GetType() == typeof(GH_Brep)) { GH_Brep ghValue = goo as GH_Brep; Brep rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue)); } else if (goo.GetType() == typeof(GH_Mesh)) { GH_Mesh ghValue = goo as GH_Mesh; Mesh rhValue = ghValue.Value; ResthopperObjectList.Add(GetResthopperObject <Mesh>(rhValue)); } } GhPath path = new GhPath(new int[] { p }); OutputTree.Add(path.ToString(), ResthopperObjectList); } OutputSchema.Values.Add(OutputTree); } } if (OutputSchema.Values.Count < 1) { throw new System.Exceptions.PayAttentionException("Looks like you've missed something..."); // TODO } string returnJson = JsonConvert.SerializeObject(OutputSchema); return(returnJson); }
/***************************************************/ public static void RenderRhinoWires(RHG.NurbsCurve curve, Rhino.Display.DisplayPipeline pipeline, Color bhColour, int thickness) { pipeline.DrawCurve(curve, bhColour, thickness); }
public void generateModel() { //TODO-simplify the curve and pass to model function simplifyCurve(ref ((Geometry.GeometryStroke)(stroke_g)).mPoints); //result curve is simplifiedCurve if (dynamicRender == "none" || simplifiedCurve == null) { return; } else if (dynamicRender == "Revolve") { if (mScene.iCurveList.Count == 0) { if (drawnType != DrawnType.In3D && curveOnObjRef != null) { simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), curveOnObjRef.ObjectId.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); } mScene.iCurveList.Add(simplifiedCurve); } else { //get the curve info for later update use oldCurveOnObjID = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString()); oldPlaneOrigin = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneOrigin.ToString()); oldPlaneNormal = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneNormal.ToString()); simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), oldCurveOnObjID); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), oldPlaneOrigin); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), oldPlaneNormal); mScene.iCurveList[0] = simplifiedCurve; } dynamicBrep = UtilOld.RevolveFunc(ref mScene, ref mScene.iCurveList); } //TODO- fix the bug that when release, it accidently assign the old plane id to the curve. else if (dynamicRender == "Loft") { if (mScene.iCurveList.Count == 1) { if (drawnType != DrawnType.In3D && curveOnObjRef != null) { simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), curveOnObjRef.ObjectId.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); } mScene.iCurveList.Add(simplifiedCurve); } else { oldCurveOnObjID = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString()); oldPlaneOrigin = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneOrigin.ToString()); oldPlaneNormal = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneNormal.ToString()); simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), oldCurveOnObjID); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), oldPlaneOrigin); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), oldPlaneNormal); mScene.iCurveList[1] = simplifiedCurve; } dynamicBrep = UtilOld.LoftFunc(ref mScene, ref mScene.iCurveList); } else if (dynamicRender == "Extrude") { //Rhino.Geometry.Vector3d heightVector = simplifiedCurve.PointAtEnd - simplifiedCurve.PointAtStart; Rhino.Geometry.Vector3d heightVector = simplifiedCurve.PointAtEnd - snapPointsList[0]; Rhino.Geometry.Vector3d planeNormal = UtilOld.getVectorfromString(localListCurve[0].GetUserString(CurveData.PlaneNormal.ToString())); planeNormal.Unitize(); double height = Rhino.Geometry.Vector3d.Multiply(heightVector, planeNormal) / planeNormal.Length; List <Point3d> extrudeCurveP = new List <Point3d>(); Point3d startP = snapPointsList[0]; extrudeCurveP.Add(startP); Point3d endP = new Point3d(startP.X + height * planeNormal.X, startP.Y + height * planeNormal.Y, startP.Z + height * planeNormal.Z); extrudeCurveP.Add(endP); //update the edit curve editCurve = Rhino.Geometry.NurbsCurve.Create(false, 1, extrudeCurveP.ToArray()); if (localListCurve.Count == 1) { if (drawnType != DrawnType.In3D && curveOnObjRef != null) { editCurve.SetUserString(CurveData.CurveOnObj.ToString(), curveOnObjRef.ObjectId.ToString()); editCurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); editCurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); } localListCurve.Add(editCurve); } else { oldCurveOnObjID = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString()); oldPlaneOrigin = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneOrigin.ToString()); oldPlaneNormal = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneNormal.ToString()); editCurve.SetUserString(CurveData.CurveOnObj.ToString(), oldCurveOnObjID); editCurve.SetUserString(CurveData.PlaneOrigin.ToString(), oldPlaneOrigin); editCurve.SetUserString(CurveData.PlaneNormal.ToString(), oldPlaneNormal); localListCurve[1] = editCurve; } dynamicBrep = UtilOld.ExtrudeFunc(ref mScene, ref localListCurve); } else if (dynamicRender == "Sweep") { if (localListCurve.Count == 1) { if (drawnType != DrawnType.In3D && curveOnObjRef != null) { simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), curveOnObjRef.ObjectId.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), curvePlane.Origin.ToString()); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), curvePlane.Normal.ToString()); } localListCurve.Add(simplifiedCurve); } else { oldCurveOnObjID = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.CurveOnObj.ToString()); oldPlaneOrigin = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneOrigin.ToString()); oldPlaneNormal = mScene.iCurveList[mScene.iCurveList.Count - 1].GetUserString(CurveData.PlaneNormal.ToString()); simplifiedCurve.SetUserString(CurveData.CurveOnObj.ToString(), oldCurveOnObjID); simplifiedCurve.SetUserString(CurveData.PlaneOrigin.ToString(), oldPlaneOrigin); simplifiedCurve.SetUserString(CurveData.PlaneNormal.ToString(), oldPlaneNormal); localListCurve[1] = simplifiedCurve; } dynamicBrep = UtilOld.SweepFun(ref mScene, ref localListCurve); } /* * mScene.iCurveList.Clear(); * //mScene.iCurveList = localListCurve; * foreach (Curve c in localListCurve) * { * mScene.iCurveList.Add(c); * } */ }
static internal Rhino.Geometry.Curve ToRhino(this Autodesk.Revit.DB.Curve curve) { switch (curve) { case Autodesk.Revit.DB.Line line: { return(line.IsBound ? new Rhino.Geometry.LineCurve(line.GetEndPoint(0).ToRhino(), line.GetEndPoint(1).ToRhino()) : null); } case Autodesk.Revit.DB.Arc arc: { var plane = new Rhino.Geometry.Plane(arc.Center.ToRhino(), new Vector3d(arc.XDirection.ToRhino()), new Vector3d(arc.YDirection.ToRhino())); if (arc.IsBound) { var p0 = arc.GetEndPoint(0).ToRhino(); var p1 = arc.Evaluate(0.5, true).ToRhino(); var p2 = arc.GetEndPoint(1).ToRhino(); return(new Rhino.Geometry.ArcCurve(new Rhino.Geometry.Arc(p0, p1, p2))); } else { return(new Rhino.Geometry.ArcCurve(new Rhino.Geometry.Circle(plane, arc.Radius))); } } case Autodesk.Revit.DB.Ellipse ellipse: { var plane = new Rhino.Geometry.Plane(ellipse.Center.ToRhino(), new Vector3d(ellipse.XDirection.ToRhino()), new Vector3d(ellipse.YDirection.ToRhino())); var e = new Rhino.Geometry.Ellipse(plane, ellipse.RadiusX, ellipse.RadiusY); var n = e.ToNurbsCurve(); if (ellipse.IsBound) { var t0 = Math.IEEERemainder(ellipse.GetEndParameter(0), 2.0 * Math.PI); var t1 = Math.IEEERemainder(ellipse.GetEndParameter(1), 2.0 * Math.PI); return(n.Trim(t0, t1)); } return(n); } case Autodesk.Revit.DB.HermiteSpline hermite: { return(NurbSpline.Create(hermite).ToRhino()); } case Autodesk.Revit.DB.NurbSpline nurb: { var controlPoints = nurb.CtrlPoints; var n = new Rhino.Geometry.NurbsCurve(3, nurb.isRational, nurb.Degree + 1, controlPoints.Count); if (nurb.isRational) { using (var Weights = nurb.Weights) { var weights = Weights.OfType <double>().ToArray(); int index = 0; foreach (var pt in controlPoints) { var w = weights[index]; n.Points.SetPoint(index++, pt.X * w, pt.Y * w, pt.Z * w, w); } } } else { int index = 0; foreach (var pt in controlPoints) { n.Points.SetPoint(index++, pt.X, pt.Y, pt.Z); } } using (var Knots = nurb.Knots) { int index = 0; foreach (var w in Knots.OfType <double>().Skip(1).Take(n.Knots.Count)) { n.Knots[index++] = w; } } return(n); } case Autodesk.Revit.DB.CylindricalHelix helix: // TODO : default: return(new Rhino.Geometry.PolylineCurve(curve.Tessellate().ToRhino())); } }
public DataContainer CollectData(List <List <object> > dataFromApp) { DataContainer dataContainer = new DataContainer(dataFromApp.Count); for (int i = 0; i < dataFromApp.Count; i++) { DataNode <ParasiteAbstractObject>[] nodeArray = new DataNode <ParasiteAbstractObject> [dataFromApp[i].Count]; for (int j = 0; j < dataFromApp[i].Count; j++) { if (dataFromApp[i][j] is GH_Point p) { if (p.CastTo(out Point3d pt)) { Parasite_Point3d point = ParasiteConversion.ToParasiteType(pt); nodeArray[j] = new DataNode <ParasiteAbstractObject>(point); } } else if (dataFromApp[i][j] is GH_Surface srf) { Brep brep = srf.Value; if (brep.IsSurface) { BrepSurfaceList srfList = brep.Surfaces; if (srfList.Count == 1) { Parasite_NurbsSurface paraSrf = ParasiteConversion.ToParasiteType(srfList[0].ToNurbsSurface()); nodeArray[j] = new DataNode <ParasiteAbstractObject>(paraSrf); } } } else if (dataFromApp[i][j] is GH_Brep b) { Brep brep = b.Value; if (brep.IsSurface && !brep.IsSolid) { Parasite_BrepSurface parasiteBrepSrf = ParasiteConversion.ToParasiteType(brep); nodeArray[j] = new DataNode <ParasiteAbstractObject>(parasiteBrepSrf); } if (!brep.IsSurface && brep.IsSolid) { // dataContainer.Data.Add(new Parasite_BrepSolid(brep)); } } else if (dataFromApp[i][j] is GH_Mesh m) { Rhino.Geometry.Mesh mesh = m.Value; Parasite_Mesh parasiteMesh = ParasiteConversion.ToParasiteType(mesh); nodeArray[j] = new DataNode <ParasiteAbstractObject>(parasiteMesh); } /// CONNVERT GH_CURVE TO PARASITE CURVE else if (dataFromApp[i][j] is GH_Curve curve) { Rhino.Geometry.NurbsCurve nc = curve.Value as NurbsCurve; if (nc.IsArc()) { throw new ParasiteNotImplementedExceptions("Object type not implemented yet!"); } else if (nc.IsCircle()) { throw new ParasiteNotImplementedExceptions("Object type not implemented yet!"); } else if (nc.IsEllipse()) { throw new ParasiteNotImplementedExceptions("Object type not implemented yet!"); } else if (nc.IsPolyline()) { throw new ParasiteNotImplementedExceptions("Object type not implemented yet!"); } else { Parasite_NurbsCurve parasiteNurbsCurve = ParasiteConversion.ToParasiteType(nc); nodeArray[j] = new DataNode <ParasiteAbstractObject>(parasiteNurbsCurve); } } else { throw new ParasiteNotImplementedExceptions("Type conversion not implemented yet!"); } } dataContainer.Data[i] = nodeArray; } return(dataContainer); }