From 05cf15667be3a3ce42a75b917c3e979237fd1491 Mon Sep 17 00:00:00 2001 From: Raf Date: Fri, 11 Nov 2022 21:58:13 +0100 Subject: [PATCH 1/7] Exported file is now formatted with 3 groups of coordinates for each line --- js/main.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/js/main.js b/js/main.js index e9c6ab2..aeb540a 100644 --- a/js/main.js +++ b/js/main.js @@ -148,8 +148,19 @@ function error(err) { //Input: Set of vertices and triangles, both are strings //Makes the Download link create an OpenScad file with a polyhedron object that represents the parsed stl file function saveResult(vertices, triangles) { + // this function groups an array 'a' in groups of 'n' + const regroup = (a, n) => [...Array(Math.ceil(a.length / n))] + .map((item, i) => a.slice(i*n, (i+1)*n)); - var poly = 'polyhedron(\r\n points=[' + vertices + ' ],\r\nfaces=[' + triangles + ']);'; + // creates the group of 3 of both arrays + var verticesGroup = regroup(vertices, 3); + var trianglesGroup = regroup(triangles, 3); + + // each line will be composed by three coordinates followed by the comma and the crlf + var verticesString = verticesGroup.map(g => g.join(',')).join(',\r\n'); + var trianglesString = trianglesGroup.map(g => g.join(',')).join(',\r\n'); + + var poly = 'polyhedron(\r\n points=[' + verticesString + ' ],\r\nfaces=[' + trianglesString + ']);'; calls = calls + 'object' + (++totalObjects) + '(1);\r\n\r\n'; From 5cae3b0f93093d3070e266a7b2e13d3da522460e Mon Sep 17 00:00:00 2001 From: Raf Date: Fri, 11 Nov 2022 22:35:11 +0100 Subject: [PATCH 2/7] Calculating and adding bounds to the html page --- index.html | 1 + js/main.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 5971708..c4814bc 100644 --- a/index.html +++ b/index.html @@ -50,6 +50,7 @@
0%

+

diff --git a/js/main.js b/js/main.js index aeb540a..070a200 100644 --- a/js/main.js +++ b/js/main.js @@ -53,6 +53,12 @@ function parseBinaryResult(stl) { br.seek(80); //Skip header var totalTriangles = br.readUInt32(); //Read # triangles + var minx = Number.MAX_VALUE; + var miny = Number.MAX_VALUE; + var minz = Number.MAX_VALUE; + var maxx = Number.MIN_VALUE; + var maxy = Number.MIN_VALUE; + var maxz = Number.MIN_VALUE; for (var tr = 0; tr < totalTriangles; tr++) { try { document.getElementById('conversion').innerText = 'In Progress - Converted ' + (++converted) + ' out of ' + totalTriangles + ' triangles!'; @@ -67,9 +73,32 @@ function parseBinaryResult(stl) { br.readFloat(); br.readFloat(); //SKIP NORMAL //Parse every 3 subsequent floats as a vertex - var v1 = '[' + br.readFloat() + ',' + br.readFloat() + ',' + br.readFloat() + ']'; - var v2 = '[' + br.readFloat() + ',' + br.readFloat() + ',' + br.readFloat() + ']'; - var v3 = '[' + br.readFloat() + ',' + br.readFloat() + ',' + br.readFloat() + ']'; + + var x1, y1, z1; + var x2, y2, z2; + var x3, y3, z3; + x1 = br.readFloat(); + y1 = br.readFloat(); + z1 = br.readFloat(); + + x2 = br.readFloat(); + y2 = br.readFloat(); + z2 = br.readFloat(); + + x3 = br.readFloat(); + y3 = br.readFloat(); + z3 = br.readFloat(); + + minx = Math.min(minx, x1, x2, x3); + maxx = Math.max(maxx, x1, x2, x3); + miny = Math.min(miny, y1, y2, y3); + maxy = Math.max(maxy, y1, y2, y3); + minz = Math.min(minz, z1, z2, z3); + maxz = Math.max(maxz, z1, z2, z3); + + var v1 = '[' + x1 + ',' + y1 + ',' + z1 + ']'; + var v2 = '[' + x2 + ',' + y2 + ',' + z2 + ']'; + var v3 = '[' + x3 + ',' + y3 + ',' + z3 + ']'; //every 3 vertices create a triangle. var triangle = '[' + (vertexIndex++) + ',' + (vertexIndex++) + ',' + (vertexIndex++) + ']'; @@ -87,7 +116,8 @@ function parseBinaryResult(stl) { } } - saveResult(vertices, triangles); + var bounds = `[${minx},${miny},${minz}], [${maxx},${maxy},${maxz}]`; + saveResult(vertices, triangles, bounds); } function parseAsciiResult(stl) { @@ -104,6 +134,12 @@ function parseAsciiResult(stl) { match = objects[o].match(patt); if (match == null) continue; + var minx = Number.MAX_VALUE; + var miny = Number.MAX_VALUE; + var minz = Number.MAX_VALUE; + var maxx = Number.MIN_VALUE; + var maxy = Number.MIN_VALUE; + var maxz = Number.MIN_VALUE; for (var i = 0; i < match.length; i++) { try { document.getElementById('conversion').innerText = 'In Progress - Object ' + (o + 1) + ' out of ' + objects.length + ' Converted ' + (++converted) + ' out of ' + match.length + ' facets!'; @@ -119,6 +155,25 @@ function parseAsciiResult(stl) { break; } + var a1, a2, a3; + a1 = parseFloat(v[1]); + a2 = parseFloat(v[4]); + a3 = parseFloat(v[7]); + minx = Math.min(minx, a1, a2, a3); + maxx = Math.max(maxx, a1, a2, a3); + + a1 = parseFloat(v[2]); + a2 = parseFloat(v[5]); + a3 = parseFloat(v[8]); + miny = Math.min(miny, a1, a2, a3); + maxy = Math.max(maxy, a1, a2, a3); + + a1 = parseFloat(v[3]); + a2 = parseFloat(v[6]); + a3 = parseFloat(v[9]); + minz = Math.min(minz, a1, a2, a3); + maxz = Math.max(maxz, a1, a2, a3); + var v1 = '[' + v[1] + ',' + v[2] + ',' + v[3] + ']'; var v2 = '[' + v[4] + ',' + v[5] + ',' + v[6] + ']'; var v3 = '[' + v[7] + ',' + v[8] + ',' + v[9] + ']'; @@ -136,7 +191,8 @@ function parseAsciiResult(stl) { } } - saveResult(vertices, triangles); + var bounds = `[${minx},${miny},${minz}], [${maxx},${maxy},${maxz}]`; + saveResult(vertices, triangles, bounds); } } @@ -147,7 +203,7 @@ function error(err) { } //Input: Set of vertices and triangles, both are strings //Makes the Download link create an OpenScad file with a polyhedron object that represents the parsed stl file -function saveResult(vertices, triangles) { +function saveResult(vertices, triangles, bounds) { // this function groups an array 'a' in groups of 'n' const regroup = (a, n) => [...Array(Math.ceil(a.length / n))] .map((item, i) => a.slice(i*n, (i+1)*n)); @@ -179,6 +235,7 @@ function saveResult(vertices, triangles) { $("#download").attr("href", window.URL.createObjectURL(blob)); $("#download").attr("download", "FromSTL.scad"); + document.getElementById("bounds").innerText = "Bounds: " + bounds; document.getElementById("conversion").innerText = "Conversion complete - Click the button below to download your OpenSCAD file! Total Triangles: " + triangles.length; document.getElementById("download").style.display = ""; } From 455518884926dc0f03ce8ccca03a741d0528decd Mon Sep 17 00:00:00 2001 From: Raf Date: Sat, 12 Nov 2022 14:04:00 +0100 Subject: [PATCH 3/7] Added two functions in the generated code returning the bounds --- js/main.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/js/main.js b/js/main.js index 070a200..80b1528 100644 --- a/js/main.js +++ b/js/main.js @@ -8,6 +8,7 @@ var vertices = []; var triangles = []; var modules = ''; var calls = ''; +var functions = ''; var vertexIndex = 0; var converted = 0; var totalObjects = 0; @@ -18,6 +19,7 @@ function _reset() { triangles = []; modules = ''; calls = ''; + functions = ''; vertexIndex = 0; converted = 0; totalObjects = 0; @@ -116,8 +118,9 @@ function parseBinaryResult(stl) { } } - var bounds = `[${minx},${miny},${minz}], [${maxx},${maxy},${maxz}]`; - saveResult(vertices, triangles, bounds); + var boundsMin = `[${minx},${miny},${minz}]`; + var boundsMax = `[${maxx},${maxy},${maxz}]`; + saveResult(vertices, triangles, boundsMin, boundsMax); } function parseAsciiResult(stl) { @@ -191,8 +194,9 @@ function parseAsciiResult(stl) { } } - var bounds = `[${minx},${miny},${minz}], [${maxx},${maxy},${maxz}]`; - saveResult(vertices, triangles, bounds); + var boundsMin = `[${minx},${miny},${minz}]`; + var boundsMax = `[${maxx},${maxy},${maxz}]`; + saveResult(vertices, triangles, boundsMin, boundsMax); } } @@ -203,7 +207,7 @@ function error(err) { } //Input: Set of vertices and triangles, both are strings //Makes the Download link create an OpenScad file with a polyhedron object that represents the parsed stl file -function saveResult(vertices, triangles, bounds) { +function saveResult(vertices, triangles, boundsMin, boundsMax) { // this function groups an array 'a' in groups of 'n' const regroup = (a, n) => [...Array(Math.ceil(a.length / n))] .map((item, i) => a.slice(i*n, (i+1)*n)); @@ -218,13 +222,19 @@ function saveResult(vertices, triangles, bounds) { var poly = 'polyhedron(\r\n points=[' + verticesString + ' ],\r\nfaces=[' + trianglesString + ']);'; - calls = calls + 'object' + (++totalObjects) + '(1);\r\n\r\n'; + var objectName = `object${++totalObjects}`; + var functionMin = `${objectName}Min()`; + var functionMax = `${objectName}Max()`; + + functions = functions + `function ${functionMin} = ${boundsMin};\r\n`; + functions = functions + `function ${functionMax} = ${boundsMax};\r\n\r\n`; modules = modules + 'module object' + totalObjects + '(scale) {'; modules = modules + poly + '}\r\n\r\n'; + + calls = calls + objectName + '(1);\r\n\r\n'; - result = modules + calls; - + result = modules + functions + calls; window.URL = window.URL || window.webkitURL; //prompt("Copy scad:", result); //prompt result in a copyable field @@ -235,7 +245,7 @@ function saveResult(vertices, triangles, bounds) { $("#download").attr("href", window.URL.createObjectURL(blob)); $("#download").attr("download", "FromSTL.scad"); - document.getElementById("bounds").innerText = "Bounds: " + bounds; + document.getElementById("bounds").innerText = "Bounds: " + boundsMin + ", " + boundsMax; document.getElementById("conversion").innerText = "Conversion complete - Click the button below to download your OpenSCAD file! Total Triangles: " + triangles.length; document.getElementById("download").style.display = ""; } From 774e4f59f58090752440ba8a77d090b581881b41 Mon Sep 17 00:00:00 2001 From: Raf Date: Thu, 9 Mar 2023 22:59:12 +0100 Subject: [PATCH 4/7] Rouding the bounds to 2 decimals --- js/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/main.js b/js/main.js index 80b1528..3a72c32 100644 --- a/js/main.js +++ b/js/main.js @@ -118,8 +118,8 @@ function parseBinaryResult(stl) { } } - var boundsMin = `[${minx},${miny},${minz}]`; - var boundsMax = `[${maxx},${maxy},${maxz}]`; + var boundsMin = `[${minx.toFixed(2)},${miny.toFixed(2)},${minz.toFixed(2)}]`; + var boundsMax = `[${maxx.toFixed(2)},${maxy.toFixed(2)},${maxz.toFixed(2)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } @@ -194,8 +194,8 @@ function parseAsciiResult(stl) { } } - var boundsMin = `[${minx},${miny},${minz}]`; - var boundsMax = `[${maxx},${maxy},${maxz}]`; + var boundsMin = `[${minx.toFixed(2)},${miny.toFixed(2)},${minz.toFixed(2)}]`; + var boundsMax = `[${maxx.toFixed(2)},${maxy.toFixed(2)},${maxz.toFixed(2)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } } From 217c69958793fd0db783805df61d609fd7765c82 Mon Sep 17 00:00:00 2001 From: Ravi Riley <8132955+raviriley@users.noreply.github.com> Date: Fri, 31 Mar 2023 02:11:31 -0700 Subject: [PATCH 5/7] Change rounding to thousandths place --- js/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/main.js b/js/main.js index 3a72c32..66a07d2 100644 --- a/js/main.js +++ b/js/main.js @@ -118,8 +118,8 @@ function parseBinaryResult(stl) { } } - var boundsMin = `[${minx.toFixed(2)},${miny.toFixed(2)},${minz.toFixed(2)}]`; - var boundsMax = `[${maxx.toFixed(2)},${maxy.toFixed(2)},${maxz.toFixed(2)}]`; + var boundsMin = `[${minx.toFixed(3)},${miny.toFixed(3)},${minz.toFixed(3)}]`; + var boundsMax = `[${maxx.toFixed(3)},${maxy.toFixed(3)},${maxz.toFixed(3)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } @@ -194,8 +194,8 @@ function parseAsciiResult(stl) { } } - var boundsMin = `[${minx.toFixed(2)},${miny.toFixed(2)},${minz.toFixed(2)}]`; - var boundsMax = `[${maxx.toFixed(2)},${maxy.toFixed(2)},${maxz.toFixed(2)}]`; + var boundsMin = `[${minx.toFixed(3)},${miny.toFixed(3)},${minz.toFixed(3)}]`; + var boundsMax = `[${maxx.toFixed(3)},${maxy.toFixed(3)},${maxz.toFixed(3)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } } From e6e38eb533699b8df2017c4a45dede65616f951b Mon Sep 17 00:00:00 2001 From: Ravi Riley <8132955+raviriley@users.noreply.github.com> Date: Fri, 31 Mar 2023 02:19:07 -0700 Subject: [PATCH 6/7] Add spaces to boundsMin & boundsMax --- js/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/main.js b/js/main.js index 66a07d2..10d343e 100644 --- a/js/main.js +++ b/js/main.js @@ -118,8 +118,8 @@ function parseBinaryResult(stl) { } } - var boundsMin = `[${minx.toFixed(3)},${miny.toFixed(3)},${minz.toFixed(3)}]`; - var boundsMax = `[${maxx.toFixed(3)},${maxy.toFixed(3)},${maxz.toFixed(3)}]`; + var boundsMin = `[${minx.toFixed(3)}, ${miny.toFixed(3)}, ${minz.toFixed(3)}]`; + var boundsMax = `[${maxx.toFixed(3)}, ${maxy.toFixed(3)}, ${maxz.toFixed(3)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } @@ -194,8 +194,8 @@ function parseAsciiResult(stl) { } } - var boundsMin = `[${minx.toFixed(3)},${miny.toFixed(3)},${minz.toFixed(3)}]`; - var boundsMax = `[${maxx.toFixed(3)},${maxy.toFixed(3)},${maxz.toFixed(3)}]`; + var boundsMin = `[${minx.toFixed(3)}, ${miny.toFixed(3)}, ${minz.toFixed(3)}]`; + var boundsMax = `[${maxx.toFixed(3)}, ${maxy.toFixed(3)}, ${maxz.toFixed(3)}]`; saveResult(vertices, triangles, boundsMin, boundsMax); } } From c19384072703d0943589d0a505f9b808d38742ab Mon Sep 17 00:00:00 2001 From: Ravi Riley <8132955+raviriley@users.noreply.github.com> Date: Fri, 31 Mar 2023 02:19:28 -0700 Subject: [PATCH 7/7] Update arrangement of post-conversion information --- index.html | 2 +- js/main.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index c4814bc..1e599da 100644 --- a/index.html +++ b/index.html @@ -45,11 +45,11 @@ -
0%

+

diff --git a/js/main.js b/js/main.js index 10d343e..df07156 100644 --- a/js/main.js +++ b/js/main.js @@ -245,8 +245,9 @@ function saveResult(vertices, triangles, boundsMin, boundsMax) { $("#download").attr("href", window.URL.createObjectURL(blob)); $("#download").attr("download", "FromSTL.scad"); + document.getElementById("conversion").innerText = "Conversion complete. Click the button below to download your OpenSCAD file!"; + document.getElementById("triangles").innerText = "Total Triangles: " + triangles.length; document.getElementById("bounds").innerText = "Bounds: " + boundsMin + ", " + boundsMax; - document.getElementById("conversion").innerText = "Conversion complete - Click the button below to download your OpenSCAD file! Total Triangles: " + triangles.length; document.getElementById("download").style.display = ""; }