From 82e2742b8dc280a806a3c6f4decfceb67ee3d57a Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Fri, 27 Jun 2025 01:02:12 -0400 Subject: [PATCH] add trace re-contouring when poly z varies, offset present, no step down --- src/kiri-mode/cam/driver.js | 11 +++++------ src/kiri-mode/cam/ops.js | 11 +++++++++++ src/kiri-mode/cam/slice.js | 37 ++++++++++++++++++++++++------------- src/kiri/conf.js | 2 +- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/kiri-mode/cam/driver.js b/src/kiri-mode/cam/driver.js index 44220e29..9d92527e 100644 --- a/src/kiri-mode/cam/driver.js +++ b/src/kiri-mode/cam/driver.js @@ -82,7 +82,6 @@ kiri.load(api => { }); }; - CAM.holes = function(indiv,rec,onProgress,onDone) { kiri.client.sync(); const settings = api.conf.get(); @@ -93,13 +92,13 @@ kiri.load(api => { //if a progress message, onProgress(out.progress,out.msg) }else{ - api.hide.alert(alert) - onDone(out) - res(out) + api.hide.alert(alert); + onDone(out); + res(out); } }); }) - } + }; } if (kiri.worker) { @@ -167,7 +166,7 @@ kiri.load(api => { const { settings, indiv, rec } = data; const widgets = Object.values(kiri.worker.cache); const fresh = []; - + for (let [i,widget] of widgets.entries() ) { if (await CAM.holes(settings, widget, indiv, rec, ( prog, msg )=>{ send.data({progress: (i/widgets.length)+(prog/widgets.length),msg})} diff --git a/src/kiri-mode/cam/ops.js b/src/kiri-mode/cam/ops.js index 63c84554..28f6a546 100644 --- a/src/kiri-mode/cam/ops.js +++ b/src/kiri-mode/cam/ops.js @@ -995,6 +995,8 @@ class OpTrace extends CamOp { let traceOffset = camTool.traceOffset() let cutdir = ov_conv; let polys = []; + let reContour = false; + let canRecontour = offset !== 'none' && down === 0; let stockRect = stock.center && stock.x && stock.y ? newPolygon().centerRectangle({x:0,y:0}, stock.x, stock.y) : undefined; updateToolDiams(toolDiam); @@ -1008,6 +1010,12 @@ class OpTrace extends CamOp { let poly = newPolygon().fromArray(arr); POLY.setWinding([ poly ], cutdir, false); polys.push(poly); + let zs = poly.points.map(p => p.z); + let min = Math.min(...zs); + let max = Math.max(...zs); + if (max - min > 0.0001 && canRecontour) { + reContour = true; + } } if (false) newSliceOut(0).output() .setLayer("polys", {line: 0xaaaa00}, false) @@ -1036,6 +1044,9 @@ class OpTrace extends CamOp { if (camStockClipTo && stockRect) { slice.camLines = cutPolys([stockRect], slice.camLines, z, true); } + if (reContour) { + state.contourPolys(widget, slice.camLines); + } slice.output() .setLayer("trace follow", {line: color}, false) .addPolys(slice.camLines) diff --git a/src/kiri-mode/cam/slice.js b/src/kiri-mode/cam/slice.js index 5843c98c..ab743b78 100644 --- a/src/kiri-mode/cam/slice.js +++ b/src/kiri-mode/cam/slice.js @@ -165,6 +165,7 @@ CAM.slice = async function(settings, widget, onupdate, ondone) { tabs, cutTabs, cutPolys, + contourPolys, healPolys, shadowAt, slicer, @@ -365,8 +366,6 @@ CAM.slice = async function(settings, widget, onupdate, ondone) { ondone(); }; - - CAM.addDogbones = function(poly, dist, reverse) { if (Array.isArray(poly)) { return poly.forEach(p => CAM.addDogbones(p, dist)); @@ -512,16 +511,16 @@ CAM.traces = async function(settings, widget, single) { return true; }; - /** - * Generate a list of holes in the model based on the given diameter. - * - * @param {Object} settings - settings object - * @param {Object} widget - widget object - * @param {boolean} individual - if true, drill holes individually - * @param {Object} rec - DrillOp record - * @param {Function} onProgress - callback function to report progress - * @returns {Array} list of hole centers as objects with `x`, `y`, `z`, `depth`, and `selected` properties. - */ +/** + * Generate a list of holes in the model based on the given diameter. + * + * @param {Object} settings - settings object + * @param {Object} widget - widget object + * @param {boolean} individual - if true, drill holes individually + * @param {Object} rec - DrillOp record + * @param {Function} onProgress - callback function to report progress + * @returns {Array} list of hole centers as objects with `x`, `y`, `z`, `depth`, and `selected` properties. + */ CAM.holes = async function(settings, widget, individual, rec,onProgress) { let {tool,mark,precision} = rec //TODO: display some visual difference if mark is selected @@ -603,7 +602,6 @@ CAM.holes = async function(settings, widget, individual, rec,onProgress) { // //if on the same xy point, if (dist <= centerDiff ) { - // console.log("overlap",center,circle); circle.overlapping.push(center); // if overlapping, don't add and continue @@ -669,6 +667,19 @@ function cutPolys(polys, offset, z, inter) { return healPolys(noff); } +function contourPolys(widget, polys) { + const raycaster = new THREE.Raycaster(); + raycaster.ray.direction.set(0, 0, -1); // ray pointing down Z + for (let poly of polys) { + for (let point of poly.points) { + raycaster.ray.origin.set(point.x, point.y, 10000); + const intersects = raycaster.intersectObject(widget.mesh, true); + const firstHit = intersects[0] || null; + point.z = firstHit.point.z; + } + } +} + function healPolys(noff) { if (noff.length > 1) { let heal = 0; diff --git a/src/kiri/conf.js b/src/kiri/conf.js index ec097ffb..abea4b43 100644 --- a/src/kiri/conf.js +++ b/src/kiri/conf.js @@ -493,7 +493,7 @@ const conf = exports({ camTracePlunge: 200, camTraceOffOver: 0, camTraceDogbone: false, - camTraceMerge: true, + camTraceMerge: false, camTraceLines: false, camTraceZTop: 0, camTraceZBottom: 0,