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-2024 by Autodesk, Inc.
  All rights reserved.
 
  $Revision: 44110 058ad3fcadd4bcf4c23db3bffce724e6c8437b07 $
  $Date: 2024-01-30 13:09:09 $
 
  FORKID {2E27B627-115A-4A16-A853-5B9B9D9AF480}
*/
 
description = "Glowforge";
vendor = "Glowforge";
legal = "Copyright (C) 2015-2024 by Autodesk, Inc.";
certificationLevel = 2;
 
longDescription = "Generic post for Glowforge laser. The post will output the toolpath as SVG graphics which can then be uploaded directly to Glowforge.";
 
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 = {
  aLineWidth: {
    title      : "SVG stroke width (mm)",
    description: "The width of lines in the SVG in mm.",
    type       : "number",
    value      : 0.1,
    scope      : "post"
  },
  aMargin: {
    title      : "Margin (mm)",
    description: "Sets the margin in mm when 'Crop to Workpiece' is used.",
    type       : "number",
    value      : 2,
    scope      : "post"
  },
  aCheckForRadiusCompensation: {
    title      : "Disallow Sideways Compensation",
    description: "Check every operation for Sideways Compensation 'In Control'. Throw an error if enabled and Sideways Compensation is used.",
    type       : "boolean",
    value      : false,
    scope      : "post"
  },
  aWorkArea: {
    title      : "Work area",
    description: "Select the method used to calculate the work area.  'Auto' = calculate from stock, 'Define' = use Work area dimensions, 'None' = use defined work origin.",
    type       : "enum",
    values     : [
      {title:"Auto", id:"auto"},
      {title:"Define", id:"work"},
      {title:"None", id:"none"}
    ],
    value: "auto",
    scope: "post"
  },
  aWorkAreaWidth: {
    title      : "Work area width (mm)",
    description: "Work Area Width in mm, used when 'Work area' is set to 'Defined'. Typically the max cutting width of the Glowforge.",
    type       : "number",
    value      : 495,
    scope      : "post"
  },
  aWorkAreaHeight: {
    title      : "Work area height (mm)",
    description: "Height in mm, used when 'Work area' is set to 'Defined'. Typically the max cutting height of the Glowforge.",
    type       : "number",
    value      : 279,
    scope      : "post"
  },
  aIgnoreLeadMoves: {
    title      : "Ignore lead-in/lead-out moves",
    description: "Enable to ignore lead-in/lead-out moves.",
    type       : "boolean",
    value      : true,
    scope      : "post"
  }
};
 
xyzFormat = createFormat({decimals:3});
decimalFormat = createFormat({decimals:3});
 
 
// Recommended colors for color mapping.
var COLOR_GREEN = "1FB714";
var COLOR_YELLOW = "FBF305";
var COLOR_DARK_GREEN = "006412";
var COLOR_ORANGE = "FF6403";
var COLOR_BROWN = "562C05";
var COLOR_RED = "DD0907";
var COLOR_TAN = "90713A";
var COLOR_MAGENTA = "F20884";
var COLOR_PURPLE = "4700A5";
var COLOR_BLUE = "0000D3";
var COLOR_CYAN = "02ABEA";
var COLOR_BLACK = "000000";
 
var COLOR_CYCLE = [COLOR_CYAN,
  COLOR_MAGENTA,
  COLOR_YELLOW,
  COLOR_RED,
  COLOR_GREEN,
  COLOR_BLUE,
  COLOR_ORANGE,
  COLOR_DARK_GREEN,
  COLOR_PURPLE,
  COLOR_BROWN,
  COLOR_TAN,
  COLOR_BLACK];
 
var MIN_COLORS = 6; // minimum number of colors
 
/** Select a subset of colors so our preferred color pallet is used (and not simply the color with the lowest hex value first). */
function selectColors() {
  var requiredColors = Math.max(MIN_COLORS, getNumberOfSections()); // makes sure that more than enough colors get made
  var finalColorCycle = [];
  var numColors = COLOR_CYCLE.length;
 
  // if the number of default colors is too small, we will build lighter shades of those colors to fill in the extra needed colors
  var alphaSteps = Math.ceil(requiredColors / numColors);
  var alphaStepSize = 1 / (alphaSteps + 1);  // + 1 stops the last alpha blend stage for all colors being FFFFFF
  var alphaStepIndex = 0;
  var colorIndex = 0;
  var finalColorCycle = [];
 
  for (var i = 0; i < requiredColors; i++) {
    finalColorCycle.push(alphaBlendHexColor(COLOR_CYCLE[colorIndex], 1 - (alphaStepSize * alphaStepIndex)));
    colorIndex += 1;  // next color
    if (colorIndex >= numColors) {
      colorIndex = 0;  // start back at the first color
      alphaStepIndex++;  // next lighter shade
    }
  }
 
  // reset all color related variables to allow re-runs
  machineState.activeColorCycle = sortColors(finalColorCycle);
}
 
function compareHexValues(a, b) {
  if (a.hexValue < b.hexValue) {
    return -1;
  }
  if (a.hexValue > b.hexValue) {
    return 1;
  }
  return 0;
}
 
/** Glowforge doesn't respect the order of operations in the SVG file, it re-sorts them by the hex color value in ascending order
    so here the color cycle is sorted to preserve op order from CAM. */
function sortColors(inputColors) {
  var mappedColors = new Array();
  for (var color in inputColors) {
    mappedColors.push({hexColor:"#" + inputColors[color], hexValue:parseInt(inputColors[color], 16)});
  }
  mappedColors.sort(compareHexValues);
 
  var returnColors = new Array();
  for (var color in mappedColors) {
    returnColors.push(mappedColors[color].hexColor);
  }
  return returnColors;
}
 
/** Returns a hex color that is alphaPercent lighter than the input color. */
function alphaBlendHexColor(hexColorString, alphaPercent) {
  // alphaPercent needs to be converted from a float to a fraction of 255
  var alpha = alphaPercent;
 
  // hex color needs to be converted from a hex string to its constituent parts:
  var red = parseInt(hexColorString.substring(0, 2), 16);
  var green = parseInt(hexColorString.substring(2, 4), 16);
  var blue = parseInt(hexColorString.substring(4, 6), 16);
 
  return [alphaBlend(red, alpha), alphaBlend(green, alpha), alphaBlend(blue, alpha)].join("");
}
 
/** Returns properly padded 2 digit hex strings for RGB color channels. */
function toHexColorChannel(decimal) {
  var hex = decimal.toString(16);
  return (hex.length === 1 ? "0" : "") + hex;
}
 
/** Alpha blend a color channel white. */
function alphaBlend(colorChannel, alpha) {
  return toHexColorChannel(Math.round((1 - alpha) * 255 + alpha * colorChannel));
}
 
/** Called on the start of each section, initalizes the first color from the active color cycle. */
function nextColor() {
  machineState.currentColorIndex++;
  if (machineState.currentColorIndex >= machineState.activeColorCycle.length) {
    error(localize("Not enough colors were generated!"));
  }
 
  machineState.currentHexColor = machineState.activeColorCycle[machineState.currentColorIndex];
}
 
var useFillForSection = false; // should the current section be cut (using a stroke) or etched (using a fill)?
/** For Etch/Vaporize/Engrave, returns fill settings, otherwise none. */
function fill() {
  if (useFillForSection) {
    return "fill=\"" + machineState.currentHexColor + "\"";
  }
  return "fill=\"none\"";
}
 
/** for through cuts, returns stroke settings, otherwise none */
function stroke() {
  if (useFillForSection) {
    return "stroke=\"none\"";
  }
  return "stroke=\"" + machineState.currentHexColor + "\" stroke-width=\"" + getProperty("aLineWidth") + "\"";
}
 
var activePathElements = [];
function addPathElement() {
  var args = Array.prototype.slice.call(arguments);
 
  // don't allow moves after a rapid or similar move
  if (arguments[0] === "M") {
    if (machineState.allowMoveCommand) {
      // if this is a move, this should disable further moves untill rapid or similar is detected.
      machineState.allowMoveCommand = false;
    } else {
      // skip rendering this move command since it was not preceeded by a rapid move
      return;
    }
  }
 
  activePathElements.push(args.join(" "));
}
 
function finishPath() {
  if (!activePathElements || activePathElements.length === 0) {
    error(localize("An operation resulted in no detectable paths!"));
    return;
  }
 
  var opComment = hasParameter("operation-comment") ? getParameter("operation-comment") : "[No Title]";
 
  writeln("<g id="\"operation-"" +="" (1="" currentsection.getid())="" "\"="">");
  writeln("    <title>" + opComment + " (" + localize("Op") + ": " + (1 + currentSection.getId()) + "/" + getNumberOfSections() + ")</title>");
  writeln(
    "    <path d="\""" +="" activepathelements.join("\n="" ")="" "\"="" "="" fill()="" stroke()="">"
  );
  writeln("</path></g>");
  activePathElements = [];
  machineState.allowMoveCommand = true;
}
 
/** return true if the program should halt because of missing radius compensation in the computer */
function isRadiusCompensationInvalid() {
  if (getProperty("aCheckForRadiusCompensation") && (radiusCompensation != RADIUS_COMPENSATION_OFF)) {
    error(localize(subst("Operation: %1. The Sideways Compensation type 'In Control' is not supported. This must be set to 'In Computer' in the passes tab.", (1 + currentSection.getId()))));
    return;
  }
}
 
function printVector(v) {
  return decimalFormat.format(v.x) + "," + decimalFormat.format(v.y);
}
 
var machineState;
 
/** Global state. */
function resetState() {
  return {
    // selected colors to use for this run
    activeColorCycle : null,
    // the hex string of the current color
    currentHexColor  : null,
    // the index of the current color
    currentColorIndex: -1,
    // track if the next path element can be a move command
    allowMoveCommand : null,
    // is the work area too small?
    workAreaTooSmall : false
  };
}
 
function onOpen() {
  unit = MM; // machine requires input in MM
 
  if (getProperty("aMargin") < 0) {
    error(localize("Margin must be 0 or positive."));
    return;
  }
 
  // reset all per-run state
  machineState = resetState();
 
  // select colors now that the number of ops is available
  selectColors();
 
  // calculate workpiece dimensions
  var stock = getWorkpiece();
  var workpiece = getWorkpiece();
  workpiece.expandTo(new Vector(0, 0, 0)); // include origin of WCS, which is always 0,0,0
  var delta = Vector.diff(workpiece.upper, workpiece.lower);
  var marginX = getProperty("aMargin");
  var marginY = getProperty("aMargin");
  if (getProperty("aWorkArea") == "work") { // use work area size specified in properties
    var width = Math.max(getProperty("aWorkAreaWidth"), delta.x);
    var height = Math.max(getProperty("aWorkAreaHeight"), delta.y);
    machineState.workAreaTooSmall = width > getProperty("aWorkAreaWidth") || height > getProperty("aWorkAreaHeight");
    marginX = (width - delta.x) / 2;
    marginY = (height - delta.y) / 2;
  }
  workpiece.expandTo(new Vector((workpiece.lower.x - marginX), (workpiece.lower.y - marginY), 0));
  workpiece.expandTo(new Vector((workpiece.upper.x + marginX), (workpiece.upper.y + marginY), 0));
  delta = Vector.diff(workpiece.upper, workpiece.lower);
 
  // calculate translations
  var translateX = 0;
  var translateY = 0;
 
  if (getProperty("aWorkArea") !== "none") {
    translateX = -workpiece.lower.x;
    translateY = workpiece.upper.y;
  }
 
  writeln("<!--?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?-->");
  writeln("<svg xmlns="\"http://www.w3.org/2000/svg\"" width="\""" +="" delta.x="" "mm\"="" height="\""" delta.y="" viewBox="\"0" 0="" "="" "\"="">");
  writeln("<desc>Created with " + description + " for Fusion CAM and HSM. To download visit: " + POST_URL + "</desc>");
 
  // write a comment explaining what info we got from the CAM system about the stock and coordinate system
  writeln("<!-- CAM Setup Info:");
  writeln("Work Area Width: " + delta.x + "mm");
  writeln("Work Area Height: " + delta.y + "mm");
  if (machineState.workAreaTooSmall) {
    writeln("### WORK AREA IS TOO SMALL ###");
    warning(localize("Work area is too small"));
  }
  writeln("Stock box Upper Right: " + printVector(stock.upper));
  writeln("Stock box Lower Left: " + printVector(stock.lower));
  writeln("Origin: " + "0,0");
  writeln("Selected Colors: " + machineState.activeColorCycle.join(", "));
  writeln("-->");
 
  // translate + scale operation to flip the Y axis so the output is in the same x/y orientation it was in Fusion
  writeln("<g id="\"global-translation-frame\"" transform="\"translate("" +="" decimalformat.format(translatex)="" ",="" "="" decimalformat.format(translatey)="" ")="" scale(1,="" -1)\"="">");
}
 
function onComment(text) {
  writeln("<!--" + 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;
  }
 
  // use Jet Mode to decide if the shape should be filled or have no fill
  switch (currentSection.jetMode) {
  case JET_MODE_THROUGH:
    useFillForSection = false;
    break;
  case JET_MODE_ETCHING:
  case JET_MODE_VAPORIZE:
    useFillForSection = true;
    break;
  default:
    error(localize("Unsupported cutting mode."));
    return;
  }
 
  var remaining = currentSection.workPlane;
  if (!isSameDirection(remaining.forward, new Vector(0, 0, 1))) {
    error(localize("Tool orientation is not supported."));
    return;
  }
  setRotation(remaining);
  nextColor();
}
 
function onParameter(name, value) {
}
 
function onDwell(seconds) {
}
 
function onCycle() {
}
 
function onCyclePoint(x, y, z) {
}
 
function onCycleEnd() {
}
 
function ignoreMove() {
  switch (movement) {
  case MOVEMENT_CUTTING:
  case MOVEMENT_REDUCED:
  case MOVEMENT_FINISH_CUTTING:
    break;
  case MOVEMENT_LEAD_IN:
  case MOVEMENT_LEAD_OUT:
    if (getProperty("aIgnoreLeadMoves")) {
      machineState.allowMoveCommand = true;
      return true;
    }
    break;
  case MOVEMENT_RAPID:
  case MOVEMENT_HIGH_FEED:
  case MOVEMENT_LINK_TRANSITION:
  case MOVEMENT_LINK_DIRECT:
  default:
    machineState.allowMoveCommand = true;
    return true;
  }
  return false;
}
 
function writeLine(x, y) {
  isRadiusCompensationInvalid();
  if (ignoreMove()) {
    return;
  }
 
  var start = getCurrentPosition();
  if ((xyzFormat.format(start.x) == xyzFormat.format(x)) &&
      (xyzFormat.format(start.y) == xyzFormat.format(y))) {
    return; // ignore vertical
  }
 
  addPathElement("M", xyzFormat.format(start.x), xyzFormat.format(start.y));
  addPathElement("L", xyzFormat.format(x), xyzFormat.format(y));
}
 
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) {
  onExpandedRapid(x, y, z);
}
 
function onLinear5D(x, y, z, dx, dy, dz, feed) {
  onExpandedLinear(x, y, z);
}
 
function onCircular(clockwise, cx, cy, cz, x, y, z, feed) {
  isRadiusCompensationInvalid();
  if (ignoreMove()) {
    return;
  }
 
  var start = getCurrentPosition();
 
  var largeArc = (getCircularSweep() > Math.PI) ? 1 : 0;
  var sweepFlag = isClockwise() ? 0 : 1;
  addPathElement("M", xyzFormat.format(start.x), xyzFormat.format(start.y));
  addPathElement("A", xyzFormat.format(getCircularRadius()), xyzFormat.format(getCircularRadius()), 0, largeArc, sweepFlag, xyzFormat.format(x), xyzFormat.format(y));
}
 
function onCommand() {
}
 
function onSectionEnd() {
  finishPath();
}
 
function onClose() {
  writeln("</g>");
  // draw an untranslated box to represent the work are boundary on top of everything
  if (machineState.workAreaTooSmall) {
    writeln("<rect id="\"work-area-boundary\"" x="\""" +="" 0="" "\"="" y="\""" width="\""" decimalformat.format(getproperty("aworkareawidth"))="" height="\""" decimalformat.format(getproperty("aworkareaheight"))="" style="\"fill:none;stroke:red;stroke-width:1;\"/">");
  }
  writeln("</rect></svg>");
}
 
function setProperty(property, value) {
  properties[property].current = value;
}