start to structure tracing code split as client/worker

This commit is contained in:
Stewart Allen 2020-11-30 18:01:32 -05:00
commit 563fcb71d0
7 changed files with 116 additions and 147 deletions

View file

@ -147,6 +147,9 @@
api.event.on("cam.trace.add", func.traceAdd = () => {
alert = api.show.alert("<esc> key cancels editing traces");
KIRI.client.traces(done => {
console.log({got_traces: done});
});
api.feature.hover = true;
});
api.event.on("cam.trace.done", func.traceDone = () => {

View file

@ -21,77 +21,25 @@
DRILL: 7
};
/**
* Find top paths to trace when using ball and taper mills
* in waterline outlining and tracing modes.
*/
function findTracingPaths(widget, slice, tool, profile, partial) {
// for now, only emit completed polys and not segments
// TODO consider limiting to nup path lengths that are >= tool radius
let only_whole = !partial;
// check for ball and taper mills paths and add to top[0].inner
let polys = [];
let nups = [];
let cull = [];
slice.gatherTopPolys([]).forEach(poly => poly.flattenTo(polys));
polys.forEach(poly => {
let pz = poly.first().z;
let mz = -Infinity;
let np = newPolygon().setOpen();
let mp = 0;
// find top poly segments that are not significantly offset
// from tool profile and add to new polygons which accumulate
// to the top inner array
poly.forEachSegment((p1,p2) => {
let nz = getTopoZPathMax(widget, profile, p1.x, p1.y, p2.x, p2.y);
if (nz > mz) {
mz = nz;
}
// this # should be computed from topo resolution
if (nz - pz < 0.01) {
mp++
if (np.length) {
if (!np.first().isEqual(p2)) {
np.append(p2);
} else {
np.setClosed();
}
} else {
np.append(p1).append(p2);
}
} else if (np.length) {
if (!only_whole) {
nups.append(np);
}
np = newPolygon().setOpen();
}
});
if (np.length) {
if (np.length === poly.length) {
np.setClosed();
}
// if a trace poly has no interruptions for an endmill
// and it's an inner poly, eliminate it from the parent
// so it won't be offset.
let parent = poly.parent;
if (np.isClosed() && parent) {
// console.log(slice.z,'cull',poly);
if (parent.inner) {
parent.inner = parent.inner.filter(p => p !== poly);
}
if (only_whole) {
nups.append(np);
}
}
if (!only_whole) {
nups.append(np);
}
}
// defer loading until KIRI.client and KIRI.worker exist
KIRI.loader.push(function(API) {
if (KIRI.client)
KIRI.client.traces = function(ondone) {
KIRI.client.sync();
send("traces", {
settings: API.conf.get()
}, ondone);
};
if (KIRI.worker)
KIRI.worker.traces = function(data, send) {
const { settings } = data;
const widgets = Object.values(cache);
widgets.forEach(widget => CAM.traces(settings, widget));
send.done(123);
};
});
if (nups.length) {
// console.log(slice.z,'nups',nups.length);
slice.tops[0].inner = nups;
}
}
})();

View file

@ -483,31 +483,10 @@
});
}
// prepare for tracing paths
// generate tracing offsets from chosen features
if (procTrace) {
let traceTool = new CAM.Tool(conf, proc.camTraceTool);
let traceToolDiam = traceTool.fluteDiameter();
maxToolDiam = Math.max(maxToolDiam, traceToolDiam);
// unique merge of z flats and z line indices
let indices = [...new Set(Object.keys(slicer.zFlat)
.map(kv => parseFloat(kv).round(5))
.appendAll(Object.entries(slicer.zLine).map(ze => {
let [ zk, zv ] = ze;
return zv > 0 ? parseFloat(zk).round(5) : null;
})
.filter(v => v !== null)))]
.sort((a,b) => b - a);
let slices = [];
slicer.slice(indices, { each: (data, index, total) => {
let slice = data.slice;
slice.camMode = PRO.TRACE;
slices.push(slice);
sliceAll.append(slice);
if (true) slice.output()
.setLayer("trace", {line: 0x88aa55}, false)
.addPolys(slice.topPolys())
updateOp(index, total);
}, genso: true, emptyok: true, flatoff: 0, edges: true, openok: true });
// todo
// slice.camMode = PRO.TRACE;
}
if (procDrill) {
@ -539,6 +518,36 @@
ondone();
};
CAM.traces = function computeTraces(settings, widget) {
let slicer = new KIRI.slicer2(widget.getPoints(), {
zlist: true,
zline: true
});
let proc = settings.process;
let traceTool = new CAM.Tool(settings, proc.camTraceTool);
let traceToolDiam = traceTool.fluteDiameter();
// widget.maxToolDiam = Math.max(maxToolDiam, traceToolDiam);
// unique merge of z flats and z line indices
let indices = [...new Set(Object.keys(slicer.zFlat)
.map(kv => parseFloat(kv).round(5))
.appendAll(Object.entries(slicer.zLine).map(ze => {
let [ zk, zv ] = ze;
return zv > 0 ? parseFloat(zk).round(5) : null;
})
.filter(v => v !== null)))]
.sort((a,b) => b - a);
let traces = [];
slicer.slice(indices, { each: (data, index, total) => {
let slice = data.slice;
traces.push(slice);
if (true) slice.output()
.setLayer("trace", {line: 0x88aa55}, false)
.addPolys(slice.topPolys())
// updateOp(index, total);
}, genso: true, emptyok: true, flatoff: 0, edges: true, openok: true });
widget.traces = traces;
};
// drilling op
function sliceDrill(tool, slices, output) {
let drills = [],