add obj export

This commit is contained in:
Stewart Allen 2021-10-08 21:15:54 -04:00
commit 5b1e2d5eeb
5 changed files with 94 additions and 39 deletions

View file

@ -582,14 +582,14 @@
});
}
function objectsExport() {
function objectsExport(format = "stl") {
// return API.selection.export();
UC.confirm("Export Filename", {ok:true, cancel: false}, "selected.stl").then(name => {
UC.confirm("Export Filename", {ok:true, cancel: false}, `selected.${format}`).then(name => {
if (!name) return;
if (name.toLowerCase().indexOf(".stl") < 0) {
name = `${name}.stl`;
if (name.toLowerCase().indexOf(`.${format}`) < 0) {
name = `${name}.${format}`;
}
API.util.download(API.selection.export(), name);
API.util.download(API.selection.export(format), name);
});
}
@ -1515,6 +1515,11 @@
mesh: () => { UI.tool.show('ft-mesh') },
select: () => { UI.tool.show('ft-select') },
},
mesh: {
name: $('mesh-name'),
points: $('mesh-points'),
faces: $('mesh-faces'),
},
fps: $('fps'),
load: $('load-file'),
@ -2612,9 +2617,11 @@
$('render-ghost').onclick = () => { API.view.wireframe(false, 0, 0.5); };
$('render-wire').onclick = () => { API.view.wireframe(true, 0, 0.5); };
$('render-solid').onclick = () => { API.view.wireframe(false, 0, 1); };
// mesh buttons
$('mesh-heal').onclick = () => { API.widgets.heal() };
$('mesh-export-stl').onclick = () => { objectsExport('stl') };
$('mesh-export-obj').onclick = () => { objectsExport('obj') };
// context menu
$('context-heal-mesh').onclick = () => { API.widgets.heal() };
$('context-export-stl').onclick = () => { objectsExport() };
$('context-export-workspace').onclick = () => { profileExport(true) };
$('context-clear-workspace').onclick = () => {
API.view.set(VIEWS.ARRANGE);

View file

@ -1593,7 +1593,7 @@
auto_save();
}
function exportSelection() {
function exportSelection(format = "stl") {
let widgets = API.selection.widgets();
if (widgets.length === 0) {
widgets = API.widgets.all();
@ -1606,6 +1606,24 @@
outs.push({geo, widget});
facets += geo.attributes.position.count;
});
if (format === "obj") {
let obj = [];
for (let out of outs) {
let meta = out.widget.meta;
let name = meta.file || 'unnamed';
obj.push(`g ${name}`);
let { position } = out.geo.attributes;
let pvals = position.array;
for (let i=0, il=position.count; i<il; i += 3) {
let pi = i * position.itemSize;
obj.push(`v ${pvals[pi++]} ${pvals[pi++]} ${pvals[pi++]}`);
obj.push(`v ${pvals[pi++]} ${pvals[pi++]} ${pvals[pi++]}`);
obj.push(`v ${pvals[pi++]} ${pvals[pi++]} ${pvals[pi++]}`);
obj.push(`f ${i+1} ${i+2} ${i+3}`);
}
}
return obj.join('\n');
}
let stl = new Uint8Array(80 + 4 + facets * 50);
let dat = new DataView(stl.buffer);
let pos = 84;
@ -1813,11 +1831,7 @@
function platformUpdateSelected() {
let selcount = platform.selected_count();
let extruders = settings.device.extruders;
let menu_show = selcount ? 'flex': '';
$('winfo').style.display = 'none';
if (selcount) {
// UI.scale.classList.add('lt-active');
// UI.rotate.classList.add('lt-active');
UI.nozzle.classList.add('lt-active');
UI.trash.style.display = 'flex';
if (feature.meta && selcount === 1) {
@ -1833,13 +1847,19 @@
if (sp >= 0) {
name = name.substring(sp + 1);
}
$('winfo').style.display = 'flex';
$('winfo').innerHTML = `${UTIL.comma(sel.meta.vertices)}<br>${name}`;
UI.mesh.name.innerText = name;
UI.mesh.points.innerText = sel.meta.vertices;
UI.mesh.faces.innerText = sel.meta.vertices / 3;
}
} else {
UI.mesh.name.innerText = `[${selcount}]`;
UI.mesh.points.innerText = '-';
UI.mesh.faces.innerText = '-';
}
} else {
// UI.scale.classList.remove('lt-active');
// UI.rotate.classList.remove('lt-active');
UI.mesh.name.innerText = '[0]';
UI.mesh.points.innerText = '-';
UI.mesh.faces.innerText = '-';
UI.nozzle.classList.remove('lt-active');
UI.trash.style.display = '';
}