Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added layer splitting, type decoding, relative/absolute etc #1

Merged
merged 1 commit into from Apr 25, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 105 additions & 36 deletions web/gcode-model.js
Expand Up @@ -3,15 +3,69 @@ function createObjectFromGCode(gcode) {
// http://reprap.org/wiki/G-code
// http://en.wikipedia.org/wiki/G-code
// SprintRun source code

var object = new THREE.Object3D();

var geometry = new THREE.Geometry();


var lastLine = {x:0, y:0, z:0, e:0, f:0, extruding:false};

var parser = new GCodeParser({


var layers = [];
var layer = undefined;
var bbbox = { min: { x:100000,y:100000,z:100000 }, max: { x:-100000,y:-100000,z:-100000 } };

function newLayer(line) {
layer = { type: {}, layer: layers.count(), z: line.z, };
layers.push(layer);
}
function getLineGroup(line) {
if (layer == undefined)
newLayer(line);
var speed = Math.round(line.e / 1000);
var grouptype = (line.extruding ? 10000 : 0) + speed;
var color = new THREE.Color(line.extruding ? 0xffffff : 0x0000ff);
if (layer.type[grouptype] == undefined) {
layer.type[grouptype] = {
type: grouptype,
feed: line.e,
extruding: line.extruding,
color: color,
segmentCount: 0,
material: new THREE.LineBasicMaterial({
opacity:line.extruding ? 0.5 : 0.4,
transparent: true,
linewidth: 1,
vertexColors: THREE.FaceColors }),
geometry: new THREE.Geometry(),
}
}
return layer.type[grouptype];
}
function addSegment(p1, p2) {
var group = getLineGroup(p2);
var geometry = group.geometry;

group.segmentCount++;
geometry.vertices.push(new THREE.Vertex(
new THREE.Vector3(p1.x, p1.y, p1.z)));
geometry.vertices.push(new THREE.Vertex(
new THREE.Vector3(p2.x, p2.y, p2.z)));
geometry.colors.push(group.color);
geometry.colors.push(group.color);
if (p2.extruding) {
bbbox.min.x = Math.min(bbbox.min.x, p2.x);
bbbox.min.y = Math.min(bbbox.min.y, p2.y);
bbbox.min.z = Math.min(bbbox.min.z, p2.z);
bbbox.max.x = Math.max(bbbox.max.x, p2.x);
bbbox.max.y = Math.max(bbbox.max.y, p2.y);
bbbox.max.z = Math.max(bbbox.max.z, p2.z);
}
}
var relative = false;
function delta(v1, v2) {
return relative ? v2 : v2 - v1;
}
function absolute (v1, v2) {
return relative ? v1 + v2 : v2;
}

var parser = new GCodeParser({
G1: function(args, line) {
// Example: G1 Z1.0 F3000
// G1 X99.9948 Y80.0611 Z15.0 F1500.0 E981.64869
Expand All @@ -22,25 +76,20 @@ function createObjectFromGCode(gcode) {
// 22.4 mm.

var newLine = {
x: args.x !== undefined ? args.x : lastLine.x,
y: args.y !== undefined ? args.y : lastLine.y,
z: args.z !== undefined ? args.z : lastLine.z,
e: args.e !== undefined ? args.e : lastLine.e,
f: args.f !== undefined ? args.f : lastLine.f,
x: args.x !== undefined ? absolute(lastLine.x, args.x) : lastLine.x,
y: args.y !== undefined ? absolute(lastLine.y, args.y) : lastLine.y,
z: args.z !== undefined ? absolute(lastLine.z, args.z) : lastLine.z,
e: args.e !== undefined ? absolute(lastLine.e, args.e) : lastLine.e,
f: args.f !== undefined ? absolute(lastLine.f, args.f) : lastLine.f,
};

newLine.extruding = (newLine.e - lastLine.e) > 0;
var color = new THREE.Color(newLine.extruding ? 0xFFFFFF : 0x0000FF);

if (newLine.extruding) {
geometry.vertices.push(new THREE.Vertex(
new THREE.Vector3(lastLine.x, lastLine.y, lastLine.z)));
geometry.vertices.push(new THREE.Vertex(
new THREE.Vector3(newLine.x, newLine.y, newLine.z)));
geometry.colors.push(color);
geometry.colors.push(color);
}

/* layer change detection is or made by watching Z, it's made by
watching when we extrude at a new Z position */
if (delta(lastLine.e, newLine.e) > 0) {
newLine.extruding = delta(lastLine.e, newLine.e) > 0;
if (layer == undefined || newLine.z != layer.z)
newLayer(newLine);
}
addSegment(lastLine, newLine);
lastLine = newLine;
},

Expand All @@ -58,7 +107,7 @@ function createObjectFromGCode(gcode) {
// All coordinates from now on are absolute relative to the
// origin of the machine. (This is the RepRap default.)

// TODO!
relative = false;
},

G91: function(args) {
Expand All @@ -67,6 +116,7 @@ function createObjectFromGCode(gcode) {
// All coordinates from now on are relative to the last position.

// TODO!
relative = true;
},

G92: function(args) { // E0
Expand All @@ -78,6 +128,12 @@ function createObjectFromGCode(gcode) {
// No physical motion will occur.

// TODO: Only support E0
var newLine = lastLine;
newLine.x= args.x !== undefined ? args.x : newLine.x;
newLine.y= args.y !== undefined ? args.y : newLine.y;
newLine.z= args.z !== undefined ? args.z : newLine.z;
newLine.e= args.e !== undefined ? args.e : newLine.e;
lastLine = newLine;
},

M82: function(args) {
Expand Down Expand Up @@ -106,19 +162,32 @@ function createObjectFromGCode(gcode) {

parser.parse(gcode);

var lineMaterial = new THREE.LineBasicMaterial({
opacity:0.4,
linewidth: 1,
vertexColors: THREE.FaceColors});
object.add(new THREE.Line(geometry, lineMaterial, THREE.LinePieces));
console.log("Layer Count ", layers.count());

var object = new THREE.Object3D();

for (var lid in layers) {
var layer = layers[lid];
// console.log("Layer ", layer.layer);
for (var tid in layer.type) {
var type = layer.type[tid];
// console.log("Layer ", layer.layer, " type ", type.type, " seg ", type.segmentCount);
object.add(new THREE.Line(type.geometry, type.material, THREE.LinePieces));
}
}
console.log("bbox ", bbbox);

// Center
geometry.computeBoundingBox();
var center = new THREE.Vector3()
.add(geometry.boundingBox.min, geometry.boundingBox.max)
.divideScalar(2);
var scale = 3; // TODO: Auto size

var center = new THREE.Vector3(
bbbox.min.x + ((bbbox.max.x - bbbox.min.x) / 2),
bbbox.min.y + ((bbbox.max.y - bbbox.min.y) / 2),
bbbox.min.z + ((bbbox.max.z - bbbox.min.z) / 2));
console.log("center ", center);

object.position = center.multiplyScalar(-scale);

object.scale.multiplyScalar(scale);

return object;
Expand Down