forked from MattStultz/extrusiongen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipFileImporter.js
265 lines (190 loc) · 8.34 KB
/
ZipFileImporter.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @author Ikaros Kappler
* @date 2013-09-11
* @modified 2014-07-04 Ikaros Kappler (added new values shapeTwist, shapeStyle and version).
* @version 1.0.1
**/
ZipFileImporter = {
_apply_mesh_settings: function( meshSettings ) {
/*
return {
geometry: getPreviewMesh().geometry,
bezierPath: getBezierPath(),
meshSettings: {
shapeSegments: document.forms[ "mesh_form" ].elements[ "shape_segments" ].value,
pathSegments: document.forms[ "mesh_form" ].elements[ "path_segments" ].value,
bendAngle: document.forms[ "mesh_form" ].elements[ "preview_bend" ].value,
buildNegativeMesh: document.forms[ "mesh_form" ].elements[ "build_negative_mesh" ].checked,
meshHullStrength: document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].value,
closePathBegin: document.forms[ "mesh_form" ].elements[ "mesh_close_path_begin" ].checked,
closePathEnd: document.forms[ "mesh_form" ].elements[ "mesh_close_path_end" ].checked,
wireframe: document.forms[ "mesh_form" ].elements[ "wireframe" ].checked,
triangulate: document.forms[ "mesh_form" ].elements[ "triangulate" ].checked,
// This is new since 2013-09-16
splitShape: document.forms[ "mesh_form" ].elements[ "split_shape" ].checked,
// This is new since 2013-10-30
arrangeSplitsOnPlane: document.forms[ "mesh_form" ].elements[ "arrange_splits_on_plane" ].checked,
// These are new since 2014-04-23
directions: getSelectedMeshDirection(), // "xyz" or "zxy"
meshHullType: getSelectedMeshHullType() // "perpendicular" or "prism"
// This is new since 2014-04-25
parts: getSelectedMeshParts(), // "both" or "left" or "right"
// These are new since 2014-07-04
shapeTwist: getTwistValue(), // percentage
shapeStyle: getSelectedShapeStyle(), // "circle", "oval", ...
version: VERSION_STRING
},
// This is new since 2014-04-23
colorSettings: {
color: document.forms["color_form"].elements["color"].value
},
compress: document.forms[ "zip_form" ].elements[ "compress_zip" ].checked
}; // END object
*/
if( meshSettings.shapeSegments )
document.forms[ "mesh_form" ].elements[ "shape_segments" ].value = meshSettings.shapeSegments;
if( meshSettings.pathSegments )
document.forms[ "mesh_form" ].elements[ "path_segments" ].value = meshSettings.pathSegments;
if( typeof meshSettings.bendAngle != "undefined" ) // || meshSettings.bendAngle == 0 )
document.forms[ "mesh_form" ].elements[ "preview_bend" ].value = meshSettings.bendAngle;
else
document.forms[ "mesh_form" ].elements[ "preview_bend" ].value = 0;
if( meshSettings.buildNegativeMesh ) {
document.forms[ "mesh_form" ].elements[ "build_negative_mesh" ].checked = "checked";
document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = false;
} else {
document.forms[ "mesh_form" ].elements[ "build_negative_mesh" ].checked = false;
document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = true;
}
if( typeof meshSettings.meshHullStrength != "undefined" )
document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].value = meshSettings.meshHullStrength;
if( meshSettings.closePathBegin )
document.forms[ "mesh_form" ].elements[ "mesh_close_path_begin" ].checked = "checked";
else
document.forms[ "mesh_form" ].elements[ "mesh_close_path_begin" ].checked = false;
if( meshSettings.closePathEnd )
document.forms[ "mesh_form" ].elements[ "mesh_close_path_end" ].checked = "checked";
else
document.forms[ "mesh_form" ].elements[ "mesh_close_path_end" ].checked = false;
if( meshSettings.wireframe )
document.forms[ "mesh_form" ].elements[ "wireframe" ].checked = "checked";
else
document.forms[ "mesh_form" ].elements[ "wireframe" ].checked = false;
if( meshSettings.triangulate )
document.forms[ "mesh_form" ].elements[ "triangulate" ].checked = "checked";
else
document.forms[ "mesh_form" ].elements[ "triangulate" ].checked = false;
if( meshSettings.splitShape ) {
document.forms[ "mesh_form" ].elements[ "split_shape" ].checked = "checked";
document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = false;
} else {
document.forms[ "mesh_form" ].elements[ "split_shape" ].checked = false;
document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = true;
}
if( meshSettings.arrangeSplitsOnPlane ) {
document.forms[ "mesh_form" ].elements[ "arrange_splits_on_plane" ].checked = "checked";
//document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = false;
} else {
document.forms[ "mesh_form" ].elements[ "arrange_splits_on_plane" ].checked = false;
//document.forms[ "mesh_form" ].elements[ "mesh_hull_strength" ].disabled = true;
}
setSelectedMeshDirection( meshSettings.meshDirection ); // May be null
setSelectedMeshHullType( meshSettings.meshHullType ); // May be null
setSelectedMeshParts( meshSettings.parts ); // May be null
if( meshSettings.shapeTwist )
setTwistValue( meshSettings.shapeTwist );
else
setTwistValue( 0 );
setSelectedShapeStyle( meshSettings.shapeStyle ); // May be null
return true;
},
_apply_color_settings: function( colorSettings ) {
if( !colorSettings )
return false;
if( colorSettings.meshColor != null )
setSelectedMeshColor( colorSettings.meshColor ); // Not null
return true;
},
importZipFile: function( inputFileElement ) {
//window.alert( JSON.stringify(inputFileElement) );
//window.alert( "FileReader=" + FileReader + ", type=" + (typeof FileReader) );
if( typeof FileReader == "undefined" || !inputFileElement.files ) {
window.alert( "Your browser does not support the HTML5 file API!" );
return;
}
if( inputFileElement.files.length == 0 ) {
window.alert( "Please select a file." );
return;
}
// window.alert( inputFileElement.files[0] );
var json_file = inputFileElement.files[ 0 ];
var reader = new FileReader();
reader.onload = function( e ) {
try {
var zip = new JSZip( e.target.result );
var shapedPathFile = zip.file( "shape.bezierpath.json" );
var meshSettingsFile = zip.file( "mesh_settings.json" );
var colorSettingsFile = zip.file( "color_settings.json" );
if( !shapedPathFile ) {
window.alert( "The passed zip file does not contain the 'shape.bezierpath.json' file." );
return false;
}
if( !meshSettingsFile ) {
window.alert( "The passed zip file does not contain the 'mesh_settings.json' file." );
return false;
}
// Prepare variables
var bezierPath = null;
var meshSettings = null;
var colorSettings = null;
//window.alert( "shapedPathFile=" + shapedPathFile + ", meshSettingsFile=" + meshSettingsFile );
// Parse bezier path
try {
bezierPath = IKRS.BezierPath.fromJSON( shapedPathFile.asText() );
} catch( e ) {
window.alert( "Error[0]: " + e );
return false;
}
// Parse mesh settings
try {
meshSettings = JSON.parse( meshSettingsFile.asText() );
} catch( e ) {
window.alert( "Error[1]: " + e );
return false;
}
// Parse color settings?
// The color settings file 'color_settings.json' was added in a later version.
// For backwards compatibility ignore this if the file does not exist
if( colorSettingsFile ) {
try {
colorSettings = JSON.parse( colorSettingsFile.asText() );
} catch( e ) {
window.alert( "Error[2]: " + e );
return false;
}
}
// Apply path
setBezierPath( bezierPath );
// Apply settings
ZipFileImporter._apply_mesh_settings( meshSettings );
// Apply color settings
ZipFileImporter._apply_color_settings( colorSettings );
// Enable/disable respective form elements
toggleFormElementsEnabled();
// Rebuild model with new settings
preview_rebuild_model();
//window.alert( "meshSettings=" + JSON.stringify(meshSettings) );
} catch( e ) {
window.alert( "Error: " + e );
}
};
reader.onprogress = function( e ) {
// NOOP
// (display progress?)
};
reader.onerror = function( e ) {
window.alert( "File upload error (code=" + e.target.error.code+")." );
};
reader.readAsBinaryString( json_file );
}
};