add obj import grouping, naming, and auto-scaling for Onshape exports

This commit is contained in:
Stewart Allen 2021-08-28 19:20:37 -04:00
commit 94b85351c2
3 changed files with 44 additions and 9 deletions

View file

@ -2456,12 +2456,28 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
}
} else if (isobj) {
let objs = MOTO.OBJ.parse(e.target.result);
for (let obj of objs) {
platform.add(
newWidget(undefined,group)
.loadVertices(obj.toFloat32(),unitScale())
.saveToCatalog(e.target.file.name)
);
let odon = function() {
for (let obj of objs) {
let name = e.target.file.name;
if (obj.name) {
name = obj.name + ' - ' + name;
}
platform.add(
newWidget(undefined,group)
.loadVertices(obj.toFloat32(), true)
.saveToCatalog(name)
);
}
};
if (objs.length > 1 && !group) {
UC.confirm('group objects?').then(ok => {
if (ok) {
group = [];
}
odon();
});
} else {
odon();
}
}
else if (isgcode) loadCode(e.target.result, 'gcode');

View file

@ -275,7 +275,19 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
* @param {Float32Array} vertices
* @returns {Widget}
*/
PRO.loadVertices = function(vertices) {
PRO.loadVertices = function(vertices, autoscale) {
if (autoscale === true) {
// onshape exports obj in meters by default :/
let maxv = 0;
for (let i=0; i<vertices.length; i++) {
maxv = Math.max(Math.abs(vertices[i]));
}
if (maxv < 1) {
for (let i=0; i<vertices.length; i++) {
vertices[i] *= 1000;
}
}
}
if (this.mesh) {
this.mesh.geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
this.mesh.geometry.computeFaceNormals();

View file

@ -18,6 +18,7 @@
let lines = text.split('\n').map(l => l.trim());
let verts = [];
let faces = [];
let obj = [ faces ];
for (let line of lines) {
let toks = line.split(' ');
@ -27,17 +28,23 @@
break;
case 'f':
let tok = toks.map(f => parseInt(f.split('/')[0]));
// add support for negative indices (offset from last vertex array point)
faces.appendAll(verts[tok[0]-1]);
faces.appendAll(verts[tok[1]-1]);
faces.appendAll(verts[tok[2]-1]);
break;
case 'g':
case 'o':
if (faces.length) {
obj.push(faces = []);
}
if (toks[0]) {
faces.name = toks[0];
}
break;
}
}
return [ faces ];
return obj;
}
})();