コード例 #1
0
        private Brep ToSurface(Face face, double tolerance)
        {
            Rhino.Geometry.Surface ghSurface = ToRhinoSurface(face);

            double width = 0.0, height = 0.0;
            bool   canGetSurfaceSize = ghSurface.GetSurfaceSize(out width, out height);

            if (!canGetSurfaceSize)
            {
                throw new Exception("Fails to get the surface size.");
            }
            double maxSize          = Math.Max(width, height);
            double maxSizeAndMargin = maxSize + 2;

            ghSurface = ghSurface.Extend(IsoStatus.North, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.South, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.West, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.East, maxSizeAndMargin, true);
            //List<Wire> wires = face.Wires;

            //List<GeometryBase> ghGeometryBases = new List<GeometryBase>();
            //foreach (Wire wire in wires)
            //{
            //    List<Object> ghCurves = ToCurves(wire);
            //    foreach (Object ghCurve in ghCurves)
            //    {
            //        GeometryBase geometryBase = ghCurve as GeometryBase;
            //        if (geometryBase != null)
            //        {
            //            ghGeometryBases.Add(geometryBase);
            //        }
            //    }
            //}


            //2.Create the Brep
            //Brep ghBrep = ghNurbsSurface.ToBrep();
            //Brep ghBrep = new Brep();

            //// 2a. Add vertices
            //List<Vertex> vertices = face.Vertices;
            //foreach (Vertex vertex in vertices)
            //{
            //    Point3d ghPoint = ToPoint(vertex);
            //    BrepVertex ghBrepVertex = ghBrep.Vertices.Add(ghPoint, 0.0);

            //    String ghBrepVertexLog = "";
            //    if (!ghBrepVertex.IsValidWithLog(out ghBrepVertexLog))
            //    {
            //        throw new Exception("Fails to create a valid BrepVertex with the following message: " + ghBrepVertexLog);
            //    }
            //}

            //// 2b. Add 3D curves and edges. The index dictionaries are used to identify the IDs of the edges.
            //List<Edge> edges = face.Edges;
            //Dictionary<Edge, Tuple<int, int>> edge2DIndices = new Dictionary<Edge, Tuple<int, int>>(); // edge, curve, reverse curve
            //Dictionary<Edge, int> edge3DIndices = new Dictionary<Edge, int>();
            //Dictionary<Edge, BrepEdge> edgeIndices = new Dictionary<Edge, BrepEdge>();
            //foreach (Edge edge in edges)
            //{
            //    Curve ghCurve3D = ToCurve(edge);
            //    Curve ghCurve2D = ghSurface.Pullback(ghCurve3D, 0.0001);

            //    // 2D curves --> need to check if the endpoints are near to previously generated points
            //    // if yes, change the value

            //    int curve3DID = ghBrep.Curves3D.Add(ghCurve3D);
            //    int curve2DID = ghBrep.Curves2D.Add(ghCurve2D);

            //    Curve ghReverseCurve2D = ghCurve2D.DuplicateCurve();
            //    ghReverseCurve2D.Reverse();
            //    int reverseCurve2DID = ghBrep.Curves2D.Add(ghReverseCurve2D);

            //    Point3d ghStartPoint = ghCurve3D.PointAtStart;
            //    Point3d ghEndPoint = ghCurve3D.PointAtEnd;

            //    int startVertexIndex = -1;
            //    int endVertexIndex = -1;
            //    foreach (BrepVertex ghBrepVertex in ghBrep.Vertices)
            //    {
            //        Point3d ghBrepPoint = ghBrepVertex.Location;

            //        if (startVertexIndex == -1 && ghBrepPoint.DistanceTo(ghStartPoint) < 0.0001)
            //        {
            //            startVertexIndex = ghBrepVertex.VertexIndex;
            //        }

            //        if (endVertexIndex == -1 && ghBrepPoint.DistanceTo(ghEndPoint) < 0.0001)
            //        {
            //            endVertexIndex = ghBrepVertex.VertexIndex;
            //        }
            //    }

            //    BrepEdge ghBrepEdge = ghBrep.Edges.Add(startVertexIndex, endVertexIndex, curve3DID, 0.0001);

            //    String brepEdgeLog = "";
            //    if (!ghBrepEdge.IsValidWithLog(out brepEdgeLog))
            //    {
            //        throw new Exception("Fails to create a valid BrepEdge with the following message: " + brepEdgeLog);
            //    }

            //    edge3DIndices.Add(edge, curve3DID);
            //    edge2DIndices.Add(edge, Tuple.Create(curve2DID, reverseCurve2DID));
            //    edgeIndices.Add(edge, ghBrepEdge);
            //}

            //// 2c. Add surface
            //int ghSurfaceIndex = ghBrep.AddSurface(ghSurface);

            //// 2d. Add face
            //BrepFace ghBrepFace = ghBrep.Faces.Add(ghSurfaceIndex);

            //// 2e. Create outer loop
            //Wire outerWire = face.ExternalBoundary;
            //ProcessFace(outerWire, ref ghBrep, ghBrepFace, BrepLoopType.Outer, ghSurface, edge2DIndices, edgeIndices);

            //// 2g. Create inner loops
            //List<Wire> innerWires = face.InternalBoundaries;
            //foreach (Wire innerWire in innerWires)
            //{
            //    ProcessFace(innerWire, ref ghBrep, ghBrepFace, BrepLoopType.Inner, ghSurface, edge2DIndices, edgeIndices);
            //}

            //String brepFaceLog = "";
            //if (!ghBrepFace.IsValidWithLog(out brepFaceLog))
            //{
            //    throw new Exception("Fails to create a valid Face with the following message: " + brepFaceLog);
            //}

            //ghBrep.Compact();

            List <GeometryBase> ghGeometryBases = new List <GeometryBase>();

            List <Edge>  outerEdges = face.ExternalBoundary.Edges;
            List <Curve> ghCurves   = new List <Curve>();

            foreach (Edge edge in outerEdges)
            {
                Curve ghCurve3D = ToCurve(edge);
                ghGeometryBases.Add(ghCurve3D);
                ghCurves.Add(ghCurve3D);
            }
            //ghGeometryBases.AddRange(ghBrep.Curves2D);
            //ghGeometryBases.AddRange(ghBrep.Trims);
            //ghGeometryBases.AddRange(ghBrep.Vertices);

            Brep ghBrep2 = Brep.CreatePatch(
                ghGeometryBases,
                ghSurface,
                20,
                20,
                true,
                true,
                tolerance,
                100.0,
                1,
                new Boolean[] { true, true, true, true },
                tolerance);
            //return ghBrep2; // always returns the trimmed surface

            BrepFace ghSurfaceAsBrepFace = ghSurface as BrepFace;

            if (ghBrep2 == null)
            {
                return(null);
            }

            List <Wire> internalBoundaries = face.InternalBoundaries;

            if (internalBoundaries.Count == 0)
            {
                return(ghBrep2);
            }

            BrepFace ghBrepFace = ghBrep2.Faces[0];
            //return ghBrepFace.ToBrep(); // may return the untrimmed surface!

            List <Curve> ghInternalCurves = new List <Curve>();

            foreach (Wire internalBoundary in internalBoundaries)
            {
                List <Object> ghCurvesFromWireAsObjects = ToCurves(internalBoundary);
                foreach (Object ghCurveFromWireAsObject in ghCurvesFromWireAsObjects)
                {
                    Curve ghCurveFromWire = ghCurveFromWireAsObject as Curve;
                    if (ghCurveFromWire != null)
                    {
                        Curve[] ghPulledCurveFromWire = ghCurveFromWire.PullToBrepFace(ghBrepFace, tolerance);
                        ghInternalCurves.AddRange(ghPulledCurveFromWire);
                    }
                }
            }
            //ghCurves.AddRange(ghInternalCurves);
            Brep ghBrep3 = ghBrepFace.Split(ghInternalCurves, tolerance);

            //return ghBrep3.Faces[0].DuplicateFace(true);
            return(ghBrep3.Faces.ExtractFace(0));
            //Brep[] ghBreps3 = ghBrep2.Split(ghInternalCurves, 0.0001);
            //return ghBreps3.First();


            //String brepLog = "";
            //if (!ghBrep.IsValidWithLog(out brepLog))
            //{
            //    throw new Exception("Fails to create a valid Brep with the following message: " + brepLog);
            //}

            //if (!ghBrep.IsValidGeometry(out brepLog))
            //{
            //    throw new Exception("Fails to create a valid Brep with the following message: " + brepLog);
            //}

            //if (!ghBrep.IsValidTopology(out brepLog))
            //{
            //    throw new Exception("Fails to create a valid Brep with the following message: " + brepLog);
            //}

            //if (!ghBrep.IsValidTolerancesAndFlags(out brepLog))
            //{
            //    throw new Exception("Fails to create a valid Brep with the following message: " + brepLog);
            //}


            //return ghBrep;
        }
コード例 #2
0
        public static Brep ToRhino(global::Topologic.Face face, double tolerance = Core.Tolerance.Distance)
        {
            Rhino.Geometry.Surface ghSurface = ToRhino(face);

            double width = 0.0, height = 0.0;
            bool   canGetSurfaceSize = ghSurface.GetSurfaceSize(out width, out height);

            if (!canGetSurfaceSize)
            {
                throw new Exception("Fails to get the surface size.");
            }
            double maxSize          = Math.Max(width, height);
            double maxSizeAndMargin = maxSize + 2;

            ghSurface = ghSurface.Extend(IsoStatus.North, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.South, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.West, maxSizeAndMargin, true);
            ghSurface = ghSurface.Extend(IsoStatus.East, maxSizeAndMargin, true);

            List <GeometryBase> ghGeometryBases = new List <GeometryBase>();

            IList <Edge> outerEdges = face.ExternalBoundary.Edges;
            List <Curve> ghCurves   = new List <Curve>();

            foreach (Edge edge in outerEdges)
            {
                Curve ghCurve3D = ToRhino(edge);
                ghGeometryBases.Add(ghCurve3D);
                ghCurves.Add(ghCurve3D);
            }

            Brep ghBrep2 = Brep.CreatePatch(
                ghGeometryBases,
                ghSurface,
                20,
                20,
                true,
                true,
                tolerance,
                100.0,
                1,
                new Boolean[] { true, true, true, true },
                tolerance);

            BrepFace ghSurfaceAsBrepFace = ghSurface as BrepFace;

            if (ghBrep2 == null)
            {
                return(null);
            }

            IList <Wire> internalBoundaries = face.InternalBoundaries;

            if (internalBoundaries.Count == 0)
            {
                return(ghBrep2);
            }

            BrepFace ghBrepFace = ghBrep2.Faces[0];

            List <Curve> ghInternalCurves = new List <Curve>();

            foreach (Wire internalBoundary in internalBoundaries)
            {
                List <Curve> ghCurvesFromWireAsObjects = ToRhino(internalBoundary);
                foreach (Curve ghCurveFromWireAsObject in ghCurvesFromWireAsObjects)
                {
                    Curve ghCurveFromWire = ghCurveFromWireAsObject as Curve;
                    if (ghCurveFromWire != null)
                    {
                        Curve[] ghPulledCurveFromWire = ghCurveFromWire.PullToBrepFace(ghBrepFace, tolerance);
                        ghInternalCurves.AddRange(ghPulledCurveFromWire);
                    }
                }
            }

            Brep ghBrep3 = ghBrepFace.Split(ghInternalCurves, tolerance);

            return(ghBrep3.Faces.ExtractFace(0));
        }