bump rev 3.0.D18 -- add basic 3MF support

This commit is contained in:
Stewart Allen 2021-08-28 20:35:26 -04:00
commit c37c627622
6 changed files with 119 additions and 4 deletions

2
app.js
View file

@ -221,6 +221,7 @@ const script = {
"kiri",
"ext/three",
"ext/three-bgu",
"ext/jszip",
"license",
"ext/clip2", // work.test
"ext/tween",
@ -244,6 +245,7 @@ const script = {
"moto/ctrl",
"moto/pack",
"moto/space",
"moto/load-3mf",
"moto/load-obj",
"moto/load-stl",
"moto/db",

View file

@ -1,6 +1,6 @@
{
"name": "grid-apps",
"version": "3.0.17D",
"version": "3.0.18D",
"description": "grid.space 3d slice & modeling tools",
"author": "Stewart Allen <sa@grid.space>",
"license": "MIT",

View file

@ -6,7 +6,7 @@
if (!self.kiri) {
self.kiri = {
beta: 3017,
beta: 3018,
driver: {}, // driver modules
loader: [] // module loading: array of functions
};

View file

@ -2433,6 +2433,7 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
israw = lower.indexOf(".raw") > 0 || lower.indexOf('.') < 0,
isstl = lower.indexOf(".stl") > 0,
isobj = lower.indexOf(".obj") > 0,
is3mf = lower.indexOf(".3mf") > 0,
issvg = lower.indexOf(".svg") > 0,
ispng = lower.indexOf(".png") > 0,
isjpg = lower.indexOf(".jpg") > 0,
@ -2454,7 +2455,8 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
.saveToCatalog(e.target.file.name)
);
}
} else if (isobj) {
}
else if (isobj) {
let objs = MOTO.OBJ.parse(e.target.result);
let odon = function() {
for (let obj of objs) {
@ -2480,6 +2482,18 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
odon();
}
}
else if (is3mf) {
MOTO.TMF.parse(e.target.result).then(models => {
console.log({models});
for (let model of models) {
platform.add(
newWidget(undefined,group)
.loadVertices(model.faces.toFloat32())
.saveToCatalog(name)
);
}
});
}
else if (isgcode) loadCode(e.target.result, 'gcode');
else if (issvg) loadCode(e.target.result, 'svg');
else if (isset) settingsImport(e.target.result, true);

View file

@ -3,7 +3,7 @@
let terms = {
COPYRIGHT: "Copyright (C) Stewart Allen <sa@grid.space> - All Rights Reserved",
LICENSE: "See the license.md file included with the source distribution",
VERSION: "3.0.D17"
VERSION: "3.0.D18"
};
if (typeof(module) === 'object') {

99
src/moto/load-3mf.js Normal file
View file

@ -0,0 +1,99 @@
'use strict';
(function() {
if (!self.moto) self.moto = {};
if (self.moto.OBJ) return;
self.moto.TMF = {
parse : parse
};
function query(node, path, fn) {
let collect = false;
let match = path[0];
if (match[0] === '+') {
match = match.slice(1);
collect = true;
}
for (let child of [...node.childNodes]) {
if (child.tagName === match) {
if (collect) {
fn(match, child);
}
if (path.length > 1) {
query(child, path.slice(1), fn);
}
}
}
}
function loadModel(doc) {
let models = [];
function emitModel(model, faces) {
models.push({model, faces});
}
return new Promise((resolve, reject) => {
let model;
let faces;
query(doc, ["+model","resources","object","+mesh"], (type, node) => {
console.log({type, node});
switch (type) {
case "model":
if (model) {
emitModel(mode, faces);
}
model = node;
faces = [];
break;
case "mesh":
let vertices = [];
query(node, ["vertices","+vertex"], (type, vertex) => {
vertices.push([
parseFloat(vertex.getAttribute("x")),
parseFloat(vertex.getAttribute("y")),
parseFloat(vertex.getAttribute("z"))
]);
});
query(node, ["triangles","+triangle"], (type, triangle) => {
let v1 = parseInt(triangle.getAttribute("v1"));
let v2 = parseInt(triangle.getAttribute("v2"));
let v3 = parseInt(triangle.getAttribute("v3"));
faces.appendAll(vertices[v1]);
faces.appendAll(vertices[v2]);
faces.appendAll(vertices[v3]);
});
break;
}
});
if (model) {
emitModel(model, faces);
}
resolve(models);
});
}
/**
* @param {Object} data binary file
* @returns {Array} vertex face array
*/
function parse(data) {
return new Promise((resolve, reject) => {
JSZip.loadAsync(data).then(zip => {
for (let [key,value] of Object.entries(zip.files)) {
if (key.indexOf(".model") > 0) {
value.async("string").then(xml => {
resolve(loadModel(new DOMParser().parseFromString(xml, "text/xml")));
});
}
}
});
});
}
})();