@faicad/faijs-fcstd 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/NOTICE +20 -0
- package/dist/attachment.d.ts +72 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +93 -0
- package/dist/attachment.js.map +1 -0
- package/dist/bspline.d.ts +58 -0
- package/dist/bspline.d.ts.map +1 -0
- package/dist/bspline.js +93 -0
- package/dist/bspline.js.map +1 -0
- package/dist/build-fai-zip.d.ts +40 -0
- package/dist/build-fai-zip.d.ts.map +1 -0
- package/dist/build-fai-zip.js +111 -0
- package/dist/build-fai-zip.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +37 -0
- package/dist/cli.js.map +1 -0
- package/dist/codegen.d.ts +83 -0
- package/dist/codegen.d.ts.map +1 -0
- package/dist/codegen.js +618 -0
- package/dist/codegen.js.map +1 -0
- package/dist/container.d.ts +75 -0
- package/dist/container.d.ts.map +1 -0
- package/dist/container.js +36 -0
- package/dist/container.js.map +1 -0
- package/dist/contour.d.ts +43 -0
- package/dist/contour.d.ts.map +1 -0
- package/dist/contour.js +132 -0
- package/dist/contour.js.map +1 -0
- package/dist/convert.d.ts +69 -0
- package/dist/convert.d.ts.map +1 -0
- package/dist/convert.js +303 -0
- package/dist/convert.js.map +1 -0
- package/dist/document.d.ts +48 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +140 -0
- package/dist/document.js.map +1 -0
- package/dist/expressions.d.ts +79 -0
- package/dist/expressions.d.ts.map +1 -0
- package/dist/expressions.js +162 -0
- package/dist/expressions.js.map +1 -0
- package/dist/external-geo.d.ts +49 -0
- package/dist/external-geo.d.ts.map +1 -0
- package/dist/external-geo.js +140 -0
- package/dist/external-geo.js.map +1 -0
- package/dist/feature-translate.d.ts +193 -0
- package/dist/feature-translate.d.ts.map +1 -0
- package/dist/feature-translate.js +1441 -0
- package/dist/feature-translate.js.map +1 -0
- package/dist/fillet-edges.d.ts +26 -0
- package/dist/fillet-edges.d.ts.map +1 -0
- package/dist/fillet-edges.js +36 -0
- package/dist/fillet-edges.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/placement.d.ts +93 -0
- package/dist/placement.d.ts.map +1 -0
- package/dist/placement.js +117 -0
- package/dist/placement.js.map +1 -0
- package/dist/planegcs-backend.d.ts +34 -0
- package/dist/planegcs-backend.d.ts.map +1 -0
- package/dist/planegcs-backend.js +665 -0
- package/dist/planegcs-backend.js.map +1 -0
- package/dist/sketch-parse.d.ts +199 -0
- package/dist/sketch-parse.d.ts.map +1 -0
- package/dist/sketch-parse.js +263 -0
- package/dist/sketch-parse.js.map +1 -0
- package/dist/sketch-solver.d.ts +63 -0
- package/dist/sketch-solver.d.ts.map +1 -0
- package/dist/sketch-solver.js +28 -0
- package/dist/sketch-solver.js.map +1 -0
- package/dist/sketch-verify.d.ts +53 -0
- package/dist/sketch-verify.d.ts.map +1 -0
- package/dist/sketch-verify.js +71 -0
- package/dist/sketch-verify.js.map +1 -0
- package/dist/structural-types.d.ts +96 -0
- package/dist/structural-types.d.ts.map +1 -0
- package/dist/structural-types.js +155 -0
- package/dist/structural-types.js.map +1 -0
- package/dist/unpack.d.ts +44 -0
- package/dist/unpack.d.ts.map +1 -0
- package/dist/unpack.js +64 -0
- package/dist/unpack.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* M3.2/M3.3/M3.4 — planegcs WASM backend for the SketchSolver interface.
|
|
3
|
+
*
|
|
4
|
+
* M3.3: geometry → GCS primitives, including the implicit fixed RtPnt/HAxis/
|
|
5
|
+
* VAxis (geoId -1/-2, plan §5.4). External geometry (geoId <= -3) is rejected
|
|
6
|
+
* upstream (D4: whole sketch downgrades to L2).
|
|
7
|
+
* M3.4: constraints → GCS primitives for the P0 set (plan §7 M3.4).
|
|
8
|
+
* M3.5 helper: solved params are pulled back and geometry rebuilt, so the
|
|
9
|
+
* caller can compare against on-disk coordinates (D2).
|
|
10
|
+
*/
|
|
11
|
+
import { make_gcs_wrapper, Algorithm } from '@salusoft89/planegcs';
|
|
12
|
+
import { ok } from '@faicad/faijs/api/result';
|
|
13
|
+
import { ConstraintType, PointPos } from './sketch-parse.js';
|
|
14
|
+
import { SUPPORTED_CONSTRAINT_TYPES } from './sketch-solver.js';
|
|
15
|
+
import { createRequire } from 'node:module';
|
|
16
|
+
import { dirname, join } from 'node:path';
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
let wasmPathCache;
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the planegcs WASM binary path from the installed package.
|
|
21
|
+
*
|
|
22
|
+
* @returns the absolute path of `planegcs.wasm` inside `@salusoft89/planegcs`.
|
|
23
|
+
*/
|
|
24
|
+
export function planegcsWasmPath() {
|
|
25
|
+
if (!wasmPathCache) {
|
|
26
|
+
const pkgDir = dirname(require.resolve('@salusoft89/planegcs/package.json'));
|
|
27
|
+
wasmPathCache = join(pkgDir, 'dist', 'planegcs_dist', 'planegcs.wasm');
|
|
28
|
+
}
|
|
29
|
+
return wasmPathCache;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Instantiate the planegcs WASM solver.
|
|
33
|
+
*
|
|
34
|
+
* @returns a `SketchSolver` backed by the planegcs WASM module.
|
|
35
|
+
*/
|
|
36
|
+
export async function createPlanegcsSolver() {
|
|
37
|
+
const wrapper = await make_gcs_wrapper(planegcsWasmPath());
|
|
38
|
+
return new PlanegcsSolver(wrapper);
|
|
39
|
+
}
|
|
40
|
+
const P = (geoId, pos) => `g${geoId}p${pos}`;
|
|
41
|
+
/**
|
|
42
|
+
* planegcs WASM implementation of the `SketchSolver` interface (M3.2–M3.4):
|
|
43
|
+
* pushes geometry/constraints into the GCS, solves, and pulls the solved
|
|
44
|
+
* parameters back into `SketchGeom` values.
|
|
45
|
+
*/
|
|
46
|
+
export class PlanegcsSolver {
|
|
47
|
+
wrapper;
|
|
48
|
+
constructor(wrapper) {
|
|
49
|
+
this.wrapper = wrapper;
|
|
50
|
+
}
|
|
51
|
+
async solve(geoms, constraints, external) {
|
|
52
|
+
const externalMap = new Map((external ?? []).map((e) => [e.geoId, e]));
|
|
53
|
+
// a constraint referencing an unresolvable external geoId is dropped
|
|
54
|
+
const unresolvableExternal = new Set();
|
|
55
|
+
for (const c of constraints) {
|
|
56
|
+
for (const r of c.refs) {
|
|
57
|
+
if (r.geoId <= -3 && r.geoId > -2000 && !externalMap.has(r.geoId)) {
|
|
58
|
+
unresolvableExternal.add(c.index);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const usableConstraints = constraints.filter((c) => !unresolvableExternal.has(c.index));
|
|
63
|
+
// P3-2 (2026-09-24): constraint types that do NOT affect the solved
|
|
64
|
+
// geometry are dropped-and-recorded instead of failing the whole sketch
|
|
65
|
+
// (previously any of these baked the sketch as unsupported-constraint):
|
|
66
|
+
// - 15 InternalAlignment: alignment bookkeeping between element pairs;
|
|
67
|
+
// the aligned geometry itself is already in <Geometry> — the alignment
|
|
68
|
+
// constraint adds no independent DoF removal for our solving purpose.
|
|
69
|
+
// - 17 Block: freezes a geometry's current position — the stored
|
|
70
|
+
// geometry IS that position, so dropping it changes nothing.
|
|
71
|
+
// - 19 Weight: B-Spline control-point weight — we do not solve splines.
|
|
72
|
+
const ignorable = constraints.filter((c) => c.type === 15 || c.type === 17 || c.type === 19).map((c) => c.index);
|
|
73
|
+
const ignorableSet = new Set(ignorable);
|
|
74
|
+
const unsupported = usableConstraints.find((c) => !SUPPORTED_CONSTRAINT_TYPES.has(c.type) && !ignorableSet.has(c.index));
|
|
75
|
+
if (unsupported) {
|
|
76
|
+
return ok({
|
|
77
|
+
geoms,
|
|
78
|
+
converged: false,
|
|
79
|
+
reason: 'unsupported-constraint',
|
|
80
|
+
problemConstraints: [unsupported.index],
|
|
81
|
+
droppedConstraints: [...unresolvableExternal, ...ignorable],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const w = this.wrapper;
|
|
85
|
+
w.clear_data();
|
|
86
|
+
// --- M3.3: implicit fixed frame (root point + H/V axes) ---
|
|
87
|
+
w.push_primitive({ type: 'point', id: P(-1, 1), x: 0, y: 0, fixed: true });
|
|
88
|
+
// GOTCHA (probe-sym-min.ts / probe-l1-ablation.ts): FreeCAD models HAxis/
|
|
89
|
+
// VAxis as real Line entries in Geoms (geoId -1/-2), so constraints may
|
|
90
|
+
// reference axis points (pos 1/2) or the axis edge (pos 0) — e.g.
|
|
91
|
+
// DistanceX(line-point, VAxis) fixes an x coordinate. planegcs has no
|
|
92
|
+
// implicit axes, so we materialize both axes as fixed point+line pairs
|
|
93
|
+
// and register them in `lines` (below) so all constraint shapes see them.
|
|
94
|
+
w.push_primitive({ type: 'point', id: P(-1, 2), x: 1, y: 0, fixed: true });
|
|
95
|
+
w.push_primitive({ type: 'point', id: P(-2, 1), x: 0, y: 0, fixed: true });
|
|
96
|
+
w.push_primitive({ type: 'point', id: P(-2, 2), x: 0, y: 1, fixed: true });
|
|
97
|
+
w.push_primitive({ type: 'line', id: 'L-1', p1_id: P(-1, 1), p2_id: P(-1, 2) });
|
|
98
|
+
w.push_primitive({ type: 'line', id: 'L-2', p1_id: P(-2, 1), p2_id: P(-2, 2) });
|
|
99
|
+
// --- M6.3: external fixed geometry (geoId -3, -4, ... in link order) ---
|
|
100
|
+
const externalLines = new Map();
|
|
101
|
+
for (const ext of external ?? []) {
|
|
102
|
+
if (ext.polyline.length === 2) {
|
|
103
|
+
// straight segment: two fixed endpoints + fixed line
|
|
104
|
+
const p1 = P(ext.geoId, 1);
|
|
105
|
+
const p2 = P(ext.geoId, 2);
|
|
106
|
+
w.push_primitive({ type: 'point', id: p1, x: ext.polyline[0][0], y: ext.polyline[0][1], fixed: true });
|
|
107
|
+
w.push_primitive({ type: 'point', id: p2, x: ext.polyline[1][0], y: ext.polyline[1][1], fixed: true });
|
|
108
|
+
w.push_primitive({ type: 'line', id: `X${ext.geoId}`, p1_id: p1, p2_id: p2 });
|
|
109
|
+
externalLines.set(ext.geoId, { p1, p2 });
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// multi-point polyline: pin sampled points as fixed point targets
|
|
113
|
+
ext.polyline.forEach((pt, i) => {
|
|
114
|
+
w.push_primitive({
|
|
115
|
+
type: 'point', id: P(ext.geoId, i + 1), x: pt[0], y: pt[1], fixed: true,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// --- M3.3: geometry → primitives ---
|
|
121
|
+
// Track which point keys exist as standalone points vs derived from lines.
|
|
122
|
+
const lines = new Map();
|
|
123
|
+
const arcs = new Map();
|
|
124
|
+
const circles = new Map();
|
|
125
|
+
const ellipses = new Map();
|
|
126
|
+
const standalone = new Map();
|
|
127
|
+
// implicit axes are first-class lines for constraint resolution (see
|
|
128
|
+
// the GOTCHA block above): geoId -1 = HAxis, -2 = VAxis
|
|
129
|
+
lines.set(-1, { p1: P(-1, 1), p2: P(-1, 2) });
|
|
130
|
+
lines.set(-2, { p1: P(-2, 1), p2: P(-2, 2) });
|
|
131
|
+
// pass 1: standalone points that are constraint anchors
|
|
132
|
+
const anchorNeedsStandalone = new Set();
|
|
133
|
+
for (const c of constraints) {
|
|
134
|
+
for (const r of c.refs) {
|
|
135
|
+
if (r.geoId >= 0 && r.pos !== PointPos.none)
|
|
136
|
+
anchorNeedsStandalone.add(P(r.geoId, r.pos));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
for (const g of geoms) {
|
|
140
|
+
switch (g.kind) {
|
|
141
|
+
case 'point': {
|
|
142
|
+
const id = P(g.index, 1);
|
|
143
|
+
standalone.set(g.index, id);
|
|
144
|
+
w.push_primitive({ type: 'point', id, x: g.x, y: g.y, fixed: false });
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
default:
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// pass 2: curves; create endpoint points for lines/arcs, center points
|
|
152
|
+
for (const g of geoms) {
|
|
153
|
+
switch (g.kind) {
|
|
154
|
+
case 'line': {
|
|
155
|
+
const p1 = anchorNeedsStandalone.has(P(g.index, 1)) ? P(g.index, 1) : P(g.index, 1);
|
|
156
|
+
const p2 = P(g.index, 2);
|
|
157
|
+
// create endpoint points as real GCS points, then a line over them
|
|
158
|
+
for (const [id, x, y] of [
|
|
159
|
+
[p1, g.x1, g.y1],
|
|
160
|
+
[p2, g.x2, g.y2],
|
|
161
|
+
]) {
|
|
162
|
+
w.push_primitive({ type: 'point', id, x, y, fixed: false });
|
|
163
|
+
}
|
|
164
|
+
w.push_primitive({ type: 'line', id: `L${g.index}`, p1_id: p1, p2_id: p2 });
|
|
165
|
+
lines.set(g.index, { p1, p2 });
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'circle': {
|
|
169
|
+
const c = P(g.index, 3);
|
|
170
|
+
w.push_primitive({ type: 'point', id: c, x: g.cx, y: g.cy, fixed: false });
|
|
171
|
+
w.push_primitive({ type: 'circle', id: `C${g.index}`, c_id: c, radius: g.radius });
|
|
172
|
+
circles.set(g.index, { center: c });
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'arc': {
|
|
176
|
+
const c = P(g.index, 3);
|
|
177
|
+
const s = P(g.index, 1);
|
|
178
|
+
const e = P(g.index, 2);
|
|
179
|
+
for (const [id, x, y] of [
|
|
180
|
+
[c, g.cx, g.cy],
|
|
181
|
+
[s, g.x1, g.y1],
|
|
182
|
+
[e, g.x2, g.y2],
|
|
183
|
+
]) {
|
|
184
|
+
w.push_primitive({ type: 'point', id, x, y, fixed: false });
|
|
185
|
+
}
|
|
186
|
+
w.push_primitive({
|
|
187
|
+
type: 'arc', id: `A${g.index}`, c_id: c, start_id: s, end_id: e,
|
|
188
|
+
start_angle: g.startAngle, end_angle: g.endAngle, radius: g.radius,
|
|
189
|
+
});
|
|
190
|
+
arcs.set(g.index, { center: c, start: s, end: e });
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case 'ellipse': {
|
|
194
|
+
const c = P(g.index, 3);
|
|
195
|
+
const f1 = P(g.index, 1);
|
|
196
|
+
for (const [id, x, y] of [
|
|
197
|
+
[c, g.cx, g.cy],
|
|
198
|
+
[f1, g.fx1, g.fy1],
|
|
199
|
+
]) {
|
|
200
|
+
w.push_primitive({ type: 'point', id, x, y, fixed: false });
|
|
201
|
+
}
|
|
202
|
+
w.push_primitive({
|
|
203
|
+
type: 'ellipse', id: `E${g.index}`, c_id: c, focus1_id: f1, radmin: g.minorRadius,
|
|
204
|
+
});
|
|
205
|
+
ellipses.set(g.index, { center: c, focus1: f1 });
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case 'bspline': {
|
|
209
|
+
// P4: the spline itself is not pushed to GCS (planegcs has no b-spline
|
|
210
|
+
// primitive); its exact endpoints participate as a line proxy so
|
|
211
|
+
// coincident/dimension constraints on the curve ends still solve.
|
|
212
|
+
// Interior shape is preserved at contour extraction (de Boor sampling).
|
|
213
|
+
const p1 = P(g.index, 1);
|
|
214
|
+
const p2 = P(g.index, 2);
|
|
215
|
+
for (const [id, x, y] of [
|
|
216
|
+
[p1, g.x1, g.y1],
|
|
217
|
+
[p2, g.x2, g.y2],
|
|
218
|
+
]) {
|
|
219
|
+
w.push_primitive({ type: 'point', id, x, y, fixed: false });
|
|
220
|
+
}
|
|
221
|
+
w.push_primitive({ type: 'line', id: `L${g.index}`, p1_id: p1, p2_id: p2 });
|
|
222
|
+
lines.set(g.index, { p1, p2 });
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case 'point':
|
|
226
|
+
break; // handled in pass 1
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// --- M3.4: constraints → primitives ---
|
|
230
|
+
// A constraint whose GCS shape cannot be pushed is dropped and recorded
|
|
231
|
+
// (D3: no silent loss — surfaced via droppedConstraints).
|
|
232
|
+
const droppedConstraints = [...unresolvableExternal, ...ignorable];
|
|
233
|
+
for (const c of usableConstraints) {
|
|
234
|
+
// P3-2: ignorable types (15/17/19) were recorded above; never pushed.
|
|
235
|
+
if (ignorableSet.has(c.index))
|
|
236
|
+
continue;
|
|
237
|
+
let prims;
|
|
238
|
+
try {
|
|
239
|
+
prims = this.constraintToPrimitives(c, { lines, arcs, circles, ellipses, standalone, externalLines });
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
droppedConstraints.push(c.index);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
for (const p of prims)
|
|
247
|
+
w.push_primitive(p);
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
droppedConstraints.push(c.index);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// --- solve (D2: initial values = on-disk coordinates) ---
|
|
255
|
+
const status = w.solve(Algorithm.LevenbergMarquardt);
|
|
256
|
+
const conflicting = w.has_gcs_conflicting_constraints();
|
|
257
|
+
const redundant = w.has_gcs_redundant_constraints();
|
|
258
|
+
if (status !== 0 || conflicting) {
|
|
259
|
+
const problem = conflicting ? w.get_gcs_conflicting_constraints() : w.get_gcs_redundant_constraints();
|
|
260
|
+
const problemConstraints = problem
|
|
261
|
+
.map((s) => Number(s.replace(/^c/, '')))
|
|
262
|
+
.filter((n) => Number.isFinite(n));
|
|
263
|
+
return ok({
|
|
264
|
+
geoms,
|
|
265
|
+
converged: false,
|
|
266
|
+
reason: conflicting ? 'conflicting' : redundant ? 'redundant' : 'failed',
|
|
267
|
+
problemConstraints,
|
|
268
|
+
droppedConstraints,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
w.apply_solution();
|
|
272
|
+
// --- pull back solved coordinates and rebuild geometry ---
|
|
273
|
+
const solved = this.pullBack(geoms, { lines, arcs, circles, ellipses, standalone });
|
|
274
|
+
return ok({ geoms: solved, converged: true, problemConstraints: [], droppedConstraints });
|
|
275
|
+
}
|
|
276
|
+
constraintToPrimitives(c, ctx) {
|
|
277
|
+
const out = [];
|
|
278
|
+
const id = `c${c.index}`;
|
|
279
|
+
const r = c.refs;
|
|
280
|
+
// guards: old-format fallback may yield < 2 refs for pair constraints
|
|
281
|
+
if (r.length < 1)
|
|
282
|
+
return out;
|
|
283
|
+
const pt = (ref) => {
|
|
284
|
+
if (!ref)
|
|
285
|
+
return undefined;
|
|
286
|
+
if (ref.geoId === -1)
|
|
287
|
+
return P(-1, 1); // root point
|
|
288
|
+
if (ref.geoId === -2)
|
|
289
|
+
return P(-2, ref.pos === 0 ? 1 : ref.pos); // VAxis point
|
|
290
|
+
if (ref.geoId <= -3 && ref.geoId > -2000) {
|
|
291
|
+
// M6.3 external geometry: endpoint/point refs onto fixed segments
|
|
292
|
+
const ext = ctx.externalLines.get(ref.geoId);
|
|
293
|
+
if (!ext)
|
|
294
|
+
return undefined;
|
|
295
|
+
if (ref.pos === 1)
|
|
296
|
+
return ext.p1;
|
|
297
|
+
if (ref.pos === 2)
|
|
298
|
+
return ext.p2;
|
|
299
|
+
return undefined; // pos=0 (edge itself) handled by line-typed paths
|
|
300
|
+
}
|
|
301
|
+
if (ref.geoId >= 0)
|
|
302
|
+
return P(ref.geoId, ref.pos);
|
|
303
|
+
return undefined;
|
|
304
|
+
};
|
|
305
|
+
switch (c.type) {
|
|
306
|
+
case ConstraintType.Coincident: {
|
|
307
|
+
const p1 = pt(r[0]);
|
|
308
|
+
const p2 = pt(r[1]);
|
|
309
|
+
if (p1 && p2)
|
|
310
|
+
out.push({ type: 'p2p_coincident', id, p1_id: p1, p2_id: p2 });
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
case ConstraintType.Horizontal: {
|
|
314
|
+
// Horizontal with pos=0 refs is the HAxis case: line horizontal
|
|
315
|
+
if (r[0].pos === PointPos.none || r.length >= 1 && r[0].pos !== PointPos.mid) {
|
|
316
|
+
if (r.length === 1) {
|
|
317
|
+
// horizontal via axis: geoId is the line itself
|
|
318
|
+
const line = ctx.lines.get(r[0].geoId);
|
|
319
|
+
if (line)
|
|
320
|
+
out.push({ type: 'horizontal_l', id, l_id: line ? `L${r[0].geoId}` : '' });
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
const p1 = pt(r[0]);
|
|
324
|
+
const p2 = pt(r[1]);
|
|
325
|
+
if (p1 && p2)
|
|
326
|
+
out.push({ type: 'horizontal_pp', id, p1_id: p1, p2_id: p2 });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
case ConstraintType.Vertical: {
|
|
332
|
+
if (r.length === 1) {
|
|
333
|
+
const gid = `L${r[0].geoId}`;
|
|
334
|
+
if (ctx.lines.has(r[0].geoId))
|
|
335
|
+
out.push({ type: 'vertical_l', id, l_id: gid });
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
const p1 = pt(r[0]);
|
|
339
|
+
const p2 = pt(r[1]);
|
|
340
|
+
if (p1 && p2)
|
|
341
|
+
out.push({ type: 'vertical_pp', id, p1_id: p1, p2_id: p2 });
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
case ConstraintType.Parallel: {
|
|
346
|
+
if (r.length === 2 && ctx.lines.has(r[0].geoId) && ctx.lines.has(r[1].geoId)) {
|
|
347
|
+
out.push({
|
|
348
|
+
type: 'parallel', id,
|
|
349
|
+
l1_id: `L${r[0].geoId}`, l2_id: `L${r[1].geoId}`,
|
|
350
|
+
internalalignment: 0,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case ConstraintType.Perpendicular: {
|
|
356
|
+
if (r.length === 2 && ctx.lines.has(r[0].geoId) && ctx.lines.has(r[1].geoId)) {
|
|
357
|
+
out.push({ type: 'perpendicular_ll', id, l1_id: `L${r[0].geoId}`, l2_id: `L${r[1].geoId}` });
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case ConstraintType.Distance: {
|
|
362
|
+
// p2p / p2l / p2c / c2c variants by ref shapes.
|
|
363
|
+
// GOTCHA (probe-l1-ablation.ts): a second ref that is an AXIS EDGE
|
|
364
|
+
// (geoId -1/-2, pos=0) is a point-to-LINE distance against the axis
|
|
365
|
+
// (FreeCAD Sketch.cpp addDistanceConstraint(point, line)) — e.g.
|
|
366
|
+
// Distance(p, HAxis) = |y|. Resolving the axis edge to the root point
|
|
367
|
+
// and emitting p2p_distance instead drags the point onto a circle
|
|
368
|
+
// around the origin (taperedballnose collapse, delta 2.3e1).
|
|
369
|
+
const p1 = pt(r[0]);
|
|
370
|
+
const axisEdgeDist = r.length >= 2 && r[1].pos === PointPos.none && (r[1].geoId === -1 || r[1].geoId === -2);
|
|
371
|
+
if (axisEdgeDist && p1) {
|
|
372
|
+
out.push({ type: 'p2l_distance', id, p_id: p1, l_id: `L${r[1].geoId}`, distance: c.value });
|
|
373
|
+
}
|
|
374
|
+
else if (p1 && r[1] && r[1].pos === PointPos.none && ctx.lines.has(r[1].geoId)) {
|
|
375
|
+
out.push({ type: 'p2l_distance', id, p_id: p1, l_id: `L${r[1].geoId}`, distance: c.value });
|
|
376
|
+
}
|
|
377
|
+
else if (p1 && r[1]) {
|
|
378
|
+
const p2 = pt(r[1]);
|
|
379
|
+
if (p2)
|
|
380
|
+
out.push({ type: 'p2p_distance', id, p1_id: p1, p2_id: p2, distance: c.value });
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
case ConstraintType.DistanceX: {
|
|
385
|
+
// planegcs 'difference' semantics: param2 - param1 = difference
|
|
386
|
+
// (verified empirically). FCStd DistanceX value = second.x - first.x.
|
|
387
|
+
// GOTCHA (probe-l1-ablation.ts, Sketch.cpp:2053): when the second ref
|
|
388
|
+
// is an AXIS EDGE (geoId -1/-2, pos=0) FreeCAD treats it as GeoUndef —
|
|
389
|
+
// an ABSOLUTE coordinate constraint (coordinate_x), not a difference
|
|
390
|
+
// against the axis point. Mapping it to 'difference' forces x = -value
|
|
391
|
+
// and produced the taperedballnose wrong solution (delta 2.3e1).
|
|
392
|
+
const axisEdge = r.length >= 2 && r[1].pos === PointPos.none && (r[1].geoId === -1 || r[1].geoId === -2);
|
|
393
|
+
const firstIsPoint = r[0].pos !== PointPos.none || r[0].geoId < 0;
|
|
394
|
+
if (axisEdge && firstIsPoint) {
|
|
395
|
+
const p = pt(r[0]);
|
|
396
|
+
if (p)
|
|
397
|
+
out.push({ type: 'coordinate_x', id, p_id: p, x: c.value });
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
const p1 = pt(r[0]);
|
|
401
|
+
const p2 = pt(r[1]);
|
|
402
|
+
if (p1 && p2) {
|
|
403
|
+
out.push({
|
|
404
|
+
type: 'difference',
|
|
405
|
+
id,
|
|
406
|
+
param1: { o_id: p1, prop: 'x' },
|
|
407
|
+
param2: { o_id: p2, prop: 'x' },
|
|
408
|
+
difference: c.value,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case ConstraintType.DistanceY: {
|
|
414
|
+
// symmetric case to DistanceX: axis-edge second ref → absolute y
|
|
415
|
+
const axisEdge = r.length >= 2 && r[1].pos === PointPos.none && (r[1].geoId === -1 || r[1].geoId === -2);
|
|
416
|
+
const firstIsPoint = r[0].pos !== PointPos.none || r[0].geoId < 0;
|
|
417
|
+
if (axisEdge && firstIsPoint) {
|
|
418
|
+
const p = pt(r[0]);
|
|
419
|
+
if (p)
|
|
420
|
+
out.push({ type: 'coordinate_y', id, p_id: p, y: c.value });
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
const p1 = pt(r[0]);
|
|
424
|
+
const p2 = pt(r[1]);
|
|
425
|
+
if (p1 && p2) {
|
|
426
|
+
out.push({
|
|
427
|
+
type: 'difference',
|
|
428
|
+
id,
|
|
429
|
+
param1: { o_id: p1, prop: 'y' },
|
|
430
|
+
param2: { o_id: p2, prop: 'y' },
|
|
431
|
+
difference: c.value,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
case ConstraintType.Angle: {
|
|
437
|
+
// FreeCAD Sketch.cpp case Angle — 4 shapes by ref count/pos:
|
|
438
|
+
// ① Third != GeoUndef → angle-via-point (not needed by corpus P0 set;
|
|
439
|
+
// dropped explicitly below if encountered)
|
|
440
|
+
// ② SecondPos != none → l2l_angle_pppp (lines with explicit start points:
|
|
441
|
+
// pos==start keeps direction, pos==end swaps the point pair)
|
|
442
|
+
// ③ Second set, both pos none → l2l_angle_ll (line-level)
|
|
443
|
+
// ④ only First → p2p_angle on the line's own points (orientation)
|
|
444
|
+
// refs may carry a Third = -2000 (GeoUndef sentinel) — filter it.
|
|
445
|
+
const refs = r.filter((x) => x.geoId !== -2000);
|
|
446
|
+
if (refs.length >= 3) {
|
|
447
|
+
// ① via-point angle: not in the P0 mapping set — record as dropped
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
if (refs.length === 2) {
|
|
451
|
+
const [a, b] = refs;
|
|
452
|
+
const lineA = ctx.lines.has(a.geoId);
|
|
453
|
+
const lineB = ctx.lines.has(b.geoId);
|
|
454
|
+
if (lineA && lineB && a.pos !== PointPos.none && b.pos !== PointPos.none) {
|
|
455
|
+
// ② explicit start points; pos=end reverses that line's direction
|
|
456
|
+
const a1 = a.pos === PointPos.start ? P(a.geoId, 1) : P(a.geoId, 2);
|
|
457
|
+
const a2 = a.pos === PointPos.start ? P(a.geoId, 2) : P(a.geoId, 1);
|
|
458
|
+
const b1 = b.pos === PointPos.start ? P(b.geoId, 1) : P(b.geoId, 2);
|
|
459
|
+
const b2 = b.pos === PointPos.start ? P(b.geoId, 2) : P(b.geoId, 1);
|
|
460
|
+
out.push({ type: 'l2l_angle_pppp', id, l1p1_id: a1, l1p2_id: a2, l2p1_id: b1, l2p2_id: b2, angle: c.value });
|
|
461
|
+
}
|
|
462
|
+
else if (lineA && lineB) {
|
|
463
|
+
// ③ line-level angle
|
|
464
|
+
out.push({
|
|
465
|
+
type: 'l2l_angle_ll', id,
|
|
466
|
+
l1_id: `L${a.geoId}`, l2_id: `L${b.geoId}`,
|
|
467
|
+
angle: c.value,
|
|
468
|
+
internalalignment: 0,
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
else if (refs.length === 1 && ctx.lines.has(refs[0].geoId)) {
|
|
473
|
+
// ④ orientation angle of a single line (p2p_angle on its points)
|
|
474
|
+
out.push({ type: 'p2p_angle', id, p1_id: P(refs[0].geoId, 1), p2_id: P(refs[0].geoId, 2), angle: c.value });
|
|
475
|
+
}
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
case ConstraintType.Radius: {
|
|
479
|
+
if (ctx.circles.has(r[0].geoId)) {
|
|
480
|
+
out.push({ type: 'circle_radius', id, c_id: `C${r[0].geoId}`, radius: c.value });
|
|
481
|
+
}
|
|
482
|
+
else if (ctx.arcs.has(r[0].geoId)) {
|
|
483
|
+
out.push({ type: 'arc_radius', id, a_id: `A${r[0].geoId}`, radius: c.value });
|
|
484
|
+
}
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
case ConstraintType.Diameter: {
|
|
488
|
+
if (ctx.circles.has(r[0].geoId)) {
|
|
489
|
+
out.push({ type: 'circle_diameter', id, c_id: `C${r[0].geoId}`, diameter: c.value });
|
|
490
|
+
}
|
|
491
|
+
else if (ctx.arcs.has(r[0].geoId)) {
|
|
492
|
+
out.push({ type: 'arc_diameter', id, a_id: `A${r[0].geoId}`, diameter: c.value });
|
|
493
|
+
}
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
case ConstraintType.Equal: {
|
|
497
|
+
// equal_length for line pairs; equal_radii for circle/arc pairs
|
|
498
|
+
if (r.length === 2) {
|
|
499
|
+
const g0 = r[0].geoId;
|
|
500
|
+
const g1 = r[1].geoId;
|
|
501
|
+
if (ctx.lines.has(g0) && ctx.lines.has(g1)) {
|
|
502
|
+
out.push({ type: 'equal_length', id, l1_id: `L${g0}`, l2_id: `L${g1}` });
|
|
503
|
+
}
|
|
504
|
+
else if ((ctx.circles.has(g0) || ctx.arcs.has(g0)) && (ctx.circles.has(g1) || ctx.arcs.has(g1))) {
|
|
505
|
+
// arcs use their own primitive id (A{n}); circles use C{n}
|
|
506
|
+
const id0 = ctx.circles.has(g0) ? `C${g0}` : `A${g0}`;
|
|
507
|
+
const id1 = ctx.circles.has(g1) ? `C${g1}` : `A${g1}`;
|
|
508
|
+
out.push({ type: 'equal_radius_cc', id, c1_id: id0, c2_id: id1 });
|
|
509
|
+
}
|
|
510
|
+
else if (ctx.ellipses.has(g0) && ctx.ellipses.has(g1)) {
|
|
511
|
+
out.push({ type: 'equal_radii_ee', id, e1_id: `E${g0}`, e2_id: `E${g1}` });
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
case ConstraintType.PointOnObject: {
|
|
517
|
+
const p = pt(r[0]);
|
|
518
|
+
if (p && r[1].pos === PointPos.none) {
|
|
519
|
+
const onGeoId = r[1].geoId;
|
|
520
|
+
if (ctx.lines.has(onGeoId)) {
|
|
521
|
+
out.push({ type: 'point_on_line_pl', id, p_id: p, l_id: `L${onGeoId}` });
|
|
522
|
+
}
|
|
523
|
+
else if (ctx.externalLines.has(onGeoId)) {
|
|
524
|
+
// M6.3: point-on-external-edge → point_on_line against the fixed line
|
|
525
|
+
out.push({ type: 'point_on_line_pl', id, p_id: p, l_id: `X${onGeoId}` });
|
|
526
|
+
}
|
|
527
|
+
else if (ctx.circles.has(onGeoId)) {
|
|
528
|
+
out.push({ type: 'point_on_circle', id, p_id: p, c_id: `C${onGeoId}` });
|
|
529
|
+
}
|
|
530
|
+
else if (ctx.arcs.has(onGeoId)) {
|
|
531
|
+
out.push({ type: 'point_on_arc', id, p_id: p, a_id: `A${onGeoId}` });
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
case ConstraintType.Symmetric: {
|
|
537
|
+
// Symmetric(p1, p2, line) or Symmetric(p1, p2, p3)
|
|
538
|
+
const p1 = pt(r[0]);
|
|
539
|
+
const p2 = pt(r[1]);
|
|
540
|
+
if (p1 && p2) {
|
|
541
|
+
if (r.length >= 3 && r[2].pos === PointPos.none && ctx.lines.has(r[2].geoId)) {
|
|
542
|
+
out.push({ type: 'p2p_symmetric_ppl', id, p1_id: p1, p2_id: p2, l_id: `L${r[2].geoId}` });
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
const p3 = pt(r[2]);
|
|
546
|
+
if (p3)
|
|
547
|
+
out.push({ type: 'p2p_symmetric_ppp', id, p1_id: p1, p2_id: p2, p_id: p3 });
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
case ConstraintType.Tangent: {
|
|
553
|
+
// line-circle / circle-circle / line-arc / arc-arc (edge-level, pos=0)
|
|
554
|
+
const a = r[0];
|
|
555
|
+
const b = r[1];
|
|
556
|
+
if (!a || !b)
|
|
557
|
+
break;
|
|
558
|
+
const lineA = ctx.lines.has(a.geoId);
|
|
559
|
+
const circA = ctx.circles.has(a.geoId);
|
|
560
|
+
const arcA = ctx.arcs.has(a.geoId);
|
|
561
|
+
const lineB = ctx.lines.has(b.geoId);
|
|
562
|
+
const circB = ctx.circles.has(b.geoId);
|
|
563
|
+
const arcB = ctx.arcs.has(b.geoId);
|
|
564
|
+
if (lineA && circB)
|
|
565
|
+
out.push({ type: 'tangent_lc', id, l_id: `L${a.geoId}`, c_id: `C${b.geoId}`, internalalignment: 0 });
|
|
566
|
+
else if (circA && lineB)
|
|
567
|
+
out.push({ type: 'tangent_lc', id, l_id: `L${b.geoId}`, c_id: `C${a.geoId}`, internalalignment: 0 });
|
|
568
|
+
else if (circA && circB)
|
|
569
|
+
out.push({ type: 'tangent_cc', id, c1_id: `C${a.geoId}`, c2_id: `C${b.geoId}`, internalalignment: 0 });
|
|
570
|
+
else if (lineA && arcB)
|
|
571
|
+
out.push({ type: 'tangent_la', id, l_id: `L${a.geoId}`, a_id: `A${b.geoId}`, internalalignment: 0 });
|
|
572
|
+
else if (arcA && lineB)
|
|
573
|
+
out.push({ type: 'tangent_la', id, l_id: `L${b.geoId}`, a_id: `A${a.geoId}`, internalalignment: 0 });
|
|
574
|
+
else if (arcA && arcB)
|
|
575
|
+
out.push({ type: 'tangent_aa', id, a1_id: `A${a.geoId}`, a2_id: `A${b.geoId}`, internalalignment: 0 });
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
default:
|
|
579
|
+
break; // unsupported types never reach here (guard in solve())
|
|
580
|
+
}
|
|
581
|
+
return out;
|
|
582
|
+
}
|
|
583
|
+
pullBack(geoms, ctx) {
|
|
584
|
+
// After apply_solution, re-derive geometry from the wrapper's primitive
|
|
585
|
+
// state via pull through the wrapper's sketch index.
|
|
586
|
+
const out = [];
|
|
587
|
+
for (const g of geoms) {
|
|
588
|
+
switch (g.kind) {
|
|
589
|
+
case 'point': {
|
|
590
|
+
const key = ctx.standalone.get(g.index);
|
|
591
|
+
const pt = key ? this.readPoint(key) : undefined;
|
|
592
|
+
out.push(pt ? { ...g, x: pt.x, y: pt.y } : g);
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case 'line': {
|
|
596
|
+
const l = ctx.lines.get(g.index);
|
|
597
|
+
const p1 = this.readPoint(l.p1);
|
|
598
|
+
const p2 = this.readPoint(l.p2);
|
|
599
|
+
out.push(p1 && p2 ? { ...g, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y } : g);
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
case 'circle': {
|
|
603
|
+
const cc = ctx.circles.get(g.index).center;
|
|
604
|
+
const c = this.readPoint(cc);
|
|
605
|
+
const rad = this.readCircleRadius(g.index);
|
|
606
|
+
out.push(c && rad !== undefined ? { ...g, cx: c.x, cy: c.y, radius: rad } : g);
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
case 'arc': {
|
|
610
|
+
const a = ctx.arcs.get(g.index);
|
|
611
|
+
const c = this.readPoint(a.center);
|
|
612
|
+
const s = this.readPoint(a.start);
|
|
613
|
+
const e = this.readPoint(a.end);
|
|
614
|
+
if (c && s && e) {
|
|
615
|
+
const r = Math.hypot(s.x - c.x, s.y - c.y);
|
|
616
|
+
const startAngle = Math.atan2(s.y - c.y, s.x - c.x);
|
|
617
|
+
const endAngle = Math.atan2(e.y - c.y, e.x - c.x);
|
|
618
|
+
out.push({ ...g, cx: c.x, cy: c.y, radius: r, startAngle, endAngle, x1: s.x, y1: s.y, x2: e.x, y2: e.y });
|
|
619
|
+
}
|
|
620
|
+
else
|
|
621
|
+
out.push(g);
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
case 'bspline': {
|
|
625
|
+
// P4: spline endpoints ride the line proxy; pull their solved coords.
|
|
626
|
+
const l = ctx.lines.get(g.index);
|
|
627
|
+
const p1 = this.readPoint(l.p1);
|
|
628
|
+
const p2 = this.readPoint(l.p2);
|
|
629
|
+
out.push(p1 && p2 ? { ...g, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y } : g);
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
case 'ellipse': {
|
|
633
|
+
const el = ctx.ellipses.get(g.index);
|
|
634
|
+
const c = this.readPoint(el.center);
|
|
635
|
+
const f1 = this.readPoint(el.focus1);
|
|
636
|
+
if (c && f1) {
|
|
637
|
+
const major = Math.hypot(f1.x - c.x, f1.y - c.y) * 1;
|
|
638
|
+
const angleXU = Math.atan2(f1.y - c.y, f1.x - c.x);
|
|
639
|
+
// minor radius is a parameter on the ellipse primitive; approximate
|
|
640
|
+
// pull via ratio is not exposed — keep stored minor (M3 scope).
|
|
641
|
+
out.push({ ...g, cx: c.x, cy: c.y, majorRadius: g.majorRadius * (major / g.majorRadius), angleXU });
|
|
642
|
+
}
|
|
643
|
+
else
|
|
644
|
+
out.push(g);
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return out;
|
|
650
|
+
}
|
|
651
|
+
/** Read a point's solved coordinates from the sketch index.
|
|
652
|
+
* apply_solution() already pulled every primitive's solved values back into
|
|
653
|
+
* sketch_index (gcs_wrapper.js:104-108), so get_primitive is authoritative. */
|
|
654
|
+
readPoint(key) {
|
|
655
|
+
const p = this.wrapper.sketch_index.get_primitive(key);
|
|
656
|
+
if (!p || typeof p.x !== 'number' || typeof p.y !== 'number')
|
|
657
|
+
return undefined;
|
|
658
|
+
return { x: p.x, y: p.y };
|
|
659
|
+
}
|
|
660
|
+
readCircleRadius(geoIndex) {
|
|
661
|
+
const c = this.wrapper.sketch_index.get_primitive(`C${geoIndex}`);
|
|
662
|
+
return typeof c?.radius === 'number' ? c.radius : undefined;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
//# sourceMappingURL=planegcs-backend.js.map
|