-
Notifications
You must be signed in to change notification settings - Fork 14
/
IKRS.DivisibleSTLBuilder.js~
203 lines (148 loc) · 5.02 KB
/
IKRS.DivisibleSTLBuilder.js~
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* A Three.js geometry-to-STL converter.
*
* Inspired by
* http://buildaweso.me/project/2013/2/25/converting-threejs-objects-to-stl-files
*
* @author Ikaros Kappler
* @date 2013-10-10 (based on the STLBuilder.js)
* @version 1.0.0
**/
IKRS.DivisibleSTLBuilder = function( meshes,
filename,
processListener,
maxChunkSize
) {
IKRS.Object.call( this );
//window.alert( meshes.length );
if( !filename )
filename = "extrusion.stl";
//window.alert( meshes.length );
// It is possible to pass an array or a single gerometry
if( typeof meshes.length === "undefined" )
meshes = [ meshes ];
this.meshes = meshes;
this.filename = filename;
this.processListener = processListener;
this.maxChunkSize = this.maxChunkSize;
this.currentMeshIndex = 0;
this.currentTriangleIndex = 0;
this.chunkResults = [];
}
IKRS.DivisibleSTLBuilder.prototype = new IKRS.Object();
IKRS.DivisibleSTLBuilder.prototype.constructor = IKRS.DivisibleSTLBuilder;
/**
* Processes the next chunk of data.
*
* @return true if there is at least one next part, false otherwise.
**/
IKRS.DivisibleSTLBuilder.prototype.processNextChunk = function() {
//var startMeshIndex = this.currentMeshIndex;
//var startTriangleIndex = this.currentTriangleIndex;
var tmpResult = this._saveSTL( this.currentMeshIndex,
this.currentTriangleIndex,
this.maxChunkSize
);
this.currentMeshIndex = tmpResult.meshIndex;
this.currentTriangleIndex = tmpResult.triangleIndex;
return (tmpResult.returnCode==0);
}
IKRS.Divisible.prototype._saveSTL = function( meshIndex,
triangleIndex,
maxChunkSize
) {
// First step: calculate total triganle count
/*
var totalTriCount = 0;
for( var i = 0; i < meshes.length; i++ ) {
totalTriCount += meshes[ i ].geometry.faces.length;
}
*/
//var buffer = [];
//for( var i = this.currentMeshIndex; i < meshes.length; i++ ) {
var currentChunkSize = 0;
while( meshIndex < this.meshes.length &&
currentChunkSize < maxChunkSize
) {
var currentMesh = meshes[ meshIndex ];
//window.alert( meshes[i].geometry );
var tmpResult = STLBuilder._buildSTL( currentMesh.geometry,
triangleIndex,
currentChunkSize,
maxChunkSize
);
this.chunkResults[ i ] = tmpResult.value;
currentChunkSize += tmpResult.currentChunkSize;
triangleIndex = tmpResult.triangleIndex;
// Next mesh?
if( triangleIndex >= currentMesh.geometry.faces.length ) {
meshIndex++;
triangleIndex = 0;
}
}
//var stlString = buffer.join( "\n\n" );
//var blob = new Blob([stlString], {type: 'text/plain'});
//window.saveAs(blob, filename);
return { returnCode: 0,
meshIndex: meshIndex,
triangleIndex: trigangleIndex
};
};
/*
buildSTLFromMeshArray: function( meshes, processListener ) {
var buffer = [];
for( var i = 0; i < meshes.length; i++ ) {
//window.alert( meshes[i].geometry );
buffer[i] = STLBuilder.buildSTL( meshes[i].geometry );
}
var stlString = buffer.join( "\n\n" );
return stlString;
},
*/
IKRS.DivisibleSTLBuilder.prototype._buildSTL = function( geometry,
triangleIndex,
currentChunkSize,
maxChunkSize
) {
var vertices = geometry.vertices;
// Warning!
// The faces may be Face4 and not Face3!
var tris = geometry.faces;
// Use an array as StringBuffer (concatenation is extremely slow in IE6).
var stl = [];
stl.push( "solid pixel\n" );
currentChunkSize += ("solid pixel\n").length;
//for(var i = 0; i < tris.length; i++) {
//for( var i = triangleIndex; i < tris.length; i++ ) {
while( triangleIndex < tris.length &&
currentChunkSize < maxChunkSize
) {
var tmpBuffer = [];
tmpBuffer.push( " facet normal "+ STLBuilder._stringifyVector( tris[i].normal )+"\n");
tmpBuffer.push(" outer loop\n");
tmpBuffer.push(" " + STLBuilder._stringifyVertex( vertices[ tris[i].a ] ) );
tmpBuffer.push(" " + STLBuilder._stringifyVertex( vertices[ tris[i].b ] ) );
tmpBuffer.push(" " + STLBuilder._stringifyVertex( vertices[ tris[i].c ] ) );
tmpBuffer.push(" endloop \n");
tmpBuffer.push(" endfacet \n");
triangleIndex++;
var tmpData = tmpBuffer.join("");
stl.push( tmpData );
currentChunkSize += tmpData.length;
}
stl.push("endsolid\n");
currentChunkSize += ("endsolid\n").length;
// Convert array to string
var data = stl.join("");
return { returnCode: 0,
value: data,
currentChunkSize: currentChunkSize,
triangleIndex: triangleIndex
};
};
IKRS.DivisibleSTLBuilder.prototype._stringifyVector = function(vec) {
return ""+vec.x+" "+vec.y+" "+vec.z;
};
IKRS.DivisibleSTLBuilder.prototype._stringifyVertex = function(vec) {
return "vertex " + STLBuilder._stringifyVector(vec) + " \n";
};