void Nurbs2x(Rhino.Geometry.NurbsSurface srf, double[,] _x) { for (int i = 0; i < srf.Points.CountV; i++) { for (int j = 0; j < srf.Points.CountU; j++) { _x[(i * srf.Points.CountU + j), 0] = srf.Points.GetControlPoint(j, i).Location.X; _x[(i * srf.Points.CountU + j), 1] = srf.Points.GetControlPoint(j, i).Location.Y; _x[(i * srf.Points.CountU + j), 2] = srf.Points.GetControlPoint(j, i).Location.Z; } } }
public static Rhino.Geometry.NurbsSurface ToRhino_NurbsSurface(this global::Topologic.NurbsSurface nurbsSurface) { int uDegree = nurbsSurface.UDegree; int vDegree = nurbsSurface.VDegree; bool isRational = nurbsSurface.IsURational && nurbsSurface.IsVRational; int uCount = nurbsSurface.NumOfUControlVertices; int vCount = nurbsSurface.NumOfVControlVertices; Rhino.Geometry.NurbsSurface ghNurbsSurface = Rhino.Geometry.NurbsSurface.Create( 3, isRational, uDegree + 1, vDegree + 1, uCount, vCount ); int i = 0; for (int u = 0; u < uCount; ++u) { for (int v = 0; v < vCount; ++v) { global::Topologic.Vertex controlVertex = nurbsSurface.ControlVertex(u, v); ghNurbsSurface.Points.SetPoint(u, v, ToRhino(controlVertex)); ++i; } } List <double> uKnots = nurbsSurface.UKnots; uKnots = uKnots.GetRange(1, uKnots.Count - 2); for (int u = 0; u < uKnots.Count; u++) { ghNurbsSurface.KnotsU[u] = uKnots[u]; } List <double> vKnots = nurbsSurface.VKnots; vKnots = vKnots.GetRange(1, vKnots.Count - 2); for (int v = 0; v < vKnots.Count; v++) { ghNurbsSurface.KnotsV[v] = vKnots[v]; } if (!ghNurbsSurface.IsValid) { throw new Exception("A valid surface cannot be created from this Face."); } return(ghNurbsSurface); }
private static IntPtr CRhinoObjectGrips_NurbsSurface(int serial_number) { IntPtr rc = IntPtr.Zero; CustomObjectGrips grips = FromSerialNumber(serial_number); if (grips != null) { try { Rhino.Geometry.NurbsSurface ns = grips.NurbsSurface(); if (ns != null) { rc = ns.ConstPointer(); } } catch (Exception ex) { Rhino.Runtime.HostUtils.ExceptionReport(ex); } } return(rc); }
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 surface. This is a good function to override /// to display object-related meshes. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="obj">A Rhino object corresponding to the surface.</param> /// <param name="surface">The surface geometry.</param> /// <param name="pipeline">The current display pipeline.</param> protected virtual void DrawNurbsSurface(Rhino.DocObjects.RhinoObject obj, Rhino.Geometry.NurbsSurface surface, 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); }