forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openjscad
executable file
·298 lines (245 loc) · 9.83 KB
/
openjscad
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/local/bin/node --log_all
// == OpenJSCAD.org CLI interface, written by Rene K. Mueller <[email protected]>, Licensed under MIT License
//
// Version: 0.4.0
//
// Description:
// openjscad <file> [-of <format>] [-o <output>]
// e.g.
// openjscad test.jscad
// openjscad test.jscad -o test.stl
// openjscad test.jscad -o test.amf
// openjscad test.jscad -o test.dxf
// openjscad test.scad -o testFromSCAD.jscad
// openjscad test.scad -o test.stl
// openjscad test.stl -o test2.stl # reprocessed: stl -> jscad -> stl
// openjscad test.amf -o test2.jscad
// openjscad test.jscad -of amf
// openjscad test.jscad -of dxf
// openjscad test.jscad -of stl
// openjscad name_plate.jscad --name "Just Me" --title "CEO" -o amf test.amf
//
// History:
// 2016/06/27: 0.5.1: refactored AMF import and export by Z3 Dev
// enhanced STL import, adding support for MM colors by Z3 Dev
// 2016/02/02: 0.4.0: GUI refactored, functionality split up into more files, mostly done by Z3 Dev
// 2015/07/02: 0.3.0: node 0.10.x support, /usr/bin/nodejs, new files involved: formats.js (Stefan Baumann) and Blob.js (Z3 Dev)
// 2014/12/09: 0.019: support of DXF output for 2D objects (laser cutter)
// 2013/04/25: 0.010: support of params passed to main()
// 2013/04/12: 0.008: reimplement parseAMF without jquery
// 2013/04/11: 0.007: support of alpha for AMF addded, bumping version
// 2013/04/05: 0.006: support of AMF added, requires node 0.8.1+
// 2013/03/25: 0.005: more sanity check on input and local installation support
// 2013/03/18: 0.004: STL .stl (binary & ascii) support (experimental via openscad.js)
// 2013/03/18: 0.003: OpenSCAD .scad support by Gary Hodgson's openscad-openjscad-translator module
// 2013/03/02: 0.002: proper installation of the dependencies (csg.js & openscad.js) so openjscad can be used properly
// 2013/03/01: 0.001: initial version, with base function from openscad.jscad
//
global.time = new Date();
gMainParam = {};
var fs = require('fs');
var vm = require('vm');
//var os = require('os');
//include('./openscad.js'); // later
global.lib = '/usr/local/lib/openjscad/'; // for now hard-coded
global.nodeModules = '/usr/local/lib/node_modules/'; // for now hard-coded too
if (!fs.existsSync(global.lib)) // requires node 0.10.1
global.lib = './';
//if(!fs.statSync(global.lib).isDirectory()) // same but works with 0.6.1+
// global.lib = './';
var CSG = require(lib+'./formats.js').CSG; // use the CSG with extended prototypes
var CAG = require(lib+'./formats.js').CAG; // use the CAG with extended prototypes
var Blob = require(lib+'./Blob.js').Blob;
var OpenJsCad = require(lib+'./openjscad.js').OpenJsCad;
var openscad = require(lib+'./openscad.js');
// hint: https://github.com/substack/node-optimist
// https://github.com/visionmedia/commander.js
//
//process.argv.forEach(function (val, index, array) {
// console.log(index + ': ' + val);
//});
var args = process.argv.splice(2);
var meta = [];
meta.producer = "OpenJSCAD "+" "+OpenJsCad.version;
meta.date = new Date();
var formatName = {
jscad: "OpenJSCAD.org Source",
js: "JavaScript Source",
stl: "STereoLithography, ASCII",
stla: "STereoLithography, ASCII",
stlb: "STereoLithography, Binary",
amf: "Additive Manufacturing File Format",
x3d: "X3D File Format",
gcode: "G Programming Language File Format",
dxf: "AutoCAD Drawing Exchange Format",
svg: "Scalable Vector Graphics Format",
};
if(args.length < 1) {
console.log("USAGE:\n\nopenjscad [-v] <file> [-of <format>] [-o <output>]");
console.log("\t<file> :\tinput file (Supported types: .jscad, .js, .scad, .stl, .amf, .obj, .gcode, .svg)");
console.log("\t<output>:\toutput file (Supported types: .jscad, .stl, .amf, .dxf, .svg)");
console.log("\t<format>:\t'jscad', 'stla' (STL ASCII, default), 'stlb' (STL Binary), 'amf', 'dxf', 'svg'");
process.exit(1);
}
// arguments found
var inf = null;
var outFormat, inFormat;
var outf = null;
for(var i=0; i<args.length; i++) {
if(args[i]=='-of') { // -of <format>
outFormat = args[++i];
} else if(args[i].match(/^-o(\S.+)/)) { // -o<output>
outf = args[i];
outf = outf.replace(/^\-o(\S+)$/,'$1');
} else if(args[i]=='-o') { // -o <output>
outf = args[++i];
} else if(args[i].match(/^--(\w+)=(.*)/)) { // params for main()
gMainParam[RegExp.$1] = RegExp.$2;
} else if(args[i].match(/^--(\w+)$/)) { // params for main()
gMainParam[RegExp.$1] = args[++i];
} else if(args[i].match(/^--(\w+)$/)) { // params for main()
gMainParam[RegExp.$1] = args[++i];
} else if(args[i].match(/.+\.(jscad|js|scad|stl|amf|obj|gcode|svg)$/i)) {
inf = args[i];
inFormat = RegExp.$1;
if(!fs.statSync(inf).isFile()) {
console.log("ERROR: cannot open file <"+inf+">");
process.exit(1);
}
} else if(args[i].match(/^-v$/)) { // show the version and the environment information
OpenJsCad.env();
console.log("OpenSCAD Compatibility ("+openscad.version()+")");
} else {
console.log("ERROR: invalid file name or argument <"+args[i]+">");
console.log("Type 'openjscad' for help");
process.exit(1);
}
}
// exit if a input file was not provided
if (inf==null) process.exit(1);
if(!outFormat&&!outf) {
outFormat = 'stla';
}
//console.log("reading "+inf);
var src = fs.readFileSync(inf,inf.match(/\.stl$/i)?"binary":"UTF8");
var scad = fs.readFileSync(lib+'./openscad.js');
var inc = [];
//var csg = sphere(1); // -- basic test
//var csg = require(file).main; // -- treating .jscad as module? later perhaps
if(!outFormat&&outf&&outf.length&&outf.match(/\.(jscad|js|stl|amf|dxf|svg)$/)) { // output filename set
outFormat = RegExp.$1;
} else if(!outFormat&&outf&&outf.length) { // output filename isn't valid
console.log("ERROR: invalid output file <"+outf+">");
process.exit(1);
} else if(outFormat.match(/(jscad|js|stl|stla|stlb|amf|dxf|svg)/i)) { // output format defined?
var ext = RegExp.$1;
if(!outf) { // unless output filename not set, compose it
ext = ext.replace(/stl[ab]/,'stl'); // drop [ab] from stl
outf = inf;
outf = outf.replace(/\.([^\.]+)$/,"."+ext); // compose output filename
}
} else {
console.log("ERROR: invalid output format <"+outFormat+">");
process.exit(1);
}
console.log("converting "+inf+" -> "+outf+" ("+formatName[outFormat]+")");
// -- include input, and convert into JSCAD source
if(inFormat=='scad') {
var scadParser = require('openscad-openjscad-translator'); // hardcoded is bad, but works
src = scadParser.parse(src); // doing the magick
src = "// producer: OpenJSCAD "+OpenJsCad.version+"\n"+src;
src = "// source: "+outf+"\n\n"+src;
} else if(inFormat=='stl') {
//console.log("converting "+inf+" to jscad");
//var openscad = require(lib+'openscad.js');
src = openscad.parseSTL(src,inf);
//src = "// "+outf+" created by openjscad-"+OpenJsCad.version+" from "+inf+"\n\n"+src;
} else if(inFormat=='amf') {
OpenJsCad = require(lib+'./js/jscad-parseAMF.js').OpenJsCad;
src = OpenJsCad.parseAMF(src,inf);
} else if(inFormat=='obj') {
src = openscad.parseOBJ(src,inf);
} else if(inFormat=='gcode') {
src = openscad.parseGCode(src,inf);
} else if(inFormat=='svg') {
OpenJsCad = require(lib+'./js/jscad-parseSVG.js').OpenJsCad;
src = OpenJsCad.parseSVG(src,inf);
} else {
// jscad or js
;
}
// -- convert from JSCAD script into the desired output format
var out;
if(outFormat=='jscad'||outFormat=='js') {
out = new Blob([src], { type: "application/javascript" });
} else {
//console.log("render jscad to "+outFormat);
//console.log(JSON.stringify(gMainParam));
var csg = eval(src+"\n"+scad+"\nmain(_getParameterDefinitions("+JSON.stringify(gMainParam)+"))\n"); // *.jscad + openscad.js + main()
if(csg.length) {
var o = csg[0];
if(o instanceof CAG) {
o = o.extrude({offset: [0,0,0.1]});
}
for(i=1; i<csg.length; i++) {
var c = csg[i];
if(c instanceof CAG) {
c = c.extrude({offset: [0,0,0.1]});
}
o = o.unionForNonIntersecting(c);
}
csg = o;
}
if(outFormat=='amf') {
out = csg.toAMFString(meta); // CSG to AMF
} else if(outFormat=='stlb') {
out = csg.toStlBinary(); // CSG to STL BINARY
} else if(outFormat=='stl'||outFormat=='stla') {
out = csg.toStlString(); // CSG to STL ASCII
// Only possible via browsers
//} else if(outFormat=='x3d') {
// out = csg.toX3D(); // CSG to X3D
} else if(outFormat=='dxf') {
out = csg.toDxf(); // CAG to DXF
} else if(outFormat=='svg') {
out = csg.toSvg(); // CAG to SVG
} else {
console.log("ERROR: only jscad, stl, amf, dxf or svg as output format");
process.exit(1);
}
}
console.log('Blob: type ['+out.type+'] size ['+out.size+']');
fs.writeFile(outf,out.asBuffer(),0,
function(err) {
if(err) {
console.log("err", err);
} else {
console.log('success');
}
}
);
// -- helper functions ---------------------------------------------------------------------------------------
function include(fn) {
//console.log(arguments.callee.caller,"include:"+fn);
if(0) {
//var script = vm.createScript(fs.readFileSync(fn),fn);
//script.runInThisContext();
var script = vm.runInThisContext(fs.readFileSync(fn),fn);
return script;
} else if(0) {
inc.push(fn);
} else {
var src = fs.readFileSync(fn, {encoding: 'utf8'});
//console.log("include: ",src);
var r;
try {
r = eval(src+scad);
} catch(e) {
if(e instanceof SyntaxError) {
console.log(e.message);
}
}
//echo("result:",r);
return r;
}
}