A TPLang inspired JavaScript to G-Code preprocessor in pure JavaScript.
npm install @makercam/openjscamA 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.
``shell`
npm install --save openjscam
You can also grab the bundle.js file in the root of the project, which contains the code + dependencies all together in one file.window.openjscam
It puts all the openjscam functions on the 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:`js
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) {}`js
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)
#### example
`js`
const { units, METRIC, log } = require('openjscam')
units(METRIC)
log()
output
`gcode`
G21
Set the tool to use (usefull when having a tool changer)
NOT IMPLEMENTED YET
#### example
`js`
const { tool, log } = require('openjscam')
tool(1)
log()
Set the feedrate
#### example
`js`
const { feed, log } = require('openjscam')
feed(200)
log()
output
`gcode`
F200
Set the spindle speed (in RPM)
#### example
`js`
const { speed, log } = require('openjscam')
speed(25000)
log()
output
`gcode`
M3 S25000
Pause for given duration, duration is given in seconds
#### example
`js`
const { dwell, log } = require('openjscam')
dwell(0.5)
log()
output
`gcode`
G4 P0.5
Linear movement to given coordinate. A Coordinate is an object with x, y, z properties.
Provide a coordinate for at least one of the axes.
#### example
`js`
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
`gcode`
G1 X10 Y10
Incremental 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.
#### example
`js`
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.
`gcode`
G1 X20 Y20
Rapid movement to given coordinate. A Coordinate is an object with x, y, z properties.
Provide a coordinate for at least one of the axes.
#### example
`js`
const { rapid, log } = require('openjscam')
rapid({ x: 10, y: 10 })
log()
output
`gcode`
G0 X10 Y10
Incremental 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.
#### example
`js`
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.
`gcode`
G0 X20 Y20
Create an arc from with a given offset from the last point and an angle in degrees.
#### example
`js`
const { arc, log } = require('openjscam')
arc({ x: 10 }, 180)
log()
output
Note that we assumed the last point was X0 Y0
`gcode`
G2 X20 Y0 I10 J0 Z0
Translate (move) child operations by given offset
#### example
`js`
const { translate, cut, log } = require('openjscam')
translate({ x: 100, y: 50 }, function () {
cut({ x: 10, y: 10 })
})
log()
output
`gcode`
G1 X110 Y60
Rotate child operations by given angle in degrees
#### example
`js`
const { rotate, cut, log } = require('openjscam')
rotate(45, function () {
cut({ x: 10, y: 10 })
})
log()
output
`gcode`
G1 X14.142 Y0 Z0
Log the G-Code to the console
#### example
`js`
const { cut, log } = require('openjscam')
cut({ x: 10, y: 10 })
log()
output
`gcode`
G1 X10 Y10
Save the G-Code to a file (only works in Node.JS)
#### example
`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)
#### example
`js``
const { cut, gcode } = require('openjscam')
cut({ x: 10, y: 10 })
var code = gcode()
// do something with the gcode