Merge branch 'layer-part-test'

This commit is contained in:
casperlamboo 2015-05-13 17:38:07 +02:00
commit 0bc518dcc9
10 changed files with 992 additions and 363 deletions

250
build/d3d.js vendored
View File

@ -568,140 +568,135 @@ D3D.Printer.prototype.subsituteVariables = function (gcode) {
* *
******************************************************/ ******************************************************/
D3D.Path = function (path, closed) { D3D.Paths = function (paths, closed) {
"use strict"; "use strict";
this.path = path || []; Array.call(this);
this.setPaths(paths || []);
this.closed = (closed !== undefined) ? closed : true; this.closed = (closed !== undefined) ? closed : true;
}; };
D3D.Path.prototype.setPath = function (path) { D3D.Paths.prototype = Object.create(Array.prototype);
D3D.Paths.prototype.setPaths = function (paths) {
"use strict"; "use strict";
this.path = path; for (var i = 0; i < paths.length; i ++) {
var path = paths[i];
this.push(path);
}
return this; return this;
}; };
D3D.Path.prototype.union = function (path) { D3D.Paths.prototype.clip = function (path, type) {
"use strict"; "use strict";
var solution = new ClipperLib.Paths(); var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper(); var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed); clipper.AddPaths(this, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed); clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctUnion, solution); clipper.Execute(type, solution);
return new D3D.Path(solution, this.closed); return new D3D.Paths(solution);
}; };
D3D.Path.prototype.difference = function (path) { D3D.Paths.prototype.union = function (path) {
"use strict"; "use strict";
var solution = new ClipperLib.Paths(); return this.clip(path, ClipperLib.ClipType.ctUnion);
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctDifference, solution);
return new D3D.Path(solution, this.closed);
}; };
D3D.Path.prototype.intersect = function (path) { D3D.Paths.prototype.difference = function (path) {
"use strict"; "use strict";
var solution = new ClipperLib.Paths(); return this.clip(path, ClipperLib.ClipType.ctDifference);
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctIntersection, solution);
return new D3D.Path(solution, this.closed);
}; };
D3D.Path.prototype.xor = function () { D3D.Paths.prototype.intersect = function (path) {
"use strict"; "use strict";
var solution = new ClipperLib.Paths(); return this.clip(path, ClipperLib.ClipType.ctIntersection);
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctXor, solution);
return new D3D.Path(solution, this.closed);
}; };
D3D.Path.prototype.offset = function (offset) { D3D.Paths.prototype.xor = function () {
"use strict";
return this.clip(path, ClipperLib.ClipType.ctXor);
};
D3D.Paths.prototype.offset = function (offset) {
"use strict"; "use strict";
var solution = new ClipperLib.Paths(); var solution = new ClipperLib.Paths();
var co = new ClipperLib.ClipperOffset(1, 1); var co = new ClipperLib.ClipperOffset(1, 1);
co.AddPaths(this.path, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon); co.AddPaths(this, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
co.Execute(solution, offset); co.Execute(solution, offset);
return new D3D.Path(solution, this.closed); return new D3D.Paths(solution);
}; };
D3D.Path.prototype.scaleUp = function (factor) { D3D.Paths.prototype.scaleUp = function (factor) {
"use strict"; "use strict";
var path = ClipperLib.JS.ScaleUpPaths(this.path, factor); var path = ClipperLib.JS.ScaleUpPaths(this, factor);
return this; return this;
}; };
D3D.Path.prototype.scaleDown = function (factor) { D3D.Paths.prototype.scaleDown = function (factor) {
"use strict"; "use strict";
var path = ClipperLib.JS.ScaleDownPaths(this.path, factor); var path = ClipperLib.JS.ScaleDownPaths(this, factor);
return this; return this;
}; };
D3D.Path.prototype.tresholdArea = function (minArea) { D3D.Paths.prototype.tresholdArea = function (minArea) {
"use strict"; "use strict";
for (var i = 0; i < this.path.length; i ++) { for (var i = 0; i < this.length; i ++) {
var shape = this.path[i]; var shape = this[i];
var area = ClipperLib.Clipper.Area(shape); var area = ClipperLib.Clipper.Area(shape);
if (area < minArea) { if (area < minArea) {
this.path.splice(i, 1); this.splice(i, 1);
i --; i --;
} }
} }
return areas; return areas;
}; };
D3D.Path.prototype.area = function () { D3D.Paths.prototype.area = function () {
"use strict"; "use strict";
var areas = []; return ClipperLib.Clipper.Area(this);
for (var i = 0; i < this.path.length; i ++) {
var shape = this.path[i];
areas.push(ClipperLib.Clipper.Area(shape))
}
return areas;
}; };
D3D.Path.prototype.join = function (path) { D3D.Paths.prototype.join = function (path) {
"use strict"; "use strict";
this.path = this.path.concat(path.path); for (var i = 0; i < path.length; i ++) {
this.push(path[i]);
}
return this; return this;
} };
D3D.Path.prototype.clone = function () { D3D.Paths.prototype.clone = function () {
"use strict"; "use strict";
var path = ClipperLib.JS.Clone(this.path); return new D3D.Paths(ClipperLib.JS.Clone(this), this.closed);
};
D3D.Paths.prototype.bounds = function () {
"use strict";
return new D3D.Path(path, this.closed); return ClipperLib.Clipper.GetBounds(this);
} };
D3D.Path.prototype.draw = function (context, color) { D3D.Paths.prototype.reverse = function () {
"use strict";
ClipperLib.Clipper.ReversePaths(this);
return this;
};
D3D.Paths.prototype.draw = function (context, color) {
"use strict"; "use strict";
context.strokeStyle = color; context.strokeStyle = color;
for (var i = 0; i < this.path.length; i ++) { for (var i = 0; i < this.length; i ++) {
var shape = this.path[i]; var shape = this[i];
context.beginPath(); context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length; var length = this.closed ? (shape.length + 1) : shape.length;
@ -738,12 +733,11 @@ D3D.Slicer.prototype.setMesh = function (mesh) {
mesh.updateMatrix(); mesh.updateMatrix();
var geometry = mesh.geometry.clone(); var geometry = mesh.geometry.clone();
geometry.mergeVertices();
geometry.applyMatrix(mesh.matrix);
if (geometry instanceof THREE.BufferGeometry) { if (geometry instanceof THREE.BufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry); geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} }
geometry.mergeVertices();
geometry.applyMatrix(mesh.matrix);
this.geometry = geometry; this.geometry = geometry;
@ -770,7 +764,8 @@ D3D.Slicer.prototype.createLines = function () {
self.lines.push({ self.lines.push({
line: new THREE.Line3(self.geometry.vertices[a], self.geometry.vertices[b]), line: new THREE.Line3(self.geometry.vertices[a], self.geometry.vertices[b]),
connects: [], connects: [],
normals: [] normals: [],
ignore: 0
}); });
} }
@ -779,6 +774,8 @@ D3D.Slicer.prototype.createLines = function () {
for (var i = 0; i < this.geometry.faces.length; i ++) { for (var i = 0; i < this.geometry.faces.length; i ++) {
var face = this.geometry.faces[i]; var face = this.geometry.faces[i];
//if (face.normal.y !== 1 && face.normal.y !== -1) {
var normal = new THREE.Vector2().set(face.normal.x, face.normal.z).normalize(); var normal = new THREE.Vector2().set(face.normal.x, face.normal.z).normalize();
//check for only adding unique lines //check for only adding unique lines
@ -795,6 +792,7 @@ D3D.Slicer.prototype.createLines = function () {
this.lines[a].normals.push(normal); this.lines[a].normals.push(normal);
this.lines[b].normals.push(normal); this.lines[b].normals.push(normal);
this.lines[c].normals.push(normal); this.lines[c].normals.push(normal);
//}
} }
}; };
D3D.Slicer.prototype.slice = function (height, step) { D3D.Slicer.prototype.slice = function (height, step) {
@ -820,7 +818,7 @@ D3D.Slicer.prototype.slice = function (height, step) {
var slices = []; var slices = [];
for (var layer = 1; layer < layersIntersections.length; layer ++) { for (var layer = 1; layer < layersIntersections.length-1; layer ++) {
var layerIntersections = layersIntersections[layer]; var layerIntersections = layersIntersections[layer];
var y = layer*step; var y = layer*step;
@ -837,7 +835,7 @@ D3D.Slicer.prototype.slice = function (height, step) {
} }
var done = []; var done = [];
var slice = []; var slice = new D3D.Paths([], true);
for (var i = 0; i < layerIntersections.length; i ++) { for (var i = 0; i < layerIntersections.length; i ++) {
var index = layerIntersections[i]; var index = layerIntersections[i];
@ -898,15 +896,34 @@ D3D.Slicer.prototype.slice = function (height, step) {
//think this check is not nescesary, always higher as 0 //think this check is not nescesary, always higher as 0
if (shape.length > 0) { if (shape.length > 0) {
slice.push(shape); slice.push(new D3D.Paths([shape]));
} }
} }
} }
var layerParts = [];
for (var i = 0; i < slice.length; i ++) {
var layerPart1 = slice[i];
var merge = false;
for (var j = 0; j < layerParts.length; j ++) {
var layerPart2 = layerParts[j];
if (layerPart2.intersect(layerPart1).length > 0) {
layerPart2.join(layerPart1);
merge = true;
break;
}
}
if (!merge) {
layerParts.push(layerPart1);
}
}
//stop when ther are no intersects //stop when ther are no intersects
if (slice.length > 0) { if (layerParts.length > 0) {
slices.push(new D3D.Path(slice, true)); slices.push(layerParts);
//slices.push(slice);
} }
else { else {
break; break;
@ -935,10 +952,33 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
for (var layer = 0; layer < slices.length; layer ++) { for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer]; var slice = slices[layer];
var outerLayer = slice.clone(); var layerData = [];
data.push(layerData);
var downSkin = new D3D.Paths();
if (layer - skinCount >= 0) {
var downLayer = slices[layer - skinCount];
for (var i = 0; i < downLayer.length; i ++) {
downSkin.join(downLayer[i]);
}
}
var upSkin = new D3D.Paths();
if (layer + skinCount < slices.length) {
var downLayer = slices[layer + skinCount];
for (var i = 0; i < downLayer.length; i ++) {
upSkin.join(downLayer[i]);
}
}
var surroundingLayer = upSkin.intersect(downSkin).clone().scaleUp(scale);
var sliceData = [];
for (var i = 0; i < slice.length; i ++) {
var part = slice[i];
var outerLayer = part.clone();
outerLayer.scaleUp(scale); outerLayer.scaleUp(scale);
var insets = new D3D.Path(); var insets = new D3D.Paths();
for (var offset = wallThickness; offset <= shellThickness; offset += wallThickness) { for (var offset = wallThickness; offset <= shellThickness; offset += wallThickness) {
var inset = outerLayer.offset(-offset); var inset = outerLayer.offset(-offset);
@ -947,32 +987,31 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var fillArea = (inset || outerLayer).offset(-wallThickness/2); var fillArea = (inset || outerLayer).offset(-wallThickness/2);
var downFill = (layer - skinCount >= 0) ? slices[layer - skinCount] : new D3D.Path(); var highFillArea = fillArea.difference(surroundingLayer);
var upFill = (layer + skinCount < slices.length) ? slices[layer + skinCount] : new D3D.Path();
var highFillArea = fillArea.difference(downFill.intersect(upFill).scaleUp(scale));
var lowFillArea = fillArea.difference(highFillArea); var lowFillArea = fillArea.difference(highFillArea);
var fill = new D3D.Path([], false); var fill = new D3D.Paths([]);
fill.join(lowFillTemplate.intersect(lowFillArea)); fill.join(lowFillTemplate.intersect(lowFillArea));
if (highFillArea.path.length > 0) {
var highFillTemplate = this.getFillTemplate(dimensionsZ, wallThickness, (layer % 2 === 0), (layer % 2 === 1)); var highFillTemplate = this.getFillTemplate(dimensionsZ, wallThickness, (layer % 2 === 0), (layer % 2 === 1));
fill.join(highFillTemplate.intersect(highFillArea)); fill.join(highFillTemplate.intersect(highFillArea));
}
data.push({ layerData.push({
outerLayer: outerLayer.scaleDown(scale), outerLayer: outerLayer.scaleDown(scale),
insets: insets.scaleDown(scale), fill: fill.scaleDown(scale),
fill: fill.scaleDown(scale) insets: insets.scaleDown(scale)
}); });
} }
}
return data; return data;
}; };
D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven) { D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven) {
"use strict"; "use strict";
var paths = []; var paths = new D3D.Paths([], false);
if (even) { if (even) {
for (var length = 0; length <= dimension; length += size) { for (var length = 0; length <= dimension; length += size) {
@ -986,7 +1025,7 @@ D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven)
} }
//return paths; //return paths;
return new D3D.Path(paths, false); return paths;
}; };
D3D.Slicer.prototype.dataToGcode = function (data, printer) { D3D.Slicer.prototype.dataToGcode = function (data, printer) {
"use strict"; "use strict";
@ -1008,13 +1047,15 @@ D3D.Slicer.prototype.dataToGcode = function (data, printer) {
function sliceToGcode (slice) { function sliceToGcode (slice) {
var gcode = []; var gcode = [];
for (var i = 0; i < slice.path.length; i ++) { for (var i = 0; i < slice.length; i ++) {
var shape = slice.path[i]; var shape = slice[i];
var previousPoint; var previousPoint;
for (var j = 0; j < shape.length; j ++) { var length = slice.closed ? (shape.length + 1) : shape.length;
var point = shape[j];
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
if (j === 0) { if (j === 0) {
//TODO //TODO
@ -1082,9 +1123,13 @@ D3D.Slicer.prototype.dataToGcode = function (data, printer) {
var z = ((layer + 1) * layerHeight).toFixed(3); var z = ((layer + 1) * layerHeight).toFixed(3);
gcode = gcode.concat(sliceToGcode(slice.outerLayer)); for (var i = 0; i < slice.length; i ++) {
gcode = gcode.concat(sliceToGcode(slice.insets)); var layerPart = slice[i];
gcode = gcode.concat(sliceToGcode(slice.fill));
gcode = gcode.concat(sliceToGcode(layerPart.outerLayer));
gcode = gcode.concat(sliceToGcode(layerPart.insets));
gcode = gcode.concat(sliceToGcode(layerPart.fill));
}
} }
gcode = gcode.concat(printer.getEndCode()); gcode = gcode.concat(printer.getEndCode());
@ -1106,13 +1151,16 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) {
canvas.height = 400; canvas.height = 400;
var context = canvas.getContext("2d"); var context = canvas.getContext("2d");
for (var layer = min; layer < max; layer ++) { for (var layer = min; layer < max; layer ++) {
var slice = data[layer % data.length]; var slice = data[layer % data.length];
slice.insets.draw(context, "blue"); for (var i = 0; i < slice.length; i ++) {
slice.outerLayer.draw(context, "green"); var layerPart = slice[i];
slice.fill.draw(context, "red");
layerPart.insets.draw(context, "blue");
layerPart.outerLayer.draw(context, "green");
layerPart.fill.draw(context, "red");
}
} }
return canvas; return canvas;

2
build/d3d.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -10,7 +10,7 @@ var files = [
"src/utils.js", "src/utils.js",
"src/box.js", "src/box.js",
"src/printer.js", "src/printer.js",
"src/path.js", "src/paths.js",
"src/slicer.js" "src/slicer.js"
]; ];

View File

@ -2876,8 +2876,8 @@
}; };
ClipperLib.ClipperBase.prototype.AddPaths = function (ppg, polyType, closed) ClipperLib.ClipperBase.prototype.AddPaths = function (ppg, polyType, closed)
{ {
// console.log("-------------------------------------------"); // console.warn("-------------------------------------------");
// console.log(JSON.stringify(ppg)); // console.warn(JSON.stringify(ppg));
var result = false; var result = false;
for (var i = 0, ilen = ppg.length; i < ilen; ++i) for (var i = 0, ilen = ppg.length; i < ilen; ++i)
if (this.AddPath(ppg[i], polyType, closed)) if (this.AddPath(ppg[i], polyType, closed))
@ -3185,7 +3185,7 @@
if (this.m_Scanbeam === null) if (this.m_Scanbeam === null)
break; break;
var topY = this.PopScanbeam(); var topY = this.PopScanbeam();
//console.log("botY:" + botY + ", topY:" + topY); //console.warn("botY:" + botY + ", topY:" + topY);
if (!this.ProcessIntersections(botY, topY)) if (!this.ProcessIntersections(botY, topY))
return false; return false;
this.ProcessEdgesAtTopOfScanbeam(topY); this.ProcessEdgesAtTopOfScanbeam(topY);
@ -4747,7 +4747,7 @@
return; return;
//prepare for sorting ... //prepare for sorting ...
var e = this.m_ActiveEdges; var e = this.m_ActiveEdges;
//console.log(JSON.stringify(JSON.decycle( e ))); //console.warn(JSON.stringify(JSON.decycle( e )));
this.m_SortedEdges = e; this.m_SortedEdges = e;
while (e !== null) while (e !== null)
{ {
@ -4766,13 +4766,13 @@
{ {
var eNext = e.NextInSEL; var eNext = e.NextInSEL;
var pt = new ClipperLib.IntPoint(); var pt = new ClipperLib.IntPoint();
//console.log("e.Curr.X: " + e.Curr.X + " eNext.Curr.X" + eNext.Curr.X); //console.warn("e.Curr.X: " + e.Curr.X + " eNext.Curr.X" + eNext.Curr.X);
if (e.Curr.X > eNext.Curr.X) if (e.Curr.X > eNext.Curr.X)
{ {
if (!this.IntersectPoint(e, eNext, pt) && e.Curr.X > eNext.Curr.X + 1) if (!this.IntersectPoint(e, eNext, pt) && e.Curr.X > eNext.Curr.X + 1)
{ {
//console.log("e.Curr.X: "+JSON.stringify(JSON.decycle( e.Curr.X ))); //console.warn("e.Curr.X: "+JSON.stringify(JSON.decycle( e.Curr.X )));
//console.log("eNext.Curr.X+1: "+JSON.stringify(JSON.decycle( eNext.Curr.X+1))); //console.warn("eNext.Curr.X+1: "+JSON.stringify(JSON.decycle( eNext.Curr.X+1)));
ClipperLib.Error("Intersection error"); ClipperLib.Error("Intersection error");
} }
if (pt.Y > botY) if (pt.Y > botY)
@ -4884,8 +4884,8 @@
else ClipperLib.Clipper.Round = R2; // eg. browser.chrome || browser.firefox || browser.opera else ClipperLib.Clipper.Round = R2; // eg. browser.chrome || browser.firefox || browser.opera
ClipperLib.Clipper.TopX = function (edge, currentY) ClipperLib.Clipper.TopX = function (edge, currentY)
{ {
//if (edge.Bot == edge.Curr) alert ("edge.Bot = edge.Curr"); //if (edge.Bot == edge.Curr) console.warn ("edge.Bot = edge.Curr");
//if (edge.Bot == edge.Top) alert ("edge.Bot = edge.Top"); //if (edge.Bot == edge.Top) console.warn ("edge.Bot = edge.Top");
if (currentY == edge.Top.Y) if (currentY == edge.Top.Y)
return edge.Top.X; return edge.Top.X;
return edge.Bot.X + ClipperLib.Clipper.Round(edge.Dx * (currentY - edge.Bot.Y)); return edge.Bot.X + ClipperLib.Clipper.Round(edge.Dx * (currentY - edge.Bot.Y));
@ -6469,7 +6469,7 @@
if (solution.length > 0) if (solution.length > 0)
solution.splice(0, 1); solution.splice(0, 1);
} }
//console.log(JSON.stringify(solution)); //console.warn(JSON.stringify(solution));
} }
else // function (polytree, delta) else // function (polytree, delta)
{ {
@ -6596,7 +6596,7 @@
} }
catch (err) catch (err)
{ {
alert(err.message); console.warn(err.message);
} }
}; };
// --------------------------------- // ---------------------------------

BIN
models/cube.stl Normal file

Binary file not shown.

506
models/diamond.stl Executable file
View File

@ -0,0 +1,506 @@
solid OpenSCAD_Model
facet normal 0 0 0
outer loop
vertex 24 0 45.03
vertex 12 0 54.03
vertex 20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 8.48528 8.48528 54.03
vertex 12 0 54.03
vertex 20.7846 12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 -20.7846 45.03
vertex -20.7846 -12 45
vertex -12 -20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 1.46953e-015 54.03
vertex 8.48528 8.48528 54.03
vertex 7.34764e-016 12 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 0 54.03
vertex -8.48528 -8.48528 54.03
vertex 8.48528 -8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 0 54.03
vertex 8.48528 -8.48528 54.03
vertex 20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 0 54.03
vertex 8.48528 8.48528 54.03
vertex -8.48528 -8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 7.34764e-016 12 54.03
vertex 1.46953e-015 24 45.03
vertex -12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 20.7846 45.03
vertex 20.7846 12 45
vertex 12 20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 7.34764e-016 12 54.03
vertex 8.48528 8.48528 54.03
vertex 12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 -0.259808 15
vertex 0.15 0.259808 15
vertex 0.15 -0.259808 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 -12 45.03
vertex 12 -20.7846 45
vertex 20.7846 -12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 1.46953e-015 54.03
vertex -20.7846 12 45.03
vertex -24 2.93906e-015 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -24 2.93906e-015 45.03
vertex -20.7846 -12 45
vertex -20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 8.48528 8.48528 54.03
vertex 20.7846 12 45.03
vertex 12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -20.7846 -12 45
vertex -12 -20.7846 45.03
vertex -20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 0.259808 15
vertex -12 20.7846 45
vertex 1.46953e-015 24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 0.15 0.259808 15
vertex 20.7846 12 45
vertex 0.3 0 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -2.20429e-015 -12 54.03
vertex -4.40858e-015 -24 45.03
vertex 12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -2.20429e-015 -12 54.03
vertex -8.48528 -8.48528 54.03
vertex -12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -24 2.93906e-015 45
vertex -20.7846 12 45.03
vertex -20.7846 12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 0.259808 15
vertex -0.15 -0.259808 15
vertex -0.3 3.67382e-017 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 12 45
vertex 20.7846 12 45.03
vertex 24 0 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 12 45.03
vertex 12 0 54.03
vertex 24 0 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -8.48528 -8.48528 54.03
vertex 8.48528 8.48528 54.03
vertex -12 1.46953e-015 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 12 45.03
vertex 20.7846 12 45
vertex 12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 1.46953e-015 24 45.03
vertex -12 20.7846 45
vertex -12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 7.34764e-016 12 54.03
vertex -12 20.7846 45.03
vertex -8.48528 8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 12 45
vertex 0.15 0.259808 15
vertex 12 20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 1.46953e-015 24 45.03
vertex 7.34764e-016 12 54.03
vertex 12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -4.40858e-015 -24 45.03
vertex 12 -20.7846 45
vertex 12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 8.48528 -8.48528 54.03
vertex -8.48528 -8.48528 54.03
vertex -2.20429e-015 -12 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 20.7846 45
vertex 1.46953e-015 24 45.03
vertex 1.46953e-015 24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -4.40858e-015 -24 45.03
vertex -2.20429e-015 -12 54.03
vertex -12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 -0.259808 15
vertex -20.7846 -12 45
vertex -0.3 3.67382e-017 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 0.259808 15
vertex 0.15 0.259808 15
vertex -0.15 -0.259808 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 0.15 -0.259808 15
vertex 0.3 0 15
vertex 20.7846 -12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -20.7846 -12 45
vertex -0.15 -0.259808 15
vertex -12 -20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 -20.7846 45
vertex 20.7846 -12 45.03
vertex 12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 0.15 -0.259808 15
vertex 0.15 0.259808 15
vertex 0.3 0 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -24 2.93906e-015 45.03
vertex -20.7846 12 45.03
vertex -24 2.93906e-015 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 1.46953e-015 54.03
vertex -24 2.93906e-015 45.03
vertex -20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 20.7846 45
vertex -20.7846 12 45.03
vertex -12 20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 1.46953e-015 54.03
vertex 7.34764e-016 12 54.03
vertex -8.48528 8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 1.46953e-015 24 45.03
vertex 12 20.7846 45
vertex 1.46953e-015 24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 1.46953e-015 24 45.03
vertex 12 20.7846 45.03
vertex 12 20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 -12 45.03
vertex 8.48528 -8.48528 54.03
vertex 12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 8.48528 -8.48528 54.03
vertex -2.20429e-015 -12 54.03
vertex 12 -20.7846 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 20.7846 45
vertex 0.15 0.259808 15
vertex 1.46953e-015 24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 0.15 0.259808 15
vertex -0.15 0.259808 15
vertex 1.46953e-015 24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 -20.7846 45
vertex -4.40858e-015 -24 45.03
vertex -4.40858e-015 -24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -4.40858e-015 -24 45.03
vertex -12 -20.7846 45.03
vertex -12 -20.7846 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -4.40858e-015 -24 45.03
vertex -12 -20.7846 45
vertex -4.40858e-015 -24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 -20.7846 45
vertex -0.15 -0.259808 15
vertex -4.40858e-015 -24 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 -20.7846 45
vertex -4.40858e-015 -24 45
vertex 0.15 -0.259808 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -4.40858e-015 -24 45
vertex -0.15 -0.259808 15
vertex 0.15 -0.259808 15
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.15 0.259808 15
vertex -0.3 3.67382e-017 15
vertex -20.7846 12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.3 3.67382e-017 15
vertex -24 2.93906e-015 45
vertex -20.7846 12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 -20.7846 45.03
vertex -8.48528 -8.48528 54.03
vertex -20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -8.48528 -8.48528 54.03
vertex -12 1.46953e-015 54.03
vertex -20.7846 -12 45.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 20.7846 45.03
vertex -20.7846 12 45.03
vertex -8.48528 8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -20.7846 12 45.03
vertex -12 1.46953e-015 54.03
vertex -8.48528 8.48528 54.03
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 0.3 0 15
vertex 20.7846 12 45
vertex 24 0 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 12 -20.7846 45
vertex 0.15 -0.259808 15
vertex 20.7846 -12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -0.3 3.67382e-017 15
vertex -20.7846 -12 45
vertex -24 2.93906e-015 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -20.7846 -12 45
vertex -24 2.93906e-015 45.03
vertex -24 2.93906e-015 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 12 45
vertex 24 0 45.03
vertex 24 0 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 24 0 45.03
vertex 20.7846 -12 45.03
vertex 20.7846 -12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 24 0 45.03
vertex 20.7846 -12 45
vertex 24 0 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex 20.7846 -12 45
vertex 0.3 0 15
vertex 24 0 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -20.7846 12 45.03
vertex -12 20.7846 45
vertex -20.7846 12 45
endloop
endfacet
facet normal 0 0 0
outer loop
vertex -12 20.7846 45
vertex -0.15 0.259808 15
vertex -20.7846 12 45
endloop
endfacet
endsolid OpenSCAD_Model

View File

@ -11,7 +11,7 @@
<script src="src/utils.js"></script> <script src="src/utils.js"></script>
<script src="src/box.js"></script> <script src="src/box.js"></script>
<script src="src/printer.js"></script> <script src="src/printer.js"></script>
<script src="src/path.js"></script> <script src="src/paths.js"></script>
<script src="src/slicer.js"></script> <script src="src/slicer.js"></script>
<script src="gcode/testgcode.js"></script> <script src="gcode/testgcode.js"></script>
@ -115,20 +115,55 @@ mesh.position.z = 100;
//scene.add(mesh); //scene.add(mesh);
var loader = new THREE.STLLoader(); var loader = new THREE.STLLoader();
loader.load("models/cubes.stl", function (geometry) { loader.load("models/diamond.stl", function (geometry) {
var geometry = new THREE.BoxGeometry(10, 10, 10, 1, 1, 1);
//var geometry = new THREE.TorusGeometry(20, 10, 30, 30);
var geometry = (function () {
"use strict";
var circle = new THREE.Shape();
circle.absarc(0, 0, 10, 0, Math.PI*2, false);
var hole = new THREE.Path();
hole.absarc(0, 0, 5, 0, Math.PI*2, true );
circle.holes.push(hole);
var matrix = new THREE.Matrix4();
matrix.makeRotationX(Math.PI*1.5);
var geometry = new THREE.ExtrudeGeometry(circle, {
amount: 3,
bevelEnabled: false,
steps: 1
});
geometry.applyMatrix(matrix);
return geometry;
})();
var mesh = new THREE.Mesh(geometry, material); var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = 100; mesh.position.x = 100;
mesh.position.z = 100; mesh.position.z = 100;
/*
mesh.rotation.x = -Math.PI/2; mesh.rotation.x = -Math.PI/2;
mesh.scale.x = mesh.scale.y = mesh.scale.z = 3; mesh.scale.x = mesh.scale.y = mesh.scale.z = 3;
//mesh is lifted a little bit... //mesh is lifted a little bit...
mesh.position.y = -0.6552429199218741; mesh.position.y = -0.6552429199218741;
*/
scene.add(mesh); scene.add(mesh);
var slicer = new D3D.Slicer().setMesh(mesh); var slicer = new D3D.Slicer().setMesh(mesh);
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
//var img = slicer.drawPaths(printer, 7, 8);
//context.drawImage(img, 0, 0);
gcode = slicer.getGcode(printer); gcode = slicer.getGcode(printer);
}); });

View File

@ -1,153 +0,0 @@
/******************************************************
*
* Path
*
* Abstraction layer for annoying clipper js
*
******************************************************/
D3D.Path = function (path, closed) {
"use strict";
this.path = path || [];
this.closed = (closed !== undefined) ? closed : true;
};
D3D.Path.prototype.setPath = function (path) {
"use strict";
this.path = path;
return this;
};
D3D.Path.prototype.union = function (path) {
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctUnion, solution);
return new D3D.Path(solution, this.closed);
};
D3D.Path.prototype.difference = function (path) {
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctDifference, solution);
return new D3D.Path(solution, this.closed);
};
D3D.Path.prototype.intersect = function (path) {
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctIntersection, solution);
return new D3D.Path(solution, this.closed);
};
D3D.Path.prototype.xor = function () {
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this.path, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path.path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(ClipperLib.ClipType.ctXor, solution);
return new D3D.Path(solution, this.closed);
};
D3D.Path.prototype.offset = function (offset) {
"use strict";
var solution = new ClipperLib.Paths();
var co = new ClipperLib.ClipperOffset(1, 1);
co.AddPaths(this.path, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
co.Execute(solution, offset);
return new D3D.Path(solution, this.closed);
};
D3D.Path.prototype.scaleUp = function (factor) {
"use strict";
var path = ClipperLib.JS.ScaleUpPaths(this.path, factor);
return this;
};
D3D.Path.prototype.scaleDown = function (factor) {
"use strict";
var path = ClipperLib.JS.ScaleDownPaths(this.path, factor);
return this;
};
D3D.Path.prototype.tresholdArea = function (minArea) {
"use strict";
for (var i = 0; i < this.path.length; i ++) {
var shape = this.path[i];
var area = ClipperLib.Clipper.Area(shape);
if (area < minArea) {
this.path.splice(i, 1);
i --;
}
}
return areas;
};
D3D.Path.prototype.area = function () {
"use strict";
var areas = [];
for (var i = 0; i < this.path.length; i ++) {
var shape = this.path[i];
areas.push(ClipperLib.Clipper.Area(shape))
}
return areas;
};
D3D.Path.prototype.join = function (path) {
"use strict";
this.path = this.path.concat(path.path);
return this;
}
D3D.Path.prototype.clone = function () {
"use strict";
var path = ClipperLib.JS.Clone(this.path);
return new D3D.Path(path, this.closed);
}
D3D.Path.prototype.draw = function (context, color) {
"use strict";
context.strokeStyle = color;
for (var i = 0; i < this.path.length; i ++) {
var shape = this.path[i];
context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length;
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
context.lineTo(point.X*2, point.Y*2);
}
context.stroke();
}
};

148
src/paths.js Normal file
View File

@ -0,0 +1,148 @@
/******************************************************
*
* Path
*
* Abstraction layer for annoying clipper js
*
******************************************************/
D3D.Paths = function (paths, closed) {
"use strict";
Array.call(this);
this.setPaths(paths || []);
this.closed = (closed !== undefined) ? closed : true;
};
D3D.Paths.prototype = Object.create(Array.prototype);
D3D.Paths.prototype.setPaths = function (paths) {
"use strict";
for (var i = 0; i < paths.length; i ++) {
var path = paths[i];
this.push(path);
}
return this;
};
D3D.Paths.prototype.clip = function (path, type) {
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
clipper.AddPaths(this, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(type, solution);
return new D3D.Paths(solution);
};
D3D.Paths.prototype.union = function (path) {
"use strict";
return this.clip(path, ClipperLib.ClipType.ctUnion);
};
D3D.Paths.prototype.difference = function (path) {
"use strict";
return this.clip(path, ClipperLib.ClipType.ctDifference);
};
D3D.Paths.prototype.intersect = function (path) {
"use strict";
return this.clip(path, ClipperLib.ClipType.ctIntersection);
};
D3D.Paths.prototype.xor = function () {
"use strict";
return this.clip(path, ClipperLib.ClipType.ctXor);
};
D3D.Paths.prototype.offset = function (offset) {
"use strict";
var solution = new ClipperLib.Paths();
var co = new ClipperLib.ClipperOffset(1, 1);
co.AddPaths(this, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
co.Execute(solution, offset);
return new D3D.Paths(solution);
};
D3D.Paths.prototype.scaleUp = function (factor) {
"use strict";
var path = ClipperLib.JS.ScaleUpPaths(this, factor);
return this;
};
D3D.Paths.prototype.scaleDown = function (factor) {
"use strict";
var path = ClipperLib.JS.ScaleDownPaths(this, factor);
return this;
};
D3D.Paths.prototype.tresholdArea = function (minArea) {
"use strict";
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
var area = ClipperLib.Clipper.Area(shape);
if (area < minArea) {
this.splice(i, 1);
i --;
}
}
return areas;
};
D3D.Paths.prototype.area = function () {
"use strict";
return ClipperLib.Clipper.Area(this);
};
D3D.Paths.prototype.join = function (path) {
"use strict";
for (var i = 0; i < path.length; i ++) {
this.push(path[i]);
}
return this;
};
D3D.Paths.prototype.clone = function () {
"use strict";
return new D3D.Paths(ClipperLib.JS.Clone(this), this.closed);
};
D3D.Paths.prototype.bounds = function () {
"use strict";
return ClipperLib.Clipper.GetBounds(this);
};
D3D.Paths.prototype.reverse = function () {
"use strict";
ClipperLib.Clipper.ReversePaths(this);
return this;
};
D3D.Paths.prototype.draw = function (context, color) {
"use strict";
context.strokeStyle = color;
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length;
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
context.lineTo(point.X*2, point.Y*2);
}
context.stroke();
}
};

View File

@ -64,6 +64,8 @@ D3D.Slicer.prototype.createLines = function () {
for (var i = 0; i < this.geometry.faces.length; i ++) { for (var i = 0; i < this.geometry.faces.length; i ++) {
var face = this.geometry.faces[i]; var face = this.geometry.faces[i];
//if (face.normal.y !== 1 && face.normal.y !== -1) {
var normal = new THREE.Vector2().set(face.normal.x, face.normal.z).normalize(); var normal = new THREE.Vector2().set(face.normal.x, face.normal.z).normalize();
//check for only adding unique lines //check for only adding unique lines
@ -80,12 +82,7 @@ D3D.Slicer.prototype.createLines = function () {
this.lines[a].normals.push(normal); this.lines[a].normals.push(normal);
this.lines[b].normals.push(normal); this.lines[b].normals.push(normal);
this.lines[c].normals.push(normal); this.lines[c].normals.push(normal);
//}
if (face.normal.y === 1 || face.normal.y === -1) {
this.lines[a].ignore ++;
this.lines[b].ignore ++;
this.lines[c].ignore ++;
}
} }
}; };
D3D.Slicer.prototype.slice = function (height, step) { D3D.Slicer.prototype.slice = function (height, step) {
@ -99,7 +96,6 @@ D3D.Slicer.prototype.slice = function (height, step) {
var min = Math.ceil(Math.min(line.line.start.y, line.line.end.y) / step); var min = Math.ceil(Math.min(line.line.start.y, line.line.end.y) / step);
var max = Math.floor(Math.max(line.line.start.y, line.line.end.y) / step); var max = Math.floor(Math.max(line.line.start.y, line.line.end.y) / step);
if (line.ignore < 2) {
for (var layerIndex = min; layerIndex <= max; layerIndex ++) { for (var layerIndex = min; layerIndex <= max; layerIndex ++) {
if (layerIndex >= 0) { if (layerIndex >= 0) {
if (layersIntersections[layerIndex] === undefined) { if (layersIntersections[layerIndex] === undefined) {
@ -109,11 +105,10 @@ D3D.Slicer.prototype.slice = function (height, step) {
} }
} }
} }
}
var slices = []; var slices = [];
for (var layer = 1; layer < layersIntersections.length; layer ++) { for (var layer = 1; layer < layersIntersections.length-1; layer ++) {
var layerIntersections = layersIntersections[layer]; var layerIntersections = layersIntersections[layer];
var y = layer*step; var y = layer*step;
@ -130,7 +125,7 @@ D3D.Slicer.prototype.slice = function (height, step) {
} }
var done = []; var done = [];
var slice = []; var slice = new D3D.Paths([], true);
for (var i = 0; i < layerIntersections.length; i ++) { for (var i = 0; i < layerIntersections.length; i ++) {
var index = layerIntersections[i]; var index = layerIntersections[i];
@ -191,15 +186,34 @@ D3D.Slicer.prototype.slice = function (height, step) {
//think this check is not nescesary, always higher as 0 //think this check is not nescesary, always higher as 0
if (shape.length > 0) { if (shape.length > 0) {
slice.push(shape); slice.push(new D3D.Paths([shape]));
} }
} }
} }
var layerParts = [];
for (var i = 0; i < slice.length; i ++) {
var layerPart1 = slice[i];
var merge = false;
for (var j = 0; j < layerParts.length; j ++) {
var layerPart2 = layerParts[j];
if (layerPart2.intersect(layerPart1).length > 0) {
layerPart2.join(layerPart1);
merge = true;
break;
}
}
if (!merge) {
layerParts.push(layerPart1);
}
}
//stop when ther are no intersects //stop when ther are no intersects
if (slice.length > 0) { if (layerParts.length > 0) {
slices.push(new D3D.Path(slice, true)); slices.push(layerParts);
//slices.push(slice);
} }
else { else {
break; break;
@ -228,10 +242,33 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
for (var layer = 0; layer < slices.length; layer ++) { for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer]; var slice = slices[layer];
var outerLayer = slice.clone(); var layerData = [];
data.push(layerData);
var downSkin = new D3D.Paths();
if (layer - skinCount >= 0) {
var downLayer = slices[layer - skinCount];
for (var i = 0; i < downLayer.length; i ++) {
downSkin.join(downLayer[i]);
}
}
var upSkin = new D3D.Paths();
if (layer + skinCount < slices.length) {
var downLayer = slices[layer + skinCount];
for (var i = 0; i < downLayer.length; i ++) {
upSkin.join(downLayer[i]);
}
}
var surroundingLayer = upSkin.intersect(downSkin).clone().scaleUp(scale);
var sliceData = [];
for (var i = 0; i < slice.length; i ++) {
var part = slice[i];
var outerLayer = part.clone();
outerLayer.scaleUp(scale); outerLayer.scaleUp(scale);
var insets = new D3D.Path(); var insets = new D3D.Paths();
for (var offset = wallThickness; offset <= shellThickness; offset += wallThickness) { for (var offset = wallThickness; offset <= shellThickness; offset += wallThickness) {
var inset = outerLayer.offset(-offset); var inset = outerLayer.offset(-offset);
@ -240,33 +277,31 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var fillArea = (inset || outerLayer).offset(-wallThickness/2); var fillArea = (inset || outerLayer).offset(-wallThickness/2);
var downFill = (layer - skinCount >= 0) ? slices[layer - skinCount] : new D3D.Path(); var highFillArea = fillArea.difference(surroundingLayer);
var upFill = (layer + skinCount < slices.length) ? slices[layer + skinCount] : new D3D.Path();
var highFillArea = fillArea.difference(downFill.intersect(upFill).scaleUp(scale));
var lowFillArea = fillArea.difference(highFillArea); var lowFillArea = fillArea.difference(highFillArea);
var fill = new D3D.Path([], false); var fill = new D3D.Paths([]);
fill.join(lowFillTemplate.intersect(lowFillArea));
if (highFillArea.path.length > 0) {
var highFillTemplate = this.getFillTemplate(dimensionsZ, wallThickness, (layer % 2 === 0), (layer % 2 === 1));
var highFillStrokes = highFillArea;
fill.join(highFillTemplate.intersect(highFillStrokes));
}
data.push({ fill.join(lowFillTemplate.intersect(lowFillArea));
var highFillTemplate = this.getFillTemplate(dimensionsZ, wallThickness, (layer % 2 === 0), (layer % 2 === 1));
fill.join(highFillTemplate.intersect(highFillArea));
layerData.push({
outerLayer: outerLayer.scaleDown(scale), outerLayer: outerLayer.scaleDown(scale),
insets: insets.scaleDown(scale), fill: fill.scaleDown(scale),
fill: fill.scaleDown(scale) insets: insets.scaleDown(scale)
}); });
} }
}
return data; return data;
}; };
D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven) { D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven) {
"use strict"; "use strict";
var paths = []; var paths = new D3D.Paths([], false);
if (even) { if (even) {
for (var length = 0; length <= dimension; length += size) { for (var length = 0; length <= dimension; length += size) {
@ -280,7 +315,7 @@ D3D.Slicer.prototype.getFillTemplate = function (dimension, size, even, uneven)
} }
//return paths; //return paths;
return new D3D.Path(paths, false); return paths;
}; };
D3D.Slicer.prototype.dataToGcode = function (data, printer) { D3D.Slicer.prototype.dataToGcode = function (data, printer) {
"use strict"; "use strict";
@ -302,13 +337,15 @@ D3D.Slicer.prototype.dataToGcode = function (data, printer) {
function sliceToGcode (slice) { function sliceToGcode (slice) {
var gcode = []; var gcode = [];
for (var i = 0; i < slice.path.length; i ++) { for (var i = 0; i < slice.length; i ++) {
var shape = slice.path[i]; var shape = slice[i];
var previousPoint; var previousPoint;
for (var j = 0; j < shape.length; j ++) { var length = slice.closed ? (shape.length + 1) : shape.length;
var point = shape[j];
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
if (j === 0) { if (j === 0) {
//TODO //TODO
@ -376,9 +413,13 @@ D3D.Slicer.prototype.dataToGcode = function (data, printer) {
var z = ((layer + 1) * layerHeight).toFixed(3); var z = ((layer + 1) * layerHeight).toFixed(3);
gcode = gcode.concat(sliceToGcode(slice.outerLayer)); for (var i = 0; i < slice.length; i ++) {
gcode = gcode.concat(sliceToGcode(slice.insets)); var layerPart = slice[i];
gcode = gcode.concat(sliceToGcode(slice.fill));
gcode = gcode.concat(sliceToGcode(layerPart.outerLayer));
gcode = gcode.concat(sliceToGcode(layerPart.insets));
gcode = gcode.concat(sliceToGcode(layerPart.fill));
}
} }
gcode = gcode.concat(printer.getEndCode()); gcode = gcode.concat(printer.getEndCode());
@ -403,9 +444,13 @@ D3D.Slicer.prototype.drawPaths = function (printer, min, max) {
for (var layer = min; layer < max; layer ++) { for (var layer = min; layer < max; layer ++) {
var slice = data[layer % data.length]; var slice = data[layer % data.length];
slice.insets.draw(context, "blue"); for (var i = 0; i < slice.length; i ++) {
slice.outerLayer.draw(context, "green"); var layerPart = slice[i];
slice.fill.draw(context, "red");
layerPart.insets.draw(context, "blue");
layerPart.outerLayer.draw(context, "green");
layerPart.fill.draw(context, "red");
}
} }
return canvas; return canvas;