seperate arc generation from g2g3 func

This commit is contained in:
bakedpotatolord 2025-05-27 12:49:27 -06:00
commit 4704861633

View file

@ -277,7 +277,6 @@ class Print {
seq = [], seq = [],
autolayer = true, autolayer = true,
newlayer = false, newlayer = false,
arcdivs = Math.PI / 24,
hasmoved = false, hasmoved = false,
lastG = 'G1'; lastG = 'G1';
@ -288,61 +287,57 @@ class Print {
console.log(...[...arguments].map(o => Object.clone(o))); console.log(...[...arguments].map(o => Object.clone(o)));
} }
function G2G3(g2, line, index) { function arcToPoints( radius, clockwise, start, end,arcdivs=Math.PI / 24) {
const rec = {};
line.forEach(tok => {
rec[tok.charAt(0)] = parseFloat(tok.substring(1));
});
console.log({radius, clockwise, start, end, arcdivs});
let center = { x:0, y:0, r:0 }; let center = { x:0, y:0, r:0 };
if (rec.X === undefined && rec.X === rec.Y) { if (end.x === undefined && end.x === end.y) {
// bambu generates loop z or wipe loop arcs in place // bambu generates loop z or wipe loop arcs in place
// console.log({ skip_empty_arc: rec }); // console.log({ skip_empty_arc: rec });
return; return;
} }
// G0G1(false, [`X${rec.X}`, `Y${rec.Y}`, `E1`]);return; // G0G1(false, [`X${rec.x}`, `Y${rec.y}`, `E1`]);return;
if (rec.I !== undefined && rec.J !== undefined) { if (end.I !== undefined && end.J !== undefined) {
center.x = pos.X + rec.I; center.x = start.x + end.I;
center.y = pos.Y + rec.J; center.y = start.y + end.J;
center.r = Math.sqrt(rec.I*rec.I + rec.J*rec.J); center.r = Math.sqrt(end.I*end.I + end.J*end.J);
} else if (rec.R !== undefined) { } else if (radius !== undefined) {
let pd = { x: rec.X - pos.X, y: rec.Y - pos.Y }; let pd = { x: end.x - start.x, y: end.y - start.y }; //position delta
let dst = Math.sqrt(pd.x * pd.x + pd.y * pd.y) / 2; let dst = Math.sqrt(pd.x * pd.x + pd.y * pd.y) / 2; // distance
let pr2; let pr2;
if (Math.abs(dst - rec.R) < 0.001) { if (Math.abs(dst - radius) < 0.001) {
// center point radius // center point radius
pr2 = { x: (rec.X + pos.X) / 2, y: (rec.Y + pos.Y) / 2}; pr2 = { x: (end.x + start.x) / 2, y: (end.y + start.y) / 2};
} else { } else {
// triangulate // triangulate
pr2 = base.util.center2pr({ pr2 = base.util.center2pr({
x: pos.X, x: start.x,
y: pos.Y y: start.y
}, { }, {
x: rec.X, x: end.x,
y: rec.Y y: end.y
}, rec.R, g2); }, radius, clockwise);
} }
center.x = pr2.x; center.x = pr2.x;
center.y = pr2.y; center.y = pr2.y;
center.r = rec.R; center.r = radius;
} else { } else {
console.log({malfomed_arc: line}); console.log({malfomed_arc: {radius, clockwise, start, end}});
} }
// line angles // line angles
let a1 = Math.atan2(center.y - pos.Y, center.x - pos.X) + Math.PI; let a1 = Math.atan2(center.y - start.y, center.x - start.x) + Math.PI;
let a2 = Math.atan2(center.y - rec.Y, center.x - rec.X) + Math.PI; let a2 = Math.atan2(center.y - end.y, center.x - end.x) + Math.PI;
let ad = base.util.thetaDiff(a1, a2, g2); let ad = base.util.thetaDiff(a1, a2, clockwise);
let steps = Math.max(Math.floor(Math.abs(ad) / arcdivs), 3); let steps = Math.max(Math.floor(Math.abs(ad) / arcdivs), 3);
let step = (Math.abs(ad) > 0.001 ? ad : Math.PI * 2) / steps; let step = (Math.abs(ad) > 0.001 ? ad : Math.PI * 2) / steps;
let rot = a1 + step; let rot = a1 + step;
let da = Math.abs(a1 - a2); let da = Math.abs(a1 - a2);
let dx = pos.X - rec.X; let dx = start.x - end.x;
let dy = pos.Y - rec.Y; let dy = start.y - end.y;
let dd = Math.sqrt(dx * dx + dy * dy); let dd = Math.sqrt(dx * dx + dy * dy);
// LOG({index, da, dd, first: pos, last: rec, center, a1, a2, ad, step, steps, rot, line}); // LOG({index, da, dd, first: pos, last: rec, center, a1, a2, ad, step, steps, rot, line});
@ -350,24 +345,41 @@ class Print {
// under 1 degree arc and 5mm, convert to straight line // under 1 degree arc and 5mm, convert to straight line
if (da < 0.005 && dd < 5) { if (da < 0.005 && dd < 5) {
G0G1(false, [`X${rec.X}`, `Y${rec.Y}`, `E1`]); G0G1(false, [`X${end.x}`, `Y${end.y}`, `E1`]);
return; return;
} }
let pc = { X: pos.X, Y: pos.Y }; let arr = [] // point accumulator
for (let i=0; i<=steps-2; i++) { for (let i=0; i<=steps-2; i++) {
let np = { arr.push(newPoint(
X: center.x + Math.cos(rot) * center.r, center.x + Math.cos(rot) * center.r,
Y: center.y + Math.sin(rot) * center.r center.y + Math.sin(rot) * center.r));
};
rot += step; rot += step;
G0G1(false, [`X${np.X}`, `Y${np.Y}`, `E1`]);
} }
return arr
}
G0G1(false, [`X${rec.X}`, `Y${rec.Y}`, `E1`]); function G2G3(g2, line, index) {
console.log({index, g2, line});
const rec = {};
pos.X = rec.X; line.forEach(tok => {
pos.Y = rec.Y; rec[tok.charAt(0).toLowerCase()] = parseFloat(tok.substring(1));
});
pos.x = pos.X;
pos.y = pos.Y;
arcToPoints(rec.r, g2, pos, rec, 0.02).forEach(np => {
G0G1(false, [`X${np.x}`, `Y${np.y}`, `E1`]);
})
G0G1(false, [`X${rec.x}`, `Y${rec.y}`, `E1`]);
pos.X = rec.x;
pos.Y = rec.y;
} }
function G0G1(g0, line) { function G0G1(g0, line) {