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();