/// <summary> /// Create Edge (Line or Circle or Arc) from Rhino NurbsCurve. /// </summary> public static Geometry.Edge FromRhinoNurbsCurve(this Rhino.Geometry.NurbsCurve obj) { // check if NurbsCurve is a line if (obj.IsLinear()) { return(FromRhinoLinearNurbsCurve(obj)); } // check if NurbsCurve is a circle else if (obj.IsArc() && obj.IsClosed) { obj.TryGetArc(out Rhino.Geometry.Arc _obj); return(FromRhinoCircle(new Rhino.Geometry.ArcCurve(_obj))); } // check if NurbsCurve is an arc else if ((obj.IsArc() || obj.Degree == 2) && !obj.IsClosed) { obj.TryGetArc(out Rhino.Geometry.Arc _obj); return(FromRhinoArc1(new Rhino.Geometry.ArcCurve(_obj))); } else { throw new System.ArgumentException($"Unsupported Curve. Degree of NurbsCurve is likely to high. Degree is: {obj.Degree}, versus a supported degree of 2."); } }
void Nurbs2x(Rhino.Geometry.NurbsCurve crv, double[,] _x) { for (int i = 0; i < crv.Points.Count; i++) { _x[i, 0] = crv.Points[i].Location.X; _x[i, 1] = crv.Points[i].Location.Y; _x[i, 2] = crv.Points[i].Location.Z; } }
public bool PickFrustumTest(Rhino.Geometry.NurbsCurve curve, out double t, out double depth, out double distance) { t = -1; depth = -1; distance = -1; IntPtr pConstThis = ConstPointer(); IntPtr pConstCurve = curve.ConstPointer(); return(UnsafeNativeMethods.CRhinoPickContext_PickNurbsCurve(pConstThis, pConstCurve, ref t, ref depth, ref distance)); }
/// <summary> /// Create Edge (Line) from Rhino linear NurbsCurve. /// </summary> public static Geometry.Edge FromRhinoLinearNurbsCurve(this Rhino.Geometry.NurbsCurve obj) { FdPoint3d startPoint = obj.PointAtStart.FromRhino(); FdPoint3d endPoint = obj.PointAtEnd.FromRhino(); // lcs FdCoordinateSystem cs = obj.FromRhinoCurve(); // return return(new Geometry.Edge(startPoint, endPoint, cs)); }
protected override Result RunCommand(RhinoDoc doc, RunMode mode) { 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 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 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 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); }
static void OnDrawGeometryProc(Guid am_id, IntPtr pConstRhinoObject, IntPtr pConstGeometry, IntPtr pRhinoDisplayPipeline) { VisualAnalysisMode mode = FindLocal(am_id); if (mode != null) { var rhobj = Rhino.DocObjects.RhinoObject.CreateRhinoObjectHelper(pConstRhinoObject); var geom = Rhino.Geometry.GeometryBase.CreateGeometryHelper(pConstGeometry, null); if (geom != null) { geom.DoNotDestructOnDispose(); } DisplayPipeline dp = new DisplayPipeline(pRhinoDisplayPipeline); Rhino.Geometry.Mesh mesh = geom as Rhino.Geometry.Mesh; try { if (mesh != null) { mode.DrawMesh(rhobj, mesh, dp); return; } Rhino.Geometry.NurbsCurve nurbscurve = geom as Rhino.Geometry.NurbsCurve; if (nurbscurve != null) { mode.DrawNurbsCurve(rhobj, nurbscurve, dp); return; } Rhino.Geometry.NurbsSurface nurbssurf = geom as Rhino.Geometry.NurbsSurface; if (nurbssurf != null) { mode.DrawNurbsSurface(rhobj, nurbssurf, dp); return; } } catch (Exception) { } } }
/// <summary> /// Draws a NURBS curve. This is a good function to override for /// analysis modes like curvature hair display. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="obj">A Rhino object corresponding to the curve.</param> /// <param name="curve">The curve geometry.</param> /// <param name="pipeline">The current display pipeline.</param> protected virtual void DrawNurbsCurve(Rhino.DocObjects.RhinoObject obj, Rhino.Geometry.NurbsCurve curve, DisplayPipeline pipeline) { }
private static DirectShapeInfo ConvertBrep(Document doc, RhinoObjectInfo rhinoInfo) { DirectShapeInfo shapeInfo = new DirectShapeInfo(); try { BRepBuilder brepBuilder = null; Rhino.Geometry.Brep rhinoBrep = rhinoInfo.Geometry as Rhino.Geometry.Brep; if (rhinoBrep.IsSolid) { brepBuilder = new BRepBuilder(BRepType.Solid); } else if (rhinoBrep.IsSurface) { brepBuilder = new BRepBuilder(BRepType.OpenShell); } foreach (Rhino.Geometry.BrepFace brepFace in rhinoBrep.Faces) { BRepBuilderGeometryId nurbSplineFaceId = BRepBuilderGeometryId.InvalidGeometryId(); BRepBuilderGeometryId loopId = BRepBuilderGeometryId.InvalidGeometryId(); bool reverse = brepFace.OrientationIsReversed; if (brepFace.ObjectType == Rhino.DocObjects.ObjectType.Surface) { Rhino.Geometry.NurbsSurface nurbsSurface = brepFace.ToNurbsSurface(); Rhino.Geometry.Collections.NurbsSurfacePointList points = nurbsSurface.Points; int dirU = points.CountU; int dirV = points.CountV; int degreeU = nurbsSurface.Degree(0); int degreeV = nurbsSurface.Degree(1); // knots Rhino.Geometry.Collections.NurbsSurfaceKnotList knotsU = nurbsSurface.KnotsU; Rhino.Geometry.Collections.NurbsSurfaceKnotList knotsV = nurbsSurface.KnotsV; List <XYZ> controlPoints = new List <XYZ>(); XYZ[][] rvtPoints = new XYZ[dirU][]; double[][] rvtWeights = new double[dirU][]; for (int u = 0; u < dirU; u++) { rvtPoints[u] = new XYZ[dirV]; rvtWeights[u] = new double[dirV]; for (int v = 0; v < dirV; v++) { // point coordinates at u, v Rhino.Geometry.Point3d pt = points.GetControlPoint(u, v).Location; XYZ xyz = new XYZ(pt.X, pt.Y, pt.Z); rvtPoints[u][v] = xyz; controlPoints.Add(xyz); // weights at u, v rvtWeights[u][v] = points.GetControlPoint(u, v).Weight; } } // knots U List <double> rvt_knotsU = new List <double>(); rvt_knotsU.Add(knotsU[0]); for (int i = 0; i < knotsU.Count; i++) { rvt_knotsU.Add(knotsU[i]); } rvt_knotsU.Add(knotsU[knotsU.Count - 1]); // knots V List <double> rvt_knotsV = new List <double>(); rvt_knotsV.Add(knotsV[0]); for (int i = 0; i < knotsV.Count; i++) { rvt_knotsV.Add(knotsV[i]); } rvt_knotsV.Add(knotsV[knotsV.Count - 1]); BRepBuilderSurfaceGeometry brepSurface = BRepBuilderSurfaceGeometry.CreateNURBSSurface(degreeU, degreeV, rvt_knotsU, rvt_knotsV, controlPoints, reverse, null); nurbSplineFaceId = brepBuilder.AddFace(brepSurface, reverse); loopId = brepBuilder.AddLoop(nurbSplineFaceId); } foreach (Rhino.Geometry.BrepLoop loop in brepFace.Loops) { Rhino.Geometry.Curve curve = loop.To3dCurve(); Rhino.Geometry.NurbsCurve nurbsCurve = curve.ToNurbsCurve(); int degree = nurbsCurve.Degree; List <XYZ> controlPoints = new List <XYZ>(); List <double> weights = new List <double>(); List <double> knots = new List <double>(); for (int i = 0; i < nurbsCurve.Points.Count; i++) { Rhino.Geometry.ControlPoint ctrlPoint = nurbsCurve.Points[i]; Rhino.Geometry.Point3d point = ctrlPoint.Location; XYZ xyz = new XYZ(point.X, point.Y, point.Z); controlPoints.Add(xyz); weights.Add(ctrlPoint.Weight); } knots.Add(nurbsCurve.Knots[0]); for (int i = 0; i < nurbsCurve.Knots.Count; i++) { double knot = nurbsCurve.Knots[i]; knots.Add(knot); } knots.Add(nurbsCurve.Knots[nurbsCurve.Knots.Count - 1]); Curve rvtCurve = NurbSpline.CreateCurve(degree, knots, controlPoints, weights); BRepBuilderEdgeGeometry edgeGeo = BRepBuilderEdgeGeometry.Create(rvtCurve); BRepBuilderGeometryId edgeId = brepBuilder.AddEdge(edgeGeo); brepBuilder.AddCoEdge(loopId, edgeId, false); } brepBuilder.FinishLoop(loopId); brepBuilder.FinishFace(nurbSplineFaceId); } brepBuilder.Finish(); DirectShape shape = DirectShape.CreateElement(doc, new ElementId((int)BuiltInCategory.OST_GenericModel)); shape.ApplicationId = "RhinoBrep"; shape.ApplicationDataId = rhinoInfo.ObjectId.ToString(); if (null != shape) { shape.SetShape(brepBuilder); } shapeInfo.DirectShapeId = shape.Id; shapeInfo.RhinoObjectId = rhinoInfo.ObjectId; } catch (Exception ex) { string message = ex.Message; MessageBox.Show("Cannot Conver To Brep.\n" + ex.Message); } return(shapeInfo); }
/// <summary> /// Get Grevit Curve Component /// </summary> /// <param name="curve"></param> /// <param name="referenceID"></param> /// <returns></returns> public static Component ToGrevitCurve(this Rhino.Geometry.Curve curve, string referenceID = "") { if (curve.IsArc(Rhino.RhinoMath.ZeroTolerance)) { Rhino.Geometry.Arc a; if (curve.TryGetArc(out a)) { Curve3Points arc = new Curve3Points(); arc.a = curve.PointAtStart.ToGrevitPoint(); arc.b = curve.PointAt(0.5).ToGrevitPoint(); arc.c = curve.PointAtEnd.ToGrevitPoint(); arc.GID = referenceID; return(arc); } } else if (curve.IsPolyline()) { Rhino.Geometry.Polyline pline; if (curve.TryGetPolyline(out pline)) { PLine arc = new PLine(); arc.points = new List <Grevit.Types.Point>(); foreach (Rhino.Geometry.Point3d pkt in pline) { arc.points.Add(pkt.ToGrevitPoint()); } arc.closed = pline.IsClosed; arc.GID = referenceID; return(arc); } } else if (curve.IsEllipse()) { Curve3Points arc = new Curve3Points(); arc.a = curve.PointAtStart.ToGrevitPoint(); arc.b = curve.PointAt(0.5).ToGrevitPoint(); arc.c = curve.PointAtEnd.ToGrevitPoint(); arc.GID = referenceID; return(arc); } else if (curve.GetType() == typeof(Rhino.Geometry.NurbsCurve)) { Rhino.Geometry.NurbsCurve nc = (Rhino.Geometry.NurbsCurve)curve; Grevit.Types.Spline spline = new Grevit.Types.Spline(); spline.controlPoints = new List <Grevit.Types.Point>(); spline.weights = new List <double>(); foreach (Rhino.Geometry.ControlPoint p in nc.Points) { spline.controlPoints.Add(p.Location.ToGrevitPoint()); spline.weights.Add(p.Weight); } spline.degree = nc.Degree; spline.isClosed = nc.IsClosed; spline.isRational = nc.IsRational; spline.isPeriodic = nc.IsPeriodic; spline.GID = referenceID; spline.knots = new List <double>(); foreach (double dbl in nc.Knots) { spline.knots.Add(dbl); } return(spline); } else { Line arc = new Line(); arc.from = curve.PointAtStart.ToGrevitPoint(); arc.to = curve.PointAtEnd.ToGrevitPoint(); arc.GID = referenceID; return(arc); } return(null); }
/// <summary> /// Splits <paramref name="curve"/> as a <see cref="Rhino.Geometry.PolyCurve"/> where knot multiplicity is > degree - 2. /// </summary> /// <remarks> /// Collapses knots using <paramref name="knotTolerance"/>. /// </remarks> /// <param name="curve"></param> /// <param name="polyCurve"></param> /// <param name="knotTolerance"></param> /// <returns>false if no new polycurve is created.</returns> public static bool TryGetPolyCurveC2(this Rhino.Geometry.NurbsCurve curve, out Rhino.Geometry.PolyCurve polyCurve, double knotTolerance = KnotTolerance) { bool duplicate = false; var spans = default(List <double>); var degree = curve.Degree; var knots = curve.Knots; for (int k = degree; k < knots.Count - degree;) { var multiplicity = KnotMultiplicity(knots, k, knotTolerance, out var _, out var strict); if (multiplicity > degree - 2) { if (spans is null) { spans = new List <double>(); } spans.Add(knots[k]); } if (!strict) { // We are going to modify curve so we need a duplicate here if (!duplicate) { curve = curve.Duplicate() as Rhino.Geometry.NurbsCurve; duplicate = true; } var excess = multiplicity - knots.KnotMultiplicity(k); // Correct multiplicity in case more than degree knots are snapped here multiplicity = Math.Min(multiplicity, degree); // Insert new knot multiplicity knots.InsertKnot(knots[k], multiplicity); // Remove old knots that do not overlap knots[k] if (excess > 0) { knots.RemoveKnots(k + multiplicity, k + multiplicity + excess); } } k += multiplicity; } if (spans is null) { polyCurve = default; return(false); } polyCurve = new Rhino.Geometry.PolyCurve(); foreach (var span in curve.Split(spans)) { polyCurve.AppendSegment(span); } // Split may generate PolyCurves on seams. if (curve.IsClosed) { polyCurve.RemoveNesting(); } return(true); }