コード例 #1
0
        private GH_Brep NonPlanarBrep(double depth, IList <PolylineCurve> curveList)
        {
            var nonPlanarBrep = new List <Brep>();
            var topBrep       = Brep.CreatePatch(new[] { curveList[0] }, 5, 5, _tolerance[0]);

            nonPlanarBrep.Add(topBrep);

            BrepFace face       = topBrep.Faces[0];
            Vector3d faceNormal = face.NormalAt(face.Domain(0).Mid, face.Domain(1).Mid);

            if (Vector3d.VectorAngle(faceNormal, Vector3d.ZAxis) < Vector3d.VectorAngle(faceNormal, -Vector3d.ZAxis))
            {
                faceNormal = -faceNormal;
            }

            Brep bottomBrep = topBrep.DuplicateBrep();

            bottomBrep.Translate(faceNormal * depth);
            nonPlanarBrep.Add(bottomBrep);

            IEnumerable <Curve> edgeCurveList = topBrep.Edges.Select(edge => edge.DuplicateCurve());

            nonPlanarBrep.AddRange(edgeCurveList.Select(edgeCurve =>
                                                        Surface.CreateExtrusion(edgeCurve, faceNormal * depth).ToBrep()));
            return(new GH_Brep(Brep.JoinBreps(nonPlanarBrep, _tolerance[0])[0] ?? topBrep));
        }
コード例 #2
0
ファイル: CreatePatch.cs プロジェクト: lanicon/SparrowHawk
        public void renderPatch()
        {
            //Brep patchSurface = Brep.CreatePatch(curvelist, 4, 4, mScene.rhinoDoc.ModelAbsoluteTolerance);
            Brep patchSurface = Brep.CreatePatch(allPoints, 10, 10, mScene.rhinoDoc.ModelAbsoluteTolerance);
            Guid planGuid     = UtilOld.addRhinoObjectSceneNode(ref mScene, ref patchSurface, ref mesh_m, "patchSurface", out planeSN);

            //clear profile curves
            foreach (Guid id in curveGuids)
            {
                foreach (SceneNode sn in mScene.tableGeometry.children)
                {
                    if (sn.guid == id)
                    {
                        mScene.tableGeometry.children.Remove(sn);
                        break;
                    }
                }
            }

            allPoints.Clear();
            curvelist.Clear();
        }
コード例 #3
0
        private GH_Brep CreateSlabBrep(double depth, IList <PolylineCurve> curveList, IEnumerable <Point3d> topPts)
        {
            if (depth > 0)
            {
                Vector3d normal = Vector3d.CrossProduct(curveList[0].TangentAtEnd, curveList[0].TangentAtStart);
                curveList[1] = new PolylineCurve(topPts.Select(pt => pt - normal * depth));
                Brep loftBrep  = Brep.CreateFromLoft(curveList, Point3d.Unset, Point3d.Unset, LoftType.Straight, false)[0];
                Brep capedBrep = loftBrep.CapPlanarHoles(_tolerance[0]);

                if (capedBrep == null)
                {
                    return(NonPlanarBrep(depth, curveList));
                }
                CheckBrepOrientation(capedBrep);

                return(new GH_Brep(capedBrep));
            }

            return(new GH_Brep(curveList[0].IsPlanar()
                     ? Brep.CreatePlanarBreps(curveList[0], _tolerance[0])[0]
                     : Brep.CreatePatch(new[] { curveList[0] }, 5, 5, _tolerance[0])));
        }
コード例 #4
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;
        }
コード例 #5
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));
        }
コード例 #6
0
        public Brep CreateBrep()
        {
            List <Point3d> curvePts = Vertices.Select(x => x.Point).ToList();

            //var brpPoints = curvePts.Select(x => new Point(x)).ToList();
            curvePts.Add(curvePts[0]);


            Polyline pl     = new Polyline(curvePts);
            Curve    plcrv  = pl.ToNurbsCurve();
            var      result = Brep.CreatePlanarBreps(plcrv, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);

            if (result != null && result.Length == 1)
            {
                //result[0].SetVertices();
                return(result[0]);
            }
            if (Vertices.Count == 4)
            {
                var bFace = Brep.CreateFromCornerPoints(Vertices[0].Point,
                                                        Vertices[1].Point, Vertices[2].Point, Vertices[3].Point, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
                //bFace.SetVertices();
                return(bFace);
            }

            if (Vertices.Count == 3)
            {
                var bFace = Brep.CreateFromCornerPoints(Vertices[0].Point,
                                                        Vertices[1].Point, Vertices[2].Point, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
                //bFace.SetVertices();
                return(bFace);
            }

            ComputeCentroid();

            var brepFace = Brep.CreatePatch(new List <GeometryBase>()
            {
                plcrv, new Point(Centroid)
            }, 5, 5, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance / 1000);

            //var brepFace = Brep.CreateFromMesh(FMesh, true);


            //brpPoints.Add(new Point(Centroid));

            //brepFace.SetVertices();
            return(brepFace);

            /*
             * else
             * {
             *  Brep faceBrep = new Brep();
             *  ComputeCentroid();
             *  for (int i = 0; i < Vertices.Count; i++)
             *  {
             *      faceBrep.Append(Brep.CreateFromCornerPoints(Centroid, Vertices[i].Point, Vertices[(i + 1) % Vertices.Count].Point,
             *          Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance));
             *  }
             *  faceBrep.JoinNakedEdges(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
             *  return faceBrep;
             * }
             *
             *
             */
        }
コード例 #7
0
ファイル: KDG.cs プロジェクト: 11o9/TuringAndCorbusier
        /////////////////////////////////

        public Vector3d normalDirection(List <Point3d> groundPoints, Polyline outline, out double slopeRatio, out Brep ground)
        {
            //create patch

            double MCminZ = 0;


            var outrectsegs = TuringAndCorbusierPlugIn.InstanceClass.kdgInfoSet.outrect.DuplicateSegments();

            foreach (var seg in outrectsegs)
            {
                groundPoints.Add(seg.PointAtStart);
                groundPoints.Add(seg.PointAtEnd);
            }


            foreach (Point3d pointz in groundPoints)
            {
                if (pointz.Z < MCminZ)
                {
                    MCminZ = pointz.Z;
                }
            }

            BoundingBox bb = new BoundingBox(groundPoints);

            var      dgnl        = bb.Diagonal;
            var      cp          = bb.Center;
            Vector3d dgnl_shadow = new Vector3d(dgnl.X, dgnl.Y, 0);

            List <Point3d> external_points = new List <Point3d>();

            Point3d p1 = new Point3d(cp) + dgnl_shadow;
            Point3d p2 = new Point3d(cp) - dgnl_shadow;

            Vector3d dgnl_shadow_perp = new Vector3d();

            var zz = dgnl_shadow_perp.PerpendicularTo(dgnl_shadow);

            Point3d p3 = new Point3d(cp) - dgnl_shadow_perp;
            Point3d p4 = new Point3d(cp) + dgnl_shadow_perp;


            external_points.Add(p1);
            external_points.Add(p2);
            external_points.Add(p3);
            external_points.Add(p4);

            List <Rhino.Geometry.Point> pts = groundPoints.Select(n => new Rhino.Geometry.Point(n)).ToList();

            pts.AddRange(external_points.Select(n => new Rhino.Geometry.Point(n)).ToList());

            Brep patch = Brep.CreatePatch(pts, 30, 30, 1);

            Surface surface = patch.Surfaces[0];

            //Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(patch);

            //evaluate surface & extract x and y from average normal
            int    resolution = 10;
            double xDir       = 0;
            double yDir       = 0;

            for (int i = 0; i < resolution + 1; i++)
            {
                for (int j = 0; j < resolution + 1; j++)
                {
                    Point3d    point;
                    Vector3d[] derivatives;
                    surface.Evaluate((double)i / (double)resolution, (double)j / (double)resolution, 1, out point, out derivatives);

                    Vector3d normal = Vector3d.CrossProduct(derivatives[0], derivatives[1]);
                    normal.Unitize();
                    xDir += normal.X;
                    yDir += normal.Y;
                }
            }

            //unit vector of average normal direction
            Vector3d avrgNormal = new Vector3d(xDir, yDir, 0);

            avrgNormal.Unitize();

            ////create line along average normal vector from polyline center, and get longest intersecting segment
            //Point3d center = outline.CenterPoint();
            //Point3d lineEnd = new Point3d(center);
            //lineEnd.Transform(Transform.Translation(-outline.BoundingBox.Diagonal.Length * avrgNormal));



            //Line dirLine = new Line(lineEnd, outline.BoundingBox.Diagonal.Length * avrgNormal * 2);
            //Rhino.RhinoDoc.ActiveDoc.Objects.AddLine(dirLine);

            //Rhino.RhinoDoc.ActiveDoc.Objects.AddPolyline(outline);

            //var lxp = Rhino.Geometry.Intersect.Intersection.CurveCurve(dirLine.ToNurbsCurve(), outline.ToNurbsCurve(), 0, 0);

            ////get end points of segment and project onto the patch
            //Point3d pt1 = lxp[0].PointA;
            //Point3d pt2 = lxp[lxp.Count - 1].PointA;

            //double moveL = patch.GetBoundingBox(false).Diagonal.Length;
            //pt1.Transform(Transform.Translation(-moveL * Vector3d.ZAxis));
            //pt2.Transform(Transform.Translation(-moveL * Vector3d.ZAxis));

            //List<Brep> brep = new List<Brep>();
            //brep.Add(patch);
            //List<Point3d> twoPts = new List<Point3d>();
            //twoPts.Add(pt1);
            //twoPts.Add(pt2);

            //Point3d ptpt1 = Rhino.Geometry.Intersect.Intersection.ProjectPointsToBreps(brep, twoPts, Vector3d.ZAxis, 0)[0];
            //Point3d ptpt2 = Rhino.Geometry.Intersect.Intersection.ProjectPointsToBreps(brep, twoPts, Vector3d.ZAxis, 0)[1];

            slopeRatio = 0;

            //create line from projected points and calculate slope ratio
            //Line slopeLine = new Line(ptpt1, ptpt2);
            //slopeRatio = slopeLine.Direction.Z / Math.Pow(Math.Pow(slopeLine.Direction.X, 2) + Math.Pow(slopeLine.Direction.Y, 2), 0.5);

            ground = patch;
            return(avrgNormal);
        }