コード例 #1
0
ファイル: StlBodyExporter.cs プロジェクト: Macad3D/Macad3D
        public static bool Export(IEnumerable <Body> bodies, string fileName, bool binaryFormat)
        {
            var sumTriangleCount = 0;
            var triangulations   = new List <TriangulationData>();

            foreach (var body in bodies)
            {
                var shape = body.Shape.GetTransformedBRep();
                if (shape == null)
                {
                    continue;
                }

                var triangulation = TriangulationHelper.GetTriangulation(shape, false);
                if (triangulation.TriangleCount == 0)
                {
                    continue;
                }

                triangulations.Add(triangulation);
                sumTriangleCount += triangulation.TriangleCount;
            }

            if (sumTriangleCount == 0)
            {
                return(false);
            }

            IStlWriter writer;

            if (binaryFormat)
            {
                writer = new StlBinaryWriter();
            }
            else
            {
                writer = new StlAsciiWriter();
            }
            writer.Init("Written by Macad3D STL-Export", sumTriangleCount);

            foreach (var triangulation in triangulations)
            {
                var index = 0;
                for (int triangle = 0; triangle < triangulation.TriangleCount; triangle++)
                {
                    // Get vertices
                    var vertex1 = triangulation.Vertices[triangulation.Indices[index]];
                    var vertex2 = triangulation.Vertices[triangulation.Indices[index + 1]];
                    var vertex3 = triangulation.Vertices[triangulation.Indices[index + 2]];
                    index += 3;

                    // Calculate normal of facet
                    var vec1   = new Vec(vertex1, vertex2);
                    var vec2   = new Vec(vertex1, vertex3);
                    var normal = vec1.Crossed(vec2);
                    if (normal.SquareMagnitude() > gp.Resolution)
                    {
                        normal.Normalize();
                    }
                    else
                    {
                        normal = default;
                    }

                    // Write out facet
                    writer.AddFacet(normal, vertex1, vertex2, vertex3);
                }
            }

            return(writer.WriteToFile(fileName));
        }
コード例 #2
0
        public static bool Export(string fileName, IEnumerable <Body> bodies, bool smoothEdges)
        {
            ObjAsciiWriter writer = new ObjAsciiWriter();

            writer.Init("Written by Macad3D OBJ-Export");

            var indexOffset = 1;                           // OBJ list index starts with 1
            var vertexMap   = new Dictionary <Pnt, int>(); // Written vertex to resulting index
            var indexList   = new List <int>();

            foreach (var body in bodies)
            {
                var shape = body.Shape.GetTransformedBRep();
                if (shape == null)
                {
                    continue;
                }

                var triangulation = TriangulationHelper.GetTriangulation(shape, false);
                if (triangulation.TriangleCount == 0)
                {
                    continue;
                }

                writer.AddObject(body.Name);

                if (smoothEdges) // Export smooth edges
                {
                    for (var vtxIndex = 0; vtxIndex < triangulation.Vertices.Length; vtxIndex++)
                    {
                        var vertex = triangulation.Vertices[vtxIndex];
                        int newIndex;
                        if (!vertexMap.TryGetValue(vertex, out newIndex))
                        {
                            writer.AddVertex(vertex);
                            newIndex = vertexMap.Count + indexOffset;
                            vertexMap.Add(vertex, newIndex);
                        }

                        indexList.Add(newIndex);
                    }

                    for (int i = 0; i < triangulation.Indices.Length; i += 3)
                    {
                        writer.AddFace(indexList[triangulation.Indices[i]], indexList[triangulation.Indices[i + 1]], indexList[triangulation.Indices[i + 2]]);
                    }
                }
                else // Export face by face
                {
                    foreach (var vertex in triangulation.Vertices)
                    {
                        writer.AddVertex(vertex);
                    }

                    for (int i = 0; i < triangulation.Indices.Length; i += 3)
                    {
                        writer.AddFace(triangulation.Indices[i] + indexOffset, triangulation.Indices[i + 1] + indexOffset, triangulation.Indices[i + 2] + indexOffset);
                    }
                }


                indexOffset += smoothEdges ? vertexMap.Count : triangulation.Vertices.Length;
                vertexMap.Clear(); // Don't share vertices between objects
                indexList.Clear();
            }

            return(writer.WriteToFile(fileName));
        }