-
Notifications
You must be signed in to change notification settings - Fork 1
/
STLExporter.cs
157 lines (148 loc) · 4.66 KB
/
STLExporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace GeoTIFFConverter
{
class STLExporter : IDisposable
{
bool binary = false;
FileStream file;
int triCount = 0;
int triCountOffset = 80;//byte 80
public STLExporter(string filename, bool binary = true)
{
this.binary = binary;
file = new FileStream(filename, FileMode.Create, FileAccess.Write);
//header
if (binary)
{
byte[] bytes = new byte[80];
file.Write(bytes, 0, 80);
file.Write(bytes, 0, 4);
}
else
{
write("solid stlobject\n");
}
}
public void WritePoint(float x, float y, float z)
{
if (binary)
{
write(0.0F);
write(0.0F);
write(0.0F);
//point 1
write((float)x);
write((float)y);
write((float)z);
//point 2
write((float)x);
write((float)y);
write((float)z);
//point 3
write((float)x);
write((float)y);
write((float)z);
file.WriteByte(0);
file.WriteByte(0);
triCount++;
}
else
{
write(" facet normal 0 0 0\n");
write(" outer loop\n");
write($" vertex {x} {y} {z}\n");
write($" vertex {x} {y} {z}\n");
write($" vertex {x} {y} {z}\n");
write(" endloop\n");
write(" endfacet\n");
}
}
public void WriteTriangle(Vector3 a, Vector3 b, Vector3 c)
{
if (binary)
{
write(0.0F);
write(0.0F);
write(0.0F);
write(a.X);
write(a.Y);
write(a.Z);
write(b.X);
write(b.Y);
write(b.Z);
write(c.X);
write(c.Y);
write(c.Z);
file.WriteByte(0);
file.WriteByte(0);
triCount++;
}
else
{
write(" facet normal 0 0 0\n");
write(" outer loop\n");
write($" vertex {a.X} {a.Y} {a.Z}\n");
write($" vertex {b.X} {b.Y} {b.Z}\n");
write($" vertex {c.X} {c.Y} {c.Z}\n");
write(" endloop\n");
write(" endfacet\n");
}
}
private void write(int intToWrite)
{
byte[] bytes = BitConverter.GetBytes(intToWrite);
file.Write(bytes, 0, bytes.Length);
}
private void write(float floatToWrite)
{
byte[] bytes = BitConverter.GetBytes(floatToWrite);
file.Write(bytes, 0, bytes.Length);
}
private void write(string stringToWrite)
{
byte[] bytes = Encoding.UTF8.GetBytes(stringToWrite);
file.Write(bytes, 0, bytes.Length);
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (binary)
{
file.Seek(triCountOffset, SeekOrigin.Begin);
write(triCount);
}
else
{
write("endsolid stlobject");
}
file.Close();
file.Dispose();
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
}