Exemplo n.º 1
0
        public override byte[] Write(Shape shape)
        {
            var stlShape = StlHelper.ToStlShape(shape);

            using var stream = new MemoryStream();
            using var writer = new BinaryWriter(stream);

            byte[] header        = new byte[80];
            uint   triangleCount = (uint)stlShape.Triangles.Length;

            writer.Write(header);
            writer.Write(triangleCount);

            foreach (var triangle in stlShape.Triangles)
            {
                writer.Write(triangle.Normal.X);
                writer.Write(triangle.Normal.Y);
                writer.Write(triangle.Normal.Z);

                foreach (var position in triangle.Positions)
                {
                    writer.Write(position.X);
                    writer.Write(position.Y);
                    writer.Write(position.Z);
                }

                writer.Write(triangle.Attribute);
            }

            return(stream.ToArray());
        }
Exemplo n.º 2
0
        public override byte[] Write(Shape shape)
        {
            var stlShape = StlHelper.ToStlShape(shape);

            var sb = new StringBuilder();

            sb.AppendLine($"solid {stlShape.Name}");

            foreach (var triangle in stlShape.Triangles)
            {
                sb.AppendLine("\tfacet normal " + Ascii_Vector3Format(triangle.Normal));
                sb.AppendLine("\t\touter loop");

                foreach (var position in triangle.Positions)
                {
                    sb.AppendLine("\t\t\tvertex " + Ascii_Vector3Format(position));
                }

                sb.AppendLine("\t\tendloop");
                sb.AppendLine("\tendfacet");
            }

            sb.AppendLine("endsolid");

            return(Encoding.ASCII.GetBytes(sb.ToString()));
        }
Exemplo n.º 3
0
        public override Shape Read(MemoryStream stream)
        {
            using var reader = new BinaryReader(stream);

            byte[] header        = reader.ReadBytes(80);
            uint   triangleCount = reader.ReadUInt32();

            var triangles = new List <StlTriangle>();

            for (int i = 0; i < triangleCount; i++)
            {
                var normalX = reader.ReadSingle();
                var normalY = reader.ReadSingle();
                var normalZ = reader.ReadSingle();

                var v1X = reader.ReadSingle();
                var v1Y = reader.ReadSingle();
                var v1Z = reader.ReadSingle();

                var v2X = reader.ReadSingle();
                var v2Y = reader.ReadSingle();
                var v2Z = reader.ReadSingle();

                var v3X = reader.ReadSingle();
                var v3Y = reader.ReadSingle();
                var v3Z = reader.ReadSingle();

                var attribute = reader.ReadUInt16();

                triangles.Add(new StlTriangle(
                                  new Vector3(normalX, normalY, normalZ),
                                  new Vector3[]
                {
                    new Vector3(v1X, v1Y, v1Z),
                    new Vector3(v2X, v2Y, v2Z),
                    new Vector3(v3X, v3Y, v3Z)
                },
                                  attribute
                                  ));
            }

            var stlShape = new StlShape(string.Empty, triangles.ToArray());

            return(StlHelper.ToShape(stlShape));
        }