-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fusion360-SVG-CAMPost.cps
501 lines (432 loc) · 13.4 KB
/
Fusion360-SVG-CAMPost.cps
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
Copyright (C) 2015 by Autodesk, Inc.
All rights reserved.
$Revision: 41602 8a235290846bfe71ead6a010711f4fc730f48827 $
$Date: 2017-09-14 12:16:32 $
FORKID {2E27B627-115A-4A16-A853-5B9B9D9AF480}
W10 location: %AppData%\Autodesk\Fusion 360 CAM\Posts
*/
description = "Fusion360-SVG-postprocessor";
vendor = "Robotika Brno";
vendorUrl = "https://github.com/RoboticsBrno/Fusion360-SVG-postprocessor";
certificationLevel = 2;
longDescription = "Post processor for generating SVG. Based on Glowforge post processor.\nSupport Inkscape hairline";
extension = "svg";
mimetype = "image/svg+xml";
setCodePage("utf-8");
capabilities = CAPABILITY_JET;
minimumCircularSweep = toRad(0.01);
maximumCircularSweep = toRad(90); // avoid potential center calculation errors for CNC
allowHelicalMoves = true;
allowedCircularPlanes = (1 << PLANE_XY); // only XY arcs
properties = {
generateStock: true,
stockColor: "rgb(0,255,0)", // color of rectangle
width: 500, // width in mm used when useWCS is disabled
height: 400, // height in mm used when useWCS is disabled
spacing: 10,
lineWidth: 1,
isHairline: true,
};
// user-defined property definitions
propertyDefinitions = {
generateStock: {title:"Generate stock", description:"Generate rectangular stock shape", type:"boolean"},
stockColor: {title:"Stock color", description:"RGB color of rectangular stock shape 'rgb(0,0,0)'", type:"string"},
width: {title:"Stock width(mm)", description:"Width of stock in mm", type:"number"},
height: {title:"Stock height(mm)", description:"Height of stock in mm", type:"number"},
spacing: {title:"Spacing", description:"Specify space between the components", type:"number"},
lineWidth: {title:"Line width", description:"Width of lines in SVG document (mm / 'hairline')", type:"number"},
isHairline: {title:"Inkscape hairline", description:"Generate as Inkscape hairline (from v1.1)", type:"boolean"},
};
var xyzFormat = createFormat({decimals:(unit == MM ? 3 : 4)});
// State
outputState = {
xOffset: 0,
yOffset: 0,
zeroXOffset: 0,
zeroYOffset: 0,
wcsXOffset: 0,
wcsYOffset: 0,
maxX: 0,
maxY: 0,
minX: 0,
minY: 0,
lastPos: getCurrentPosition(),
indent: 0,
text: "",
pendingRapid: "",
start: { x: 0, y: 0 },
openedGroup: false,
stock: {
lower: { x: 0, y: 0 },
upper: { x: 0, y: 0 }
},
prevStock: {
lower: { x: 0, y: 0 },
upper: { x: 0, y: 0 }
},
openJob: null,
patternStart: null,
patternCounter: 0,
initialPosition: null
};
/** Returns the given spatial value in MM. */
function toMM(value) {
return value * ((unit == IN) ? 25.4 : 1);
}
function indent() {
return new Array(outputState.indent * 4 + 1).join( " " );
}
function tX(x) {
var xx = x + outputState.xOffset - outputState.zeroXOffset - outputState.wcsXOffset;
outputState.maxX = Math.max(outputState.maxX, xx);
outputState.minX = Math.min(outputState.minX, xx);
return xx;
}
function tY(y) {
var yy = -(y + outputState.yOffset + outputState.zeroYOffset - outputState.wcsYOffset);
outputState.maxY = Math.max(outputState.maxY, yy);
outputState.minY = Math.min(outputState.minY, yy);
return yy;
}
function echo(text) {
outputState.text += text;
}
function echoln(text) {
outputState.text += text + "\n";
}
function toolFill() {
param = getParameter("operation:tool_comment");
fill = param.split(" ")[0];
if (fill == "nofill") {
return "none";
}
if (fill == "fill") {
rgb = param.match(/\d+/g);
return "rgb(" + rgb.join(",") + ")";
}
err = "Cannot parse comment: '" + param + "'";
writeln(err);
error(err);
}
function toolStroke() {
param = getParameter("operation:tool_comment");
fill = param.split(" ")[0];
if (fill == "nofill") {
rgb = param.match(/\d+/g);
return "rgb(" + rgb.join(",") + ")";
}
if (fill == "fill") {
return "none";
}
err = "Cannot parse comment: '" + param + "'";
writeln(err);
error(err);
}
function hairline() {
if(properties.isHairline) {
return "style=\"stroke-width:0.00264583;stroke-miterlimit:4;stroke-dasharray:none;vector-effect:non-scaling-stroke;-inkscape-stroke:hairline\"";
} else {
return "";
}
}
function echoStock() {
echoln(indent() + "<polyline points=\"0,0 "
+ tX(properties.width) + ",0 "
+ tX(properties.width) + "," + tY(properties.height)
+ " 0," + tY(properties.height) + " 0,0\" "
+ " stroke=\""+ properties.stockColor +"\" stroke-width=\"" + properties.lineWidth + "\" fill=\"none\" transform=\"translate(0,0)\" " + hairline() + "/>");
outputState.xOffset += properties.width + properties.spacing;
}
function onOpen() {
// we output in mm always so scale from inches
xyzFormat = createFormat({decimals:(unit == MM ? 3 : 4), scale:(unit == MM) ? 1 : 25.4});
outputState.indent++;
if (properties.generateStock) {
echoStock();
}
}
function getPatternInstance() {
var patternId = currentSection.getPatternId();
var sections = [];
var first = true;
for (var i = 0; i < getNumberOfSections(); ++i) {
var section = getSection(i);
if (section.getPatternId() == patternId) {
if (i < getCurrentSectionId()) {
first = false; // not the first pattern instance
}
if (i != getCurrentSectionId()) {
sections.push(section.getId());
}
}
}
return sections;
}
function onComment(text) {
echoln(indent() + "<!-- " + text + " -->");
}
function onSection() {
switch (tool.type) {
case TOOL_WATER_JET: // allow any way for Epilog
warning(localize("Using waterjet cutter but allowing it anyway."));
break;
case TOOL_LASER_CUTTER:
break;
case TOOL_PLASMA_CUTTER: // allow any way for Epilog
warning(localize("Using plasma cutter but allowing it anyway."));
break;
/*
case TOOL_MARKER: // allow any way for Epilog
warning(localize("Using marker but allowing it anyway."));
break;
*/
default:
error(localize("The CNC does not support the required tool."));
return;
}
handlePattern();
var id = "op_" + getParameter("operation-comment").replace(/[ \(\)]/, "_") +
"-" + getParameter("autodeskcam:operation-id") + "-" + getCurrentSectionId();
echo(indent() + "<path id=\"" + id
+ "\" transform=\"translate(0,0)\" fill=\"" + toolFill() + "\" stroke=\"" + toolStroke() + "\" stroke-width=\"" + properties.lineWidth + "\" " + hairline() + " d=\"");
outputState.lastPos = getCurrentPosition();
outputState.pendingRapid = " M" + xyzFormat.format(tX(getCurrentPosition().x)) + " " + xyzFormat.format(tY(getCurrentPosition().y));
onM(getCurrentPosition());
}
function cloneBb(b) {
return {
upper: {
x: b.upper.x,
y: b.upper.y,
z: b.upper.z
},
lower: {
x: b.lower.x,
y: b.lower.y,
z: b.upper.z
}
};
}
function onParameter(name, value) {
if (name.lastIndexOf("part-", 0) === 0) {
if (name == "part-lower-x") {
outputState.prevStock = cloneBb(outputState.stock);
}
var vals = name.split("-");
v = parseFloat(value);
var tmp = outputState.stock[vals[1]];
tmp[vals[2]] = v;
outputState.stock[vals[1]] = tmp;
if (name != "part-upper-z") {
return;
}
var stock = outputState.stock;
outputState.yOffset = stock.upper.y - stock.lower.y;
outputState.zeroXOffset = stock.lower.x;
outputState.zeroYOffset = stock.lower.y;
tX(stock.upper.x);
tX(stock.lower.x);
tY(stock.upper.y);
tY(stock.lower.y);
var w = stock.upper.x - stock.lower.x;
var h = stock.upper.y - stock.lower.y;
}
}
function openPart(name) {
if (outputState.openedGroup) {
outputState.indent--;
echoln(indent() + "</g>");
}
echoln(indent() + "<g id=\"" + name + "\" " + hairline() +" >");
outputState.indent++;
outputState.openedGroup = true;
outputState.xOffset += outputState.prevStock.upper.x - outputState.prevStock.lower.x + properties.spacing;
outputState.prevStock = cloneBb(outputState.stock);
}
function handlePattern() {
var jD = getParameter("job-description");
if (jD != outputState.openJob) {
outputState.patternStart = null;
outputState.initialPosition = currentSection.getInitialPosition();
}
if (!currentSection.isPatterned()) {
if (jD == outputState.openJob) {
return;
}
openPart(jD.replace(/[ \(\)]/, "_"));
outputState.wcsXOffset = 0;
outputState.wcsYOffset = 0;
outputState.openJob = jD;
return;
}
var opId = getParameter("autodeskcam:operation-id");
if (outputState.patternStart == null) {
outputState.patternStart = opId;
outputState.patternCounter = 1;
}
if (outputState.patternStart == opId) {
openPart(jD.replace(/[ \(\)]/, "_") + "-" + outputState.patternCounter);
outputState.patternCounter++;
var init = currentSection.getInitialPosition();
outputState.wcsXOffset = init.x - outputState.initialPosition.x;
outputState.wcsYOffset = init.y - outputState.initialPosition.y;
}
outputState.openJob = jD;
}
function dumpObject(obj, prefix, depth) {
if (depth == 3) {
return;
}
for (var property in obj) {
if (property == "text") {
continue;
}
if (obj.hasOwnProperty(property)) {
onComment(prefix + property + ": " + obj[property])
}
if (typeof(obj[property]) == "object") {
dumpObject(obj[property], prefix + "\t", depth + 1);
}
}
}
function onM(start) {
outputState.start.x = start.x;
outputState.start.y = start.y;
}
function onPoint(point) {
if ((xyzFormat.format(point.x) == xyzFormat.format(outputState.start.x)) &&
(xyzFormat.format(point.y) == xyzFormat.format(outputState.start.y)))
{
echo(" z");
}
}
function onDwell(seconds) {
}
function onCycle() {
}
function onCyclePoint(x, y, z) {
}
function onCycleEnd() {
}
function writeLine(x, y) {
if (radiusCompensation != RADIUS_COMPENSATION_OFF) {
echoln(localize("Compensation in control is not supported."));
error(localize("Compensation in control is not supported."));
return;
}
switch (movement) {
case MOVEMENT_CUTTING:
case MOVEMENT_REDUCED:
case MOVEMENT_FINISH_CUTTING:
break;
case MOVEMENT_RAPID:
case MOVEMENT_HIGH_FEED:
case MOVEMENT_LEAD_IN:
case MOVEMENT_LEAD_OUT:
case MOVEMENT_LINK_TRANSITION:
case MOVEMENT_LINK_DIRECT:
default:
return; // skip
}
var start = getCurrentPosition();
if ((xyzFormat.format(start.x) == xyzFormat.format(x)) &&
(xyzFormat.format(start.y) == xyzFormat.format(y))) {
return; // ignore vertical
}
if ((xyzFormat.format(start.x) != xyzFormat.format(outputState.lastPos.x)) ||
(xyzFormat.format(start.y) != xyzFormat.format(outputState.lastPos.y))) {
echo(" M" + xyzFormat.format(tX(start.x)) + " " + xyzFormat.format(tY(start.y)));
outputState.pendingRapid = "";
onM(start);
}
if (outputState.pendingRapid != "") {
echo(outputState.pendingRapid);
outputState.pendingRapid = "";
}
echo(" L" + xyzFormat.format(tX(x)) + " " + xyzFormat.format(tY(y)));
outputState.lastPos = new Vector(x, y, 0);
onPoint(outputState.lastPos);
}
function onRapid(x, y, z) {
writeLine(x, y);
}
function onLinear(x, y, z, feed) {
writeLine(x, y);
}
function onRapid5D(x, y, z, dx, dy, dz) {
onRapid(x, y, z);
}
function onLinear5D(x, y, z, dx, dy, dz, feed) {
onLinear(x, y, z);
}
function onCircular(clockwise, cx, cy, cz, x, y, z, feed) {
if (radiusCompensation != RADIUS_COMPENSATION_OFF) {
writeln(localize("Compensation in control is not supported."));
error(localize("Compensation in control is not supported."));
return;
}
switch (movement) {
case MOVEMENT_CUTTING:
case MOVEMENT_REDUCED:
case MOVEMENT_FINISH_CUTTING:
break;
case MOVEMENT_RAPID:
case MOVEMENT_HIGH_FEED:
case MOVEMENT_LEAD_IN:
case MOVEMENT_LEAD_OUT:
case MOVEMENT_LINK_TRANSITION:
case MOVEMENT_LINK_DIRECT:
default:
return; // skip
}
var start = getCurrentPosition();
var largeArc = (getCircularSweep() > Math.PI) ? 1 : 0;
var sweepFlag = isClockwise() ? 1 : 0;
if ((xyzFormat.format(start.x) != xyzFormat.format(outputState.lastPos.x)) ||
(xyzFormat.format(start.y) != xyzFormat.format(outputState.lastPos.y)))
{
echo(" M" + xyzFormat.format(tX(start.x)) + " " + xyzFormat.format(tY(start.y)));
outputState.pendingRapid = "";
onM(start);
}
if (outputState.pendingRapid != "") {
echo(outputState.pendingRapid);
outputState.pendingRapid = "";
}
echo(["A",
xyzFormat.format(getCircularRadius()),
xyzFormat.format(getCircularRadius()),
0,
largeArc,
sweepFlag,
xyzFormat.format(tX(x)),
xyzFormat.format(tY(y))
].join(" "));
outputState.lastPos = new Vector(x, y, 0);
onPoint(outputState.lastPos);
}
function onCommand() {
}
function onSectionEnd() {
echoln("\"/>");
}
function onClose() {
if (outputState.openedGroup) {
outputState.indent--;
echoln(indent() + "</g>");
}
outputState.indent--;
echoln(indent() + "</svg>");
var width = outputState.maxX - outputState.minX + 2 * properties.spacing;
var height = outputState.maxY - outputState.minY + 2 * properties.spacing;
// Echo header
writeln("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
writeln("<svg xmlns=\"http://www.w3.org/2000/svg\" "
+ "width=\"" + (width + properties.spacing) + "mm\" "
+ "height=\"" + (height + properties.spacing) + "mm\" "
+ "viewBox=\"" + [-properties.spacing, outputState.minY - properties.spacing,
width + properties.spacing, height + properties.spacing].join(" ") + "\""
+ ">");
writeln(outputState.text);
}