apply workspace unit scale to dropped loaded STLs

This commit is contained in:
Stewart Allen 2020-12-13 09:55:30 -05:00
commit f10879edda
7 changed files with 39 additions and 18 deletions

9
app.js
View file

@ -22,6 +22,7 @@ const synth = {};
const api = {};
let level;
let setupFn;
let cacheDir;
let startTime;
let lastmod;
@ -73,6 +74,7 @@ function init(mod) {
return vmatch.indexOf(cookie) >= 0;
});
mod.add(handleSetup);
mod.add(handleOptions);
mod.add(fullpath({
"/kiri" : redir("/kiri/", 301),
@ -212,7 +214,8 @@ function initModule(mod, file, dir) {
};
},
redir: redir,
remap: remap
remap: remap,
setup: fn => { setupFn = fn }
},
handler: {
addCORS: addCorsHeaders,
@ -428,6 +431,10 @@ function string2obj(s) {
return JSON.parse(s);
}
function handleSetup(req, res, next) {
if (setupFn) setupFn(req, res, next);
}
function handleVersion(req, res, next) {
if (req.app.path === "/kiri/" && req.url.indexOf(version) < 0) {
if (req.url.indexOf("?") > 0) {

View file

@ -44,7 +44,10 @@
js2o = API.js2o,
o2js = API.o2js,
platform = API.platform,
selection = API.selection;
selection = API.selection,
proto = location.protocol,
root = "grid.space",
ver = Date.now().toString(36);
let currentDevice = null,
deviceURL = null,
@ -1331,6 +1334,8 @@
function init_one() {
API.event.emit('init.one');
$('gsbox').src = `${proto}//${root}/kiri/gsbox.png?ver=${ver}`;
// ensure we have settings from last session
API.conf.restore();

View file

@ -1413,6 +1413,7 @@
}
function platformLoadSTL(url, onload, formdata) {
let scale = 1 / unitScale();
new MOTO.STL().load(url, function(vertices, filename) {
if (vertices) {
let widget = newWidget().loadVertices(vertices);
@ -1422,7 +1423,7 @@
onload(vertices, widget);
}
}
}, formdata);
}, formdata, scale);
}
function platformComputeMaxZ() {
@ -1888,9 +1889,10 @@
if (API.feature.on_add_stl) {
API.feature.on_add_stl(e.target.result, file);
} else {
let scale = unitScale();
platform.add(
newWidget(undefined,group)
.loadVertices(new MOTO.STL().parse(e.target.result))
.loadVertices(new MOTO.STL().parse(e.target.result,scale))
.saveToCatalog(e.target.file.name)
);
}

View file

@ -26,13 +26,13 @@
}
SP.load = function(url, callback, formdata) {
SP.load = function(url, callback, formdata, scale) {
let stl = this,
xhr = new XMLHttpRequest();
function onloaded (event) {
if (event.target.status === 200 || event.target.status === 0) {
stl.parse(event.target.response || event.target.responseText);
stl.parse(event.target.response || event.target.responseText, scale);
let cd = undefined;
if (xhr.getAllResponseHeaders().indexOf(CDH) > 0) {
cd = xhr.getResponseHeader(CDH)
@ -107,7 +107,7 @@
return bin;
};
SP.parse = function(data) {
SP.parse = function(data, scale) {
let binData = this.convertToBinary(data);
let isBinary = function () {
@ -120,11 +120,11 @@
};
return isBinary()
? this.parseBinary(binData)
: this.parseASCII(this.convertToString(data));
? this.parseBinary(binData, scale)
: this.parseASCII(this.convertToString(data), scale);
};
SP.parseBinary = function(data) {
SP.parseBinary = function(data, scale = 1) {
let reader = new DataView(data),
faces = reader.getUint32 (80, true),
r, g, b, hasColors = false, colors,
@ -176,9 +176,9 @@
while (i <= 3) {
vertexstart = start + (i++) * 12;
vertices[offset ] = reader.getFloat32 (vertexstart, true);
vertices[offset + 1] = reader.getFloat32 (vertexstart + 4, true);
vertices[offset + 2] = reader.getFloat32 (vertexstart + 8, true);
vertices[offset ] = reader.getFloat32 (vertexstart, true) * scale;
vertices[offset + 1] = reader.getFloat32 (vertexstart + 4, true) * scale;
vertices[offset + 2] = reader.getFloat32 (vertexstart + 8, true) * scale;
normals[offset ] = normalX;
normals[offset + 1] = normalY;
normals[offset + 2] = normalZ;
@ -198,7 +198,7 @@
return vertices;
};
SP.parseASCII = function(data) {
SP.parseASCII = function(data, scale = 1) {
let result,
resultText,
patternNormal,
@ -217,9 +217,9 @@
normals.push(parseFloat(result[5]));
}
while ((result = patternVertex.exec(resultText)) !== null) {
vertices.push(parseFloat(result[1]));
vertices.push(parseFloat(result[3]));
vertices.push(parseFloat(result[5]));
vertices.push(parseFloat(result[1]) * scale);
vertices.push(parseFloat(result[3]) * scale);
vertices.push(parseFloat(result[5]) * scale);
}
}

BIN
web/kiri/gsbox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View file

@ -27,6 +27,13 @@ a, a:hover, a:visited {
border: 0px;
margin: 0px;
}
#gsbox {
width: 40px;
height: 40px;
padding-right: 15px;
position: fixed;
display: none;
}
div {
position: relative;
}

View file

@ -22,7 +22,7 @@
</head>
<body>
<div id="app" class="f-col">
<div id="curtain" class="j-center a-center">loading...</div>
<div id="curtain" class="j-center a-center"><img id="gsbox"/>loading...</div>
<div id="container"></div>
<div id="tracker"></div>
<div id="top-sep"></div>