mesh: add svg import to sketch, kiri: fix svg import multi obj

This commit is contained in:
Stewart Allen 2024-07-27 13:49:28 -04:00
commit d8655a60a9
5 changed files with 30 additions and 20 deletions

View file

@ -857,7 +857,7 @@ function platformLoadFiles(files, group) {
let svg = load.SVG.parse(data.textDecode('utf-8'));
let ind = 0;
for (let v of svg) {
platformLoadWidget(group, svg[ind], ind ? `${name}-${ind++}` : name);
platformLoadWidget(group, svg[ind++], ind ? `${name}-${ind}` : name);
}
load_dec();
}

View file

@ -39,7 +39,8 @@ const types = {
},
svg(data, file, resolve, reject, opt = {}) {
resolve(load.SVG.parse(data, opt).map(m => { return {mesh: m.toFloat32(), file} }));
let out = load.SVG.parse(data, opt);
resolve(opt.flat ? out : out.map(m => { return {mesh: m.toFloat32(), file} }));
},
png(data, file, resolve, reject, opt = {}) {

View file

@ -17,7 +17,8 @@ function parseAsync(text, opt) {
}
function parse(text, opt = { soup: true }) {
const fromSoup = opt.soup || false;
const justPoly = opt.flat || false;
const fromSoup = opt.soup || justPoly;
const objs = [];
const data = new THREE.SVGLoader().parse(text);
const paths = data.paths;
@ -34,11 +35,16 @@ function parse(text, opt = { soup: true }) {
for (let node of shapes) {
let { shape, holes } = node.extractPoints();
for (let path of [ shape, ...holes ]) {
polys.push(base.newPolygon().addPoints(path.map(p => base.newPoint(p.x, p.y, 0))));
let poly = base.newPolygon().addPoints(path.map(p => base.newPoint(p.x, p.y, 0)));
if (poly.appearsClosed()) poly.points.pop();
polys.push(poly);
}
}
continue;
}
if (justPoly) {
continue;
}
let geom = new THREE.ExtrudeGeometry(shapes, {
steps: 1,
depth: parseFloat(depth.value),
@ -46,8 +52,8 @@ function parse(text, opt = { soup: true }) {
});
let array = geom.attributes.position.array;
// invert y
for (let i=0; i<array.length; i+=3) {
array[i+1] = -array[i+1];
for (let i=1; i<array.length; i+=3) {
array[i] = -array[i];
}
// invert vertex order to compensate for inverted y
for (let i=0; i<array.length; i+=9) {
@ -72,18 +78,18 @@ function parse(text, opt = { soup: true }) {
}
}));
if (justPoly) {
return nest;
}
let z = parseFloat(depth.value);
for (let poly of nest) {
let obj = poly.extrude(z);
objs.push(obj);
// invert y
for (let i=1, l=obj.length; i<l; i += 3) {
obj[i] = -obj[i];
}
}
}
return objs;
return justPoly ? polys : objs;
}
});

View file

@ -471,6 +471,13 @@ function load_files(files) {
has_svg = has_svg || file.name.toLowerCase().indexOf(".svg") > 0;
}
if (has_svg) {
let sketch = api.sketch.selection.one;
if (sketch) {
return load.File.load([...files], { flat: true })
.then(polys => polys.flat().forEach(poly => sketch.add_polygon({ poly })))
.catch(error => dbug.error(error))
.finally(() => mesh.api.log.hide());
}
api.modal.dialog({
title: `svg import`,
body: [ h.div({ class: "image-import" }, [
@ -540,15 +547,9 @@ function load_files(files) {
function load_files_opt(files, opt) {
load.File.load([...files], opt)
.then(data => {
call.space_load(data);
})
.catch(error => {
dbug.error(error);
})
.finally(() => {
mesh.api.log.hide();
});
.then(data => call.space_load(data))
.catch(error => dbug.error(error))
.finally(() => mesh.api.log.hide());
}
// add object loader

View file

@ -739,6 +739,8 @@ let file = {
import() {
// binding created in mesh.build
$('import').click();
// allow importing the same file twice
setTimeout(() => { $('import').value = ''; }, 100);
},
export() {