Example #1
0
        public static List <Point3d> surfacePoint(Surface x, out int U, out int V) //////得到曲面控制点
        {
            U = 0;                                                                 ///u方向控制点数量
            V = 0;                                                                 ///v方向控制点数量
            NurbsSurface sf = x.ToNurbsSurface();

            Rhino.Geometry.Collections.NurbsSurfacePointList list = sf.Points;
            U = list.CountU;
            V = list.CountV;
            List <Point3d> pts = new List <Point3d>();

            for (int i = 0; i < list.CountU; i++)
            {
                for (int j = 0; j < list.CountV; j++)
                {
                    ControlPoint ps = list.GetControlPoint(i, j);
                    pts.Add(ps.Location);
                }
            }
            return(pts);
        }
Example #2
0
        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);
        }