private Rhino.Geometry.Surface ToRhinoSurface(Face face) { Object faceGeometry = face.BasicGeometry; // 1. Compute the base surface // Based on https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_NurbsSurface_Create.htm Topologic.NurbsSurface nurbsSurface = faceGeometry as Topologic.NurbsSurface; if (nurbsSurface != null) { return(ToRhinoNurbsSurface(nurbsSurface)); } Topologic.PlanarSurface planarSurface = faceGeometry as Topologic.PlanarSurface; if (planarSurface != null) { //Topologic.NurbsSurface planarSurfaceAsNurbs = planarSurface.ToNurbsSurface(); //return ToRhinoNurbsSurface(planarSurfaceAsNurbs); PlaneSurface planeSurface = ToRhinoPlaneSurface(planarSurface, face); return(planeSurface); //Rhino.Geometry.NurbsSurface planeSurfaceAsNurbsSurface = planeSurface.ToNurbsSurface(); //return planeSurfaceAsNurbsSurface; } throw new Exception("An invalid surface is created."); }
//private void ProcessFace( // Wire wire, ref Brep ghBrep, BrepFace ghBrepFace, BrepLoopType ghBrepLoopType, Rhino.Geometry.Surface ghSurface, // Dictionary<Edge, Tuple<int, int>> edge2DIndices, Dictionary<Edge, BrepEdge> edgeIndices) //{ // List<Edge> edges = wire.Edges; // BrepLoop ghBrepLoop = ghBrep.Loops.Add(ghBrepLoopType, ghBrepFace); // // 2f.For each loop, add a trim(2D edge) // List<BrepEdge> ghEdges = new List<BrepEdge>(); // List<Tuple<Curve, int, Curve, int>> gh2DCurves = new List<Tuple<Curve, int, Curve, int>>(); // original curve, index, reverse curve, reverse index // foreach (Edge edge in edges) // { // Tuple<int, int> thisEdge2DIndices = edge2DIndices. // Where(edgeIndexPair => edgeIndexPair.Key.IsSame(edge)). // Select(edgeIndexPair => edgeIndexPair.Value). // FirstOrDefault(); // int thisEdge2DIndex = thisEdge2DIndices.Item1; // int thisReverseEdge2DIndex = thisEdge2DIndices.Item2; // Curve ghCurve2D = ghBrep.Curves2D[thisEdge2DIndex]; // Curve ghReverseCurve2D = ghBrep.Curves2D[thisReverseEdge2DIndex]; // gh2DCurves.Add(Tuple.Create(ghCurve2D, thisEdge2DIndex, ghReverseCurve2D, thisReverseEdge2DIndex)); // BrepEdge ghBrepEdge = edgeIndices. // Where(edgeIndexPair => edgeIndexPair.Key.IsSame(edge)). // Select(edgeIndexPair => edgeIndexPair.Value). // FirstOrDefault(); // String ghBrepEdgeLog = ""; // if (!ghBrepEdge.IsValidWithLog(out ghBrepEdgeLog)) // { // throw new Exception("Fails to create a valid Brep with the following message: " + ghBrepEdgeLog); // } // ghEdges.Add(ghBrepEdge); // } // for (int currentEntryID = 0; currentEntryID < gh2DCurves.Count; ++currentEntryID) // { // int previousEntryID = currentEntryID - 1; // if (previousEntryID < 0) // { // previousEntryID = edges.Count - 1; // } // bool isCurrentStartEqualToPreviousStart = gh2DCurves[currentEntryID].Item1.PointAtStart.DistanceTo( // gh2DCurves[previousEntryID].Item1.PointAtStart) < tolerance; // bool isCurrentStartEqualToPreviousEnd = gh2DCurves[currentEntryID].Item1.PointAtStart.DistanceTo( // gh2DCurves[previousEntryID].Item1.PointAtEnd) < tolerance; // bool isTrimReversed = false; // if (!isCurrentStartEqualToPreviousStart && !isCurrentStartEqualToPreviousEnd) // { // // Reverse trim // isTrimReversed = true; // } // BrepTrim ghBrepTrim = ghBrep.Trims.Add( // ghEdges[currentEntryID], // 3D edge // isTrimReversed, // is reversed? // ghBrepLoop, // 2D loop // isTrimReversed ? gh2DCurves[currentEntryID].Item4 : gh2DCurves[currentEntryID].Item2); // 2D curve index, use the reversed one if reversed // ghBrepTrim.IsoStatus = ghSurface.IsIsoparametric(gh2DCurves[currentEntryID].Item1); // ghBrepTrim.TrimType = BrepTrimType.Boundary; // ghBrepTrim.SetTolerances(0.0, 0.0); // String ghBrepTrimLog = ""; // if (!ghBrepTrim.IsValidWithLog(out ghBrepTrimLog)) // { // throw new Exception("Fails to create a valid BrepTrim with the following message: " + ghBrepTrimLog); // } // } // String brepLoopLog = ""; // if (!ghBrepLoop.IsValidWithLog(out brepLoopLog)) // { // throw new Exception("Fails to create a valid outer BrepLoop with the following message: " + brepLoopLog); // } //} private Rhino.Geometry.NurbsSurface ToRhinoNurbsSurface(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) { Topologic.Vertex controlVertex = nurbsSurface.ControlVertex(u, v); ghNurbsSurface.Points.SetPoint(u, v, ToPoint(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); }