Skip to content

Commit 21e9795

Browse files
committed
Adds support for accessing position and modal state with the toolpath
object Methods: * setPosition() * getPosition() * setModal() * getModal()
1 parent 6369739 commit 21e9795

File tree

4 files changed

+144
-9
lines changed

4 files changed

+144
-9
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const Toolpath = require('gcode-toolpath');
1313

1414
const toolpaths = [];
1515
const gcode = new Toolpath({
16-
modal: { // [optional] initial modal state
16+
// Initial position (optional)
17+
position: { x: 0, y: 0, z: 0 },
18+
19+
// Initial modal state (optional)
20+
modal: {
1721
motion: 'G0', // G0, G1, G2, G3, G38.2, G38.3, G38.4, G38.5, G80
1822
wcs: 'G54', // G54, G55, G56, G57, G58, G59
1923
plane: 'G17', // G17: xy-plane, G18: xz-plane, G19: yz-plane
@@ -25,6 +29,7 @@ const gcode = new Toolpath({
2529
coolant: 'M9', // M7, M8, M9
2630
tool: 0
2731
},
32+
2833
// @param {object} modal The modal object.
2934
// @param {object} v1 A 3D vector of the start point.
3035
// @param {object} v2 A 3D vector of the end point.
@@ -33,6 +38,7 @@ const gcode = new Toolpath({
3338
const tool = modal.tool;
3439
toolpaths.push({ motion: motion, tool: tool, v1: v1, v2: v2 });
3540
},
41+
3642
// @param {object} modal The modal object.
3743
// @param {object} v1 A 3D vector of the start point.
3844
// @param {object} v2 A 3D vector of the end point.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gcode-toolpath",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "G-code Toolpath Generator",
55
"author": "Cheton Wu <cheton@gmail.com>",
66
"homepage": "https://github.com/cncjs/gcode-toolpath",

src/Toolpath.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Toolpath {
6969
// M7, M8, M9
7070
coolant: 'M9', // 'M7', 'M8', 'M7,M8', or 'M9'
7171

72+
// Tool Select
7273
tool: 0
7374
};
7475

@@ -559,11 +560,25 @@ class Toolpath {
559560
};
560561

561562
// @param {object} [options]
562-
// @param {object} [options.modalState]
563+
// @param {object} [options.position]
564+
// @param {object} [options.modal]
563565
// @param {function} [options.addLine]
564566
// @param {function} [options.addArcCurve]
565567
constructor(options) {
566-
const { modal, addLine = noop, addArcCurve = noop } = { ...options };
568+
const {
569+
modal,
570+
position,
571+
addLine = noop,
572+
addArcCurve = noop
573+
} = { ...options };
574+
575+
// Position
576+
if (position) {
577+
const { x, y, z } = { ...position };
578+
this.setPosition(x, y, z);
579+
}
580+
581+
// Modal
567582
const nextModal = {};
568583
Object.keys({ ...modal }).forEach(key => {
569584
if (!Object.prototype.hasOwnProperty.call(this.modal, key)) {
@@ -572,9 +587,20 @@ class Toolpath {
572587
nextModal[key] = modal[key];
573588
});
574589
this.setModal(nextModal);
590+
575591
this.fn = { addLine, addArcCurve };
576592

577-
return new Interpreter({ handlers: this.handlers });
593+
const interpreter = new Interpreter({ handlers: this.handlers });
594+
interpreter.setPosition = (...pos) => {
595+
return this.setPosition(...pos);
596+
};
597+
interpreter.getPosition = () => ({ ...this.position });
598+
interpreter.setModal = (modal) => {
599+
return this.setModal(modal);
600+
};
601+
interpreter.getModal = () => ({ ...this.modal });
602+
603+
return interpreter;
578604
}
579605
setModal(modal) {
580606
this.modal = {
@@ -604,10 +630,18 @@ class Toolpath {
604630
isYZPlane() {
605631
return this.modal.plane === 'G19';
606632
}
607-
setPosition(x, y, z) {
608-
this.position.x = (typeof x === 'number') ? x : this.position.x;
609-
this.position.y = (typeof y === 'number') ? y : this.position.y;
610-
this.position.z = (typeof z === 'number') ? z : this.position.z;
633+
setPosition(...pos) {
634+
if (typeof pos[0] === 'object') {
635+
const { x, y, z } = { ...pos[0] };
636+
this.position.x = (typeof x === 'number') ? x : this.position.x;
637+
this.position.y = (typeof y === 'number') ? y : this.position.y;
638+
this.position.z = (typeof z === 'number') ? z : this.position.z;
639+
} else {
640+
const [x, y, z] = pos;
641+
this.position.x = (typeof x === 'number') ? x : this.position.x;
642+
this.position.y = (typeof y === 'number') ? y : this.position.y;
643+
this.position.z = (typeof z === 'number') ? z : this.position.z;
644+
}
611645
}
612646
translateX(x, relative) {
613647
if (x !== undefined) {

test/index.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,101 @@ describe('G-code Toolpath', () => {
8181
});
8282
});
8383

84+
describe('position', () => {
85+
it('should match the specified position.', (done) => {
86+
const toolpath = new Toolpath({
87+
position: { x: 200, y: 100 }
88+
});
89+
expect(toolpath.getPosition()).to.deep.equal({ x: 200, y: 100, z: 0 });
90+
toolpath.setPosition({ y: 200, z: 10 });
91+
expect(toolpath.getPosition()).to.deep.equal({ x: 200, y: 200, z: 10 });
92+
toolpath.setPosition(10, 10);
93+
expect(toolpath.getPosition()).to.deep.equal({ x: 10, y: 10, z: 10 });
94+
toolpath.setPosition(0, 0, 0);
95+
expect(toolpath.getPosition()).to.deep.equal({ x: 0, y: 0, z: 0 });
96+
done();
97+
});
98+
});
99+
100+
describe('modal', () => {
101+
it('should match the specified modal state.', (done) => {
102+
const toolpath = new Toolpath({
103+
modal: {
104+
tool: 1
105+
}
106+
});
107+
const expectedModal = {
108+
// Moton Mode
109+
// G0, G1, G2, G3, G38.2, G38.3, G38.4, G38.5, G80
110+
motion: 'G0',
111+
112+
// Coordinate System Select
113+
// G54, G55, G56, G57, G58, G59
114+
wcs: 'G54',
115+
116+
// Plane Select
117+
// G17: XY-plane, G18: ZX-plane, G19: YZ-plane
118+
plane: 'G17',
119+
120+
// Units Mode
121+
// G20: Inches, G21: Millimeters
122+
units: 'G21',
123+
124+
// Distance Mode
125+
// G90: Absolute, G91: Relative
126+
distance: 'G90',
127+
128+
// Arc IJK distance mode
129+
arc: 'G91.1',
130+
131+
// Feed Rate Mode
132+
// G93: Inverse time mode, G94: Units per minute mode, G95: Units per rev mode
133+
feedrate: 'G94',
134+
135+
// Cutter Radius Compensation
136+
cutter: 'G40',
137+
138+
// Tool Length Offset
139+
// G43.1, G49
140+
tlo: 'G49',
141+
142+
// Program Mode
143+
// M0, M1, M2, M30
144+
program: 'M0',
145+
146+
// Spingle State
147+
// M3, M4, M5
148+
spindle: 'M5',
149+
150+
// Coolant State
151+
// M7, M8, M9
152+
coolant: 'M9', // 'M7', 'M8', 'M7,M8', or 'M9'
153+
154+
// Tool Select
155+
tool: 0
156+
};
157+
158+
expect(toolpath.getModal()).to.deep.equal({
159+
motion: 'G0',
160+
wcs: 'G54',
161+
plane: 'G17',
162+
units: 'G21',
163+
distance: 'G90',
164+
arc: 'G91.1',
165+
feedrate: 'G94',
166+
cutter: 'G40',
167+
tlo: 'G49',
168+
program: 'M0',
169+
spindle: 'M5',
170+
coolant: 'M9',
171+
tool: 1
172+
});
173+
toolpath.setModal({ tool: 2 });
174+
expect(toolpath.getModal().tool).to.equal(2);
175+
done();
176+
});
177+
});
178+
84179
describe('Linear Move: G0/G1', () => {
85180
it('should generate tool paths for linear movement.', (done) => {
86181
const expectedMotions = [

0 commit comments

Comments
 (0)