Ejemplo n.º 1
0
        private void ExportSolid(Solid solid)
        {
            SolidOrShellTessellationControls solidOrShellTessellationControls = new SolidOrShellTessellationControls
            {
                LevelOfDetail      = userSetting.LevelOfDetail / 30.0,
                Accuracy           = 0.1,
                MinAngleInTriangle = 0.0001,
                MinExternalAngleBetweenTriangles = 1.0
            };

            try
            {
                TriangulatedSolidOrShell triangulatedSolidOrShell = SolidUtils.TessellateSolidOrShell(solid, solidOrShellTessellationControls);
                int shellComponentCount = triangulatedSolidOrShell.ShellComponentCount;
                for (int i = 0; i < shellComponentCount; i++)
                {
                    TriangulatedShellComponent shellComponent = triangulatedSolidOrShell.GetShellComponent(i);
                    ModelGeometry exportedGeometry            = new ModelGeometry
                    {
                        Transform = transformationStack.Peek(),
                        Points    = new List <XYZ>(shellComponent.VertexCount)
                    };
                    for (int num = 0; num != shellComponent.VertexCount; num++)
                    {
                        exportedGeometry.Points.Add(shellComponent.GetVertex(num));
                    }
                    exportedGeometry.Indices = new List <int>(shellComponent.TriangleCount * 3);
                    for (int j = 0; j < shellComponent.TriangleCount; j++)
                    {
                        TriangleInShellComponent triangle = shellComponent.GetTriangle(j);
                        exportedGeometry.Indices.Add(triangle.VertexIndex0);
                        exportedGeometry.Indices.Add(triangle.VertexIndex1);
                        exportedGeometry.Indices.Add(triangle.VertexIndex2);
                    }
                    exportedGeometry.CalculateNormals(false);
                    exportedGeometry.CalculateUVs(true, false);
                    ElementId materialElementId       = solid.Faces.get_Item(0).MaterialElementId;
                    Tuple <Document, ElementId> tuple = new Tuple <Document, ElementId>(documentStack.Peek(), materialElementId);
                    ChangeCurrentMaterial(tuple);
                    documentAndMaterialIdToGeometries[tuple].Add(exportedGeometry);
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 2
0
        private void ExportSolid2(Solid solid)
        {
            foreach (Face face in solid.Faces)
            {
                if (!(face.Equals(null)))
                {
                    Mesh mesh = face.Triangulate(userSetting.LevelOfDetail / 15.0);
                    if (!(mesh.Equals(null)) && !mesh.Visibility.Equals(Visibility.Invisible))
                    {
                        ModelGeometry exportedGeometry = new ModelGeometry();

                        // 设置坐标系
                        exportedGeometry.Transform = transformationStack.Peek();
                        exportedGeometry.Points    = new List <XYZ>(mesh.Vertices);
                        exportedGeometry.Indices   = new List <int>(mesh.NumTriangles * 3);

                        //指示此转换是否为保形的布尔值。指示此转换是否产生反射的布尔值。反射变换会更改坐标系的惯用性。
                        if (exportedGeometry.Transform.IsConformal && exportedGeometry.Transform.HasReflection)
                        {
                            for (int i = 0; i < mesh.NumTriangles; i++)
                            {
                                MeshTriangle meshTriangle = mesh.get_Triangle(i);
                                exportedGeometry.Indices.Add((int)meshTriangle.get_Index(0));
                                exportedGeometry.Indices.Add((int)meshTriangle.get_Index(2));
                                exportedGeometry.Indices.Add((int)meshTriangle.get_Index(1));
                            }
                        }
                        else
                        {
                            for (int j = 0; j < mesh.NumTriangles; j++)
                            {
                                MeshTriangle meshTriangle2 = mesh.get_Triangle(j);
                                exportedGeometry.Indices.Add((int)meshTriangle2.get_Index(0));
                                exportedGeometry.Indices.Add((int)meshTriangle2.get_Index(1));
                                exportedGeometry.Indices.Add((int)meshTriangle2.get_Index(2));
                            }
                        }
                        ModelGeometry TGeometry = exportedGeometry;

                        //计算模型法线
                        TGeometry.CalculateNormals(TGeometry.Transform.IsConformal && exportedGeometry.Transform.HasReflection);

                        exportedGeometry.CalculateUVs(true, false);

                        //判断是不是反正双面实体
                        if (face.IsTwoSided)
                        {
                            exportedGeometry.MakeDoubleSided();
                        }

                        //将当前元素面的材质ID放入元组中(文档,元素材质ID)
                        ElementId materialElementId       = face.MaterialElementId;
                        Tuple <Document, ElementId> tuple = new Tuple <Document, ElementId>(documentStack.Peek(), materialElementId);

                        //加入元组索引
                        ChangeCurrentMaterial(tuple);

                        //用元组索引将模型加入到模型数据池
                        documentAndMaterialIdToGeometries[tuple].Add(exportedGeometry);
                    }
                }
            }
        }