Compare commits

..

61 Commits

Author SHA1 Message Date
forbes
d5c2887f5a fix(assembly): update solver submodule — enforce drag quaternion continuity (#338)
Points solver to fix/drag-quat-continuity (solver#40) which fixes
the planar constraint drift during interactive drag by enforcing
quaternion continuity on dragged parts and detecting branch jumps
beyond simple hemisphere negation.
2026-02-27 09:37:42 -06:00
forbes
983e211f12 chore: update solver submodule — add planar drag console test (#338)
Some checks failed
Build and Test / build (pull_request) Has been cancelled
2026-02-27 09:31:31 -06:00
forbes
1788b5778a Add CLAUDE.md to .gitignore
All checks were successful
Build and Test / build (push) Successful in 29m54s
2026-02-27 08:19:52 -06:00
418e947cbd Merge pull request 'fix(solver): world-anchored reference normal prevents planar axial drift' (#336) from fix/planar-halfspace-drag-flip into main
All checks were successful
Build and Test / build (push) Successful in 29m17s
Sync Silo Server Docs / sync (push) Successful in 34s
Reviewed-on: #336
2026-02-26 17:10:39 +00:00
4c9ff957e3 Merge branch 'main' into fix/planar-halfspace-drag-flip
All checks were successful
Build and Test / build (pull_request) Successful in 28m48s
2026-02-26 17:10:27 +00:00
9aaf244179 fix(solver): update solver submodule — world-anchored planar reference normal
All checks were successful
Build and Test / build (pull_request) Successful in 29m11s
Updates solver to include the fix for PlanarConstraint axial drift
when combined with CylindricalConstraint. The distance residual now
uses a world-frame reference normal (Const nodes) instead of the
body-attached normal that rotates with the body.
2026-02-26 11:07:06 -06:00
69ccdbf742 Merge pull request 'fix(assembly): use instance suffixes for duplicate part labels' (#335) from fix/assembly-part-number-suffix into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #335
2026-02-26 16:34:34 +00:00
4acd09171e Merge pull request 'fix(theme): prevent panel element headings from being clipped' (#334) from fix/theme-panel-heading-clipping into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #334
2026-02-26 16:34:17 +00:00
7f909f166f Merge pull request 'fix(ci): add target_commitish to release payload to fix HTTP 500' (#333) from fix/ci-release-target-commitish into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #333
2026-02-26 16:34:04 +00:00
f69e0efec7 Merge pull request 'docs: add CLAUDE.md for developer context' (#332) from docs/claude-md into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #332
2026-02-26 16:33:36 +00:00
forbes
559a240799 fix(assembly): update solver submodule — fix planar half-space drag flip
Some checks failed
Build and Test / build (pull_request) Has been cancelled
Updates mods/solver to include fix for the planar half-space correction
that caused 'flipped orientation' rejections when dragging a body
connected by a Cylindrical joint + distance=0 Planar constraint.

The solver's PlanarConstraint half-space tracker was reflecting the body
through the plane when the face normal dot product crossed zero during
legitimate rotation about the cylindrical axis. Now returns a tracking-
only HalfSpace (no correction) for on-plane constraints, matching the
pattern used by Cylindrical/Revolute/Concentric trackers.

See: kindred/solver#38
2026-02-26 09:04:14 -06:00
7c85b2ad93 fix(assembly): use instance suffixes for duplicate part labels
All checks were successful
Build and Test / build (pull_request) Successful in 29m11s
When parts with structured part numbers (e.g., P03-0001) are inserted
into an assembly multiple times, UniqueNameManager::decomposeName()
treats the trailing digits as an auto-generated suffix and increments
them (P03-0002, P03-0003), corrupting the part number.

Add a makeInstanceLabel() helper in AssemblyLink.cpp that appends -N
instance suffixes instead (P03-0001-1, P03-0001-2). All instances get
a suffix starting at -1 so the original part number is never modified.

Applied at all three Label.setValue() sites in
synchronizeComponents() (AssemblyLink, link group, and regular link
creation paths).

Also add a UniqueNameManager test documenting the trailing-digit
decomposition behavior for structured part numbers.

Closes #327
2026-02-26 09:00:13 -06:00
311d911cfa fix(theme): prevent panel element headings from being clipped
All checks were successful
Build and Test / build (pull_request) Successful in 29m23s
Three QSS issues caused headings to render with only the top ~60%
visible:

- QGroupBox: margin-top 12px was insufficient for the title rendered
  in subcontrol-origin: margin. Increased to 16px and added 2px
  vertical padding to the title.
- QDockWidget::title: min-height 18px conflicted with padding 8px 6px,
  constraining the content area. Removed min-height to let Qt auto-size
  from padding + font metrics.
- QSint--ActionGroup QToolButton: min-height 18px forced a height that
  was then clipped by the C++ setFixedHeight(headerSize) calculation.
  Set min-height to 0px so the C++ layout controls sizing.

Closes #325
2026-02-26 08:48:01 -06:00
4ef8e64a7c fix(ci): add target_commitish to release payload to fix HTTP 500
All checks were successful
Build and Test / build (pull_request) Successful in 29m30s
The publish-release job was missing the target_commitish field in the
Gitea release creation API payload. Without it, Gitea cannot resolve
the tag to a git object and returns HTTP 500 with 'object does not
exist'.

Add COMMIT_SHA (from github.sha) to the job env and pass it as
target_commitish in the JSON payload.

Closes #326
2026-02-26 08:45:58 -06:00
d94e8c8294 docs: add CLAUDE.md for developer context
All checks were successful
Build and Test / build (pull_request) Successful in 29m9s
2026-02-26 08:40:34 -06:00
3550d916bd Merge pull request 'docs(solver): update drag protocol docs to reflect implemented caching' (#330) from docs/solver-drag-cache into main
All checks were successful
Deploy Docs / build-and-deploy (push) Successful in 40s
Build and Test / build (push) Successful in 29m53s
Reviewed-on: #330
2026-02-26 14:25:17 +00:00
6e15b25134 docs(solver): update drag protocol docs to reflect implemented caching
All checks were successful
Build and Test / build (pull_request) Successful in 30m27s
The interactive drag section described the original naive implementation
(re-solve from scratch each step) and called the caching layer a
'planned future optimization'. In reality _DragCache is fully
implemented: pre_drag() builds the system, Jacobian, and compiled
evaluator once, and drag_step() reuses them.

Update code snippets, add _DragCache field table, and document the
single_equation_pass exclusion from the drag path.
2026-02-25 13:23:41 -06:00
82f2422285 Merge pull request 'fix(solver): skip single_equation_pass during drag to prevent stale constraints' (#329) from fix/planar-drag-prepass into main
Some checks failed
Build and Test / build (push) Has been cancelled
Sync Silo Server Docs / sync (push) Successful in 40s
Reviewed-on: #329
2026-02-25 19:03:16 +00:00
314955c3ef fix(solver): update solver submodule — skip prepass during drag
Some checks failed
Build and Test / build (pull_request) Has been cancelled
Updates solver submodule to include fix for planar distance=0
constraints not holding during drag operations.
2026-02-25 12:58:03 -06:00
c7a7436e7b Merge pull request 'chore: update solver submodule' (#328) from chore/update-solver-submodule into main
All checks were successful
Sync Silo Server Docs / sync (push) Successful in 38s
Build and Test / build (push) Successful in 30m20s
Reviewed-on: #328
2026-02-25 02:49:57 +00:00
forbes
40dd8e09d7 chore: update solver submodule
All checks were successful
Build and Test / build (pull_request) Successful in 29m42s
Picks up fix/drag-orientation-stability (kindred/solver#36):
- Half-space tracking for all compound constraints with branch ambiguity
- Quaternion continuity enforcement during interactive drag
2026-02-24 20:49:27 -06:00
1fd52ccf1c Merge pull request 'feat: add QuickNav addon — Phase 1 core infrastructure (#320)' (#324) from feat/quicknav-phase1 into main
All checks were successful
Deploy Docs / build-and-deploy (push) Successful in 46s
Build and Test / build (push) Successful in 29m41s
Reviewed-on: #324
2026-02-24 18:37:19 +00:00
e73c5fc750 feat: add QuickNav addon — Phase 1 core infrastructure (#320)
All checks were successful
Build and Test / build (pull_request) Successful in 29m58s
Add quicknav submodule and create-side integration for keyboard-driven
command navigation.

Submodule: mods/quicknav (https://git.kindred-systems.com/kindred/quicknav)

Create-side changes:
- CMakeLists.txt: add quicknav install rules
- test_kindred_pure.py: add 16 workbench_map validation tests
- docs/src/quicknav/SPEC.md: QuickNav specification

QuickNav provides numbered-key access to workbenches (Ctrl+1-5),
command groupings (Shift+1-9), and individual commands (1-9), with
a navigation bar toolbar and input-widget safety guards.
2026-02-23 14:12:02 -06:00
f652d6ccf8 Merge pull request 'fix(assembly): handle non-standard datum element types in Distance joint classification' (#319) from fix/datum-plane-classification-all-hierarchies into main
Some checks failed
Build and Test / build (push) Has been cancelled
Sync Silo Server Docs / sync (push) Successful in 37s
Reviewed-on: #319
2026-02-23 03:20:04 +00:00
forbes
14ee8c673f fix(assembly): classify datum planes from all class hierarchies in Distance joints
Some checks failed
Build and Test / build (pull_request) Has been cancelled
The datum plane detection in getDistanceType() only checked for
App::Plane (origin planes). This missed two other class hierarchies:

  - PartDesign::Plane (inherits Part::Datum, NOT App::Plane)
  - Part::Plane primitive referenced bare (no Face element)

Both produce empty element types (sub-name ends with ".") but failed
the isDerivedFrom<App::Plane>() check, falling through to
DistanceType::Other and the Planar fallback. This caused incorrect
constraint geometry, leading to conflicting/unsatisfiable constraints
and solver failures.

Add shape-based isDatumPlane/Line/Point helpers that cover all three
hierarchies by inspecting the actual OCCT geometry rather than relying
on class identity alone. Extend getDistanceType() to use these helpers
for all datum-vs-datum and datum-vs-element combinations.

Adds TestDatumClassification.py with tests for PartDesign::Plane,
Part::Plane (bare ref), and cross-hierarchy datum combinations.
2026-02-22 21:18:34 -06:00
a6a5db11f8 Merge pull request 'fix(assembly): classify datum planes from all class hierarchies in Distance joints' (#318) from fix/datum-plane-classification-all-hierarchies into main
All checks were successful
Build and Test / build (push) Successful in 31m20s
Reviewed-on: #318
2026-02-23 00:56:20 +00:00
forbes
962b521f5c fix(assembly): classify datum planes from all class hierarchies in Distance joints
All checks were successful
Build and Test / build (pull_request) Successful in 30m13s
The datum plane detection in getDistanceType() only checked for
App::Plane (origin planes). This missed two other class hierarchies:

  - PartDesign::Plane (inherits Part::Datum, NOT App::Plane)
  - Part::Plane primitive referenced bare (no Face element)

Both produce empty element types (sub-name ends with ".") but failed
the isDerivedFrom<App::Plane>() check, falling through to
DistanceType::Other and the Planar fallback. This caused incorrect
constraint geometry, leading to conflicting/unsatisfiable constraints
and solver failures.

Add shape-based isDatumPlane/Line/Point helpers that cover all three
hierarchies by inspecting the actual OCCT geometry rather than relying
on class identity alone. Extend getDistanceType() to use these helpers
for all datum-vs-datum and datum-vs-element combinations.

Adds TestDatumClassification.py with tests for PartDesign::Plane,
Part::Plane (bare ref), and cross-hierarchy datum combinations.
2026-02-22 18:55:39 -06:00
5c9212247a Merge pull request 'fix(assembly): add datum plane logging + fix cross-product singularity' (#317) from fix/distance-datum-plane-classification into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #317
2026-02-22 22:05:48 +00:00
forbes
cf0cd3db7e fix(assembly): classify datum plane references in Distance joints
Some checks failed
Build and Test / build (pull_request) Has been cancelled
When a Distance joint references a datum plane (XY_Plane, XZ_Plane,
YZ_Plane), getDistanceType() failed to recognize it because datum
plane sub-names yield an empty element type. This caused the fallback
to DistanceType::Other → BaseJointKind::Planar, which adds spurious
parallel-normal residuals that overconstrain the system.

For example, three vertex-to-datum-plane Distance joints produced
10 residuals (3×Planar) with 6 mutually contradictory orientation
constraints, causing the solver to find garbage least-squares
solutions.

Add early detection of App::Plane datum objects before the main
geometry classification chain. Datum planes are now correctly mapped:
- Vertex + DatumPlane → PointPlane → PointInPlane (1 residual)
- Edge + DatumPlane → LinePlane → LineInPlane
- Face/DatumPlane + DatumPlane → PlanePlane → Planar
2026-02-22 15:52:22 -06:00
50dc8c8ea1 Merge pull request 'fix(assembly): classify datum plane references in Distance joints' (#316) from fix/distance-datum-plane-classification into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #316
2026-02-22 20:21:07 +00:00
forbes
b4835e1b05 fix(assembly): classify datum plane references in Distance joints
Some checks failed
Build and Test / build (pull_request) Has been cancelled
When a Distance joint references a datum plane (XY_Plane, XZ_Plane,
YZ_Plane), getDistanceType() failed to recognize it because datum
plane sub-names yield an empty element type. This caused the fallback
to DistanceType::Other → BaseJointKind::Planar, which adds spurious
parallel-normal residuals that overconstrain the system.

For example, three vertex-to-datum-plane Distance joints produced
10 residuals (3×Planar) with 6 mutually contradictory orientation
constraints, causing the solver to find garbage least-squares
solutions.

Add early detection of App::Plane datum objects before the main
geometry classification chain. Datum planes are now correctly mapped:
- Vertex + DatumPlane → PointPlane → PointInPlane (1 residual)
- Edge + DatumPlane → LinePlane → LineInPlane
- Face/DatumPlane + DatumPlane → PlanePlane → Planar
2026-02-22 14:19:11 -06:00
cf2fc82eac Merge pull request 'fix(assembly): classify datum plane references in Distance joints' (#315) from fix/distance-datum-plane-classification into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #315
2026-02-22 18:25:32 +00:00
forbes
e5b07449d7 fix(assembly): classify datum plane references in Distance joints
Some checks failed
Build and Test / build (pull_request) Has been cancelled
When a Distance joint references a datum plane (XY_Plane, XZ_Plane,
YZ_Plane), getDistanceType() failed to recognize it because datum
plane sub-names yield an empty element type. This caused the fallback
to DistanceType::Other → BaseJointKind::Planar, which adds spurious
parallel-normal residuals that overconstrain the system.

For example, three vertex-to-datum-plane Distance joints produced
10 residuals (3×Planar) with 6 mutually contradictory orientation
constraints, causing the solver to find garbage least-squares
solutions.

Add early detection of App::Plane datum objects before the main
geometry classification chain. Datum planes are now correctly mapped:
- Vertex + DatumPlane → PointPlane → PointInPlane (1 residual)
- Edge + DatumPlane → LinePlane → LineInPlane
- Face/DatumPlane + DatumPlane → PlanePlane → Planar
2026-02-22 12:24:44 -06:00
58d98c6d92 Merge pull request 'fix(assembly): classify datum plane references in Distance joints' (#314) from fix/distance-datum-plane-classification into main
All checks were successful
Build and Test / build (push) Successful in 41m35s
Sync Silo Server Docs / sync (push) Successful in 34s
Reviewed-on: #314
2026-02-22 04:04:41 +00:00
forbes
a10b9d9a9f fix(assembly): classify datum plane references in Distance joints
All checks were successful
Build and Test / build (pull_request) Successful in 41m13s
When a Distance joint references a datum plane (XY_Plane, XZ_Plane,
YZ_Plane), getDistanceType() failed to recognize it because datum
plane sub-names yield an empty element type. This caused the fallback
to DistanceType::Other → BaseJointKind::Planar, which adds spurious
parallel-normal residuals that overconstrain the system.

For example, three vertex-to-datum-plane Distance joints produced
10 residuals (3×Planar) with 6 mutually contradictory orientation
constraints, causing the solver to find garbage least-squares
solutions.

Add early detection of App::Plane datum objects before the main
geometry classification chain. Datum planes are now correctly mapped:
- Vertex + DatumPlane → PointPlane → PointInPlane (1 residual)
- Edge + DatumPlane → LinePlane → LineInPlane
- Face/DatumPlane + DatumPlane → PlanePlane → Planar
2026-02-21 22:04:18 -06:00
forbes
d0e6d91642 chore: update solver submodule (drag step caching)
Some checks failed
Build and Test / build (push) Has been cancelled
2026-02-21 12:23:42 -06:00
forbes
05428f8a1c chore: bump version to 0.1.5
Some checks failed
Deploy Docs / build-and-deploy (push) Successful in 40s
Build and Test / build (push) Has been cancelled
2026-02-21 12:05:09 -06:00
forbes
14f314e137 chore: update solver submodule (distance=0 fix)
Some checks failed
Build and Test / build (push) Has been cancelled
2026-02-21 11:46:52 -06:00
forbes
30c35af3be chore: update solver submodule (compiled Jacobian evaluation)
Some checks are pending
Build and Test / build (push) Has started running
2026-02-21 11:42:48 -06:00
441cf9e826 Merge pull request 'feat(assembly): add diagnostic logging to solver and assembly' (#313) from feat/solver-diagnostic-logging into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #313
2026-02-21 16:11:34 +00:00
forbes
c682c5d153 feat(assembly): add diagnostic logging to solver and assembly
Some checks failed
Build and Test / build (pull_request) Has been cancelled
C++ (AssemblyObject):
- getOrCreateSolver: log which solver backend was loaded
- solve: log assembly name, grounded/joint counts, context size,
  result status with DOF and placement count, per-constraint
  diagnostics on failure
- preDrag/doDragStep/postDrag: log drag part count, per-step
  validation failures, and summary (total steps / rejected count)
- buildSolveContext: log grounded/free part counts, constraint count,
  limits count, and bundle_fixed flag

Python (kindred_solver submodule):
- solver.py: log solve entry/exit with timing, system build stats,
  decomposition decisions, Newton/BFGS fallback events, drag lifecycle
- decompose.py: log cluster stats and per-cluster convergence
- Init.py: FreeCAD log handler routing Python logging to Console
2026-02-21 10:08:51 -06:00
f65a4a5e2b Merge pull request 'fix(assembly): update flip-detection baseline during drag steps' (#312) from fix/assembly-drag-flip-detection into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #312
2026-02-21 15:59:55 +00:00
forbes
5d55f091d0 fix(assembly): update flip-detection baseline during drag steps
Some checks failed
Build and Test / build (pull_request) Has been cancelled
During drag operations, validateNewPlacements() compared each solver
result against the pre-drag positions saved once in preDrag().  As the
user dragged further, the cumulative rotation from that fixed baseline
easily exceeded the 91-degree threshold, causing valid intermediate
results to be rejected with 'flipped orientation' warnings and making
parts appear to explode.

Fix: call savePlacementsForUndo() after each accepted drag step so
that the flip check compares against the last accepted state rather
than the original pre-drag origin.
2026-02-21 09:59:04 -06:00
a445275fd2 Merge pull request 'fix(kc_format): eliminate duplicate silo/manifest.json in .kc files' (#311) from fix/kc-duplicate-manifest into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #311
2026-02-21 15:50:58 +00:00
forbes
88efa2a6ae fix(kc_format): eliminate duplicate silo/manifest.json entries in .kc files
Some checks failed
Build and Test / build (pull_request) Has been cancelled
Two code paths were appending silo/manifest.json to the ZIP without
removing the previous entry, causing Python's zipfile module to warn
about duplicate names:

1. slotFinishSaveDocument() re-injected the cached manifest from
   entries, then the modified_at update branch wrote a second copy.

2. update_manifest_fields() opened the ZIP in append mode and wrote
   an updated manifest without removing the old one.

Fix slotFinishSaveDocument() by preparing the final manifest (with
updated modified_at) in the entries dict before writing, so only one
copy is written to the ZIP.

Fix update_manifest_fields() by rewriting the ZIP atomically via a
temp file, deduplicating any pre-existing duplicate entries in the
process.
2026-02-21 09:49:36 -06:00
62f077a267 Merge pull request 'fix(assembly): prevent segfault when all joints are removed' (#310) from fix/assembly-empty-joints-segfault into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #310
2026-02-21 15:47:59 +00:00
forbes
b6b0ebb4dc fix(assembly): prevent segfault when all joints are removed
Some checks failed
Build and Test / build (pull_request) Has been cancelled
updateSolveStatus() calls solve() when lastResult_.placements is empty,
but solve() calls updateSolveStatus() at the end. When an assembly has
zero constraints (all joints removed), the solver returns zero
placements, causing infinite recursion until stack overflow (segfault).

Add a static re-entrancy guard so the recursive solve() call is skipped
if updateSolveStatus() is already on the call stack.
2026-02-21 09:47:15 -06:00
a6d0427639 Merge pull request 'fix(gui): resolve PartDesign toolbars and breadcrumb when editing body inside assembly' (#309) from fix/partdesign-context-in-assembly into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #309
2026-02-21 15:44:49 +00:00
forbes
5883ac8a0d fix(gui): resolve PartDesign toolbars and breadcrumb when editing body inside assembly
Some checks failed
Build and Test / build (pull_request) Has been cancelled
When double-clicking a PartDesign Body inside an Assembly, the editing
context resolver failed to show PartDesign toolbars or a proper
breadcrumb. Three root causes:

1. Priority preemption: assembly.edit (priority 90) always matched
   because the Assembly VP remained in edit mode, blocking all
   PartDesign contexts (priorities 30-40).

2. Wrong active-object key: PartDesign matchers only queried the
   "part" active object, but ViewProviderBody::toggleActiveBody()
   sets "part" to the containing App::Part (which is the Assembly
   itself). The Body is set under "pdbody", which was never queried.

3. Missing refresh trigger: ActiveObjectList::setObject() fires
   Document::signalActivatedViewProvider, but EditingContextResolver
   had no connection to it, so setActiveObject("pdbody", body) never
   triggered a context re-resolve.

Changes:
- Forward signalActivatedViewProvider from Gui::Document to
  Gui::Application (same pattern as signalInEdit/signalResetEdit)
- Connect EditingContextResolver to the new application-level signal
- Add getActivePdBodyObject() helper querying the "pdbody" key
- Add partdesign.in_assembly context (priority 95) that matches when
  a Body is active pdbody while an Assembly is in edit
- Update partdesign.body and partdesign.feature matchers to check
  pdbody before falling back to the part key
- Add Assembly > Body breadcrumb with Blue > Mauve coloring
- Update label resolution to prefer pdbody name
2026-02-21 09:43:51 -06:00
f9b13710f3 Merge pull request 'fix(solver): add networkx to runtime dependencies' (#308) from fix/solver-networkx-dep into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #308
2026-02-21 15:29:30 +00:00
forbes
39e78ee0a2 fix(solver): add networkx to runtime dependencies
All checks were successful
Build and Test / build (pull_request) Successful in 30m3s
The kindred-solver addon imports networkx in its decompose module.
Without it, KindredSolver fails to import and solver registration
silently fails, leaving only the ondsel backend available.
2026-02-21 09:24:30 -06:00
0f8fa0be86 Merge pull request 'feat(assembly): fixed reference planes (Top/Front/Right) + solver docs' (#307) from feat/assembly-origin-planes into main
Some checks failed
Deploy Docs / build-and-deploy (push) Successful in 51s
Build and Test / build (push) Has been cancelled
Reviewed-on: #307
2026-02-21 15:09:55 +00:00
forbes
acc255972d feat(assembly): fixed reference planes + solver docs
Some checks failed
Build and Test / build (pull_request) Failing after 7m52s
Assembly Origin Planes:
- AssemblyObject::setupObject() relabels origin planes to
  Top (XY), Front (XZ), Right (YZ) on assembly creation
- CommandCreateAssembly.py makes origin planes visible by default
- AssemblyUtils.cpp getObjFromRef() resolves LocalCoordinateSystem
  to child datum elements for joint references to origin planes
- TestAssemblyOriginPlanes.py: 9 integration tests covering
  structure, labels, grounding, reference resolution, solver,
  and save/load round-trip

Solver Documentation:
- docs/src/solver/: 7 new pages covering architecture overview,
  expression DAG, constraints, solving algorithms, diagnostics,
  assembly integration, and writing custom solvers
- docs/src/SUMMARY.md: added Kindred Solver section
2026-02-21 09:09:16 -06:00
148bed59f6 Merge pull request 'feat(templates): document templating system for .kc files' (#306) from feat/document-templates into main
Some checks failed
Deploy Docs / build-and-deploy (push) Successful in 40s
Build and Test / build (push) Has been cancelled
Reviewed-on: #306
2026-02-21 15:07:43 +00:00
forbes
b8cb7ca267 feat(templates): document templating system for .kc files
All checks were successful
Build and Test / build (pull_request) Successful in 29m12s
Add a template system integrated with Silo new-item creation that lets
users create parts from pre-configured .kc templates and save existing
documents as reusable templates.

Changes:
- mods/silo: template discovery, picker UI, Save as Template command,
  3-tier search paths (system, personal, org-shared)
- docs: template guide, SUMMARY.md entry, silo.md command reference
2026-02-21 09:06:36 -06:00
ae576629c5 Merge pull request 'fix(assembly): move resetSolver() out-of-line to fix incomplete type error' (#305) from fix/resetsolver-incomplete-type into main
Some checks failed
Build and Test / build (push) Has been cancelled
Reviewed-on: #305
2026-02-21 13:09:42 +00:00
forbes
6e7d2b582e fix(assembly): move resetSolver() out-of-line to fix incomplete type error
Some checks failed
Build and Test / build (pull_request) Has been cancelled
unique_ptr::reset() requires the complete type for its deleter, but
IKCSolver is only forward-declared in AssemblyObject.h. Move the
definition to AssemblyObject.cpp where the full header is included.
2026-02-21 07:08:59 -06:00
6d08161ae6 Merge pull request 'feat(solver): KCSolve solver addon with assembly integration (#289)' (#303) from feat/solver-context-packing into main
Some checks failed
Build and Test / build (push) Has been cancelled
Sync Silo Server Docs / sync (push) Successful in 52s
Reviewed-on: #303
2026-02-21 05:48:46 +00:00
forbes
72e7e32133 feat(solver): KCSolve solver addon with assembly integration (#289)
Some checks failed
Build and Test / build (pull_request) Has been cancelled
Adds the Kindred constraint solver as a pluggable Assembly workbench
backend, covering phases 3d through 5 of the solver roadmap.

Phase 3d: SolveContext packing
- Pack/unpack SolveContext into .kc archive on document save

Solver addon (mods/solver):
- Phase 1: Expression DAG, Newton-Raphson + BFGS, 3 basic constraints
- Phase 2: Full constraint vocabulary — all 24 BaseJointKind types
- Phase 3: Graph decomposition for cluster-by-cluster solving
- Phase 4: Per-entity DOF diagnostics, overconstrained detection,
  half-space preference tracking, minimum-movement weighting
- Phase 5: _build_system extraction, diagnose(), drag protocol,
  joint limits warning

Assembly workbench integration:
- Preference-driven solver selection (reads Mod/Assembly/Solver param)
- Solver backend combo box in Assembly preferences UI
- resetSolver() on AssemblyObject for live preference switching
- Integration tests (TestKindredSolverIntegration.py)
- In-client console test script (console_test_phase5.py)
2026-02-20 23:47:50 -06:00
805be1e213 Merge pull request 'feat(solver): pack SolveContext into .kc archives on save (#289 phase 3d)' (#302) from feat/solver-context-packing into main
All checks were successful
Build and Test / build (push) Successful in 30m10s
Reviewed-on: #302
2026-02-21 00:03:13 +00:00
forbes
4cf54caf7b feat(solver): pack SolveContext into .kc archives on save (#289 phase 3d)
All checks were successful
Build and Test / build (pull_request) Successful in 29m51s
Expose AssemblyObject::getSolveContext() to Python and hook into the
.kc save flow so that silo/solver/context.json is packed into every
assembly archive. This lets server-side solver runners operate on
pre-extracted constraint graphs without a full FreeCAD installation.

Changes:
- Add public getSolveContext() to AssemblyObject (C++ and Python)
- Build Python dict via CPython C API matching kcsolve.SolveContext.to_dict()
- Register _solver_context_hook in kc_format.py pre-reinject hooks
- Add silo/solver/context.json to silo_tree.py _KNOWN_ENTRIES
2026-02-20 17:12:25 -06:00
43 changed files with 3297 additions and 66 deletions

View File

@@ -322,6 +322,7 @@ jobs:
env:
BUILD_TAG: ${{ github.ref_name || inputs.tag }}
COMMIT_SHA: ${{ github.sha }}
NODE_EXTRA_CA_CERTS: /etc/ssl/certs/ca-certificates.crt
steps:
@@ -386,6 +387,7 @@ jobs:
'name': f'Kindred Create {tag}',
'body': body,
'prerelease': prerelease,
'target_commitish': '${COMMIT_SHA}',
}))
")

2
.gitignore vendored
View File

@@ -77,3 +77,5 @@ docs/book/
# To regenerate themed icons: python3 icons/retheme.py
# icons/themed/ is tracked (committed) so CI builds include them
CLAUDE.md

3
.gitmodules vendored
View File

@@ -22,3 +22,6 @@
path = mods/solver
url = https://git.kindred-systems.com/kindred/solver.git
branch = main
[submodule "mods/quicknav"]
path = mods/quicknav
url = https://git.kindred-systems.com/kindred/quicknav.git

285
CLAUDE.md Normal file
View File

@@ -0,0 +1,285 @@
# CLAUDE.md — Developer Context for Kindred Create
## Project Overview
Kindred Create is a fork of FreeCAD 1.0+ that adds integrated tooling for professional engineering workflows. It ships a context-aware UI system, two addon command sets (ztools and Silo), a Catppuccin Mocha dark theme, and a pluggable file origin layer on top of FreeCAD's parametric modeling core.
- **Kindred Create version:** 0.1.5
- **FreeCAD base version:** 1.2.0
- **License:** LGPL-2.1-or-later
- **Repository:** `git.kindred-systems.com/kindred/create` (Gitea)
- **Main branch:** `main`
## Quick Start
```bash
git clone --recursive ssh://git@git.kindred-systems.com:2222/kindred/create.git
cd create
pixi run configure # CMake configure (debug by default)
pixi run build # Build
pixi run install # Install to build dir
pixi run freecad # Launch
pixi run test # Run C++ tests (ctest)
pixi run test-kindred # Run Python/Kindred tests
```
Build variants: append `-debug` or `-release` (e.g., `pixi run build-release`). See `CMakePresets.json` for platform-specific presets (Linux x86_64/aarch64, macOS Intel/ARM, Windows x64).
## Repository Structure
```
create/
├── src/
│ ├── App/ Core application (C++)
│ ├── Base/ Base classes, type system, persistence (C++)
│ ├── Gui/ GUI framework (C++)
│ │ ├── EditingContext.h Editing context resolver (Kindred feature)
│ │ ├── BreadcrumbToolBar.h Breadcrumb navigation widget (Kindred feature)
│ │ ├── FileOrigin.h Abstract origin interface (Kindred feature)
│ │ ├── OriginManager.h Origin lifecycle management
│ │ ├── CommandOrigin.cpp Origin_Commit/Pull/Push/Info/BOM commands
│ │ ├── ApplicationPy.h All FreeCADGui.* Python bindings
│ │ ├── Application.h App signals (fastsignals)
│ │ ├── Stylesheets/ QSS theme files
│ │ └── PreferencePacks/ Preference configurations (build-time generated)
│ ├── Mod/ FreeCAD modules (PartDesign, Assembly, Sketcher, etc.)
│ │ └── Create/ Kindred Create module
│ │ ├── Init.py Console bootstrap — loads addons
│ │ ├── InitGui.py GUI bootstrap — loads addons, Silo setup, update checker
│ │ ├── addon_loader.py Manifest-driven loader with dependency resolution
│ │ └── kc_format.py .kc file format preservation
│ └── 3rdParty/ Vendored dependencies
│ ├── OndselSolver/ [submodule] Assembly constraint solver (forked)
│ ├── FastSignals/ Signal/slot library (NOT Boost)
│ └── GSL/ [submodule] Microsoft Guidelines Support Library
├── mods/ Kindred addon modules
│ ├── sdk/ Addon SDK — stable API contract (priority 0)
│ ├── ztools/ [submodule] Command provider (priority 50)
│ ├── silo/ [submodule] PLM workbench (priority 60)
│ ├── solver/ [submodule] Assembly solver research (GNN-based)
│ └── quicknav/ [submodule] Navigation addon
├── docs/ mdBook documentation + architecture docs
├── tests/ C++ unit tests (GoogleTest)
├── package/ Packaging (debian/, rattler-build/)
├── resources/ Branding, icons, desktop integration
├── cMake/ CMake helper modules
├── .gitea/workflows/ CI/CD pipelines
├── CMakeLists.txt Root build configuration (CMake 3.22.0+)
├── CMakePresets.json Platform build presets
└── pixi.toml Pixi environment and build tasks
```
## Build System
- **Primary:** CMake 3.22.0+ with Ninja generator
- **Environment:** [Pixi](https://pixi.sh) (conda-forge) manages all dependencies
- **Key deps:** Qt 6.8.x, Python 3.11.x, OpenCASCADE 7.8.x, PySide6, Boost, VTK, SMESH
- **Presets:** `conda-linux-debug`, `conda-linux-release`, `conda-macos-debug`, `conda-macos-release`, `conda-windows-debug`, `conda-windows-release`
- **Tasks summary:**
| Task | Description |
|------|-------------|
| `pixi run configure` | CMake configure (debug) |
| `pixi run build` | Build (debug) |
| `pixi run install` | Install to build dir |
| `pixi run freecad` | Launch FreeCAD |
| `pixi run test` | C++ tests via ctest |
| `pixi run test-kindred` | Python/Kindred test suite |
## Architecture Patterns
### Signals — Use FastSignals, NOT Boost
```cpp
#include <fastsignals/signal.h>
// See src/Gui/Application.h:121-155 for signal declarations
```
All signals in `src/Gui/` use `fastsignals::signal`. Never use Boost.Signals2.
### Type Checking Across Modules
Avoid header dependencies between `src/Gui/` and `src/Mod/` by using runtime type checks:
```cpp
auto type = Base::Type::fromName("Sketcher::SketchObject");
if (obj->isDerivedFrom(type)) { ... }
```
### Python Bindings
All `FreeCADGui.*` functions go in `src/Gui/ApplicationPy.h` and `src/Gui/ApplicationPy.cpp`. Use `METH_VARARGS` only (no `METH_KEYWORDS` in this file). Do not create separate files for new Python bindings.
### Toolbar Visibility
Use `ToolBarItem::DefaultVisibility::Unavailable` to hide toolbars by default, then `ToolBarManager::setState(ForceAvailable)` to show them contextually. This pattern is proven by the Sketcher module.
The `appendToolbar` Python API accepts an optional 3rd argument: `"Visible"`, `"Hidden"`, or `"Unavailable"`.
### Editing Context System
The `EditingContextResolver` singleton (`src/Gui/EditingContext.h/.cpp`) drives the context-aware UI. It evaluates registered context definitions in priority order and activates the matching one, setting toolbar visibility and updating the `BreadcrumbToolBar`.
Built-in contexts: `sketcher.edit`, `assembly.edit`, `partdesign.feature`, `partdesign.body`, `assembly.idle`, `spreadsheet`, `empty_document`, `no_document`.
Python API:
- `FreeCADGui.registerEditingContext()` — register a new context
- `FreeCADGui.registerEditingOverlay()` — conditional toolbar overlay
- `FreeCADGui.injectEditingCommands()` — add commands to existing contexts
- `FreeCADGui.currentEditingContext()` — query active context
- `FreeCADGui.refreshEditingContext()` — force re-evaluation
### Addon Loading
Addons in `mods/` are loaded by `src/Mod/Create/addon_loader.py`. Each addon provides a `package.xml` with `<kindred>` extensions declaring version bounds, load priority, and dependencies. The loader resolves via topological sort: **sdk** (0) -> **ztools** (50) -> **silo** (60).
A `<workbench>` tag in `package.xml` is required for `InitGui.py` to be loaded, even if no actual workbench is registered.
### Deferred Initialization
GUI setup uses `QTimer.singleShot` with staggered delays:
- 500ms: `.kc` file format registration
- 1500ms: Silo origin registration
- 2000ms: Auth dock + ztools commands
- 2500ms: Silo overlay
- 3000ms: Silo first-start check
- 4000ms: Activity panel
- 10000ms: Update checker
### Unified Origin System
File operations (New, Open, Save, Commit, Pull, Push) are abstracted behind `FileOrigin` (`src/Gui/FileOrigin.h`). `LocalFileOrigin` handles local files; `SiloOrigin` (`mods/silo/freecad/silo_origin.py`) backs Silo-tracked documents. The active origin is selected automatically based on document properties (`SiloItemId`, `SiloPartNumber`).
## Submodules
| Path | Repository | Branch | Purpose |
|------|------------|--------|---------|
| `mods/ztools` | `git.kindred-systems.com/forbes/ztools` | `main` | Extended PartDesign/Assembly/Spreadsheet tools |
| `mods/silo` | `git.kindred-systems.com/kindred/silo-mod` | `main` | PLM workbench (includes silo-client submodule) |
| `mods/solver` | `git.kindred-systems.com/kindred/solver` | `main` | Assembly solver research (GNN-based) |
| `mods/quicknav` | `git.kindred-systems.com/kindred/quicknav` | — | Navigation addon |
| `src/3rdParty/OndselSolver` | `git.kindred-systems.com/kindred/solver` | — | Constraint solver (forked with NR fix) |
| `src/3rdParty/GSL` | `github.com/microsoft/GSL` | — | Guidelines Support Library |
| `src/Mod/AddonManager` | `github.com/FreeCAD/AddonManager` | — | FreeCAD addon manager |
| `tests/lib` | `github.com/google/googletest` | — | C++ test framework |
Update a submodule:
```bash
cd mods/silo
git checkout main && git pull
cd ../..
git add mods/silo
git commit -m "chore: update silo submodule"
```
Initialize all submodules: `git submodule update --init --recursive`
## Key Addon Modules
### ztools (`mods/ztools/`)
Command provider (NOT a workbench). Injects tools into PartDesign, Assembly, and Spreadsheet contexts via `_ZToolsManipulator` (WorkbenchManipulator) and `injectEditingCommands()`.
Commands: `ZTools_DatumCreator`, `ZTools_EnhancedPocket`, `ZTools_RotatedLinearPattern`, `ZTools_AssemblyLinearPattern`, `ZTools_AssemblyPolarPattern`, spreadsheet formatting (Bold, Italic, Underline, alignment, colors, QuickAlias).
Source: `mods/ztools/ztools/ztools/commands/` (note the double `ztools` nesting).
### Silo (`mods/silo/`)
PLM workbench with 14 commands for parts lifecycle management. Go REST API server + PostgreSQL + MinIO backend. FreeCAD client communicates via shared `silo-client` submodule.
Silo origin detection: `silo_origin.py:ownsDocument()` checks for `SiloItemId`/`SiloPartNumber` properties on the active document.
### SDK (`mods/sdk/`)
Stable API contract for addons. Provides wrappers for editing contexts, theme tokens (Catppuccin Mocha YAML palette), FileOrigin registration, and deferred dock panels. Addons should use `kindred_sdk.*` instead of `FreeCADGui.*` internals where possible.
## Theme
- **Canonical source:** `src/Gui/Stylesheets/KindredCreate.qss`
- The PreferencePacks copy at `src/Gui/PreferencePacks/KindredCreate/KindredCreate.qss` is **generated at build time** via `configure_file()`. Only edit the Stylesheets copy.
- Color palette: Catppuccin Mocha (26 colors + 14 semantic roles, defined in `mods/sdk/kindred_sdk/palettes/catppuccin-mocha.yaml`)
- Default preferences: `src/Gui/PreferencePacks/KindredCreate/KindredCreate.cfg`
## Git Conventions
### Branch Names
`type/kebab-case-description`
Types: `feat/`, `fix/`, `chore/`, `docs/`, `refactor/`, `art/`
### Commit Messages
[Conventional Commits](https://www.conventionalcommits.org/):
```
type(scope): lowercase imperative description
```
| Prefix | Purpose |
|--------|---------|
| `feat:` | New feature |
| `fix:` | Bug fix |
| `chore:` | Maintenance, dependencies |
| `docs:` | Documentation only |
| `art:` | Icons, theme, visual assets |
| `refactor:` | Code restructuring |
Scopes: `solver`, `sketcher`, `editing-context`, `toolbar`, `ztools`, `silo`, `breadcrumb`, `gui`, `assembly`, `ci`, `theme`, `quicknav`, or omitted.
### PR Workflow
1. Create a branch from `main`: `git checkout -b feat/my-feature main`
2. Commit with conventional commit messages
3. Push and open a PR against `main` via Gitea (or `tea pulls create`)
4. CI runs automatically on PRs
### Code Style
- **C++:** clang-format (`.clang-format`), clang-tidy (`.clang-tidy`)
- **Python:** black (100-char line length), pylint (`.pylintrc`)
- **Pre-commit hooks:** `pre-commit install` (runs clang-format, black, trailing-whitespace, etc.)
## CI/CD
- **Build:** `.gitea/workflows/build.yml` — runs on pushes to `main` and on PRs
- **Release:** `.gitea/workflows/release.yml` — triggered by `v*` tags, builds AppImage and .deb
- **Platform:** Currently Linux x86_64 only in CI; other platforms have presets but no runners yet
## Documentation
| Document | Content |
|----------|---------|
| `README.md` | Project overview, installation, usage |
| `CONTRIBUTING.md` | Branch workflow, commit conventions, code style |
| `docs/ARCHITECTURE.md` | Bootstrap flow, addon lifecycle, source layout |
| `docs/COMPONENTS.md` | Feature inventory (ztools, Silo, origin, theme, icons) |
| `docs/KNOWN_ISSUES.md` | Known issues, incomplete features, next steps |
| `docs/INTEGRATION_PLAN.md` | 5-layer architecture, phase status |
| `docs/CI_CD.md` | Build and release workflows |
| `docs/KC_SPECIFICATION.md` | .kc file format specification |
| `docs/UPSTREAM.md` | FreeCAD upstream merge strategy |
| `docs/INTER_SOLVER.md` | Assembly solver integration |
| `docs/BOM_MERGE.md` | BOM-Assembly bridge specification |
The `docs/src/` directory contains an mdBook site with detailed guides organized by topic (architecture, development, guide, reference, silo-server, solver).
## Issue Tracker
Issues are tracked on Gitea at `git.kindred-systems.com/kindred/create/issues`. Use the `tea` CLI for local interaction:
```bash
tea issues # List open issues
tea issues 123 # View issue #123 details
tea pulls create # Create a PR
```
## Known Issues and Pitfalls
1. **Silo auth not production-hardened** — LDAP/OIDC backends are coded but need infrastructure deployment
2. **No unit tests** for ztools/Silo FreeCAD commands or Go backend
3. **Assembly solver datum handling is minimal** — joints referencing datum planes/points may produce incorrect placement
4. **PartDesign menu insertion fragility**`_ZToolsPartDesignManipulator.modifyMenuBar()` inserts after `PartDesign_Boolean`; upstream renames break silently
5. **`Silo_BOM` requires Silo-tracked document** — unregistered documents show a warning with no registration path
6. **QSS edits** — only edit `src/Gui/Stylesheets/KindredCreate.qss`; the PreferencePacks copy is auto-generated

View File

@@ -53,7 +53,7 @@ project(KindredCreate)
# Kindred Create version
set(KINDRED_CREATE_VERSION_MAJOR "0")
set(KINDRED_CREATE_VERSION_MINOR "1")
set(KINDRED_CREATE_VERSION_PATCH "0")
set(KINDRED_CREATE_VERSION_PATCH "5")
set(KINDRED_CREATE_VERSION "${KINDRED_CREATE_VERSION_MAJOR}.${KINDRED_CREATE_VERSION_MINOR}.${KINDRED_CREATE_VERSION_PATCH}")
# Underlying FreeCAD version

View File

@@ -2,7 +2,7 @@
**An engineering-focused parametric 3D CAD platform built on FreeCAD 1.0+**
Kindred Create 0.1.0 | FreeCAD 1.2.0 base
Kindred Create 0.1.5 | FreeCAD 1.2.0 base
[Website](https://www.kindred-systems.com/create) |
[Downloads](https://git.kindred-systems.com/kindred/create/releases) |

View File

@@ -12,6 +12,7 @@
- [Workbenches](./guide/workbenches.md)
- [ztools](./guide/ztools.md)
- [Silo](./guide/silo.md)
- [Document Templates](./guide/templates.md)
# Architecture
@@ -49,6 +50,16 @@
- [Solver Service](./silo-server/SOLVER.md)
- [Roadmap](./silo-server/ROADMAP.md)
# Kindred Solver
- [Overview](./solver/overview.md)
- [Expression DAG](./solver/expression-dag.md)
- [Constraints](./solver/constraints.md)
- [Solving Algorithms](./solver/solving.md)
- [Diagnostics](./solver/diagnostics.md)
- [Assembly Integration](./solver/assembly-integration.md)
- [Writing a Custom Solver](./solver/writing-a-solver.md)
# Reference
- [Configuration](./reference/configuration.md)

View File

@@ -13,7 +13,7 @@ Kindred Create uses **CMake** for build configuration, **pixi** (conda-based) fo
## CMake configuration
The root `CMakeLists.txt` defines:
- **Kindred Create version:** `0.1.0` (via `KINDRED_CREATE_VERSION`)
- **Kindred Create version:** `0.1.5` (via `KINDRED_CREATE_VERSION`)
- **FreeCAD base version:** `1.0.0` (via `FREECAD_VERSION`)
- CMake policy settings for compatibility
- ccache auto-detection
@@ -25,7 +25,7 @@ The root `CMakeLists.txt` defines:
The version flows from CMake to Python via `configure_file()`:
```
CMakeLists.txt (KINDRED_CREATE_VERSION = "0.1.0")
CMakeLists.txt (KINDRED_CREATE_VERSION = "0.1.5")
→ src/Mod/Create/version.py.in (template)
→ build/*/Mod/Create/version.py (generated)
→ update_checker.py (imports VERSION)

View File

@@ -157,7 +157,7 @@ Edit only the canonical file in `Stylesheets/` — the preference pack copy is g
Defined in the top-level `CMakeLists.txt` and injected as compiler definitions:
```cmake
set(KINDRED_CREATE_VERSION "0.1.0")
set(KINDRED_CREATE_VERSION "0.1.5")
set(FREECAD_VERSION "1.0.0")
add_definitions(-DKINDRED_CREATE_VERSION="${KINDRED_CREATE_VERSION}")

View File

@@ -53,6 +53,7 @@ The silo-mod repository was split from a monorepo into three repos: `silo-client
| `Silo_TagProjects` | Multi-select dialog for assigning project tags to items |
| `Silo_Rollback` | Select a previous revision and create a new revision from that point with optional comment |
| `Silo_SetStatus` | Change revision lifecycle status: draft → review → released → obsolete |
| `Silo_SaveAsTemplate` | Save a copy of the current document as a reusable [template](./templates.md) with metadata |
### Administration
@@ -129,9 +130,11 @@ mods/silo/
├── freecad/
│ ├── InitGui.py # SiloWorkbench registration
│ ├── schema_form.py # Schema-driven item creation dialog (SchemaFormDialog)
│ ├── silo_commands.py # 14 commands + dock widgets
│ ├── silo_commands.py # 15 commands + dock widgets
│ ├── silo_origin.py # FileOrigin backend
│ ├── silo_start.py # Native start panel (database items, activity feed)
│ ├── templates.py # Template discovery, filtering, injection
│ ├── templates/ # System template .kc files + CLI injection tool
│ └── resources/icons/ # 10 silo-*.svg icons
├── silo-client/ # Shared Python API client (nested submodule)
│ └── silo_client/

140
docs/src/guide/templates.md Normal file
View File

@@ -0,0 +1,140 @@
# Document Templates
Templates let you create new parts and assemblies from pre-configured `.kc` files. Instead of starting from a bare `App::Part` or `Assembly::AssemblyObject`, a template can include predefined tree structures, jobs, metadata, and workbench-specific features.
## How templates work
A template is a normal `.kc` file with an extra `silo/template.json` descriptor inside the ZIP archive. When you select a template during **Silo > New**:
1. The template `.kc` is **copied** to the canonical file path
2. `silo/template.json` and `silo/manifest.json` are **stripped** from the copy
3. The document is **opened** in FreeCAD
4. Silo properties (part number, item ID, revision, type) are **stamped** onto the root object
5. On **save**, `kc_format.py` auto-creates a fresh manifest
The original template file is never modified.
## Using templates
### Creating a new item from a template
1. **Silo > New** (Ctrl+N)
2. Select an **Item Type** (Part, Assembly, etc.)
3. The **Template** dropdown shows templates matching the selected type and category
4. Select a template (or leave as "No template" for a blank document)
5. Fill in the remaining fields and click **Create**
The template combo updates automatically when you change the item type or category.
### Saving a document as a template
1. Open the document you want to use as a template
2. **Silo > Save as Template**
3. Fill in the template metadata:
- **Name** — display name shown in the template picker (pre-filled from document label)
- **Description** — what the template is for
- **Item Types** — which types this template applies to (part, assembly, etc.)
- **Categories** — category prefix filter (e.g. `F`, `M01`); leave empty for all categories
- **Author** — pre-filled from your Silo login
- **Tags** — comma-separated search tags
4. Click **Save Template**
5. Optionally upload to Silo for team sharing
The template is saved as a copy to your personal templates directory. The original document is unchanged.
## Template search paths
Templates are discovered from three locations, checked in order. Later paths shadow earlier ones by name (so you can override a system template with a personal one).
| Priority | Path | Purpose |
|----------|------|---------|
| 1 (lowest) | `mods/silo/freecad/templates/` | System templates shipped with the addon |
| 2 | `~/.local/share/FreeCAD/Templates/` | Personal templates (sister to `Macro/`) |
| 3 (highest) | `~/projects/templates/` | Org-shared project templates |
The personal templates directory (`Templates/`) is created automatically when you first save a template. It lives alongside the `Macro/` directory in your FreeCAD user data.
## Template descriptor schema
The `silo/template.json` file inside the `.kc` ZIP has the following structure:
```json
{
"template_version": "1.0",
"name": "Sheet Metal Part",
"description": "Body with SheetMetal base feature and laser-cut job",
"item_types": ["part"],
"categories": [],
"icon": "sheet-metal",
"author": "Kindred Systems",
"tags": ["sheet metal", "fabrication"]
}
```
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `template_version` | string | yes | Schema version, currently `"1.0"` |
| `name` | string | yes | Display name in the template picker |
| `description` | string | no | Human-readable purpose |
| `item_types` | string[] | yes | Controls visibility — `["part"]`, `["assembly"]`, or both |
| `categories` | string[] | no | Category prefix filter. Empty array means all categories |
| `icon` | string | no | Icon identifier (reserved for future use) |
| `author` | string | no | Template author |
| `tags` | string[] | no | Searchable metadata tags |
### Filtering rules
- **item_types**: The template only appears when the selected item type is in this list
- **categories**: If non-empty, the template only appears when the selected category starts with one of the listed prefixes. An empty list means the template is available for all categories
## Creating templates from the command line
The `inject_template.py` CLI tool can inject `silo/template.json` into any `.kc` file:
```bash
cd mods/silo/freecad/templates/
# Create a template from an existing .kc file
python inject_template.py my-part.kc "My Custom Part" \
--type part \
--description "Part with custom features" \
--author "Your Name" \
--tag "custom"
# Assembly template
python inject_template.py my-assembly.kc "My Assembly" \
--type assembly \
--description "Assembly with predefined joint groups"
# Template with category filtering
python inject_template.py sheet-metal.kc "Sheet Metal Part" \
--type part \
--category S \
--category X \
--tag "sheet metal" \
--tag "fabrication"
```
## Module structure
```
mods/silo/freecad/
├── templates.py # Discovery, filtering, injection helpers
├── templates/
│ └── inject_template.py # CLI tool for injecting template.json
├── schema_form.py # Template combo in New Item form
└── silo_commands.py # SaveAsTemplateDialog, Silo_SaveAsTemplate,
# SiloSync.create_document_from_template()
```
### Key functions
| Function | File | Purpose |
|----------|------|---------|
| `discover_templates()` | `templates.py` | Scan search paths for `.kc` files with `silo/template.json` |
| `filter_templates()` | `templates.py` | Filter by item type and category prefix |
| `inject_template_json()` | `templates.py` | Inject/replace `silo/template.json` in a `.kc` ZIP |
| `get_default_template_dir()` | `templates.py` | Returns `{userAppData}/Templates/`, creating if needed |
| `get_search_paths()` | `templates.py` | Returns the 3-tier search path list |
| `create_document_from_template()` | `silo_commands.py` | Copy template, strip identity, stamp Silo properties |
| `_clean_template_zip()` | `silo_commands.py` | Strip `silo/template.json` and `silo/manifest.json` from a copy |

441
docs/src/quicknav/SPEC.md Normal file
View File

@@ -0,0 +1,441 @@
# QuickNav — Keyboard Navigation Addon Specification
**Addon name:** QuickNav
**Type:** Pure Python FreeCAD addon (no C++ required)
**Compatibility:** FreeCAD 1.0+, Kindred Create 0.1+
**Location:** `mods/quicknav/`
---
## 1. Overview
QuickNav provides keyboard-driven command access for FreeCAD and Kindred Create. It replaces mouse-heavy toolbar navigation with a numbered key system organized by workbench and command grouping. The addon is activated by loading its workbench and toggled on/off with the `0` key.
### Design Goals
- Numbers `1-9` execute commands within the active command grouping
- `Shift+1-9` switches command grouping within the active workbench
- `Ctrl+1-9` switches workbench context
- All groupings and workbenches are ordered by most-recently-used (MRU) history
- History is unlimited internally, top 9 shown, remainder scrollable/clickable
- Mouse interaction remains fully functional — QuickNav is purely additive
- Configuration persisted via `FreeCAD.ParamGet()`
---
## 2. Terminology
| Term | Definition |
|------|-----------|
| **Workbench** | A FreeCAD workbench (Sketcher, PartDesign, Assembly, etc.). Fixed assignment to Ctrl+N slots. |
| **Command Grouping** | A logical group of commands within a workbench, mapped from existing FreeCAD toolbar groupings. Max 9 per tier. |
| **Active Grouping** | The left-most visible grouping in the navigation bar. Its commands are accessible via `1-9`. |
| **Navigation Bar** | Bottom toolbar displaying the current state: active workbench, groupings, and numbered commands. |
| **MRU Stack** | Most-recently-used ordering. Position 0 = currently active, 1 = previously active, etc. |
| **Tier** | When a workbench has >9 command groupings, they are split: Tier 1 (most common 9), Tier 2 (next 9). |
---
## 3. Key Bindings
### 3.1 Mode Toggle
| Key | Action |
|-----|--------|
| `0` | Toggle QuickNav on/off. When off, all QuickNav key interception is disabled and the navigation bar hides. |
### 3.2 Command Execution
| Key | Action |
|-----|--------|
| `1-9` | Execute the Nth command in the active grouping. If the command is auto-executable (e.g., Pad after closed sketch), execute immediately. Otherwise, enter tool mode (same as clicking the toolbar button). |
### 3.3 Grouping Navigation
| Key | Action |
|-----|--------|
| `Shift+1-9` | Switch to the Nth command grouping (MRU ordered) within the current workbench. The newly activated grouping moves to position 0 in the MRU stack. |
| `Shift+Left/Right` | Scroll through groupings beyond the visible 9. |
### 3.4 Workbench Navigation
| Key | Action |
|-----|--------|
| `Ctrl+1` | Sketcher |
| `Ctrl+2` | Part Design |
| `Ctrl+3` | Assembly |
| `Ctrl+4` | Spreadsheet |
| `Ctrl+5` | TechDraw |
| `Ctrl+6-9` | User-configurable / additional workbenches |
Switching workbench via `Ctrl+N` also restores that workbench's last-active command grouping.
---
## 4. Navigation Bar
The navigation bar is a `QToolBar` positioned at the bottom of the main window (replacing or sitting alongside FreeCAD's default bottom toolbar area).
### 4.1 Layout
```
┌─────────────────────────────────────────────────────────────────────┐
│ [WB: Sketcher] │ ❶ Primitives │ ② Constraints │ ③ Dimensions │ ◀▶ │
│ │ 1:Line 2:Rect 3:Circle 4:Arc 5:Point 6:Slot ... │
└─────────────────────────────────────────────────────────────────────┘
```
- **Left section:** Current workbench name with Ctrl+N hint
- **Middle section (top row):** Command groupings, MRU ordered. Active grouping is ❶ (filled circle), others are ②③ etc. Scrollable horizontally if >9.
- **Middle section (bottom row):** Commands within the active grouping, numbered 1-9
- **Right section:** Scroll arrows for overflow groupings
### 4.2 Visual States
- **Active grouping:** Bold text, filled number badge, Catppuccin Mocha `blue` (#89b4fa) accent
- **Inactive groupings:** Normal text, outlined number badge, `surface1` (#45475a) text
- **Hovered command:** `surface2` (#585b70) background highlight
- **Active command (tool in use):** `green` (#a6e3a1) underline indicator
### 4.3 Mouse Interaction
- Click any grouping to activate it (equivalent to Shift+N)
- Click any command to execute it (equivalent to pressing N)
- Scroll wheel on grouping area to cycle through overflow groupings
- Click scroll arrows to page through overflow
---
## 5. Workbench Command Groupings
Each workbench's existing FreeCAD toolbars map to command groupings. Where a workbench has >9 toolbars, split into Tier 1 (default, most common) and Tier 2 (accessible via scrolling or `Shift+Left/Right`).
### 5.1 Sketcher (Ctrl+1)
| Grouping | Commands (1-9) |
|----------|---------------|
| Primitives | Line, Rectangle, Circle, Arc, Point, Slot, B-Spline, Polyline, Ellipse |
| Constraints | Coincident, Horizontal, Vertical, Parallel, Perpendicular, Tangent, Equal, Symmetric, Block |
| Dimensions | Distance, Horizontal Distance, Vertical Distance, Radius, Diameter, Angle, Lock, Constrain Refraction |
| Construction | Toggle Construction, External Geometry, Carbon Copy, Offset, Trim, Extend, Split |
| Tools | Mirror, Array (Linear), Array (Polar), Move, Rotate, Scale, Close Shape, Connect Edges |
### 5.2 Part Design (Ctrl+2)
| Grouping | Commands (1-9) |
|----------|---------------|
| Additive | Pad, Revolution, Additive Loft, Additive Pipe, Additive Helix, Additive Box, Additive Cylinder, Additive Sphere, Additive Cone |
| Subtractive | Pocket, Hole, Groove, Subtractive Loft, Subtractive Pipe, Subtractive Helix, Subtractive Box, Subtractive Cylinder, Subtractive Sphere |
| Datums | New Sketch, Datum Plane, Datum Line, Datum Point, Shape Binder, Sub-Shape Binder, ZTools Datum Creator, ZTools Datum Manager |
| Transformations | Mirrored, Linear Pattern, Polar Pattern, MultiTransform, ZTools Rotated Linear Pattern |
| Modeling | Fillet, Chamfer, Draft, Thickness, Boolean, ZTools Enhanced Pocket |
### 5.3 Assembly (Ctrl+3)
| Grouping | Commands (1-9) |
|----------|---------------|
| Components | Insert Component, Create Part, Create Assembly, Ground, BOM |
| Joints | Fixed, Revolute, Cylindrical, Slider, Ball, Planar, Distance, Angle, Parallel |
| Patterns | ZTools Linear Pattern, ZTools Polar Pattern |
### 5.4 Spreadsheet (Ctrl+4)
| Grouping | Commands (1-9) |
|----------|---------------|
| Editing | Merge Cells, Split Cell, Alias, Import CSV, Export CSV |
| Formatting | Bold, Italic, Underline, Align Left, Align Center, Align Right, BG Color, Text Color, Quick Alias |
### 5.5 TechDraw (Ctrl+5)
Groupings derived from TechDraw's existing toolbars at runtime.
> **Note:** The exact command lists above are initial defaults. The addon discovers available commands from each workbench's toolbar structure at activation time and falls back to these defaults only if discovery fails.
---
## 6. MRU History Behavior
### 6.1 Grouping History (per workbench)
Each workbench maintains its own grouping MRU stack.
- When a grouping is activated (via `Shift+N` or mouse click), it moves to position 0
- The previously active grouping moves to position 1, everything else shifts down
- Position 0 is always the active grouping (already selected, shown leftmost)
- `Shift+1` is a no-op (already active), `Shift+2` activates the previous grouping, etc.
### 6.2 Workbench History
- Workbenches have fixed Ctrl+N assignments (not MRU ordered)
- However, each workbench remembers its last-active grouping
- Switching to a workbench restores its last-active grouping as position 0
### 6.3 Persistence
Stored in `FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/QuickNav")`:
| Parameter | Type | Description |
|-----------|------|-------------|
| `Enabled` | Bool | Whether QuickNav is currently active |
| `GroupHistory/<Workbench>` | String | Semicolon-delimited list of grouping names in MRU order |
| `LastGrouping/<Workbench>` | String | Name of the last-active grouping per workbench |
| `CustomSlots/Ctrl6` through `Ctrl9` | String | Workbench names for user-configurable slots |
---
## 7. Auto-Execution Logic
When a command is invoked via number key, QuickNav checks if the command can be auto-executed:
### 7.1 Auto-Execute Conditions
A command auto-executes (runs and completes without entering a persistent mode) when:
1. **Pad/Pocket after closed sketch:** If the active body has a sketch that was just closed (sketch edit mode exited with a closed profile), pressing the Pad or Pocket command key creates the feature with default parameters. The task panel still opens for parameter adjustment.
2. **Boolean operations:** If exactly two bodies/shapes are selected, boolean commands execute with defaults.
3. **Constraint application:** If appropriate geometry is pre-selected in Sketcher, constraint commands apply immediately.
### 7.2 Mode-Entry (Default)
All other commands enter their standard FreeCAD tool mode — identical to clicking the toolbar button. The user interacts with the 3D view and/or task panel as normal.
---
## 8. Key Event Handling
### 8.1 Event Filter Architecture
```python
class QuickNavEventFilter(QObject):
"""Installed on FreeCAD's main window via installEventFilter().
Intercepts KeyPress events when QuickNav is active.
Passes through all events when QuickNav is inactive.
"""
def eventFilter(self, obj, event):
if event.type() != QEvent.KeyPress:
return False
if not self._active:
return False
# Don't intercept when a text input widget has focus
focused = QApplication.focusWidget()
if isinstance(focused, (QLineEdit, QTextEdit, QPlainTextEdit, QSpinBox, QDoubleSpinBox)):
return False
# Don't intercept when task panel input fields are focused
if self._is_task_panel_input(focused):
return False
key = event.key()
modifiers = event.modifiers()
if key == Qt.Key_0 and modifiers == Qt.NoModifier:
self.toggle_active()
return True
if key >= Qt.Key_1 and key <= Qt.Key_9:
n = key - Qt.Key_0
if modifiers == Qt.ControlModifier:
self.switch_workbench(n)
return True
elif modifiers == Qt.ShiftModifier:
self.switch_grouping(n)
return True
elif modifiers == Qt.NoModifier:
self.execute_command(n)
return True
return False # Pass through all other keys
```
### 8.2 Conflict Resolution
QuickNav's event filter takes priority when active. FreeCAD's existing keybindings for `Ctrl+1` through `Ctrl+9` (if any) are overridden while QuickNav is enabled. The original bindings are restored when QuickNav is toggled off or unloaded.
Existing `Shift+` and bare number key bindings in FreeCAD are similarly overridden only while QuickNav is active. This is safe because:
- FreeCAD does not use bare number keys as shortcuts by default
- Shift+number is not commonly bound in default FreeCAD
### 8.3 Input Widget Safety
The event filter must NOT intercept keys when the user is:
- Typing in the Python console
- Entering values in the task panel (dimensions, parameters)
- Editing spreadsheet cells
- Typing in any `QLineEdit`, `QTextEdit`, `QSpinBox`, or `QDoubleSpinBox`
- Using the Sketcher's inline dimension input
---
## 9. Addon Structure
```
mods/quicknav/
├── package.xml # FreeCAD addon manifest with <kindred> extension
├── Init.py # Non-GUI initialization (no-op)
├── InitGui.py # Registers QuickNavWorkbench
├── quicknav/
│ ├── __init__.py
│ ├── core.py # QuickNavManager singleton — orchestrates state
│ ├── event_filter.py # QuickNavEventFilter (QObject)
│ ├── nav_bar.py # NavigationBar (QToolBar subclass)
│ ├── workbench_map.py # Fixed workbench → Ctrl+N mapping + grouping discovery
│ ├── history.py # MRU stack with ParamGet persistence
│ ├── auto_exec.py # Auto-execution condition checks
│ ├── commands.py # FreeCAD command wrappers (QuickNav_Toggle, etc.)
│ └── resources/
│ ├── icons/ # Number badge SVGs, QuickNav icon
│ └── theme.py # Catppuccin Mocha color tokens
└── tests/
└── test_history.py # MRU stack unit tests
```
### 9.1 Manifest
```xml
<?xml version="1.0" encoding="UTF-8"?>
<package format="1">
<name>QuickNav</name>
<description>Keyboard-driven toolbar navigation</description>
<version>0.1.0</version>
<maintainer email="dev@kindred-systems.com">Kindred Systems</maintainer>
<license>LGPL-2.1</license>
<content>
<workbench>
<classname>QuickNavWorkbench</classname>
</workbench>
</content>
<kindred>
<min_create_version>0.1.0</min_create_version>
<load_priority>10</load_priority>
<pure_python>true</pure_python>
<dependencies>
<dependency>sdk</dependency>
</dependencies>
</kindred>
</package>
```
### 9.2 Activation
QuickNav activates when its workbench is loaded (via the addon loader or manual activation). It installs the event filter on the main window and creates the navigation bar. The workbench itself is invisible — it does not add its own toolbars or menus beyond the navigation bar. It acts as a transparent overlay on whatever workbench the user is actually working in.
```python
class QuickNavWorkbench(Gui.Workbench):
"""Invisible workbench that installs QuickNav on load.
QuickNav doesn't replace the active workbench — it layers on top.
Loading QuickNav installs the event filter and nav bar, then
immediately re-activates the previously active workbench.
"""
def Initialize(self):
QuickNavManager.instance().install()
def Activated(self):
# Re-activate the previous workbench so QuickNav is transparent
prev = QuickNavManager.instance().previous_workbench
if prev:
Gui.activateWorkbench(prev)
def Deactivated(self):
pass
def GetClassName(self):
return "Gui::PythonWorkbench"
```
**Alternative (preferred for Create):** Instead of a workbench, QuickNav can be activated directly from `Create/InitGui.py` at boot, gated by the `Enabled` preference. This avoids the workbench-switching dance entirely. The `QuickNavWorkbench` registration is kept for standalone FreeCAD compatibility.
---
## 10. Command Discovery
At activation time, QuickNav introspects each workbench's toolbars to build the command grouping map.
```python
def discover_groupings(workbench_name: str) -> list[CommandGrouping]:
"""Discover command groupings from a workbench's toolbar structure.
1. Temporarily activate the workbench (if not already active)
2. Enumerate QToolBars from the main window
3. Map toolbar name → list of QAction names
4. Filter out non-command actions (separators, widgets)
5. Split into tiers if >9 groupings
6. Restore the previously active workbench
"""
```
### 10.1 Fallback Defaults
If toolbar discovery fails (workbench not initialized, empty toolbars), QuickNav falls back to the hardcoded groupings in Section 5. These are stored as a Python dict in `workbench_map.py`.
### 10.2 ZTools Integration
ZTools commands injected via `WorkbenchManipulator` appear in the discovered toolbars and are automatically included in the relevant groupings. No special handling is needed — QuickNav discovers commands after all manipulators have run.
---
## 11. FreeCAD Compatibility
QuickNav is designed as a standalone FreeCAD addon that works without Kindred Create or the SDK.
| Feature | FreeCAD | Kindred Create |
|---------|---------|----------------|
| Core navigation (keys, nav bar) | ✅ | ✅ |
| Catppuccin Mocha theming | ❌ (uses Qt defaults) | ✅ (via SDK theme tokens) |
| Auto-boot on startup | ❌ (manual workbench activation) | ✅ (via addon loader) |
| ZTools commands in groupings | ❌ (not present) | ✅ (discovered from manipulated toolbars) |
The SDK dependency is optional — QuickNav checks for `kindred_sdk` availability and degrades gracefully:
```python
try:
from kindred_sdk.theme import get_theme_tokens
THEME = get_theme_tokens()
except ImportError:
THEME = None # Use Qt default palette
```
---
## 12. Implementation Phases
### Phase 1: Core Infrastructure
- Event filter with key interception and input widget safety
- QuickNavManager singleton with toggle on/off
- Navigation bar widget (QToolBar) with basic layout
- Hardcoded workbench/grouping maps from Section 5
- ParamGet persistence for enabled state
### Phase 2: Dynamic Discovery
- Toolbar introspection for command grouping discovery
- MRU history with persistence
- Grouping overflow scrolling
- Workbench restore (last-active grouping per workbench)
### Phase 3: Auto-Execution
- Context-aware auto-execute logic
- Sketcher closed-profile detection for Pad/Pocket
- Pre-selection constraint application
### Phase 4: Polish
- Number badge SVG icons
- Catppuccin Mocha theming (conditional on SDK)
- Scroll animations
- Settings dialog (custom Ctrl+6-9 assignments)
- FreeCAD standalone packaging
---
## 13. Open Questions
1. **Tier switching UX:** When a workbench has >9 groupings split into tiers, should `Shift+0` toggle between tiers, or should tiers be purely a scroll/mouse concept?
2. **Visual number badges:** Should the commands in the nav bar show keycap-style badges (like `⌨ 1`) or just prepend the number (`1: Line`)?
3. **Sketcher inline dimension input:** FreeCAD's Sketcher has an inline dimension entry that isn't a standard QLineEdit. Need to verify the event filter correctly identifies and skips this widget.
4. **Ctrl+N conflicts with Create shortcuts:** Verify that Create/Silo don't already bind Ctrl+1 through Ctrl+9. The Silo toggle uses Ctrl+O/S/N, so these should be clear.

View File

@@ -77,7 +77,7 @@ Defined in the root `CMakeLists.txt`:
| Constant | Value | Description |
|----------|-------|-------------|
| `KINDRED_CREATE_VERSION` | `0.1.0` | Kindred Create version |
| `KINDRED_CREATE_VERSION` | `0.1.5` | Kindred Create version |
| `FREECAD_VERSION` | `1.0.0` | FreeCAD base version |
These are injected into `src/Mod/Create/version.py` at build time via `version.py.in`.

View File

@@ -0,0 +1,219 @@
# Assembly Integration
The Kindred solver integrates with FreeCAD's Assembly workbench through the KCSolve pluggable solver framework. This page describes the bridge layer, preference system, and interactive drag protocol.
## KindredSolver class
**Source:** `mods/solver/kindred_solver/solver.py`
`KindredSolver` subclasses `kcsolve.IKCSolver` and implements the solver interface:
```python
class KindredSolver(kcsolve.IKCSolver):
def name(self):
return "Kindred (Newton-Raphson)"
def supported_joints(self):
return list(_SUPPORTED) # 20 of 24 BaseJointKind values
def solve(self, ctx): # Static solve
def diagnose(self, ctx): # Constraint analysis
def pre_drag(self, ctx, drag_parts): # Begin drag session
def drag_step(self, drag_placements): # Mouse move during drag
def post_drag(self): # End drag session
def is_deterministic(self): # Returns True
```
### Registration
The solver is registered at addon load time via `Init.py`:
```python
import kcsolve
from kindred_solver import KindredSolver
kcsolve.register_solver("kindred", KindredSolver)
```
The `mods/solver/` directory is a FreeCAD addon discovered by the addon loader through its `package.xml` manifest.
### Supported joints
The Kindred solver handles 20 of the 24 `BaseJointKind` values. The remaining 4 are stubs that produce no residuals:
| Supported | Stub (no residuals) |
|-----------|-------------------|
| Coincident, PointOnLine, PointInPlane, Concentric, Tangent, Planar, LineInPlane, Parallel, Perpendicular, Angle, Fixed, Revolute, Cylindrical, Slider, Ball, Screw, Universal, Gear, RackPinion, DistancePointPoint | Cam, Slot, DistanceCylSph, Custom |
### Joint limits
Joint travel limits (`Constraint.limits`) are accepted but not enforced. The solver logs a warning once per instance when limits are encountered. Enforcing inequality constraints requires active-set or barrier-method extensions beyond the current Newton-Raphson formulation.
## Solver selection
### C++ preference
`AssemblyObject::getOrCreateSolver()` reads the user preference to select the solver backend:
```cpp
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Assembly");
std::string solverName = hGrp->GetASCII("Solver", "");
solver_ = KCSolve::SolverRegistry::instance().get(solverName);
```
An empty string (`""`) returns the registry default (the first solver registered, which is OndselSolver). Setting `"kindred"` selects the Kindred solver.
`resetSolver()` clears the cached solver instance so the next solve picks up preference changes.
### Preferences UI
The Assembly preferences page (`Edit > Preferences > Assembly`) includes a "Solver backend" combo box populated from the registry at load time:
- **Default** -- empty string, uses the registry default (OndselSolver)
- **OndselSolver (Lagrangian)** -- `"ondsel"`
- **Kindred (Newton-Raphson)** -- `"kindred"` (available when the solver addon is loaded)
The preference is stored as `Mod/Assembly/Solver` in the FreeCAD parameter system.
### Programmatic switching
From the Python console:
```python
import FreeCAD
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Assembly")
# Switch to Kindred
pref.SetString("Solver", "kindred")
# Switch back to default
pref.SetString("Solver", "")
# Force the active assembly to pick up the change
if hasattr(FreeCADGui, "ActiveDocument"):
for obj in FreeCAD.ActiveDocument.Objects:
if hasattr(obj, "resetSolver"):
obj.resetSolver()
```
## Interactive drag protocol
The drag protocol provides real-time constraint solving during viewport part dragging. It is a three-phase protocol with a caching layer that avoids rebuilding the constraint system on every mouse move.
### pre_drag(ctx, drag_parts)
Called when the user begins dragging. Builds the constraint system once, runs the substitution pre-pass, constructs the symbolic Jacobian, compiles the evaluator, performs an initial solve, and caches everything in a `_DragCache` for reuse across subsequent `drag_step()` calls.
```python
def pre_drag(self, ctx, drag_parts):
self._drag_ctx = ctx
self._drag_parts = set(drag_parts)
system = _build_system(ctx)
half_spaces = compute_half_spaces(...)
weight_vec = build_weight_vector(system.params)
residuals = substitution_pass(system.all_residuals, system.params)
# single_equation_pass is intentionally skipped — it bakes variable
# values as constants that become stale when dragged parts move.
jac_exprs = [[r.diff(name).simplify() for name in free] for r in residuals]
compiled_eval = try_compile_system(residuals, jac_exprs, ...)
# Initial solve (Newton-Raphson + BFGS fallback)
newton_solve(residuals, system.params, ...)
# Cache for drag_step() reuse
cache = _DragCache()
cache.system = system
cache.residuals = residuals
cache.jac_exprs = jac_exprs
cache.compiled_eval = compiled_eval
cache.half_spaces = half_spaces
cache.weight_vec = weight_vec
...
return result
```
**Important:** `single_equation_pass` is not used in the drag path. It analytically solves single-variable equations and bakes the results as `Const()` nodes into downstream expressions. During drag, those baked values become stale when part positions change, causing constraints to silently stop being enforced. Only `substitution_pass` (which replaces genuinely grounded parameters) is safe to cache.
### drag_step(drag_placements)
Called on each mouse move. Updates only the dragged part's 7 parameter values in the cached `ParamTable`, then re-solves using the cached residuals, Jacobian, and compiled evaluator. No system rebuild occurs.
```python
def drag_step(self, drag_placements):
cache = self._drag_cache
params = cache.system.params
# Update only the dragged part's parameters
for pr in drag_placements:
pfx = pr.id + "/"
params.set_value(pfx + "tx", pr.placement.position[0])
params.set_value(pfx + "ty", pr.placement.position[1])
params.set_value(pfx + "tz", pr.placement.position[2])
params.set_value(pfx + "qw", pr.placement.quaternion[0])
params.set_value(pfx + "qx", pr.placement.quaternion[1])
params.set_value(pfx + "qy", pr.placement.quaternion[2])
params.set_value(pfx + "qz", pr.placement.quaternion[3])
# Solve with cached artifacts — no rebuild
newton_solve(cache.residuals, params, ...,
jac_exprs=cache.jac_exprs,
compiled_eval=cache.compiled_eval)
return result
```
### post_drag()
Called when the drag ends. Clears the cached state.
```python
def post_drag(self):
self._drag_ctx = None
self._drag_parts = None
self._drag_cache = None
```
### _DragCache
The cache holds all artifacts built in `pre_drag()` that are invariant across drag steps (constraint topology doesn't change during a drag):
| Field | Contents |
|-------|----------|
| `system` | `_System` -- owns `ParamTable` and `Expr` trees |
| `residuals` | `list[Expr]` -- after substitution pass |
| `jac_exprs` | `list[list[Expr]]` -- symbolic Jacobian |
| `compiled_eval` | `Callable` or `None` -- native compiled evaluator |
| `half_spaces` | `list[HalfSpace]` -- branch trackers |
| `weight_vec` | `ndarray` or `None` -- minimum-movement weights |
| `post_step_fn` | `Callable` or `None` -- half-space correction callback |
### Performance
The caching layer eliminates the expensive per-frame overhead (~150 ms for system build + Jacobian construction + compilation). Each `drag_step()` only evaluates the cached expressions at updated parameter values:
- Newton-Raphson converges in 1-2 iterations from a nearby initial guess
- The compiled evaluator (`codegen.py`) uses native Python `exec` for flat evaluation, avoiding the recursive tree-walk overhead
- The substitution pass compiles grounded-body parameters to constants, reducing the effective system size
- DOF counting is skipped during drag for speed (`result.dof = -1`)
## Diagnostics integration
`diagnose(ctx)` builds the constraint system and runs overconstrained detection, returning a list of `kcsolve.ConstraintDiagnostic` objects. The Assembly module calls this to populate the constraint diagnostics panel.
```python
def diagnose(self, ctx):
system = _build_system(ctx)
residuals = substitution_pass(system.all_residuals, system.params)
return _run_diagnostics(residuals, system.params, system.residual_ranges, ctx)
```
## Not yet implemented
- **Kinematic simulation** (`run_kinematic`, `num_frames`, `update_for_frame`) -- the base class defaults return `Failed`. Requires time-stepping integration with motion driver expression evaluation.
- **Joint limit enforcement** -- inequality constraints need active-set or barrier solver extensions.
- **Fixed-joint bundling** (`supports_bundle_fixed()` returns `False`) -- the solver receives unbundled parts; the Assembly module pre-bundles when needed.
- **Native export** (`export_native()`) -- no solver-native debug format defined.

View File

@@ -0,0 +1,116 @@
# Constraints
Each constraint type maps to a class that produces residual expressions. The residuals equal zero when the constraint is satisfied. The number of residuals equals the number of degrees of freedom removed.
**Source:** `mods/solver/kindred_solver/constraints.py`, `mods/solver/kindred_solver/geometry.py`
## Constraint vocabulary
### Point constraints
| Type | DOF removed | Residuals |
|------|-------------|-----------|
| **Coincident** | 3 | `p_i - p_j` (world-frame marker origins coincide) |
| **PointOnLine** | 2 | Two components of `(p_i - p_j) x z_j` (point lies on line through `p_j` along `z_j`) |
| **PointInPlane** | 1 | `(p_i - p_j) . z_j - offset` (signed distance to plane) |
### Orientation constraints
| Type | DOF removed | Residuals |
|------|-------------|-----------|
| **Parallel** | 2 | Two components of `z_i x z_j` (cross product of Z-axes is zero) |
| **Perpendicular** | 1 | `z_i . z_j` (dot product of Z-axes is zero) |
| **Angle** | 1 | `z_i . z_j - cos(angle)` |
### Axis/surface constraints
| Type | DOF removed | Residuals |
|------|-------------|-----------|
| **Concentric** | 4 | Parallel Z-axes (2) + point-on-line (2) |
| **Tangent** | 1 | `(p_i - p_j) . z_j` (signed distance along normal) |
| **Planar** | 3 | Parallel normals (2) + point-in-plane (1) |
| **LineInPlane** | 2 | Point-in-plane (1) + `z_i . n_j` (line direction perpendicular to normal) (1) |
### Kinematic joints
| Type | DOF removed | DOF remaining | Residuals |
|------|-------------|---------------|-----------|
| **Fixed** | 6 | 0 | Coincident origins (3) + quaternion error imaginary parts (3) |
| **Ball** | 3 | 3 | Coincident origins (same as Coincident) |
| **Revolute** | 5 | 1 (rotation about Z) | Coincident origins (3) + parallel Z-axes (2) |
| **Cylindrical** | 4 | 2 (rotation + slide) | Parallel Z-axes (2) + point-on-line (2) |
| **Slider** | 5 | 1 (slide along Z) | Parallel Z-axes (2) + point-on-line (2) + twist lock: `x_i . y_j` (1) |
| **Screw** | 5 | 1 (helical) | Cylindrical (4) + pitch coupling: `axial - pitch * qz_rel / pi` (1) |
| **Universal** | 4 | 2 (rotation about each Z) | Coincident origins (3) + perpendicular Z-axes (1) |
### Mechanical elements
| Type | DOF removed | Residuals |
|------|-------------|-----------|
| **Gear** | 1 | `r_i * qz_i + r_j * qz_j` (coupled rotation via quaternion Z-components) |
| **RackPinion** | 1 | `translation - 2 * pitch_radius * qz_i` (rotation-translation coupling) |
| **Cam** | 0 | Stub (no residuals) |
| **Slot** | 0 | Stub (no residuals) |
### Distance constraints
| Type | DOF removed | Residuals |
|------|-------------|-----------|
| **DistancePointPoint** | 1 | `\|p_i - p_j\|^2 - d^2` (squared form avoids sqrt in Jacobian) |
| **DistanceCylSph** | 0 | Stub (geometry classification dependent) |
## Marker convention
Every constraint references two parts (`body_i`, `body_j`) with local coordinate frames called markers. Each marker has a position (attachment point on the part) and a quaternion (orientation).
The marker Z-axis defines the constraint direction:
- **Revolute:** Z-axis = hinge axis
- **Planar:** Z-axis = face normal
- **PointOnLine:** Z-axis = line direction
- **Slider:** Z-axis = slide direction
The solver computes world-frame marker axes by composing the body quaternion with the marker quaternion: `q_world = q_body * q_marker`, then rotating unit vectors through the result.
## Fixed constraint orientation
The Fixed constraint locks all 6 DOF using a quaternion error formulation:
1. Compute total orientation: `q_i = q_body_i * q_marker_i`, `q_j = q_body_j * q_marker_j`
2. Compute relative quaternion: `q_err = conj(q_i) * q_j`
3. When orientations match, `q_err` is the identity quaternion `(1, 0, 0, 0)`
4. Residuals are the three imaginary components of `q_err` (should be zero)
The quaternion normalization constraint on each body provides the fourth equation needed to fully determine the quaternion.
## Rotation proxies for mechanical constraints
Gear, RackPinion, and Screw constraints need to measure rotation angles. Rather than extracting Euler angles (which would introduce transcendentals), they use the Z-component of a relative quaternion as a proxy:
```
q_local = conj(q_marker) * q_body * q_marker
angle ~ 2 * qz_local (for small angles)
```
This is exact at the solution and has correct gradient direction, which is sufficient for Newton-Raphson convergence from a nearby initial guess.
## Geometry helpers
The `geometry.py` module provides Expr-level vector operations used by constraint classes:
- `marker_z_axis(body, marker_quat)` -- world-frame Z-axis via `quat_rotate(q_body * q_marker, [0,0,1])`
- `marker_x_axis(body, marker_quat)` -- world-frame X-axis (used by Slider twist lock)
- `marker_y_axis(body, marker_quat)` -- world-frame Y-axis (used by Slider twist lock)
- `dot3(a, b)` -- dot product of Expr triples
- `cross3(a, b)` -- cross product of Expr triples
- `point_plane_distance(point, origin, normal)` -- signed distance
- `point_line_perp_components(point, origin, dir)` -- two perpendicular distance components
## Writing a new constraint
To add a constraint type:
1. Subclass `ConstraintBase` in `constraints.py`
2. Implement `residuals()` returning a list of `Expr` nodes
3. Add a case in `solver.py:_build_constraint()` to instantiate it from `BaseJointKind`
4. Add the `BaseJointKind` value to `_SUPPORTED` in `solver.py`
5. Add the residual count to the tables in `decompose.py`

View File

@@ -0,0 +1,117 @@
# Diagnostics
The solver provides three levels of constraint analysis: system-wide DOF counting, per-entity DOF decomposition, and overconstrained/conflicting constraint detection.
## DOF counting
**Source:** `mods/solver/kindred_solver/dof.py`
Degrees of freedom are computed from the Jacobian rank:
```
DOF = n_free_params - rank(J)
```
Where `n_free_params` is the number of non-fixed parameters and `rank(J)` is the numerical rank of the Jacobian evaluated at current parameter values (SVD with tolerance `1e-8`).
A well-constrained assembly has `DOF = 0` (exactly enough constraints to determine all positions). Positive DOF means underconstrained (parts can still move). Negative DOF is not possible with this formulation -- instead, rank deficiency in an overdetermined system indicates redundant constraints.
The DOF value is reported in `SolveResult.dof` after every solve.
## Per-entity DOF
**Source:** `mods/solver/kindred_solver/diagnostics.py`
`per_entity_dof()` breaks down the DOF count per body, identifying which motions remain free for each part:
1. Build the full Jacobian
2. For each non-grounded body, extract the 7 columns corresponding to its parameters
3. Compute SVD of the sub-matrix; rank = number of constrained directions
4. `remaining_dof = 7 - rank` (includes the quaternion normalization constraint counted in the rank)
5. Classify null-space vectors as free motions by analyzing their translation vs. rotation components:
- Pure translation: >80% of the null vector's energy is in `tx, ty, tz` components
- Pure rotation: >80% of the energy is in `qw, qx, qy, qz` components
- Helical: mixed
Returns a list of `EntityDOF` dataclasses:
```python
@dataclass
class EntityDOF:
entity_id: str
remaining_dof: int
free_motions: list[str] # e.g., ["rotation about Z", "translation along X"]
```
## Overconstrained detection
**Source:** `mods/solver/kindred_solver/diagnostics.py`
`find_overconstrained()` identifies redundant and conflicting constraints when the system is overconstrained (Jacobian is rank-deficient). It runs automatically when `solve()` fails to converge.
### Algorithm
Following the approach used by SolvSpace:
1. **Check rank.** Build the full Jacobian `J`, compute its rank via SVD. If `rank == n_residuals`, the system is not overconstrained -- return empty.
2. **Find redundant constraints.** For each constraint, temporarily remove its rows from J and re-check rank. If the rank is preserved, the constraint is **redundant** (removing it doesn't change the system's effective equations).
3. **Distinguish conflicting from merely redundant.** Compute the left null space of J (columns of U beyond the rank). Project the residual vector onto this null space:
```
null_residual = U_null^T @ r
residual_projection = U_null @ null_residual
```
If a redundant constraint's residuals have significant projection onto the null space, it is **conflicting** -- it's both redundant and unsatisfied, meaning it contradicts other constraints.
### Diagnostic output
Returns `ConstraintDiag` dataclasses:
```python
@dataclass
class ConstraintDiag:
constraint_index: int
kind: str # "redundant" or "conflicting"
detail: str # Human-readable explanation
```
These are converted to `kcsolve.ConstraintDiagnostic` objects in the IKCSolver bridge:
| ConstraintDiag.kind | kcsolve.DiagnosticKind |
|---------------------|----------------------|
| `"redundant"` | `Redundant` |
| `"conflicting"` | `Conflicting` |
### Example
Two Fixed joints between the same pair of parts:
- Joint A: 6 residuals (3 position + 3 orientation)
- Joint B: 6 residuals (same as Joint A)
Jacobian rank = 6 (Joint B's rows are linearly dependent on Joint A's). Both joints are detected as redundant. If the joints specify different relative positions, both are also flagged as conflicting.
## Solution preferences
**Source:** `mods/solver/kindred_solver/preference.py`
Solution preferences guide the solver toward physically intuitive solutions when multiple valid configurations exist.
### Minimum-movement weighting
The weight vector scales the Newton step to prefer solutions near the initial configuration. Translation parameters get weight `1.0`, quaternion parameters get weight `(180/pi)^2 ~ 3283`. This makes a 1-radian rotation equally "expensive" as a ~57-unit translation.
The weighted minimum-norm step is:
```
J_scaled = J @ diag(W^{-1/2})
dx_scaled = lstsq(J_scaled, -r)
dx = dx_scaled * W^{-1/2}
```
This produces the minimum-norm solution in the weighted parameter space, biasing toward small movements.
### Half-space tracking
Described in detail in [Solving Algorithms: Half-space tracking](solving.md#half-space-tracking). Preserves the initial configuration's "branch" for constraints with multiple valid solutions by detecting and correcting branch crossings during iteration.

View File

@@ -0,0 +1,96 @@
# Expression DAG
The expression DAG is the foundation of the Kindred solver. All constraint equations, Jacobian entries, and residuals are built as immutable trees of `Expr` nodes. This lets the solver compute exact symbolic derivatives and simplify constant sub-expressions before the iterative solve loop.
**Source:** `mods/solver/kindred_solver/expr.py`
## Node types
Every node is a subclass of `Expr` and implements three methods:
- `eval(env)` -- evaluate the expression given a name-to-value dictionary
- `diff(var)` -- return a new Expr tree for the partial derivative with respect to `var`
- `simplify()` -- return an algebraically simplified copy
### Leaf nodes
| Node | Description | diff(x) |
|------|-------------|---------|
| `Const(v)` | Literal floating-point value | 0 |
| `Var(name)` | Named parameter (from `ParamTable`) | 1 if name matches, else 0 |
### Unary nodes
| Node | Description | diff(x) |
|------|-------------|---------|
| `Neg(f)` | Negation: `-f` | `-f'` |
| `Sin(f)` | Sine: `sin(f)` | `cos(f) * f'` |
| `Cos(f)` | Cosine: `cos(f)` | `-sin(f) * f'` |
| `Sqrt(f)` | Square root: `sqrt(f)` | `f' / (2 * sqrt(f))` |
### Binary nodes
| Node | Description | diff(x) |
|------|-------------|---------|
| `Add(a, b)` | Sum: `a + b` | `a' + b'` |
| `Sub(a, b)` | Difference: `a - b` | `a' - b'` |
| `Mul(a, b)` | Product: `a * b` | `a'b + ab'` (product rule) |
| `Div(a, b)` | Quotient: `a / b` | `(a'b - ab') / b^2` (quotient rule) |
| `Pow(a, n)` | Power: `a^n` (constant exponent only) | `n * a^(n-1) * a'` |
### Sentinels
`ZERO = Const(0.0)` and `ONE = Const(1.0)` are pre-allocated constants used by `diff()` to avoid allocating trivial nodes.
## Operator overloading
Python's arithmetic operators are overloaded on `Expr`, so constraints can be written in natural notation:
```python
from kindred_solver.expr import Var, Const
x = Var("x")
y = Var("y")
# Build the expression: x^2 + 2*x*y - 1
expr = x**2 + 2*x*y - Const(1.0)
# Evaluate at x=3, y=4
expr.eval({"x": 3.0, "y": 4.0}) # 32.0
# Symbolic derivative w.r.t. x
dx = expr.diff("x").simplify() # 2*x + 2*y
dx.eval({"x": 3.0, "y": 4.0}) # 14.0
```
The `_wrap()` helper coerces plain `int` and `float` values to `Const` nodes automatically, so `2 * x` works without wrapping the `2`.
## Simplification
`simplify()` applies algebraic identities bottom-up:
- Constant folding: `Const(2) + Const(3)` becomes `Const(5)`
- Identity elimination: `x + 0 = x`, `x * 1 = x`, `x^0 = 1`, `x^1 = x`
- Zero propagation: `0 * x = 0`
- Negation collapse: `-(-x) = x`
- Power expansion: `x^2` becomes `x * x` (avoids `pow()` in evaluation)
Simplification is applied once to each Jacobian entry after symbolic differentiation, before the solve loop begins. This reduces the expression tree size and speeds up repeated evaluation.
## How the solver uses expressions
1. **Parameter registration.** `ParamTable.add("Part001/tx", 10.0)` creates a `Var("Part001/tx")` node and records its current value.
2. **Constraint building.** Constraint classes compose `Var` nodes with arithmetic to produce residual `Expr` trees. For example, `CoincidentConstraint` builds `body_i.world_point() - body_j.world_point()`, producing 3 residual expressions.
3. **Jacobian construction.** Newton-Raphson calls `r.diff(name).simplify()` for every (residual, free parameter) pair to build the symbolic Jacobian. This happens once before the solve loop.
4. **Evaluation.** Each Newton iteration calls `expr.eval(env)` on every residual and Jacobian entry using the current parameter snapshot. `eval()` is a simple recursive tree walk with dictionary lookups.
## Design notes
**Why not numpy directly?** Symbolic expressions give exact derivatives without finite-difference approximations, and enable pre-passes (substitution, single-equation solve) that can eliminate variables before the iterative solver runs. The overhead of tree evaluation is acceptable for the problem sizes encountered in assembly solving (typically tens to hundreds of variables).
**Why immutable?** Immutability means `diff()` can safely share sub-tree references between the original and derivative expressions. It also simplifies the substitution pass, which rebuilds trees with `Const` nodes replacing fixed `Var` nodes.
**Limitations.** `Pow` differentiation only supports constant exponents. Variable exponents would require logarithmic differentiation (`d/dx f^g = f^g * (g' * ln(f) + g * f'/f)`), which hasn't been needed for assembly constraints.

View File

@@ -0,0 +1,92 @@
# Kindred Solver Overview
The Kindred solver is an expression-based Newton-Raphson constraint solver for the Assembly workbench. It is a pure-Python implementation that registers as a pluggable backend through the [KCSolve framework](../architecture/ondsel-solver.md), providing an alternative to the built-in OndselSolver (Lagrangian) backend.
## Architecture
```
Assembly Module
┌───────────┴───────────┐
│ SolverRegistry │
│ get("kindred") │
└───────────┬───────────┘
┌───────────┴───────────┐
│ KindredSolver │
│ (kcsolve.IKCSolver) │
└───────────┬───────────┘
┌───────────────────┼───────────────────┐
│ │ │
┌────────┴────────┐ ┌──────┴──────┐ ┌────────┴────────┐
│ _build_system │ │ Solve │ │ Diagnostics │
│ ────────────── │ │ ───── │ │ ─────────── │
│ ParamTable │ │ pre-passes │ │ DOF counting │
│ RigidBody │ │ Newton-R │ │ overconstrained│
│ Constraints │ │ BFGS │ │ per-entity DOF │
│ Residuals │ │ decompose │ │ half-spaces │
└─────────────────┘ └─────────────┘ └─────────────────┘
```
## Design principles
**Symbolic differentiation.** All constraint equations are built as immutable expression DAGs (`Expr` trees). The Jacobian is computed symbolically via `expr.diff()` rather than finite differences. This gives exact derivatives, avoids numerical step-size tuning, and allows pre-passes to simplify or eliminate trivial equations before the iterative solver runs.
**Residual-based formulation.** Each constraint produces a list of residual expressions that should evaluate to zero when satisfied. A Coincident constraint produces 3 residuals (dx, dy, dz), a Revolute produces 5 (3 position + 2 axis alignment), and so on. The solver minimizes the residual vector norm.
**Unit quaternions for rotation.** Orientation is parameterized as a unit quaternion (w, x, y, z) rather than Euler angles, avoiding gimbal lock. A quaternion normalization residual (qw^2 + qx^2 + qy^2 + qz^2 - 1 = 0) is added for each free body, and quaternions are re-projected onto the unit sphere after each Newton step.
**Current placements as initial guess.** The solver uses the parts' current positions as the initial guess, so it naturally converges to the nearest solution. Combined with half-space tracking, this produces physically intuitive results without branch-switching surprises.
## Solve pipeline
When `KindredSolver.solve(ctx)` is called with a `SolveContext`:
1. **Build system** (`_build_system`) -- Create a `ParamTable` with 7 parameters per part (tx, ty, tz, qw, qx, qy, qz). Grounded parts have all parameters fixed. Build constraint objects from the context, collect their residual expressions, and add quaternion normalization residuals for free bodies.
2. **Solution preferences** -- Compute half-space trackers for branching constraints (Distance, Parallel, Angle, Perpendicular) and build a minimum-movement weight vector that penalizes quaternion changes more than translation changes.
3. **Pre-passes** -- Run the substitution pass (replace fixed parameters with constants) and the single-equation pass (analytically solve residuals with only one free variable).
4. **Solve** -- For assemblies with 8+ free bodies, decompose the constraint graph into biconnected components and solve each cluster independently. For smaller assemblies, solve the full system monolithically. In both cases, use Newton-Raphson first, falling back to L-BFGS-B if Newton doesn't converge.
5. **Post-process** -- Count degrees of freedom via Jacobian SVD rank. On failure, run overconstrained detection to identify redundant or conflicting constraints. Extract solved placements from the parameter table.
## Module map
| Module | Purpose |
|--------|---------|
| `solver.py` | `KindredSolver` class: IKCSolver bridge, solve/diagnose/drag entry points |
| `expr.py` | Immutable expression DAG with eval, diff, simplify |
| `params.py` | Parameter table: named variables with fixed/free tracking |
| `entities.py` | `RigidBody`: 7-DOF entity owning solver parameters |
| `quat.py` | Quaternion rotation as polynomial Expr trees |
| `geometry.py` | Marker axis extraction, vector ops (dot, cross, point-plane, point-line) |
| `constraints.py` | 24 constraint classes producing residual expressions |
| `newton.py` | Newton-Raphson with symbolic Jacobian, quaternion renormalization |
| `bfgs.py` | L-BFGS-B fallback via scipy |
| `prepass.py` | Substitution pass and single-equation analytical solve |
| `decompose.py` | Biconnected component graph decomposition and cluster-by-cluster solving |
| `dof.py` | DOF counting via Jacobian SVD rank |
| `diagnostics.py` | Overconstrained detection, per-entity DOF classification |
| `preference.py` | Half-space tracking and minimum-movement weighting |
## File locations
- **Solver addon:** `mods/solver/` (git submodule)
- **KCSolve C++ framework:** `src/Mod/Assembly/Solver/`
- **Python bindings:** `src/Mod/Assembly/Solver/bindings/`
- **Integration tests:** `src/Mod/Assembly/AssemblyTests/TestKindredSolverIntegration.py`
- **Unit tests:** `mods/solver/tests/`
## Related
- [Expression DAG](expression-dag.md) -- the Expr type system
- [Constraints](constraints.md) -- constraint vocabulary and residuals
- [Solving algorithms](solving.md) -- Newton-Raphson, BFGS, decomposition
- [Diagnostics](diagnostics.md) -- DOF counting, overconstrained detection
- [Assembly integration](assembly-integration.md) -- IKCSolver bridge, preferences, drag
- [Writing a custom solver](writing-a-solver.md) -- tutorial
- [KCSolve architecture](../architecture/ondsel-solver.md) -- pluggable solver framework
- [KCSolve Python API](../reference/kcsolve-python.md) -- kcsolve module reference

128
docs/src/solver/solving.md Normal file
View File

@@ -0,0 +1,128 @@
# Solving Algorithms
The Kindred solver uses a multi-stage pipeline: pre-passes reduce the system, Newton-Raphson iterates toward a solution, and L-BFGS-B provides a fallback. For large assemblies, graph decomposition splits the system into independent clusters solved in sequence.
## Pre-passes
**Source:** `mods/solver/kindred_solver/prepass.py`
Pre-passes run before the iterative solver and can eliminate variables analytically, reducing the problem size and improving convergence.
### Substitution pass
Replaces all fixed-parameter `Var` nodes with `Const` nodes carrying their current values, then simplifies. This compiles grounded-body parameters and previously-solved variables out of the expression trees.
After substitution, residuals involving only fixed parameters simplify to constants (typically zero), and Jacobian entries for those parameters become exactly zero. This reduces the effective system size without changing the linear algebra.
### Single-equation pass
Scans residuals for any that depend on exactly one free variable. If the residual is linear in that variable (`a*x + b = 0`), it solves `x = -b/a` analytically, fixes the variable, and re-substitutes.
The pass repeats until no more single-variable residuals can be solved. This handles cascading dependencies: solving one variable may reduce another residual to single-variable form.
## Newton-Raphson
**Source:** `mods/solver/kindred_solver/newton.py`
The primary iterative solver. Each iteration:
1. Evaluate the residual vector `r` and check convergence (`||r|| < tol`)
2. Evaluate the Jacobian matrix `J` by calling `expr.eval()` on pre-computed symbolic derivatives
3. Solve `J @ dx = -r` via `numpy.linalg.lstsq` (handles rank-deficient systems)
4. Update parameters: `x += dx`
5. Apply half-space correction (if configured)
6. Re-normalize quaternions to unit length
### Symbolic Jacobian
The Jacobian is built once before the solve loop by calling `r.diff(name).simplify()` for every (residual, free parameter) pair. The resulting `Expr` trees are stored and re-evaluated at the current parameter values each iteration. This gives exact derivatives with no step-size tuning.
### Weighted minimum-norm
When a weight vector is provided, the step is column-scaled to produce the weighted minimum-norm solution. The solver scales J by W^{-1/2}, solves the scaled system, then unscales the step. This biases the solver toward solutions requiring smaller parameter changes in high-weight dimensions.
The default weight vector assigns `1.0` to translation parameters and `~3283` to quaternion parameters (the square of 180/pi), making a 1-radian rotation equivalent to a ~57-unit translation. This produces physically intuitive solutions that prefer translating over rotating.
### Quaternion renormalization
After each Newton step, quaternion parameter groups `(qw, qx, qy, qz)` are re-projected onto the unit sphere by dividing by their norm. This prevents the quaternion from drifting away from unit length during iteration (the quaternion normalization residual only enforces this at convergence, not during intermediate steps).
If a quaternion degenerates to near-zero norm, it is reset to the identity quaternion `(1, 0, 0, 0)`.
### Convergence
Newton-Raphson runs for up to 100 iterations with tolerance `1e-10` on the residual norm. For well-conditioned systems near the solution, convergence is typically quadratic (3-5 iterations). Interactive drag from a nearby position typically converges in 1-2 iterations.
## L-BFGS-B fallback
**Source:** `mods/solver/kindred_solver/bfgs.py`
If Newton-Raphson fails to converge, L-BFGS-B minimizes the sum of squared residuals: `f(x) = 0.5 * sum(r_i^2)`. This is a quasi-Newton method that approximates the Hessian from gradient history, with bounded memory usage.
The gradient is computed analytically from the same symbolic Jacobian: `grad = J^T @ r`. This is passed directly to `scipy.optimize.minimize` via the `jac=True` interface to avoid redundant function evaluations.
L-BFGS-B is more robust for ill-conditioned systems where the Jacobian is nearly singular, but converges more slowly (superlinear rather than quadratic). It runs for up to 200 iterations.
If scipy is not available, the fallback is skipped gracefully.
## Graph decomposition
**Source:** `mods/solver/kindred_solver/decompose.py`
For assemblies with 8 or more free bodies, the solver decomposes the constraint graph into clusters and solves them independently. This improves performance for large assemblies by reducing the Jacobian size from O(n^2) to the sum of smaller cluster Jacobians.
### Algorithm
1. **Build constraint graph.** Bodies are nodes, constraints are edges weighted by their residual count (DOF removed). Grounded bodies are tagged.
2. **Find biconnected components.** Using `networkx.biconnected_components()`, decompose the graph into rigid clusters. Articulation points (bodies shared between clusters) are identified.
3. **Build block-cut tree.** A bipartite graph of clusters and articulation points, rooted at a grounded cluster.
4. **BFS ordering.** Traverse the block-cut tree root-to-leaf, producing a solve order where grounded clusters come first and boundary conditions propagate outward.
5. **Solve each cluster.** For each cluster in order:
- Fix boundary bodies that were already solved by previous clusters (their parameters become constants)
- Collect the cluster's residuals and quaternion normalization equations
- Run substitution pass (compiles fixed boundary values to constants)
- Newton-Raphson + BFGS fallback on the reduced system
- Mark the cluster's bodies as solved
- Unfix boundary parameters for downstream clusters
### Example
Consider a chain of 4 bodies: `Ground -- A -- B -- C` with joints at each connection. This decomposes into two biconnected components (if the joints create articulation points):
- Cluster 1: {Ground, A} -- solved first (grounded)
- Cluster 2: {A, B, C} -- solved second with A's parameters fixed to Cluster 1's result
The 21-variable monolithic system (3 free bodies x 7 params) becomes two smaller systems solved in sequence.
### Disconnected sub-assemblies
The decomposition also handles disconnected components. Each connected component of the constraint graph is decomposed independently. Components without a grounded body will fail to solve (returning `NoGroundedParts`).
### Pebble game integration
The `classify_cluster_rigidity()` function uses the pebble game algorithm from `GNN/solver/datagen/` to classify clusters as well-constrained, underconstrained, overconstrained, or mixed. This provides fast O(n) rigidity analysis without running the full solver.
## Half-space tracking
**Source:** `mods/solver/kindred_solver/preference.py`
Many constraints have multiple valid solutions (branches). A distance constraint between two points can be satisfied with the points on either side of each other. Parallel axes can point in the same or opposite directions.
Half-space tracking preserves the initial configuration branch:
1. **At setup:** Evaluate an indicator function for each branching constraint. Record its sign as the reference branch.
2. **After each Newton step:** Re-evaluate the indicator. If the sign flipped, apply a correction to push the solution back to the reference branch.
Tracked constraint types:
| Constraint | Indicator | Correction |
|-----------|-----------|------------|
| DistancePointPoint (d > 0) | Dot product of displacement with reference direction | Reflect the moving body's position |
| Parallel | `z_i . z_j` (same vs. opposite direction) | None (tracked for monitoring) |
| Angle | Dominant cross product component | None (tracked for monitoring) |
| Perpendicular | Dominant cross product component | None (tracked for monitoring) |

View File

@@ -0,0 +1,256 @@
# Writing a Custom Solver
The KCSolve framework lets you implement a solver backend in pure Python, register it at runtime, and select it through the Assembly preferences. This tutorial walks through building a minimal solver and then extending it.
## Minimal solver
A solver must subclass `kcsolve.IKCSolver` and implement three methods:
```python
import kcsolve
class MySolver(kcsolve.IKCSolver):
def __init__(self):
super().__init__() # required for pybind11 trampoline
def name(self):
return "My Custom Solver"
def supported_joints(self):
return [
kcsolve.BaseJointKind.Fixed,
kcsolve.BaseJointKind.Revolute,
]
def solve(self, ctx):
result = kcsolve.SolveResult()
# Find grounded parts
grounded = {p.id for p in ctx.parts if p.grounded}
if not grounded:
result.status = kcsolve.SolveStatus.NoGroundedParts
return result
# Your solving logic here...
# For each non-grounded part, compute its solved placement
for part in ctx.parts:
if part.grounded:
continue
pr = kcsolve.SolveResult.PartResult()
pr.id = part.id
pr.placement = part.placement # use current placement as placeholder
result.placements = result.placements + [pr]
result.status = kcsolve.SolveStatus.Success
result.dof = 0
return result
```
Register it:
```python
kcsolve.register_solver("my_solver", MySolver)
```
Test it from the FreeCAD console:
```python
solver = kcsolve.load("my_solver")
print(solver.name()) # "My Custom Solver"
ctx = kcsolve.SolveContext()
# ... build context ...
result = solver.solve(ctx)
print(result.status) # SolveStatus.Success
```
## Addon packaging
To make your solver load automatically, create a FreeCAD addon:
```
my_solver_addon/
package.xml # Addon manifest
Init.py # Registration entry point
my_solver/
__init__.py
solver.py # MySolver class
```
**package.xml:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<package format="1">
<name>MyCustomSolver</name>
<description>Custom assembly constraint solver</description>
<version>0.1.0</version>
<content>
<preferencepack>
<name>MySolver</name>
</preferencepack>
</content>
</package>
```
**Init.py:**
```python
import kcsolve
from my_solver.solver import MySolver
kcsolve.register_solver("my_solver", MySolver)
```
Place the addon in the FreeCAD Mod directory or as a git submodule in `mods/`.
## Working with SolveContext
The `SolveContext` contains everything the solver needs:
### Parts
```python
for part in ctx.parts:
print(f"{part.id}: grounded={part.grounded}")
print(f" position: {list(part.placement.position)}")
print(f" quaternion: {list(part.placement.quaternion)}")
print(f" mass: {part.mass}")
```
Each part has 7 degrees of freedom: 3 translation (x, y, z) and 4 quaternion components (w, x, y, z) with a unit-norm constraint reducing the rotational DOF to 3.
**Quaternion convention:** `(w, x, y, z)` where `w` is the scalar part. This differs from FreeCAD's `Base.Rotation(x, y, z, w)`. The adapter layer handles the swap.
### Constraints
```python
for c in ctx.constraints:
if not c.activated:
continue
print(f"{c.id}: {c.type} between {c.part_i} and {c.part_j}")
print(f" marker_i: pos={list(c.marker_i.position)}, "
f"quat={list(c.marker_i.quaternion)}")
print(f" params: {list(c.params)}")
print(f" limits: {len(c.limits)}")
```
The marker transforms define local coordinate frames on each part. The constraint type determines what geometric relationship is enforced between these frames.
### Returning results
```python
result = kcsolve.SolveResult()
result.status = kcsolve.SolveStatus.Success
result.dof = computed_dof
placements = []
for part_id, pos, quat in solved_parts:
pr = kcsolve.SolveResult.PartResult()
pr.id = part_id
pr.placement = kcsolve.Transform()
pr.placement.position = list(pos)
pr.placement.quaternion = list(quat)
placements.append(pr)
result.placements = placements
return result
```
**Important:** pybind11 list fields return copies. Use `result.placements = [...]` (whole-list assignment), not `result.placements.append(...)`.
## Adding optional capabilities
### Diagnostics
Override `diagnose()` to detect overconstrained or malformed assemblies:
```python
def diagnose(self, ctx):
diagnostics = []
# ... analyze constraints ...
d = kcsolve.ConstraintDiagnostic()
d.constraint_id = "Joint001"
d.kind = kcsolve.DiagnosticKind.Redundant
d.detail = "This joint duplicates Joint002"
diagnostics.append(d)
return diagnostics
```
### Interactive drag
Override the three drag methods for real-time viewport dragging:
```python
def pre_drag(self, ctx, drag_parts):
self._ctx = ctx
self._dragging = set(drag_parts)
return self.solve(ctx)
def drag_step(self, drag_placements):
# Update dragged parts in stored context
for pr in drag_placements:
for part in self._ctx.parts:
if part.id == pr.id:
part.placement = pr.placement
break
return self.solve(self._ctx)
def post_drag(self):
self._ctx = None
self._dragging = None
```
For responsive dragging, the solver should converge quickly from a nearby initial guess. Use warm-starting (current placements as initial guess) and consider caching internal state across drag steps.
### Incremental update
Override `update()` for the case where only constraint parameters changed (not topology):
```python
def update(self, ctx):
# Reuse cached factorization, only re-evaluate changed residuals
return self.solve(ctx) # default: just re-solve
```
## Testing
### Unit tests (without FreeCAD)
Test your solver logic with hand-built `SolveContext` objects:
```python
import kcsolve
def test_fixed_joint():
ctx = kcsolve.SolveContext()
base = kcsolve.Part()
base.id = "base"
base.grounded = True
arm = kcsolve.Part()
arm.id = "arm"
arm.placement.position = [100.0, 0.0, 0.0]
joint = kcsolve.Constraint()
joint.id = "Joint001"
joint.part_i = "base"
joint.part_j = "arm"
joint.type = kcsolve.BaseJointKind.Fixed
ctx.parts = [base, arm]
ctx.constraints = [joint]
solver = MySolver()
result = solver.solve(ctx)
assert result.status == kcsolve.SolveStatus.Success
```
### Integration tests (with FreeCAD)
For integration testing within FreeCAD, follow the pattern in `TestKindredSolverIntegration.py`: set the solver preference in `setUp()`, create document objects, and verify solve results.
## Reference
- [KCSolve Python API](../reference/kcsolve-python.md) -- complete type and function reference
- [KCSolve Architecture](../architecture/ondsel-solver.md) -- C++ framework details
- [Constraints](constraints.md) -- constraint types and residual counts
- [Kindred Solver Overview](overview.md) -- how the built-in Kindred solver works

1
mods/quicknav Submodule

Submodule mods/quicknav added at 658a427132

View File

@@ -30,7 +30,7 @@ fi
# Get version from git if not provided
if [ -z "$VERSION" ]; then
VERSION=$(cd "$PROJECT_ROOT" && git describe --tags --always 2>/dev/null || echo "0.1.0")
VERSION=$(cd "$PROJECT_ROOT" && git describe --tags --always 2>/dev/null || echo "0.1.5")
fi
# Convert version to Debian-compatible format

View File

@@ -155,6 +155,7 @@ requirements:
- lark
- lxml
- matplotlib-base
- networkx
- nine
- noqt5
- numpy>=1.26,<2

View File

@@ -1046,6 +1046,9 @@ void Application::slotNewDocument(const App::Document& Doc, bool isMainDoc)
);
pDoc->signalInEdit.connect(std::bind(&Gui::Application::slotInEdit, this, sp::_1));
pDoc->signalResetEdit.connect(std::bind(&Gui::Application::slotResetEdit, this, sp::_1));
pDoc->signalActivatedViewProvider.connect(
std::bind(&Gui::Application::slotActivatedViewProvider, this, sp::_1, sp::_2)
);
// NOLINTEND
signalNewDocument(*pDoc, isMainDoc);
@@ -1352,6 +1355,12 @@ void Application::slotResetEdit(const Gui::ViewProviderDocumentObject& vp)
this->signalResetEdit(vp);
}
void Application::slotActivatedViewProvider(
const Gui::ViewProviderDocumentObject* vp, const char* name)
{
this->signalActivatedViewProvider(vp, name);
}
void Application::onLastWindowClosed(Gui::Document* pcDoc)
{
try {

View File

@@ -153,6 +153,9 @@ public:
fastsignals::signal<void(const Gui::ViewProviderDocumentObject&)> signalInEdit;
/// signal on leaving edit mode
fastsignals::signal<void(const Gui::ViewProviderDocumentObject&)> signalResetEdit;
/// signal on activated view-provider (active-object change, e.g. "pdbody", "part")
fastsignals::signal<void(const Gui::ViewProviderDocumentObject*, const char*)>
signalActivatedViewProvider;
/// signal on changing user edit mode
fastsignals::signal<void(int)> signalUserEditModeChanged;
//@}
@@ -174,6 +177,7 @@ protected:
void slotActivatedObject(const ViewProvider&);
void slotInEdit(const Gui::ViewProviderDocumentObject&);
void slotResetEdit(const Gui::ViewProviderDocumentObject&);
void slotActivatedViewProvider(const Gui::ViewProviderDocumentObject*, const char*);
public:
/// message when a GuiDocument is about to vanish

View File

@@ -121,6 +121,9 @@ EditingContextResolver::EditingContextResolver()
app.signalActiveDocument.connect([this](const Document& doc) { onActiveDocument(doc); });
app.signalActivateView.connect([this](const MDIView* view) { onActivateView(view); });
app.signalActivateWorkbench.connect([this](const char*) { refresh(); });
app.signalActivatedViewProvider.connect(
[this](const ViewProviderDocumentObject*, const char*) { refresh(); }
);
}
EditingContextResolver::~EditingContextResolver()
@@ -172,6 +175,23 @@ static App::DocumentObject* getActivePartObject()
return view->getActiveObject<App::DocumentObject*>("part");
}
// ---------------------------------------------------------------------------
// Helper: get the active "pdbody" object from the active view
// ---------------------------------------------------------------------------
static App::DocumentObject* getActivePdBodyObject()
{
auto* guiDoc = Application::Instance->activeDocument();
if (!guiDoc) {
return nullptr;
}
auto* view = guiDoc->getActiveView();
if (!view) {
return nullptr;
}
return view->getActiveObject<App::DocumentObject*>("pdbody");
}
// ---------------------------------------------------------------------------
// Helper: get the label of the active "part" object
// ---------------------------------------------------------------------------
@@ -213,6 +233,34 @@ static QString getInEditLabel()
void EditingContextResolver::registerBuiltinContexts()
{
// --- PartDesign body active inside an assembly (supersedes assembly.edit) ---
registerContext({
/*.id =*/QStringLiteral("partdesign.in_assembly"),
/*.labelTemplate =*/QStringLiteral("Body: {name}"),
/*.color =*/QLatin1String(CatppuccinMocha::Mauve),
/*.toolbars =*/
{QStringLiteral("Part Design Helper Features"),
QStringLiteral("Part Design Modeling Features"),
QStringLiteral("Part Design Dress-Up Features"),
QStringLiteral("Part Design Transformation Features"),
QStringLiteral("Sketcher")},
/*.priority =*/95,
/*.match =*/
[]() {
auto* body = getActivePdBodyObject();
if (!body || !objectIsDerivedFrom(body, "PartDesign::Body")) {
return false;
}
// Only match when we're inside an assembly edit session
auto* doc = Application::Instance->activeDocument();
if (!doc) {
return false;
}
auto* vp = doc->getInEdit();
return vp && vpObjectIsDerivedFrom(vp, "Assembly::AssemblyObject");
},
});
// --- Sketcher edit (highest priority — VP in edit) ---
registerContext({
/*.id =*/QStringLiteral("sketcher.edit"),
@@ -272,7 +320,10 @@ void EditingContextResolver::registerBuiltinContexts()
/*.priority =*/40,
/*.match =*/
[]() {
auto* obj = getActivePartObject();
auto* obj = getActivePdBodyObject();
if (!obj) {
obj = getActivePartObject();
}
if (!obj || !objectIsDerivedFrom(obj, "PartDesign::Body")) {
return false;
}
@@ -301,7 +352,10 @@ void EditingContextResolver::registerBuiltinContexts()
/*.priority =*/30,
/*.match =*/
[]() {
auto* obj = getActivePartObject();
auto* obj = getActivePdBodyObject();
if (!obj) {
obj = getActivePartObject();
}
return obj && objectIsDerivedFrom(obj, "PartDesign::Body");
},
});
@@ -488,6 +542,13 @@ EditingContext EditingContextResolver::resolve() const
if (label.contains(QStringLiteral("{name}"))) {
// For edit-mode contexts, use the in-edit object name
QString name = getInEditLabel();
if (name.isEmpty()) {
// Try pdbody first for PartDesign contexts
auto* bodyObj = getActivePdBodyObject();
if (bodyObj) {
name = QString::fromUtf8(bodyObj->Label.getValue());
}
}
if (name.isEmpty()) {
name = getActivePartLabel();
}
@@ -548,6 +609,25 @@ QStringList EditingContextResolver::buildBreadcrumb(const EditingContext& ctx) c
return crumbs;
}
// Assembly > Body breadcrumb for in-assembly part editing
if (ctx.id == QStringLiteral("partdesign.in_assembly")) {
auto* guiDoc = Application::Instance->activeDocument();
if (guiDoc) {
auto* vp = guiDoc->getInEdit();
if (vp) {
auto* vpd = dynamic_cast<ViewProviderDocumentObject*>(vp);
if (vpd && vpd->getObject()) {
crumbs << QString::fromUtf8(vpd->getObject()->Label.getValue());
}
}
}
auto* body = getActivePdBodyObject();
if (body) {
crumbs << QString::fromUtf8(body->Label.getValue());
}
return crumbs;
}
// Always start with the active part/body/assembly label
QString partLabel = getActivePartLabel();
if (!partLabel.isEmpty()) {
@@ -582,6 +662,14 @@ QStringList EditingContextResolver::buildBreadcrumbColors(const EditingContext&
{
QStringList colors;
if (ctx.id == QStringLiteral("partdesign.in_assembly")) {
for (int i = 0; i < ctx.breadcrumb.size(); ++i) {
colors << (i == 0 ? QLatin1String(CatppuccinMocha::Blue)
: QLatin1String(CatppuccinMocha::Mauve));
}
return colors;
}
if (ctx.breadcrumb.size() <= 1) {
colors << ctx.color;
return colors;

View File

@@ -251,7 +251,6 @@ QDockWidget::title {
text-align: left;
padding: 8px 6px;
border-bottom: 1px solid #313244;
min-height: 18px;
}
QDockWidget::close-button,
@@ -733,7 +732,7 @@ QGroupBox {
background-color: #1e1e2e;
border: 1px solid #45475a;
border-radius: 6px;
margin-top: 12px;
margin-top: 16px;
padding-top: 8px;
}
@@ -741,7 +740,7 @@ QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
left: 12px;
padding: 0 4px;
padding: 2px 4px;
color: #bac2de;
background-color: #1e1e2e;
}
@@ -1234,7 +1233,7 @@ QSint--ActionGroup QToolButton {
border: none;
border-radius: 4px;
padding: 8px 6px;
min-height: 18px;
min-height: 0px;
}
QSint--ActionGroup QToolButton:hover {

View File

@@ -311,6 +311,19 @@ void AssemblyLink::updateContents()
purgeTouched();
}
// Generate an instance label for assembly components by appending a -N suffix.
// All instances get a suffix (starting at -1) so that structured part numbers
// like "P03-0001" are never mangled by UniqueNameManager's trailing-digit logic.
static std::string makeInstanceLabel(App::Document* doc, const std::string& baseLabel)
{
for (int i = 1;; ++i) {
std::string candidate = baseLabel + "-" + std::to_string(i);
if (!doc->containsLabel(candidate)) {
return candidate;
}
}
}
void AssemblyLink::synchronizeComponents()
{
App::Document* doc = getDocument();
@@ -428,7 +441,7 @@ void AssemblyLink::synchronizeComponents()
auto* subAsmLink = static_cast<AssemblyLink*>(newObj);
subAsmLink->LinkedObject.setValue(obj);
subAsmLink->Rigid.setValue(asmLink->Rigid.getValue());
subAsmLink->Label.setValue(obj->Label.getValue());
subAsmLink->Label.setValue(makeInstanceLabel(doc, obj->Label.getValue()));
addObject(subAsmLink);
link = subAsmLink;
}
@@ -440,7 +453,7 @@ void AssemblyLink::synchronizeComponents()
);
newLink->LinkedObject.setValue(srcLink->getTrueLinkedObject(false));
newLink->Label.setValue(obj->Label.getValue());
newLink->Label.setValue(makeInstanceLabel(doc, obj->Label.getValue()));
addObject(newLink);
newLink->ElementCount.setValue(srcLink->ElementCount.getValue());
@@ -461,7 +474,7 @@ void AssemblyLink::synchronizeComponents()
App::DocumentObject* newObj = doc->addObject("App::Link", obj->getNameInDocument());
auto* newLink = static_cast<App::Link*>(newObj);
newLink->LinkedObject.setValue(obj);
newLink->Label.setValue(obj->Label.getValue());
newLink->Label.setValue(makeInstanceLabel(doc, obj->Label.getValue()));
addObject(newLink);
link = newLink;
}

View File

@@ -30,6 +30,7 @@
#include <App/Application.h>
#include <App/Datums.h>
#include <App/Origin.h>
#include <App/Document.h>
#include <App/DocumentObjectGroup.h>
#include <App/FeaturePythonPyImp.h>
@@ -106,6 +107,24 @@ AssemblyObject::AssemblyObject()
AssemblyObject::~AssemblyObject() = default;
void AssemblyObject::setupObject()
{
App::Part::setupObject();
// Relabel origin planes with assembly-friendly names (SolidWorks convention)
if (auto* origin = getOrigin()) {
if (auto* xy = origin->getXY()) {
xy->Label.setValue("Top");
}
if (auto* xz = origin->getXZ()) {
xz->Label.setValue("Front");
}
if (auto* yz = origin->getYZ()) {
yz->Label.setValue("Right");
}
}
}
PyObject* AssemblyObject::getPyObject()
{
if (PythonObject.is(Py::_None())) {
@@ -157,6 +176,10 @@ KCSolve::IKCSolver* AssemblyObject::getOrCreateSolver()
std::string solverName = hGrp->GetASCII("Solver", "");
solver_ = KCSolve::SolverRegistry::instance().get(solverName);
// get("") returns the registry default (first registered solver)
if (solver_) {
FC_LOG("Assembly : loaded solver '" << solver_->name()
<< "' (requested='" << solverName << "')");
}
}
return solver_.get();
}
@@ -193,14 +216,22 @@ int AssemblyObject::solve(bool enableRedo, bool updateJCS)
auto groundedObjs = getGroundedParts();
if (groundedObjs.empty()) {
FC_LOG("Assembly : solve skipped — no grounded parts");
return -6;
}
std::vector<App::DocumentObject*> joints = getJoints(updateJCS);
removeUnconnectedJoints(joints, groundedObjs);
FC_LOG("Assembly : solve on '" << getFullLabel()
<< "' — " << groundedObjs.size() << " grounded, "
<< joints.size() << " joints");
KCSolve::SolveContext ctx = buildSolveContext(joints);
FC_LOG("Assembly : solve context — " << ctx.parts.size() << " parts, "
<< ctx.constraints.size() << " constraints");
// Always save placements to enable orientation flip detection
savePlacementsForUndo();
@@ -222,6 +253,13 @@ int AssemblyObject::solve(bool enableRedo, bool updateJCS)
}
if (lastResult_.status == KCSolve::SolveStatus::Failed) {
FC_LOG("Assembly : solve failed — status="
<< static_cast<int>(lastResult_.status)
<< ", " << lastResult_.diagnostics.size() << " diagnostics");
for (const auto& d : lastResult_.diagnostics) {
Base::Console().warning("Assembly : diagnostic [%s]: %s\n",
d.constraint_id.c_str(), d.detail.c_str());
}
updateSolveStatus();
return -1;
}
@@ -229,6 +267,7 @@ int AssemblyObject::solve(bool enableRedo, bool updateJCS)
// Validate that the solve didn't cause any parts to flip orientation
if (!validateNewPlacements()) {
// Restore previous placements - the solve found an invalid configuration
FC_LOG("Assembly : solve rejected — placement validation failed, undoing");
undoSolve();
lastSolverStatus = -2;
updateSolveStatus();
@@ -246,6 +285,9 @@ int AssemblyObject::solve(bool enableRedo, bool updateJCS)
updateSolveStatus();
FC_LOG("Assembly : solve succeeded — dof=" << lastResult_.dof
<< ", " << lastResult_.placements.size() << " placements");
return 0;
}
@@ -256,8 +298,14 @@ void AssemblyObject::updateSolveStatus()
//+1 because there's a grounded joint to origin
lastDoF = (1 + numberOfComponents()) * 6;
if (!solver_ || lastResult_.placements.empty()) {
// Guard against re-entrancy: solve() calls updateSolveStatus(), so if
// placements are legitimately empty (e.g. zero constraints / all parts
// grounded) the recursive solve() would never terminate.
static bool updating = false;
if (!updating && (!solver_ || lastResult_.placements.empty())) {
updating = true;
solve();
updating = false;
}
if (!solver_) {
@@ -390,6 +438,8 @@ size_t Assembly::AssemblyObject::numberOfFrames()
void AssemblyObject::preDrag(std::vector<App::DocumentObject*> dragParts)
{
bundleFixed = true;
dragStepCount_ = 0;
dragStepRejected_ = 0;
auto* solver = getOrCreateSolver();
if (!solver) {
@@ -402,6 +452,7 @@ void AssemblyObject::preDrag(std::vector<App::DocumentObject*> dragParts)
auto groundedObjs = getGroundedParts();
if (groundedObjs.empty()) {
FC_LOG("Assembly : preDrag skipped — no grounded parts");
bundleFixed = false;
return;
}
@@ -455,6 +506,10 @@ void AssemblyObject::preDrag(std::vector<App::DocumentObject*> dragParts)
}
}
FC_LOG("Assembly : preDrag — " << dragPartIds.size() << " drag part(s), "
<< joints.size() << " joints, " << ctx.parts.size() << " parts, "
<< ctx.constraints.size() << " constraints");
savePlacementsForUndo();
try {
@@ -463,11 +518,13 @@ void AssemblyObject::preDrag(std::vector<App::DocumentObject*> dragParts)
}
catch (...) {
// If pre_drag fails, we still need to be in a valid state
FC_LOG("Assembly : preDrag — solver pre_drag threw exception");
}
}
void AssemblyObject::doDragStep()
{
dragStepCount_++;
try {
std::vector<KCSolve::SolveResult::PartResult> dragPlacements;
@@ -487,9 +544,21 @@ void AssemblyObject::doDragStep()
lastResult_ = solver_->drag_step(dragPlacements);
if (lastResult_.status == KCSolve::SolveStatus::Failed) {
FC_LOG("Assembly : dragStep #" << dragStepCount_ << " — solver failed");
}
if (validateNewPlacements()) {
setNewPlacements();
// Update the baseline positions after each accepted drag step so that
// the orientation-flip check in validateNewPlacements() compares against
// the last accepted state rather than the pre-drag origin. Without this,
// cumulative rotation during a long drag easily exceeds the 91-degree
// threshold and causes the solver result to be rejected ("flipped
// orientation"), making parts appear to explode.
savePlacementsForUndo();
auto joints = getJoints(false);
for (auto* joint : joints) {
if (joint->Visibility.getValue()) {
@@ -498,9 +567,12 @@ void AssemblyObject::doDragStep()
}
}
}
else {
dragStepRejected_++;
}
}
catch (...) {
// We do nothing if a solve step fails.
FC_LOG("Assembly : dragStep #" << dragStepCount_ << " — exception");
}
}
@@ -612,6 +684,8 @@ bool AssemblyObject::validateNewPlacements()
void AssemblyObject::postDrag()
{
FC_LOG("Assembly : postDrag — " << dragStepCount_ << " steps, "
<< dragStepRejected_ << " rejected");
if (solver_) {
solver_->post_drag();
}
@@ -1041,10 +1115,19 @@ KCSolve::SolveContext AssemblyObject::buildSolveContext(
break;
default:
FC_WARN("Assembly : Distance joint '" << joint->getFullName()
<< "' — unhandled DistanceType "
<< distanceTypeName(distType)
<< ", falling back to Planar");
kind = KCSolve::BaseJointKind::Planar;
params.push_back(distance);
break;
}
FC_LOG("Assembly : Distance joint '" << joint->getFullName()
<< "' — DistanceType=" << distanceTypeName(distType)
<< ", kind=" << static_cast<int>(kind)
<< ", distance=" << distance);
break;
}
default:
@@ -1323,6 +1406,23 @@ KCSolve::SolveContext AssemblyObject::buildSolveContext(
ctx.simulation = sp;
}
// Log context summary
{
int nGrounded = 0, nFree = 0, nLimits = 0;
for (const auto& p : ctx.parts) {
if (p.grounded) nGrounded++;
else nFree++;
}
for (const auto& c : ctx.constraints) {
if (!c.limits.empty()) nLimits++;
}
FC_LOG("Assembly : buildSolveContext — "
<< nGrounded << " grounded + " << nFree << " free parts, "
<< ctx.constraints.size() << " constraints"
<< (nLimits ? (std::string(", ") + std::to_string(nLimits) + " with limits") : "")
<< (ctx.bundle_fixed ? ", bundle_fixed=true" : ""));
}
return ctx;
}

View File

@@ -84,6 +84,7 @@ public:
return "AssemblyGui::ViewProviderAssembly";
}
void setupObject() override;
App::DocumentObjectExecReturn* execute() override;
void onChanged(const App::Property* prop) override;
/* Solve the assembly. It will update first the joints, solve, update placements of the parts
@@ -279,6 +280,10 @@ private:
bool bundleFixed;
// Drag diagnostic counters (reset in preDrag, reported in postDrag)
int dragStepCount_ = 0;
int dragStepRejected_ = 0;
int lastDoF;
bool lastHasConflict;
bool lastHasRedundancies;

View File

@@ -23,6 +23,7 @@
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <gp_Circ.hxx>
@@ -54,10 +55,56 @@
namespace PartApp = Part;
FC_LOG_LEVEL_INIT("Assembly", true, true, true)
// ======================================= Utils ======================================
namespace Assembly
{
const char* distanceTypeName(DistanceType dt)
{
switch (dt) {
case DistanceType::PointPoint: return "PointPoint";
case DistanceType::LineLine: return "LineLine";
case DistanceType::LineCircle: return "LineCircle";
case DistanceType::CircleCircle: return "CircleCircle";
case DistanceType::PlanePlane: return "PlanePlane";
case DistanceType::PlaneCylinder: return "PlaneCylinder";
case DistanceType::PlaneSphere: return "PlaneSphere";
case DistanceType::PlaneCone: return "PlaneCone";
case DistanceType::PlaneTorus: return "PlaneTorus";
case DistanceType::CylinderCylinder: return "CylinderCylinder";
case DistanceType::CylinderSphere: return "CylinderSphere";
case DistanceType::CylinderCone: return "CylinderCone";
case DistanceType::CylinderTorus: return "CylinderTorus";
case DistanceType::ConeCone: return "ConeCone";
case DistanceType::ConeTorus: return "ConeTorus";
case DistanceType::ConeSphere: return "ConeSphere";
case DistanceType::TorusTorus: return "TorusTorus";
case DistanceType::TorusSphere: return "TorusSphere";
case DistanceType::SphereSphere: return "SphereSphere";
case DistanceType::PointPlane: return "PointPlane";
case DistanceType::PointCylinder: return "PointCylinder";
case DistanceType::PointSphere: return "PointSphere";
case DistanceType::PointCone: return "PointCone";
case DistanceType::PointTorus: return "PointTorus";
case DistanceType::LinePlane: return "LinePlane";
case DistanceType::LineCylinder: return "LineCylinder";
case DistanceType::LineSphere: return "LineSphere";
case DistanceType::LineCone: return "LineCone";
case DistanceType::LineTorus: return "LineTorus";
case DistanceType::CurvePlane: return "CurvePlane";
case DistanceType::CurveCylinder: return "CurveCylinder";
case DistanceType::CurveSphere: return "CurveSphere";
case DistanceType::CurveCone: return "CurveCone";
case DistanceType::CurveTorus: return "CurveTorus";
case DistanceType::PointLine: return "PointLine";
case DistanceType::PointCurve: return "PointCurve";
case DistanceType::Other: return "Other";
}
return "Unknown";
}
void swapJCS(const App::DocumentObject* joint)
{
if (!joint) {
@@ -151,6 +198,120 @@ double getEdgeRadius(const App::DocumentObject* obj, const std::string& elt)
return sf.GetType() == GeomAbs_Circle ? sf.Circle().Radius() : 0.0;
}
/// Determine whether \a obj represents a planar datum when referenced with an
/// empty element type (bare sub-name ending with ".").
///
/// Covers three independent class hierarchies:
/// 1. App::Plane (origin planes, Part::DatumPlane)
/// 2. Part::Datum (PartDesign::Plane — not derived from App::Plane)
/// 3. Any Part::Feature whose whole-object shape is a single planar face
/// (e.g. Part::Plane primitive referenced without an element)
static bool isDatumPlane(const App::DocumentObject* obj)
{
if (!obj) {
return false;
}
// Origin planes and Part::DatumPlane (both inherit App::Plane).
if (obj->isDerivedFrom<App::Plane>()) {
return true;
}
// PartDesign datum objects inherit Part::Datum but NOT App::Plane.
// Part::Datum is also the base for PartDesign::Line and PartDesign::Point,
// so inspect the shape to confirm it is actually planar.
if (obj->isDerivedFrom<PartApp::Datum>()) {
auto* feat = static_cast<const PartApp::Feature*>(obj);
const auto& shape = feat->Shape.getShape().getShape();
if (shape.IsNull()) {
return false;
}
TopExp_Explorer ex(shape, TopAbs_FACE);
if (ex.More()) {
BRepAdaptor_Surface sf(TopoDS::Face(ex.Current()));
return sf.GetType() == GeomAbs_Plane;
}
return false;
}
// Fallback for any Part::Feature (e.g. Part::Plane primitive) referenced
// bare — if its shape is a single planar face, treat it as a datum plane.
if (auto* feat = dynamic_cast<const PartApp::Feature*>(obj)) {
const auto& shape = feat->Shape.getShape().getShape();
if (shape.IsNull()) {
return false;
}
TopExp_Explorer ex(shape, TopAbs_FACE);
if (!ex.More()) {
return false;
}
BRepAdaptor_Surface sf(TopoDS::Face(ex.Current()));
if (sf.GetType() != GeomAbs_Plane) {
return false;
}
ex.Next();
// Only treat as datum if there is exactly one face — a multi-face
// solid referenced bare is ambiguous and should not be classified.
return !ex.More();
}
return false;
}
/// Same idea for datum lines (App::Line, PartDesign::Line, etc.).
static bool isDatumLine(const App::DocumentObject* obj)
{
if (!obj) {
return false;
}
if (obj->isDerivedFrom<App::Line>()) {
return true;
}
if (obj->isDerivedFrom<PartApp::Datum>()) {
auto* feat = static_cast<const PartApp::Feature*>(obj);
const auto& shape = feat->Shape.getShape().getShape();
if (shape.IsNull()) {
return false;
}
TopExp_Explorer ex(shape, TopAbs_EDGE);
if (ex.More()) {
BRepAdaptor_Curve cv(TopoDS::Edge(ex.Current()));
return cv.GetType() == GeomAbs_Line;
}
return false;
}
return false;
}
/// Same idea for datum points (App::Point, PartDesign::Point, etc.).
static bool isDatumPoint(const App::DocumentObject* obj)
{
if (!obj) {
return false;
}
if (obj->isDerivedFrom<App::Point>()) {
return true;
}
if (obj->isDerivedFrom<PartApp::Datum>()) {
auto* feat = static_cast<const PartApp::Feature*>(obj);
const auto& shape = feat->Shape.getShape().getShape();
if (shape.IsNull()) {
return false;
}
// A datum point has a vertex but no edges or faces.
TopExp_Explorer exE(shape, TopAbs_EDGE);
TopExp_Explorer exV(shape, TopAbs_VERTEX);
return !exE.More() && exV.More();
}
return false;
}
DistanceType getDistanceType(App::DocumentObject* joint)
{
if (!joint) {
@@ -164,6 +325,179 @@ DistanceType getDistanceType(App::DocumentObject* joint)
auto* obj1 = getLinkedObjFromRef(joint, "Reference1");
auto* obj2 = getLinkedObjFromRef(joint, "Reference2");
// Datum objects referenced bare have empty element types (sub-name
// ends with "."). PartDesign datums referenced through a body can
// also produce non-standard element types like "Plane" (from a
// sub-name such as "Body.DatumPlane.Plane" — Part::Datum::getSubObject
// ignores the trailing element, but splitSubName still extracts it).
//
// Detect these before the main geometry chain, which only handles
// the standard Face/Edge/Vertex element types.
//
// isDatumPlane/Line/Point cover all three independent hierarchies:
// - App::Plane / App::Line / App::Point (origin datums)
// - Part::Datum subclasses (PartDesign datums)
// - Part::Feature with single-face shape (Part::Plane primitive, bare ref)
auto isNonGeomElement = [](const std::string& t) {
return t != "Face" && t != "Edge" && t != "Vertex";
};
const bool datumPlane1 = isNonGeomElement(type1) && isDatumPlane(obj1);
const bool datumPlane2 = isNonGeomElement(type2) && isDatumPlane(obj2);
const bool datumLine1 = isNonGeomElement(type1) && !datumPlane1 && isDatumLine(obj1);
const bool datumLine2 = isNonGeomElement(type2) && !datumPlane2 && isDatumLine(obj2);
const bool datumPoint1 = isNonGeomElement(type1) && !datumPlane1 && !datumLine1 && isDatumPoint(obj1);
const bool datumPoint2 = isNonGeomElement(type2) && !datumPlane2 && !datumLine2 && isDatumPoint(obj2);
const bool datum1 = datumPlane1 || datumLine1 || datumPoint1;
const bool datum2 = datumPlane2 || datumLine2 || datumPoint2;
if (datum1 || datum2) {
// Map each datum side to a synthetic element type so the same
// classification logic applies regardless of which hierarchy
// the object comes from.
auto syntheticType = [](bool isPlane, bool isLine, bool isPoint,
const std::string& elemType) -> std::string {
if (isPlane) return "Face";
if (isLine) return "Edge";
if (isPoint) return "Vertex";
return elemType; // non-datum side keeps its real type
};
const std::string syn1 = syntheticType(datumPlane1, datumLine1, datumPoint1, type1);
const std::string syn2 = syntheticType(datumPlane2, datumLine2, datumPoint2, type2);
// Both sides are datum planes.
if (datumPlane1 && datumPlane2) {
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datum+datum → PlanePlane");
return DistanceType::PlanePlane;
}
// One side is a datum plane, the other has a real element type
// (or is another datum kind).
// For PointPlane/LinePlane, the solver's PointInPlaneConstraint
// reads the plane normal from marker_j (Reference2). Unlike
// real Face+Vertex joints (where both Placements carry the
// face normal from findPlacement), datum planes only carry
// their normal through computeMarkerTransform. So the datum
// plane must end up on Reference2 for the normal to reach marker_j.
//
// For PlanePlane the convention matches the existing Face+Face
// path (plane on Reference1).
if (datumPlane1 || datumPlane2) {
const auto& otherSyn = datumPlane1 ? syn2 : syn1;
if (otherSyn == "Vertex" || otherSyn == "Edge") {
// Datum plane must be on Reference2 (j side).
if (datumPlane1) {
swapJCS(joint); // move datum from Ref1 → Ref2
}
DistanceType result = (otherSyn == "Vertex")
? DistanceType::PointPlane : DistanceType::LinePlane;
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datum+" << otherSyn << ""
<< distanceTypeName(result)
<< (datumPlane1 ? " (swapped)" : ""));
return result;
}
// Face + datum plane or datum plane + datum plane → PlanePlane.
// No swap needed: PlanarConstraint is symmetric (uses both
// z_i and z_j), and preserving the original Reference order
// keeps the initial Placement values consistent so the solver
// stays in the correct orientation branch.
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datum+" << otherSyn << " → PlanePlane");
return DistanceType::PlanePlane;
}
// Datum line or datum point paired with a real element type.
// Map to the appropriate pair using synthetic types and fall
// through to the main geometry chain below. The synthetic
// types ("Edge", "Vertex") will match the existing if-else
// branches — but those branches call isEdgeType/isFaceType on
// the object, which requires a real sub-element name. For
// datum lines/points the element is empty, so we classify
// directly here.
if (datumLine1 && datumLine2) {
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumLine+datumLine → LineLine");
return DistanceType::LineLine;
}
if (datumPoint1 && datumPoint2) {
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumPoint+datumPoint → PointPoint");
return DistanceType::PointPoint;
}
if ((datumLine1 && datumPoint2) || (datumPoint1 && datumLine2)) {
if (datumPoint1) {
swapJCS(joint); // line first
}
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumLine+datumPoint → PointLine");
return DistanceType::PointLine;
}
// One datum line/point + one real element type.
if (datumLine1 || datumLine2) {
const auto& otherSyn = datumLine1 ? syn2 : syn1;
if (otherSyn == "Face") {
// Line + Face — need line on Reference2 (edge side).
if (datumLine1) {
swapJCS(joint);
}
// We don't know the face type without inspecting the shape,
// but LinePlane is the most common and safest classification.
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumLine+Face → LinePlane");
return DistanceType::LinePlane;
}
if (otherSyn == "Vertex") {
if (datumLine2) {
swapJCS(joint); // line first
}
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumLine+Vertex → PointLine");
return DistanceType::PointLine;
}
if (otherSyn == "Edge") {
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumLine+Edge → LineLine");
return DistanceType::LineLine;
}
}
if (datumPoint1 || datumPoint2) {
const auto& otherSyn = datumPoint1 ? syn2 : syn1;
if (otherSyn == "Face") {
// Point + Face — face first, point second.
if (!datumPoint2) {
swapJCS(joint); // put face on Ref1
}
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumPoint+Face → PointPlane");
return DistanceType::PointPlane;
}
if (otherSyn == "Edge") {
// Edge first, point second.
if (datumPoint1) {
swapJCS(joint);
}
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumPoint+Edge → PointLine");
return DistanceType::PointLine;
}
if (otherSyn == "Vertex") {
FC_LOG("Assembly : getDistanceType('" << joint->getFullName()
<< "') — datumPoint+Vertex → PointPoint");
return DistanceType::PointPoint;
}
}
// If we get here, it's an unrecognized datum combination.
FC_WARN("Assembly : getDistanceType('" << joint->getFullName()
<< "') — unrecognized datum combination (syn1="
<< syn1 << ", syn2=" << syn2 << ")");
}
if (type1 == "Vertex" && type2 == "Vertex") {
return DistanceType::PointPoint;
}
@@ -591,6 +925,19 @@ App::DocumentObject* getObjFromRef(App::DocumentObject* comp, const std::string&
if (obj->isDerivedFrom<App::Part>() || obj->isLinkGroup()) {
continue;
}
else if (obj->isDerivedFrom<App::LocalCoordinateSystem>()) {
// Resolve LCS → child datum element (e.g. Origin → XY_Plane)
auto nextIt = std::next(it);
if (nextIt != names.end()) {
for (auto* child : obj->getOutList()) {
if (child->getNameInDocument() == *nextIt
&& child->isDerivedFrom<App::DatumElement>()) {
return child;
}
}
}
return obj;
}
else if (obj->isDerivedFrom<PartDesign::Body>()) {
return handlePartDesignBody(obj, it);
}

View File

@@ -148,6 +148,7 @@ AssemblyExport double getFaceRadius(const App::DocumentObject* obj, const std::s
AssemblyExport double getEdgeRadius(const App::DocumentObject* obj, const std::string& elName);
AssemblyExport DistanceType getDistanceType(App::DocumentObject* joint);
AssemblyExport const char* distanceTypeName(DistanceType dt);
AssemblyExport JointGroup* getJointGroup(const App::Part* part);
AssemblyExport std::vector<App::DocumentObject*> getAssemblyComponents(const AssemblyObject* assembly);

View File

@@ -0,0 +1,314 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# /****************************************************************************
# *
# Copyright (c) 2025 Kindred Systems <development@kindred-systems.com> *
# *
# This file is part of FreeCAD. *
# *
# FreeCAD is free software: you can redistribute it and/or modify it *
# under the terms of the GNU Lesser General Public License as *
# published by the Free Software Foundation, either version 2.1 of the *
# License, or (at your option) any later version. *
# *
# FreeCAD is distributed in the hope that it will be useful, but *
# WITHOUT ANY WARRANTY; without even the implied warranty of *
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# Lesser General Public License for more details. *
# *
# You should have received a copy of the GNU Lesser General Public *
# License along with FreeCAD. If not, see *
# <https://www.gnu.org/licenses/>. *
# *
# ***************************************************************************/
"""
Tests for assembly origin reference planes.
Verifies that new assemblies have properly labeled, grounded origin planes
and that joints can reference them for solving.
"""
import os
import tempfile
import unittest
import FreeCAD as App
import JointObject
import UtilsAssembly
class TestAssemblyOriginPlanes(unittest.TestCase):
"""Tests for assembly origin planes (Top/Front/Right)."""
def setUp(self):
doc_name = self.__class__.__name__
if App.ActiveDocument:
if App.ActiveDocument.Name != doc_name:
App.newDocument(doc_name)
else:
App.newDocument(doc_name)
App.setActiveDocument(doc_name)
self.doc = App.ActiveDocument
self.assembly = self.doc.addObject("Assembly::AssemblyObject", "Assembly")
self.jointgroup = self.assembly.newObject("Assembly::JointGroup", "Joints")
def tearDown(self):
App.closeDocument(self.doc.Name)
# ── Helpers ─────────────────────────────────────────────────────
def _get_origin(self):
return self.assembly.Origin
def _make_box(self, x=0, y=0, z=0, size=10):
box = self.assembly.newObject("Part::Box", "Box")
box.Length = size
box.Width = size
box.Height = size
box.Placement = App.Placement(App.Vector(x, y, z), App.Rotation())
return box
def _make_joint(self, joint_type, ref1, ref2):
joint = self.jointgroup.newObject("App::FeaturePython", "Joint")
JointObject.Joint(joint, joint_type)
refs = [
[ref1[0], ref1[1]],
[ref2[0], ref2[1]],
]
joint.Proxy.setJointConnectors(joint, refs)
return joint
# ── Structure tests ─────────────────────────────────────────────
def test_assembly_has_origin(self):
"""New assembly has an Origin with 3 planes, 3 axes, 1 point."""
origin = self._get_origin()
self.assertIsNotNone(origin)
self.assertTrue(origin.isDerivedFrom("App::LocalCoordinateSystem"))
planes = origin.planes()
self.assertEqual(len(planes), 3)
axes = origin.axes()
self.assertEqual(len(axes), 3)
def test_origin_planes_labeled(self):
"""Origin planes are labeled Top, Front, Right."""
origin = self._get_origin()
xy = origin.getXY()
xz = origin.getXZ()
yz = origin.getYZ()
self.assertEqual(xy.Label, "Top")
self.assertEqual(xz.Label, "Front")
self.assertEqual(yz.Label, "Right")
def test_origin_planes_have_correct_roles(self):
"""Origin planes retain correct internal Role names."""
origin = self._get_origin()
self.assertEqual(origin.getXY().Role, "XY_Plane")
self.assertEqual(origin.getXZ().Role, "XZ_Plane")
self.assertEqual(origin.getYZ().Role, "YZ_Plane")
# ── Grounding tests ─────────────────────────────────────────────
def test_origin_in_grounded_set(self):
"""Origin is part of the assembly's grounded set."""
grounded = self.assembly.getGroundedParts()
origin = self._get_origin()
grounded_names = {obj.Name for obj in grounded}
self.assertIn(origin.Name, grounded_names)
# ── Reference resolution tests ──────────────────────────────────
def test_getObject_resolves_origin_plane(self):
"""UtilsAssembly.getObject correctly resolves an origin plane ref."""
origin = self._get_origin()
xy = origin.getXY()
# Ref structure: [Origin, ["XY_Plane.", "XY_Plane."]]
ref = [origin, [xy.Name + ".", xy.Name + "."]]
obj = UtilsAssembly.getObject(ref)
self.assertEqual(obj, xy)
def test_findPlacement_origin_plane_returns_identity(self):
"""findPlacement for an origin plane (whole-object) returns identity."""
origin = self._get_origin()
xy = origin.getXY()
ref = [origin, [xy.Name + ".", xy.Name + "."]]
plc = UtilsAssembly.findPlacement(ref)
# For datum planes with no element, identity is returned.
# The actual orientation comes from the solver's getGlobalPlacement.
self.assertTrue(
plc.isSame(App.Placement(), 1e-6),
"findPlacement for origin plane should return identity",
)
# ── Joint / solve tests ─────────────────────────────────────────
def test_fixed_joint_to_origin_plane(self):
"""Fixed joint referencing an origin plane solves correctly."""
origin = self._get_origin()
xy = origin.getXY()
box = self._make_box(50, 50, 50)
# Fixed joint (type 0): origin XY plane ↔ box Face1 (bottom, Z=0)
self._make_joint(
0,
[origin, [xy.Name + ".", xy.Name + "."]],
[box, ["Face1", "Vertex1"]],
)
# After solve, the box should have moved so that its Face1 (bottom)
# aligns with the XY plane (Z=0). The box bottom vertex1 is at (0,0,0).
self.assertAlmostEqual(
box.Placement.Base.z,
0.0,
places=3,
msg="Box should be on XY plane after fixed joint to Top plane",
)
def test_solve_return_code_with_origin_plane(self):
"""Solve with an origin plane joint returns success (0)."""
origin = self._get_origin()
xz = origin.getXZ()
box = self._make_box(0, 100, 0)
self._make_joint(
0,
[origin, [xz.Name + ".", xz.Name + "."]],
[box, ["Face1", "Vertex1"]],
)
result = self.assembly.solve()
self.assertEqual(result, 0, "Solve should succeed with origin plane joint")
# ── Distance joint to datum plane tests ────────────────────────
def test_distance_vertex_to_datum_plane_solves(self):
"""Distance(0) joint: vertex → datum plane solves and pins position."""
origin = self._get_origin()
xy = origin.getXY() # Top (Z normal)
xz = origin.getXZ() # Front (Y normal)
yz = origin.getYZ() # Right (X normal)
box = self._make_box(50, 50, 50)
# 3 Distance joints, each vertex→datum, distance=0.
# This should pin the box's Vertex1 (corner at local 0,0,0) to the
# origin, giving 3 PointInPlane constraints (1 residual each = 3 total).
for plane in [xy, xz, yz]:
joint = self._make_joint(
5, # Distance
[box, ["Vertex1", "Vertex1"]],
[origin, [plane.Name + ".", plane.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertEqual(
result, 0, "Solve should succeed for vertex→datum Distance joints"
)
# The box's Vertex1 (at local 0,0,0) should be at the origin.
v = box.Placement.Base
self.assertAlmostEqual(v.x, 0.0, places=2, msg="X should be pinned to 0")
self.assertAlmostEqual(v.y, 0.0, places=2, msg="Y should be pinned to 0")
self.assertAlmostEqual(v.z, 0.0, places=2, msg="Z should be pinned to 0")
def test_distance_vertex_to_datum_plane_preserves_orientation(self):
"""Distance(0) vertex→datum should not constrain orientation."""
origin = self._get_origin()
xy = origin.getXY()
xz = origin.getXZ()
yz = origin.getYZ()
# Start box with a known rotation (45° about Z).
rot = App.Rotation(App.Vector(0, 0, 1), 45)
box = self._make_box(50, 50, 50)
box.Placement = App.Placement(App.Vector(50, 50, 50), rot)
for plane in [xy, xz, yz]:
joint = self._make_joint(
5,
[box, ["Vertex1", "Vertex1"]],
[origin, [plane.Name + ".", plane.Name + "."]],
)
joint.Distance = 0.0
self.assembly.solve()
# 3 PointInPlane constraints pin position (3 DOF) but leave
# orientation free (3 DOF). The solver should keep the original
# orientation since it's the lowest-energy solution from the
# initial placement.
dof = self.assembly.getLastDoF()
self.assertEqual(
dof, 3, "3 PointInPlane constraints should leave 3 DOF (orientation)"
)
def test_distance_face_to_datum_plane_solves(self):
"""Distance(0) joint: face → datum plane solves (PlanePlane/Planar)."""
origin = self._get_origin()
xy = origin.getXY()
box = self._make_box(0, 0, 50)
# Face1 is the -Z face of a Part::Box.
joint = self._make_joint(
5,
[box, ["Face1", "Vertex1"]],
[origin, [xy.Name + ".", xy.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertEqual(
result, 0, "Solve should succeed for face→datum Distance joint"
)
# ── Round-trip test ──────────────────────────────────────────────
def test_save_load_preserves_labels(self):
"""Labels survive save/load round-trip."""
origin = self._get_origin()
# Verify labels before save
self.assertEqual(origin.getXY().Label, "Top")
self.assertEqual(origin.getXZ().Label, "Front")
self.assertEqual(origin.getYZ().Label, "Right")
# Save to temp file
tmp = tempfile.mktemp(suffix=".FCStd")
try:
self.doc.saveAs(tmp)
# Close and reopen
doc_name = self.doc.Name
App.closeDocument(doc_name)
App.openDocument(tmp)
doc = App.ActiveDocument
assembly = doc.getObject("Assembly")
self.assertIsNotNone(assembly)
origin = assembly.Origin
self.assertEqual(origin.getXY().Label, "Top")
self.assertEqual(origin.getXZ().Label, "Front")
self.assertEqual(origin.getYZ().Label, "Right")
App.closeDocument(doc.Name)
finally:
if os.path.exists(tmp):
os.remove(tmp)
# Reopen a fresh doc for tearDown
App.newDocument(self.__class__.__name__)

View File

@@ -0,0 +1,266 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# /****************************************************************************
# *
# Copyright (c) 2025 Kindred Systems <development@kindred-systems.com> *
# *
# This file is part of FreeCAD. *
# *
# FreeCAD is free software: you can redistribute it and/or modify it *
# under the terms of the GNU Lesser General Public License as *
# published by the Free Software Foundation, either version 2.1 of the *
# License, or (at your option) any later version. *
# *
# FreeCAD is distributed in the hope that it will be useful, but *
# WITHOUT ANY WARRANTY; without even the implied warranty of *
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# Lesser General Public License for more details. *
# *
# You should have received a copy of the GNU Lesser General Public *
# License along with FreeCAD. If not, see *
# <https://www.gnu.org/licenses/>. *
# *
# ***************************************************************************/
"""
Tests for datum plane classification in Distance joints.
Verifies that getDistanceType correctly classifies joints involving datum
planes from all three class hierarchies:
1. App::Plane — origin planes (XY, XZ, YZ)
2. PartDesign::Plane — datum planes inside a PartDesign body
3. Part::Plane — Part workbench plane primitives (bare reference)
"""
import unittest
import FreeCAD as App
import JointObject
class TestDatumClassification(unittest.TestCase):
"""Tests that Distance joints with datum plane references are
classified as PlanePlane (not Other) regardless of the datum
object's class hierarchy."""
def setUp(self):
doc_name = self.__class__.__name__
if App.ActiveDocument:
if App.ActiveDocument.Name != doc_name:
App.newDocument(doc_name)
else:
App.newDocument(doc_name)
App.setActiveDocument(doc_name)
self.doc = App.ActiveDocument
self.assembly = self.doc.addObject("Assembly::AssemblyObject", "Assembly")
self.jointgroup = self.assembly.newObject("Assembly::JointGroup", "Joints")
def tearDown(self):
App.closeDocument(self.doc.Name)
# ── Helpers ─────────────────────────────────────────────────────
def _make_box(self, x=0, y=0, z=0, size=10):
box = self.assembly.newObject("Part::Box", "Box")
box.Length = size
box.Width = size
box.Height = size
box.Placement = App.Placement(App.Vector(x, y, z), App.Rotation())
return box
def _make_joint(self, joint_type, ref1, ref2):
joint = self.jointgroup.newObject("App::FeaturePython", "Joint")
JointObject.Joint(joint, joint_type)
refs = [
[ref1[0], ref1[1]],
[ref2[0], ref2[1]],
]
joint.Proxy.setJointConnectors(joint, refs)
return joint
def _make_pd_body_with_datum_plane(self, name="Body"):
"""Create a PartDesign::Body with a datum plane inside the assembly."""
body = self.assembly.newObject("PartDesign::Body", name)
datum = body.newObject("PartDesign::Plane", "DatumPlane")
self.doc.recompute()
return body, datum
def _make_part_plane(self, name="PartPlane"):
"""Create a Part::Plane primitive inside the assembly."""
plane = self.assembly.newObject("Part::Plane", name)
plane.Length = 10
plane.Width = 10
self.doc.recompute()
return plane
# ── Origin plane tests (App::Plane — existing behaviour) ───────
def test_origin_plane_face_classified_as_plane_plane(self):
"""Distance joint: box Face → origin datum plane → PlanePlane."""
origin = self.assembly.Origin
xy = origin.getXY()
box = self._make_box(0, 0, 50)
joint = self._make_joint(
5, # Distance
[box, ["Face1", "Vertex1"]],
[origin, [xy.Name + ".", xy.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertEqual(
result,
0,
"Distance joint with origin plane should solve (not produce Other)",
)
# ── PartDesign::Plane tests ────────────────────────────────────
def test_pd_datum_plane_face_classified_as_plane_plane(self):
"""Distance joint: box Face → PartDesign::Plane → PlanePlane."""
body, datum = self._make_pd_body_with_datum_plane()
box = self._make_box(0, 0, 50)
# Ground the body so the solver has a fixed reference.
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, body)
# Reference the datum plane with a bare sub-name (ends with ".").
joint = self._make_joint(
5, # Distance
[box, ["Face1", "Vertex1"]],
[body, [datum.Name + ".", datum.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint with PartDesign::Plane should not fail to solve "
"(DistanceType should be PlanePlane, not Other)",
)
def test_pd_datum_plane_vertex_classified_as_point_plane(self):
"""Distance joint: box Vertex → PartDesign::Plane → PointPlane."""
body, datum = self._make_pd_body_with_datum_plane()
box = self._make_box(0, 0, 50)
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, body)
joint = self._make_joint(
5, # Distance
[box, ["Vertex1", "Vertex1"]],
[body, [datum.Name + ".", datum.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint vertex → PartDesign::Plane should not fail "
"(DistanceType should be PointPlane, not Other)",
)
def test_two_pd_datum_planes_classified_as_plane_plane(self):
"""Distance joint: PartDesign::Plane → PartDesign::Plane → PlanePlane."""
body1, datum1 = self._make_pd_body_with_datum_plane("Body1")
body2, datum2 = self._make_pd_body_with_datum_plane("Body2")
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, body1)
joint = self._make_joint(
5, # Distance
[body1, [datum1.Name + ".", datum1.Name + "."]],
[body2, [datum2.Name + ".", datum2.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint PartDesign::Plane → PartDesign::Plane should not fail "
"(DistanceType should be PlanePlane, not Other)",
)
# ── Part::Plane tests (primitive, bare reference) ──────────────
def test_part_plane_bare_ref_face_classified_as_plane_plane(self):
"""Distance joint: box Face → Part::Plane (bare ref) → PlanePlane."""
plane = self._make_part_plane()
box = self._make_box(0, 0, 50)
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, plane)
# Bare reference to Part::Plane (sub-name ends with ".").
joint = self._make_joint(
5, # Distance
[box, ["Face1", "Vertex1"]],
[plane, [".", "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint with Part::Plane (bare ref) should not fail "
"(DistanceType should be PlanePlane, not Other)",
)
def test_part_plane_with_face1_classified_as_plane_plane(self):
"""Distance joint: box Face → Part::Plane Face1 → PlanePlane.
When Part::Plane is referenced with an explicit Face1 element,
it should enter the normal Face+Face classification path."""
plane = self._make_part_plane()
box = self._make_box(0, 0, 50)
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, plane)
joint = self._make_joint(
5, # Distance
[box, ["Face1", "Vertex1"]],
[plane, ["Face1", "Vertex1"]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint with Part::Plane Face1 should solve normally",
)
# ── Cross-hierarchy tests ──────────────────────────────────────
def test_origin_plane_and_pd_datum_classified_as_plane_plane(self):
"""Distance joint: origin App::Plane → PartDesign::Plane → PlanePlane."""
origin = self.assembly.Origin
xy = origin.getXY()
body, datum = self._make_pd_body_with_datum_plane()
gnd = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
JointObject.GroundedJoint(gnd, body)
joint = self._make_joint(
5, # Distance
[origin, [xy.Name + ".", xy.Name + "."]],
[body, [datum.Name + ".", datum.Name + "."]],
)
joint.Distance = 0.0
result = self.assembly.solve()
self.assertNotEqual(
result,
-1,
"Distance joint origin plane → PartDesign::Plane should not fail "
"(DistanceType should be PlanePlane, not Other)",
)

View File

@@ -60,6 +60,8 @@ SET(AssemblyTests_SRCS
AssemblyTests/TestSolverIntegration.py
AssemblyTests/TestKindredSolverIntegration.py
AssemblyTests/TestKCSolvePy.py
AssemblyTests/TestAssemblyOriginPlanes.py
AssemblyTests/TestDatumClassification.py
AssemblyTests/mocks/__init__.py
AssemblyTests/mocks/MockGui.py
)

View File

@@ -22,15 +22,14 @@
# **************************************************************************/
import FreeCAD as App
from PySide.QtCore import QT_TRANSLATE_NOOP
if App.GuiUp:
import FreeCADGui as Gui
from PySide import QtCore, QtGui, QtWidgets
import UtilsAssembly
import Preferences
import UtilsAssembly
translate = App.Qt.translate
@@ -78,14 +77,22 @@ class CommandCreateAssembly:
'assembly = activeAssembly.newObject("Assembly::AssemblyObject", "Assembly")\n'
)
else:
commands = (
'assembly = App.ActiveDocument.addObject("Assembly::AssemblyObject", "Assembly")\n'
)
commands = 'assembly = App.ActiveDocument.addObject("Assembly::AssemblyObject", "Assembly")\n'
commands = commands + 'assembly.Type = "Assembly"\n'
commands = commands + 'assembly.newObject("Assembly::JointGroup", "Joints")'
Gui.doCommand(commands)
# Make origin planes visible by default so they serve as
# reference geometry (like SolidWorks Front/Top/Right planes).
Gui.doCommandGui(
"assembly.Origin.ViewObject.Visibility = True\n"
"for feat in assembly.Origin.OriginFeatures:\n"
" if feat.isDerivedFrom('App::Plane'):\n"
" feat.ViewObject.Visibility = True\n"
)
if not activeAssembly:
Gui.doCommandGui("Gui.ActiveDocument.setEdit(assembly)")
@@ -98,7 +105,9 @@ class ActivateAssemblyTaskPanel:
def __init__(self, assemblies):
self.assemblies = assemblies
self.form = QtWidgets.QWidget()
self.form.setWindowTitle(translate("Assembly_ActivateAssembly", "Activate Assembly"))
self.form.setWindowTitle(
translate("Assembly_ActivateAssembly", "Activate Assembly")
)
layout = QtWidgets.QVBoxLayout(self.form)
label = QtWidgets.QLabel(
@@ -132,9 +141,12 @@ class CommandActivateAssembly:
def GetResources(self):
return {
"Pixmap": "Assembly_ActivateAssembly",
"MenuText": QT_TRANSLATE_NOOP("Assembly_ActivateAssembly", "Activate Assembly"),
"MenuText": QT_TRANSLATE_NOOP(
"Assembly_ActivateAssembly", "Activate Assembly"
),
"ToolTip": QT_TRANSLATE_NOOP(
"Assembly_ActivateAssembly", "Sets an assembly as the active one for editing."
"Assembly_ActivateAssembly",
"Sets an assembly as the active one for editing.",
),
"CmdType": "ForEdit",
}
@@ -156,7 +168,9 @@ class CommandActivateAssembly:
def Activated(self):
doc = App.ActiveDocument
assemblies = [o for o in doc.Objects if o.isDerivedFrom("Assembly::AssemblyObject")]
assemblies = [
o for o in doc.Objects if o.isDerivedFrom("Assembly::AssemblyObject")
]
if len(assemblies) == 1:
# If there's only one, activate it directly without showing a dialog

View File

@@ -22,6 +22,7 @@
# **************************************************************************/
import TestApp
from AssemblyTests.TestAssemblyOriginPlanes import TestAssemblyOriginPlanes
from AssemblyTests.TestCommandInsertLink import TestCommandInsertLink
from AssemblyTests.TestCore import TestCore
from AssemblyTests.TestKCSolvePy import (
@@ -36,5 +37,6 @@ from AssemblyTests.TestSolverIntegration import TestSolverIntegration
# Use the modules so that code checkers don't complain (flake8)
True if TestCore else False
True if TestCommandInsertLink else False
True if TestAssemblyOriginPlanes else False
True if TestSolverIntegration else False
True if TestKindredSolverIntegration else False

View File

@@ -85,6 +85,22 @@ install(
mods/sdk
)
# Install QuickNav addon
install(
DIRECTORY
${CMAKE_SOURCE_DIR}/mods/quicknav/quicknav
DESTINATION
mods/quicknav
)
install(
FILES
${CMAKE_SOURCE_DIR}/mods/quicknav/package.xml
${CMAKE_SOURCE_DIR}/mods/quicknav/Init.py
${CMAKE_SOURCE_DIR}/mods/quicknav/InitGui.py
DESTINATION
mods/quicknav
)
# Install Kindred Solver addon
install(
DIRECTORY

View File

@@ -162,34 +162,28 @@ class _KcFormatObserver:
f"kc_format: pre_reinject hook failed: {exc}\n"
)
try:
# Ensure silo/manifest.json exists in entries and update modified_at.
# All manifest mutations happen here so only one copy is written.
if "silo/manifest.json" in entries:
try:
manifest = json.loads(entries["silo/manifest.json"])
except (json.JSONDecodeError, ValueError):
manifest = _default_manifest()
else:
manifest = _default_manifest()
manifest["modified_at"] = datetime.now(timezone.utc).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
entries["silo/manifest.json"] = (
json.dumps(manifest, indent=2) + "\n"
).encode("utf-8")
with zipfile.ZipFile(filename, "a") as zf:
existing = set(zf.namelist())
# Re-inject cached silo/ entries
if entries:
for name, data in entries.items():
if name not in existing:
zf.writestr(name, data)
existing.add(name)
# Ensure silo/manifest.json exists
if "silo/manifest.json" not in existing:
manifest = _default_manifest()
zf.writestr(
"silo/manifest.json",
json.dumps(manifest, indent=2) + "\n",
)
else:
# Update modified_at timestamp
raw = zf.read("silo/manifest.json")
manifest = json.loads(raw)
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
if manifest.get("modified_at") != now:
manifest["modified_at"] = now
# ZipFile append mode can't overwrite; write new entry
# (last duplicate wins in most ZIP readers)
zf.writestr(
"silo/manifest.json",
json.dumps(manifest, indent=2) + "\n",
)
for name, data in entries.items():
if name not in existing:
zf.writestr(name, data)
existing.add(name)
except Exception as e:
FreeCAD.Console.PrintWarning(
f"kc_format: failed to update .kc silo/ entries: {e}\n"
@@ -209,17 +203,36 @@ def update_manifest_fields(filename, updates):
return
if not os.path.isfile(filename):
return
import shutil
import tempfile
try:
with zipfile.ZipFile(filename, "a") as zf:
if "silo/manifest.json" not in zf.namelist():
return
raw = zf.read("silo/manifest.json")
manifest = json.loads(raw)
manifest.update(updates)
zf.writestr(
"silo/manifest.json",
json.dumps(manifest, indent=2) + "\n",
)
fd, tmp = tempfile.mkstemp(suffix=".kc", dir=os.path.dirname(filename))
os.close(fd)
try:
with (
zipfile.ZipFile(filename, "r") as zin,
zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_DEFLATED) as zout,
):
found = False
for item in zin.infolist():
if item.filename == "silo/manifest.json":
if found:
continue # skip duplicate entries
found = True
raw = zin.read(item.filename)
manifest = json.loads(raw)
manifest.update(updates)
zout.writestr(
item.filename,
json.dumps(manifest, indent=2) + "\n",
)
else:
zout.writestr(item, zin.read(item.filename))
shutil.move(tmp, filename)
except BaseException:
os.unlink(tmp)
raise
except Exception as e:
FreeCAD.Console.PrintWarning(f"kc_format: failed to update manifest: {e}\n")

View File

@@ -122,4 +122,15 @@ TEST(UniqueNameManager, UniqueNameWith9NDigits)
manager.addExactName("Compound123456789");
EXPECT_EQ(manager.makeUniqueName("Compound", 3), "Compound123456790");
}
TEST(UniqueNameManager, StructuredPartNumberDecomposition)
{
// Structured part numbers like P03-0001 have their trailing digits
// treated as the uniquifying suffix by UniqueNameManager. This is
// correct for default FreeCAD objects (Body -> Body001) but wrong
// for structured identifiers. Assembly module handles this separately
// via makeInstanceLabel which appends -N instance suffixes instead.
Base::UniqueNameManager manager;
manager.addExactName("P03-0001");
EXPECT_EQ(manager.makeUniqueName("P03-0001", 3), "P03-0002");
}
// NOLINTEND(cppcoreguidelines-*,readability-*)

View File

@@ -101,6 +101,7 @@ sys.path.insert(0, str(_REPO_ROOT / "src" / "Mod" / "Create"))
sys.path.insert(0, str(_REPO_ROOT / "mods" / "sdk"))
sys.path.insert(0, str(_REPO_ROOT / "mods" / "ztools" / "ztools"))
sys.path.insert(0, str(_REPO_ROOT / "mods" / "silo" / "freecad"))
sys.path.insert(0, str(_REPO_ROOT / "mods" / "quicknav"))
# ---------------------------------------------------------------------------
# Now import the modules under test
@@ -123,6 +124,15 @@ from silo_commands import _safe_float # noqa: E402
import silo_start # noqa: E402
import silo_origin # noqa: E402
from quicknav.workbench_map import ( # noqa: E402
WORKBENCH_SLOTS,
WORKBENCH_GROUPINGS,
get_workbench_slot,
get_groupings,
get_grouping,
get_command,
)
# ===================================================================
# Test: update_checker._parse_version
@@ -554,6 +564,110 @@ class TestDatumModes(unittest.TestCase):
self.assertEqual(len(points), 5)
# ===================================================================
# Test: quicknav workbench_map
# ===================================================================
class TestWorkbenchMap(unittest.TestCase):
"""Tests for quicknav.workbench_map data and helpers."""
def test_all_slots_defined(self):
for n in range(1, 6):
slot = WORKBENCH_SLOTS.get(n)
self.assertIsNotNone(slot, f"Slot {n} missing from WORKBENCH_SLOTS")
def test_slot_keys(self):
for n, slot in WORKBENCH_SLOTS.items():
self.assertIn("key", slot)
self.assertIn("class_name", slot)
self.assertIn("display", slot)
self.assertIsInstance(slot["key"], str)
self.assertIsInstance(slot["class_name"], str)
self.assertIsInstance(slot["display"], str)
def test_each_slot_has_groupings(self):
for n, slot in WORKBENCH_SLOTS.items():
groupings = WORKBENCH_GROUPINGS.get(slot["key"])
self.assertIsNotNone(groupings, f"No groupings for workbench key '{slot['key']}'")
self.assertGreater(len(groupings), 0, f"Empty groupings for slot {n}")
def test_max_nine_groupings_per_workbench(self):
for wb_key, groupings in WORKBENCH_GROUPINGS.items():
self.assertLessEqual(len(groupings), 9, f"More than 9 groupings for '{wb_key}'")
def test_max_nine_commands_per_grouping(self):
for wb_key, groupings in WORKBENCH_GROUPINGS.items():
for i, grp in enumerate(groupings):
self.assertLessEqual(
len(grp["commands"]),
9,
f"More than 9 commands in '{wb_key}' grouping {i}",
)
def test_command_tuples_are_str_str(self):
for wb_key, groupings in WORKBENCH_GROUPINGS.items():
for i, grp in enumerate(groupings):
self.assertIn("name", grp)
self.assertIn("commands", grp)
for j, cmd in enumerate(grp["commands"]):
self.assertIsInstance(cmd, tuple, f"{wb_key}[{i}][{j}] not tuple")
self.assertEqual(len(cmd), 2, f"{wb_key}[{i}][{j}] not length 2")
self.assertIsInstance(cmd[0], str, f"{wb_key}[{i}][{j}][0] not str")
self.assertIsInstance(cmd[1], str, f"{wb_key}[{i}][{j}][1] not str")
def test_get_workbench_slot_valid(self):
for n in range(1, 6):
slot = get_workbench_slot(n)
self.assertIsNotNone(slot)
self.assertEqual(slot, WORKBENCH_SLOTS[n])
def test_get_workbench_slot_invalid(self):
self.assertIsNone(get_workbench_slot(0))
self.assertIsNone(get_workbench_slot(6))
self.assertIsNone(get_workbench_slot(99))
def test_get_groupings_valid(self):
for slot in WORKBENCH_SLOTS.values():
result = get_groupings(slot["key"])
self.assertIsNotNone(result)
self.assertIsInstance(result, list)
def test_get_groupings_invalid(self):
self.assertEqual(get_groupings("nonexistent"), [])
def test_get_grouping_valid(self):
for wb_key, groupings in WORKBENCH_GROUPINGS.items():
for i in range(len(groupings)):
grp = get_grouping(wb_key, i)
self.assertIsNotNone(grp)
self.assertEqual(grp, groupings[i])
def test_get_grouping_invalid_index(self):
wb_key = WORKBENCH_SLOTS[1]["key"]
self.assertIsNone(get_grouping(wb_key, 99))
self.assertIsNone(get_grouping(wb_key, -1))
def test_get_grouping_invalid_key(self):
self.assertIsNone(get_grouping("nonexistent", 0))
def test_get_command_valid(self):
for wb_key, groupings in WORKBENCH_GROUPINGS.items():
for gi, grp in enumerate(groupings):
for ci in range(len(grp["commands"])):
cmd_id = get_command(wb_key, gi, ci + 1)
self.assertIsNotNone(cmd_id, f"None for {wb_key}[{gi}][{ci + 1}]")
self.assertEqual(cmd_id, grp["commands"][ci][0])
def test_get_command_invalid_number(self):
wb_key = WORKBENCH_SLOTS[1]["key"]
self.assertIsNone(get_command(wb_key, 0, 0))
self.assertIsNone(get_command(wb_key, 0, 99))
def test_get_command_invalid_workbench(self):
self.assertIsNone(get_command("nonexistent", 0, 1))
# ===================================================================