Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions src/kiri/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ window.addEventListener('message', msg => {
let bin = data.parse;
let widget;
switch ((data.type || 'stl').toLowerCase()) {
case 'ast': //FreeCAD saves STL as ASCII when usint .ast extension
case 'stl':
//in case stl is ASCII, this fails:
if (!bin.buffer) bin = new Float32Array(bin).buffer;
new load.STL().parse(bin, vertices => {
platform.add(widget = newWidget().loadVertices(vertices));
send({event: "parsed", data: [ widget.id ]});
});
//and we must not continue without any data at all, so revert:
if(bin.byteLength == 0) {
bin = data.parse;
}
//arrow function is never called. Traditional works just fine:
let vertices = new load.STL().parse(bin);
platform.add(widget = newWidget().loadVertices(vertices));
send({event: "parsed", data: [ widget.id ]});

break;
case 'obj':
// todo
Expand All @@ -101,6 +108,34 @@ window.addEventListener('message', msg => {
}
}


//uses data.widget_id to update widget from STL
if (data.update) {

let bin = data.update;
let widget;
switch ((data.type || 'stl').toLowerCase()) {
case 'ast': //FreeCAD saves STL as ASCII when usint .ast extension
case 'stl':
//in case stl is ASCII, this fails:
if (!bin.buffer) bin = new Float32Array(bin).buffer;
//and we must not continue without any data at all, so revert:
if(bin.byteLength == 0) {
bin = data.update;
}
let widget = api.widgets.map()[data.widget_id];
let vertices = new load.STL().parse(bin);
api.widgets.replace(vertices, widget);
//center the widget otherwise it will have odd movement limits:
widget.center();
send({event: "updated", data: [ widget.id ]});
break;
}
}




if (data.load) {
platform.load(data.load, (verts, widget) => {
send({event: "loaded", data: [ widget.id ]});
Expand Down
4 changes: 2 additions & 2 deletions src/kiri/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function duplicate() {
const bb = mesh.getBoundingBox();
const ow = widget;
const nw = api.widgets.new().loadGeometry(mesh.geometry.clone());
nw.meta.file = ow.meta.file;
nw.meta.vertices = ow.meta.vertices;
//meta should be cloned entirely to also copy additional data attached here:
nw.meta = ow.meta;
nw.anno = ow.annotations();
nw.move(bb.max.x - bb.min.x + 1, 0, 0);
api.platform.add(nw, true);
Expand Down
22 changes: 16 additions & 6 deletions src/kiri/run/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
}
}

function dispatchMessage(msg) {
let { origin, source, target, data } = msg;
if (source.window === cwin) {
recv(msg);
}
}

let API = KIRI.frame = {
setFrame: (io, target) => {
let type = typeof(io);
Expand All @@ -35,12 +42,12 @@
case 'object': setFrame(io); break;
default: throw `invalid frame type ${type}`;
}
window.addEventListener('message', msg => {
let { origin, source, target, data } = msg;
if (source.window === cwin) {
recv(msg);
}
});

//changed eventListener to a named function here:
//adding eventListeners with anonymous functions is bad practice
//as it is impossible to prevent them from being more than once!
window.removeEventListener('message', dispatchMessage);
window.addEventListener('message', dispatchMessage);
},

send: send,
Expand All @@ -51,6 +58,9 @@

parse: (data, type) => { send({ parse: data, type })},

//update widget with id widget_id from STL data:
update: (data, type, widget_id) => { send({ update: data, type, widget_id })},

get: (scope) => { send({ get: scope })},

setMode: (mode) => { send({ mode }) },
Expand Down