add auto svg extrude to 3d object on import
This commit is contained in:
parent
cccb2b1379
commit
df4a54c929
5 changed files with 76 additions and 2 deletions
2
app.js
2
app.js
|
|
@ -221,6 +221,7 @@ const script = {
|
||||||
"kiri",
|
"kiri",
|
||||||
"ext/three",
|
"ext/three",
|
||||||
"ext/three-bgu",
|
"ext/three-bgu",
|
||||||
|
"ext/three-svg",
|
||||||
"ext/jszip",
|
"ext/jszip",
|
||||||
"license",
|
"license",
|
||||||
"ext/clip2", // work.test
|
"ext/clip2", // work.test
|
||||||
|
|
@ -249,6 +250,7 @@ const script = {
|
||||||
"moto/load-3mf",
|
"moto/load-3mf",
|
||||||
"moto/load-obj",
|
"moto/load-obj",
|
||||||
"moto/load-stl",
|
"moto/load-stl",
|
||||||
|
"moto/load-svg",
|
||||||
"moto/load-url",
|
"moto/load-url",
|
||||||
"moto/db",
|
"moto/db",
|
||||||
"kiri/ui",
|
"kiri/ui",
|
||||||
|
|
|
||||||
1
src/ext/three-svg.js
Symbolic link
1
src/ext/three-svg.js
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
../../node_modules/three/examples/js/loaders/SVGLoader.js
|
||||||
|
|
@ -2650,7 +2650,23 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (isgcode) loadCode(e.target.result, 'gcode');
|
else if (isgcode) loadCode(e.target.result, 'gcode');
|
||||||
else if (issvg) loadCode(e.target.result, 'svg');
|
else if (issvg) {
|
||||||
|
if (MODE === MODES.LASER) {
|
||||||
|
loadCode(e.target.result, 'svg');
|
||||||
|
} else {
|
||||||
|
let name = e.target.file.name;
|
||||||
|
let svg = MOTO.SVG.parse(e.target.result);
|
||||||
|
let ind = 0;
|
||||||
|
for (let v of svg) {
|
||||||
|
let num = ind++;
|
||||||
|
platform.add(
|
||||||
|
newWidget(undefined, group)
|
||||||
|
.loadVertices(svg[num].toFloat32())
|
||||||
|
.saveToCatalog(num ? `${name}-${num}` : name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
else if (iskmz) settingsImportZip(e.target.result, true);
|
else if (iskmz) settingsImportZip(e.target.result, true);
|
||||||
else if (isset) settingsImport(e.target.result, true);
|
else if (isset) settingsImport(e.target.result, true);
|
||||||
else if (ispng) loadImageDialog(e.target.result, e.target.file.name);
|
else if (ispng) loadImageDialog(e.target.result, e.target.file.name);
|
||||||
|
|
|
||||||
52
src/moto/load-svg.js
Normal file
52
src/moto/load-svg.js
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
|
||||||
|
if (!self.moto) self.moto = {};
|
||||||
|
if (self.moto.SVG) return;
|
||||||
|
|
||||||
|
self.moto.SVG = {
|
||||||
|
parse,
|
||||||
|
parseAsync
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {String} text
|
||||||
|
* @returns {Array} vertex face array
|
||||||
|
*/
|
||||||
|
|
||||||
|
function parseAsync(text) {
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
resolve(parse(text));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse(text) {
|
||||||
|
|
||||||
|
let faces = [ ];
|
||||||
|
let objs = [ faces ];
|
||||||
|
|
||||||
|
let data = new THREE.SVGLoader().parse(text);
|
||||||
|
let paths = data.paths;
|
||||||
|
|
||||||
|
for (let i = 0; i < paths.length; i++) {
|
||||||
|
let path = paths[i];
|
||||||
|
let shapes = path.toShapes(true);
|
||||||
|
let geom = new THREE.ExtrudeGeometry(shapes, {
|
||||||
|
steps: 1,
|
||||||
|
depth: 5,
|
||||||
|
bevelEnabled: false
|
||||||
|
});
|
||||||
|
let array = geom.attributes.position.array;
|
||||||
|
// invert y
|
||||||
|
for (let i=0; i<array.length; i+=3) {
|
||||||
|
array[i+1] = -array[i+1];
|
||||||
|
}
|
||||||
|
// objs.push(geom.attributes.position.array);
|
||||||
|
faces.appendAll(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
return objs;
|
||||||
|
}
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
let file = options.file || options.filename || (((url.split('?')[0]).split('#')[0]).split('/')).pop();
|
let file = options.file || options.filename || (((url.split('?')[0]).split('#')[0]).split('/')).pop();
|
||||||
let ext = file.split('.').pop().toLowerCase();
|
let ext = file.split('.').pop().toLowerCase();
|
||||||
let deftype = ext === "obj" ? "text" : "arraybuffer";
|
let deftype = ext === "obj" || ext === 'svg' ? "text" : "arraybuffer";
|
||||||
let datatype = options.datatype || deftype;
|
let datatype = options.datatype || deftype;
|
||||||
let formdata = options.formdata;
|
let formdata = options.formdata;
|
||||||
|
|
||||||
|
|
@ -51,6 +51,9 @@
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case "svg":
|
||||||
|
resolve(moto.SVG.parse(data).map(m => { return {mesh: m.toFloat32(), file} }));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
reject(`unknown file type: "${ext}" from ${url}`);
|
reject(`unknown file type: "${ext}" from ${url}`);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue