예제 #1
0
        public static DB.Mesh ToHost(Mesh mesh)
        {
            using
            (
                var builder = new DB.TessellatedShapeBuilder()
            {
                GraphicsStyleId = GeometryEncoder.Context.Peek.GraphicsStyleId,
                Target = DB.TessellatedShapeBuilderTarget.Mesh,
                Fallback = DB.TessellatedShapeBuilderFallback.Salvage
            }
            )
            {
                var vertices = mesh.Vertices.ToPoint3dArray();
                var triangle = new DB.XYZ[3];
                var quad     = new DB.XYZ[4];

                var isSolid = mesh.SolidOrientation() != 0;
                builder.OpenConnectedFaceSet(isSolid);

                foreach (var face in mesh.Faces)
                {
                    if (face.IsQuad)
                    {
                        quad[0] = ToHost(vertices[face.A]);
                        quad[1] = ToHost(vertices[face.B]);
                        quad[2] = ToHost(vertices[face.C]);
                        quad[3] = ToHost(vertices[face.D]);

                        builder.AddFace(new DB.TessellatedFace(quad, GeometryEncoder.Context.Peek.MaterialId));
                    }
                    else
                    {
                        triangle[0] = ToHost(vertices[face.A]);
                        triangle[1] = ToHost(vertices[face.B]);
                        triangle[2] = ToHost(vertices[face.C]);

                        builder.AddFace(new DB.TessellatedFace(triangle, GeometryEncoder.Context.Peek.MaterialId));
                    }
                }
                builder.CloseConnectedFaceSet();

                builder.Build();
                using (var result = builder.GetBuildResult())
                {
                    if (result.Outcome != DB.TessellatedShapeBuilderOutcome.Nothing)
                    {
                        var geometries = result.GetGeometricalObjects();
                        if (geometries.Count == 1)
                        {
                            return(geometries[0] as DB.Mesh);
                        }
                    }
                }
            }

            return(null);
        }
예제 #2
0
        public MeshConverter(PointConverter ptConv) :
            base(
                (rm) => {
            ppg.Mesh pm = new ppg.Mesh();
            pm.Vertices.AddRange(rm.Vertices.Select(pt => ptConv.ToPipe <rg.XYZ, ppg.Vec>(pt)));
            for (int i = 0; i < rm.NumTriangles; i++)
            {
                rg.MeshTriangle tr = rm.get_Triangle(i);
                pm.Faces.Add(new ulong[] {
                    tr.get_Index(0), tr.get_Index(1), tr.get_Index(2)
                });
            }

            return(pm);
        },
                (ppm) => {
            //making sure to triangulate so that revit doesn't fail
            ppm.Triangulate();

            rg.TessellatedShapeBuilder builder = new rg.TessellatedShapeBuilder();
            builder.OpenConnectedFaceSet(true);
            builder.Target         = rg.TessellatedShapeBuilderTarget.Mesh;
            builder.Fallback       = rg.TessellatedShapeBuilderFallback.Salvage;
            List <rg.XYZ> allVerts = ppm.Vertices.Select((pt) => ptConv.FromPipe <rg.XYZ, ppg.Vec>(pt)).ToList();

            foreach (var face in ppm.Faces)
            {
                List <rg.XYZ> faceVerts = new List <rg.XYZ>();
                var faceReversed        = face.Reverse().ToList();
                foreach (ulong vertIndex in faceReversed)
                {
                    faceVerts.Add(allVerts[(int)vertIndex]);
                }

                rg.TessellatedFace tface = new rg.TessellatedFace(faceVerts, rg.ElementId.InvalidElementId);
                //if (!builder.DoesFaceHaveEnoughLoopsAndVertices(tface)) { continue; }
                builder.AddFace(tface);
            }
            builder.CloseConnectedFaceSet();
            builder.Build();
            rg.TessellatedShapeBuilderResult result = builder.GetBuildResult();
            List <rg.GeometryObject> geoms          = result.GetGeometricalObjects().ToList();
            return((rg.Mesh)geoms.FirstOrDefault());
        }
                ) { }
예제 #3
0
        internal static IEnumerable <DB.GeometryObject> ToGeometryObjectMany(Mesh mesh)
        {
            DB.XYZ ToHost(Point3d p) => new DB.XYZ(p.X, p.Y, p.Z);

            var faceVertices = new List <DB.XYZ>(4);

            try
            {
                using
                (
                    var builder = new DB.TessellatedShapeBuilder()
                {
                    GraphicsStyleId = GeometryEncoder.Context.Peek.GraphicsStyleId,
                    Target = DB.TessellatedShapeBuilderTarget.Mesh,
                    Fallback = DB.TessellatedShapeBuilderFallback.Salvage
                }
                )
                {
                    var pieces = mesh.DisjointMeshCount > 1 ?
                                 mesh.SplitDisjointPieces() :
                                 new Mesh[] { mesh };

                    foreach (var piece in pieces)
                    {
                        piece.Faces.ConvertNonPlanarQuadsToTriangles(Revit.VertexTolerance, RhinoMath.UnsetValue, 5);

                        var vertices = piece.Vertices.ToPoint3dArray();

                        builder.OpenConnectedFaceSet(piece.SolidOrientation() != 0);
                        foreach (var face in piece.Faces)
                        {
                            faceVertices.Add(ToHost(vertices[face.A]));
                            faceVertices.Add(ToHost(vertices[face.B]));
                            faceVertices.Add(ToHost(vertices[face.C]));
                            if (face.IsQuad)
                            {
                                faceVertices.Add(ToHost(vertices[face.D]));
                            }

                            builder.AddFace(new DB.TessellatedFace(faceVertices, GeometryEncoder.Context.Peek.MaterialId));
                            faceVertices.Clear();
                        }
                        builder.CloseConnectedFaceSet();
                    }

                    builder.Build();
                    using (var result = builder.GetBuildResult())
                    {
                        if (result.Outcome != DB.TessellatedShapeBuilderOutcome.Nothing)
                        {
                            return(result.GetGeometricalObjects());
                        }
                    }
                }
            }
            catch (Autodesk.Revit.Exceptions.ApplicationException e)
            {
                Debug.Fail(e.Source, e.Message);
            }

            return(Enumerable.Empty <DB.GeometryObject>());
        }