A TPLang inspired JavaScript to G-Code preprocessor in pure JavaScript.
OpenJSCAM can be installed using any node package manager, in this example we will use npm.
npm install --save openjscamYou can also grab the bundle.js file in the root of the project, which contains the code + dependencies all together in one file.
It puts all the openjscam functions on the window.openjscam property, this is usefull for running it in the browser.
You can add openjscam as a dependency to you project, then import the functions you need, or make all of them available in your scope by using the with (openjscam) {} operator
Example using require:
const {
rapid,
cut,
icut,
log
} = require('openjscam')
const zSafe = 3
const zCut = -1.5
rapid({ z: zSafe })
rapid({ x: 0, y: 0 })
cut({ z: zCut })
cut({ y: 100 })
cut({ x: 100 })
cut({ y: -100 })
cut({ x: -100 })
rapid({ z: zSafe })
log()Example using with (openjscam) {}
const openjscam = require('openjscam')
// by using `with`, all properties of the `openjscam` object are automatically available in it's scope.
with (openjscam) {
const zSafe = 3
const zCut = -1.5
rapid({ z: zSafe })
rapid({ x: 0, y: 0 })
cut({ z: zCut })
cut({ y: 100 })
cut({ x: 100 })
cut({ y: -100 })
cut({ x: -100 })
rapid({ z: zSafe })
log()
}Set the units (Will output G21 for METRIC or G20 for IMPERIAL)
const { units, METRIC, log } = require('openjscam')
units(METRIC)
log()output
G21Set the tool to use (usefull when having a tool changer)
NOT IMPLEMENTED YET
const { tool, log } = require('openjscam')
tool(1)
log()Set the feedrate
const { feed, log } = require('openjscam')
feed(200)
log()output
F200Set the spindle speed (in RPM)
const { speed, log } = require('openjscam')
speed(25000)
log()output
M3 S25000Pause for given duration, duration is given in seconds
const { dwell, log } = require('openjscam')
dwell(0.5)
log()output
G4 P0.5Linear movement to given coordinate. A Coordinate is an object with x, y, z properties. Provide a coordinate for at least one of the axes.
const { cut, log } = require('openjscam')
cut({ x: 10, y: 10 })
log()output
Note that the feedrate will be set using our previous call to feed(200), when no feedrate is set yet, an error will be thrown
G1 X10 Y10Incremental linear movement to given offset from last coordinate. A Coordinate is an object with x, y, z properties. Provide a coordinate for at least one of the axes.
const { icut, log } = require('openjscam')
icut({ x: 10, y: 10 })
log()output
Note that we assumed the last point was X10 Y10 from the previous example.
G1 X20 Y20Rapid movement to given coordinate. A Coordinate is an object with x, y, z properties. Provide a coordinate for at least one of the axes.
const { rapid, log } = require('openjscam')
rapid({ x: 10, y: 10 })
log()output
G0 X10 Y10Incremental rapid movement to given offset from last coordinate. A Coordinate is an object with x, y, z properties. Provide a coordinate for at least one of the axes.
const { irapid, log } = require('openjscam')
irapid({ x: 10, y: 10 })
log()output
Note that we assumed the last point was X10 Y10 from the previous example.
G0 X20 Y20Create an arc from with a given offset from the last point and an angle in degrees.
const { arc, log } = require('openjscam')
arc({ x: 10 }, 180)
log()output
Note that we assumed the last point was X0 Y0
G2 X20 Y0 I10 J0 Z0Translate (move) child operations by given offset
const { translate, cut, log } = require('openjscam')
translate({ x: 100, y: 50 }, function () {
cut({ x: 10, y: 10 })
})
log()output
G1 X110 Y60Rotate child operations by given angle in degrees
const { rotate, cut, log } = require('openjscam')
rotate(45, function () {
cut({ x: 10, y: 10 })
})
log()output
G1 X14.142 Y0 Z0Log the G-Code to the console
const { cut, log } = require('openjscam')
cut({ x: 10, y: 10 })
log()output
G1 X10 Y10Save the G-Code to a file (only works in Node.JS)
const { cut, save } = require('openjscam')
cut({ x: 10, y: 10 })
save('/path/to/gcode/file.gcode')Returns the G-Code as an array of strings (lines)
const { cut, gcode } = require('openjscam')
cut({ x: 10, y: 10 })
var code = gcode()
// do something with the gcode