Compare commits

..

33 Commits

Author SHA1 Message Date
Zoe Forbes
3fe43710fa fix(ui): remove addStretch from auth panel, use compact size policy
Replace layout.addStretch() with QSizePolicy.Maximum so the
Database Auth dock panel only takes the height its content needs,
leaving more vertical space for the Database Activity panel below.

Closes #190
2026-02-15 09:43:15 -06:00
Zoe Forbes
8cbd872e5c Merge branch 'feat/worker-client-ui' into main
Resolve conflicts in silo_commands.py:
- Keep event-based activity feed (_append_activity_event, _rebuild_activity_feed)
- Adapt DAG/job SSE handlers to use _append_activity_event
- Keep _relative_time formatting for activity entries
- Include DNS diagnostic IP display from feature branch
2026-02-15 08:32:55 -06:00
Zoe Forbes
e31321ac95 feat: add Jobs and Runners commands with SSE event wiring
- Add JobMonitorDialog (Silo_Jobs): filter, view, trigger, cancel jobs
- Add RunnerAdminDialog (Silo_Runners): list, register, delete runners
- Wire job_claimed, job_progress, job_cancelled SSE signals to handlers
- Add activity panel entries for job lifecycle events
- Register Silo_Jobs in toolbar and menu, Silo_Runners in menu
- Update silo-client submodule with worker API methods
2026-02-15 05:07:33 -06:00
Zoe Forbes
dc64a66f0f feat: show DAG status and job events in Activity panel
Connects dag_updated, dag_validated, and job lifecycle signals
from SiloEventListener to the Database Activity dock widget.

- dag.updated: inserts DAG sync status (node/edge count)
- dag.validated: inserts pass/fail badge with failed count
- job.created: inserts queued job entry
- job.completed: refreshes the full activity list
- job.failed: inserts error entry

Live entries are inserted at the top of the activity list,
styled in Catppuccin Blue, capped at 50 entries.

Closes kindred/create#219
2026-02-14 15:28:40 -06:00
Zoe Forbes
3d38e4b4c3 feat: handle DAG and job SSE events in SiloEventListener
New signals:
- dag_updated(part_number, node_count, edge_count)
- dag_validated(part_number, valid, failed_count)
- job_created/claimed/progress/completed/failed/cancelled

Dispatch logic parses payloads and emits typed signals for
downstream UI and logging consumers.

Closes kindred/create#218
2026-02-14 15:22:29 -06:00
Zoe Forbes
da2a360c56 feat: add headless runner entry points
New file: runner.py with three entry points for silorunner:
- dag_extract(input_path, output_path): extract feature DAG as JSON
- validate(input_path, output_path): rebuild features, report pass/fail
- export(input_path, output_path, format): export to STEP/IGES/STL/OBJ

Invoked via: create --console -e 'from runner import dag_extract; ...'

Closes kindred/create#217
2026-02-14 15:17:30 -06:00
Zoe Forbes
3dd0da3964 feat: push DAG on save and commit
Adds _push_dag_after_upload() helper that extracts the feature DAG
and pushes it to Silo after a successful file upload.

Hooked into both Silo_Save and Silo_Commit commands. DAG sync
failures are logged as warnings and never block the save/commit.

Closes kindred/create#216
2026-02-14 15:10:50 -06:00
Zoe Forbes
4921095296 feat: update silo-client — add DAG API methods
Points silo-client to feat/dag-api-methods with push_dag/get_dag.

Closes kindred/create#215
2026-02-14 15:06:31 -06:00
Zoe Forbes
3a9fe6aed8 feat: add DAG extraction engine
Implements extract_dag(), classify_type(), and compute_properties_hash()
for extracting feature trees from FreeCAD documents.

- classify_type: maps ~50 FreeCAD TypeIds to 8 DAG node types
- compute_properties_hash: SHA-256 of per-feature parametric inputs
- extract_dag: two-pass walk of doc.Objects producing nodes + edges

No GUI dependencies -- works in both desktop and headless mode.

Closes kindred/create#214
2026-02-14 14:41:35 -06:00
Zoe Forbes
9e99b83091 fix: default cert browser to home dir instead of /etc/ssl/certs (#203)
The CA certificate file browser hardcoded /etc/ssl/certs as fallback,
which confused users when the dialog opened to a system directory.
Default to the user's home directory instead.
2026-02-14 12:50:01 -06:00
0f407360ed Merge pull request 'feat: use .kc extension for new files, find both .kc and .FCStd' (#23) from feat/kc-file-format-layer1 into main
Reviewed-on: #23
2026-02-13 19:42:03 +00:00
fa4f3145c6 Merge branch 'main' into feat/kc-file-format-layer1 2026-02-13 19:41:55 +00:00
Zoe Forbes
fed72676bc feat: use .kc extension for new files, find both .kc and .FCStd
- get_cad_file_path() now generates .kc paths instead of .FCStd
- find_file_by_part_number() searches .kc first, falls back to .FCStd
- search_local_files() lists both .kc and .FCStd files
2026-02-13 13:39:22 -06:00
d3e27010d8 Merge pull request 'feat: live SSE-based activity feed for Database Activity panel' (#22) from feat/live-activity-panel into main
Reviewed-on: #22
2026-02-13 01:57:03 +00:00
Zoe Forbes
d7c6066030 feat: live activity panel with SSE event feed and relative timestamps
Replace static item list refresh with real-time event feed:
- Add _relative_time() helper for human-friendly timestamps
- Prepend SSE events (item updates, new revisions, mode changes) instantly
- Seed feed with 10 recent items on first SSE connect (no per-item revision calls)
- Refresh relative timestamps every 60 seconds
- Cap activity feed at 50 events
- Remove expensive list_items + get_revisions calls on every SSE event
2026-02-12 17:27:25 -06:00
91f539a18a Merge pull request 'feat(open): replace modal open dialog with MDI tab' (#20) from feat/open-item-mdi-tab into main
Reviewed-on: #20
2026-02-12 17:47:09 +00:00
2ddfea083a Merge branch 'main' into feat/open-item-mdi-tab 2026-02-12 17:46:57 +00:00
be8783bf0a feat(open): replace modal open dialog with MDI tab
Extract search-and-open UI into OpenItemWidget (open_search.py), a
plain QWidget with item_selected/cancelled signals.  Silo_Open now
adds this widget as an MDI subwindow instead of running a blocking
QDialog, matching the Silo_New tab pattern.

Improvements over the old dialog:
- Non-blocking: multiple search tabs can be open simultaneously
- 500 ms debounce on search input reduces API load
- Filter checkbox changes trigger immediate re-search
2026-02-12 10:22:42 -06:00
972dc07157 feat(silo): replace modal new-item dialog with MDI pre-document tab
Extract SchemaFormWidget from SchemaFormDialog so the creation form
can be embedded as a plain QWidget in an MDI subwindow tab.  Each
Ctrl+N invocation opens a new tab alongside document tabs.  On
successful creation the pre-document tab closes and the real document
opens in its place.

- SchemaFormWidget emits item_created/cancelled signals
- SchemaFormDialog preserved as thin modal wrapper for backward compat
- Inline error display replaces modal QMessageBox
- Live tab title updates from part number preview
2026-02-11 15:14:38 -06:00
069bb7a552 Merge pull request 'fix: pull assembly dependencies recursively before opening' (#19) from fix/pull-assembly-dependencies into main
Reviewed-on: #19
2026-02-11 19:12:22 +00:00
201e0af450 feat: register Silo overlay context for EditingContextResolver
Add 'Silo Origin' toolbar (Commit/Pull/Push/Info/BOM) registered with
Unavailable visibility. Register a Silo overlay via
FreeCADGui.registerEditingOverlay() that appends this toolbar to any
active editing context when the current document is Silo-tracked
(ownsDocument() returns True).

Consolidate PySide.QtCore imports.
2026-02-11 13:11:47 -06:00
8a6e5cdffa fix: pull assembly dependencies recursively before opening
When pulling an assembly from Silo, the linked component files were not
downloaded, causing FreeCAD to report 'Link not restored' errors for
every external reference.

Add _pull_dependencies() that queries the BOM API to discover child
part numbers, then downloads the latest file revision for each child
that doesn't already exist locally. Recurses into sub-assemblies.

Silo_Pull.Activated() now calls _pull_dependencies() after downloading
the assembly file and before opening it, so all PropertyXLink paths
resolve correctly.
2026-02-11 13:09:59 -06:00
95c56fa29a Merge pull request 'feat/schema-driven-new-item-form' (#18) from feat/schema-driven-new-item-form into main
Reviewed-on: #18
2026-02-11 16:19:05 +00:00
33d5eeb76c Merge branch 'main' into feat/schema-driven-new-item-form 2026-02-11 16:18:55 +00:00
9e83982c78 feat: schema-driven Qt form for new item creation
Replace the 3-dialog chain (category → description → projects) with a
single SchemaFormDialog that fetches schema data from the Silo REST API
and builds the UI dynamically at runtime.

New dialog features:
- Two-stage category picker (domain combo → subcategory combo)
- Dynamic property fields grouped by domain and common defaults
- Collapsible form sections (Identity, Sourcing, Details, Properties)
- Live part number preview via POST /api/generate-part-number
- Item type selection (part, assembly, consumable, tool)
- Sourcing fields (type, cost, URL)
- Project tagging via multi-select list
- Widget factory: string→QLineEdit, number→QDoubleSpinBox+unit,
  boolean→QCheckBox

The form mirrors the React CreateItemPane.tsx layout and uses the same
API endpoints:
- GET /api/schemas/kindred-rd (category enum values)
- GET /api/schemas/kindred-rd/properties?category={code}
- GET /api/projects
- POST /api/items (via _client.create_item)
2026-02-11 08:42:18 -06:00
e83769090b fix: use Qt enum for setWindowModality instead of raw integer
PySide6 requires the proper enum type QtCore.Qt.WindowModal, not the
raw integer 2. The integer form was accepted by PySide2/Qt5 but raises
TypeError in PySide6.
2026-02-11 07:40:05 -06:00
52fc9cdd3a Merge pull request 'fix: save Modified attribute and SSE retry reset' (#17) from fix/silo-sse-and-save into main
Reviewed-on: #17
2026-02-11 01:15:53 +00:00
ae132948d1 Merge branch 'main' into fix/silo-sse-and-save 2026-02-11 01:15:39 +00:00
Zoe Forbes
ab801601c9 fix: save Modified attribute and SSE retry reset
- silo_origin.py: use Gui.Document.Modified instead of App.Document.Modified
  (re-applies fix from #13 that was lost in rebase)
- silo_origin.py: add traceback logging to saveDocument error handler
- silo_commands.py: reset SSE retry counter after connections lasting >30s
  so transient disconnects don't permanently kill the listener
2026-02-10 19:00:13 -06:00
32d5f1ea1b Merge pull request 'fix: use FreeCADGui.Document.Modified instead of App.Document.IsModified()' (#16) from fix/pull-is-modified-bug into main
Reviewed-on: #16
2026-02-10 16:41:23 +00:00
de80e392f5 Merge branch 'main' into fix/pull-is-modified-bug 2026-02-10 16:41:15 +00:00
ba42343577 Merge pull request 'feat: native Qt start panel with Silo API + kindred:// URL scheme' (#15) from feat/native-start-panel-167 into main
Reviewed-on: #15
2026-02-10 16:40:58 +00:00
6c9789fdf3 fix: use FreeCADGui.Document.Modified instead of App.Document.IsModified()
App.Document has no IsModified() method, causing Silo_Pull to crash with
AttributeError. The correct API is to get the Gui document and check its
Modified property, consistent with the pattern used elsewhere in this file
(lines 891, 913).
2026-02-10 10:39:13 -06:00
8 changed files with 2486 additions and 352 deletions

View File

@@ -35,18 +35,32 @@ class SiloWorkbench(FreeCADGui.Workbench):
except Exception as e:
FreeCAD.Console.PrintWarning(f"Could not register Silo origin: {e}\n")
# Silo origin toolbar — shown as an overlay on any context when the
# active document is Silo-tracked. Registered as Unavailable so
# EditingContextResolver controls visibility via the overlay system.
self.silo_toolbar_commands = [
"Silo_Commit",
"Silo_Pull",
"Silo_Push",
"Separator",
"Silo_Info",
"Silo_BOM",
"Silo_Jobs",
]
self.appendToolbar("Silo Origin", self.silo_toolbar_commands, "Unavailable")
# Silo menu provides admin/management commands.
# File operations (New/Open/Save) are handled by the standard File
# toolbar via the origin system -- no separate Silo toolbar needed.
self.menu_commands = [
"Silo_Info",
"Silo_BOM",
"Silo_Jobs",
"Silo_TagProjects",
"Silo_SetStatus",
"Silo_Rollback",
"Separator",
"Silo_Settings",
"Silo_Auth",
"Silo_Runners",
"Silo_StartPanel",
"Silo_Diag",
]
@@ -68,6 +82,44 @@ class SiloWorkbench(FreeCADGui.Workbench):
FreeCADGui.addWorkbench(SiloWorkbench())
FreeCAD.Console.PrintMessage("Silo workbench registered\n")
# ---------------------------------------------------------------------------
# Silo overlay context — adds "Silo Origin" toolbar to any active context
# when the current document is Silo-tracked.
# ---------------------------------------------------------------------------
def _register_silo_overlay():
"""Register the Silo overlay after the Silo workbench has initialised."""
def _silo_overlay_match():
"""Return True if the active document is Silo-tracked."""
try:
doc = FreeCAD.ActiveDocument
if not doc:
return False
from silo_origin import get_silo_origin
origin = get_silo_origin()
return origin.ownsDocument(doc)
except Exception:
return False
try:
FreeCADGui.registerEditingOverlay(
"silo", # overlay id
["Silo Origin"], # toolbar names to append
_silo_overlay_match, # match function
)
except Exception as e:
FreeCAD.Console.PrintWarning(f"Silo overlay registration failed: {e}\n")
from PySide import QtCore as _QtCore
_QtCore.QTimer.singleShot(2500, _register_silo_overlay)
# Override the Start page with Silo-aware version (must happen before
# the C++ StartLauncher fires at ~100ms after GUI init)
try:
@@ -92,6 +144,4 @@ def _handle_startup_urls():
handle_kindred_url(arg)
from PySide import QtCore
QtCore.QTimer.singleShot(500, _handle_startup_urls)
_QtCore.QTimer.singleShot(500, _handle_startup_urls)

463
freecad/dag.py Normal file
View File

@@ -0,0 +1,463 @@
"""DAG extraction engine for FreeCAD documents.
Extracts the feature tree from a FreeCAD document as nodes and edges
for syncing to the Silo server. No GUI dependencies -- usable in both
desktop and headless (``--console``) mode.
Public API
----------
classify_type(type_id) -> Optional[str]
compute_properties_hash(obj) -> str
extract_dag(doc) -> (nodes, edges)
"""
import hashlib
import json
import math
from typing import Any, Dict, List, Optional, Set, Tuple
# ---------------------------------------------------------------------------
# TypeId -> DAG node type mapping
# ---------------------------------------------------------------------------
_TYPE_MAP: Dict[str, str] = {
# Sketch
"Sketcher::SketchObject": "sketch",
# Sketch-based additive
"PartDesign::Pad": "pad",
"PartDesign::Revolution": "pad",
"PartDesign::AdditivePipe": "pad",
"PartDesign::AdditiveLoft": "pad",
"PartDesign::AdditiveHelix": "pad",
# Sketch-based subtractive
"PartDesign::Pocket": "pocket",
"PartDesign::Groove": "pocket",
"PartDesign::Hole": "pocket",
"PartDesign::SubtractivePipe": "pocket",
"PartDesign::SubtractiveLoft": "pocket",
"PartDesign::SubtractiveHelix": "pocket",
# Dress-up
"PartDesign::Fillet": "fillet",
"PartDesign::Chamfer": "chamfer",
"PartDesign::Draft": "chamfer",
"PartDesign::Thickness": "chamfer",
# Transformations
"PartDesign::Mirrored": "pad",
"PartDesign::LinearPattern": "pad",
"PartDesign::PolarPattern": "pad",
"PartDesign::Scaled": "pad",
"PartDesign::MultiTransform": "pad",
# Boolean
"PartDesign::Boolean": "pad",
# Additive primitives
"PartDesign::AdditiveBox": "pad",
"PartDesign::AdditiveCylinder": "pad",
"PartDesign::AdditiveSphere": "pad",
"PartDesign::AdditiveCone": "pad",
"PartDesign::AdditiveEllipsoid": "pad",
"PartDesign::AdditiveTorus": "pad",
"PartDesign::AdditivePrism": "pad",
"PartDesign::AdditiveWedge": "pad",
# Subtractive primitives
"PartDesign::SubtractiveBox": "pocket",
"PartDesign::SubtractiveCylinder": "pocket",
"PartDesign::SubtractiveSphere": "pocket",
"PartDesign::SubtractiveCone": "pocket",
"PartDesign::SubtractiveEllipsoid": "pocket",
"PartDesign::SubtractiveTorus": "pocket",
"PartDesign::SubtractivePrism": "pocket",
"PartDesign::SubtractiveWedge": "pocket",
# Containers
"PartDesign::Body": "body",
"App::Part": "part",
"Part::Feature": "part",
# Datum / reference
"PartDesign::Point": "datum",
"PartDesign::Line": "datum",
"PartDesign::Plane": "datum",
"PartDesign::CoordinateSystem": "datum",
"PartDesign::ShapeBinder": "datum",
"PartDesign::SubShapeBinder": "datum",
}
def classify_type(type_id: str) -> Optional[str]:
"""Map a FreeCAD TypeId string to a DAG node type.
Returns one of ``sketch``, ``pad``, ``pocket``, ``fillet``,
``chamfer``, ``body``, ``part``, ``datum``, or ``None`` if the
TypeId is not a recognized feature.
"""
return _TYPE_MAP.get(type_id)
# ---------------------------------------------------------------------------
# Properties hash
# ---------------------------------------------------------------------------
def _safe_float(val: Any) -> Any:
"""Convert a float to a JSON-safe value, replacing NaN/Infinity with 0."""
if isinstance(val, float) and (math.isnan(val) or math.isinf(val)):
return 0.0
return val
def _prop_value(obj: Any, name: str) -> Any:
"""Safely read ``obj.<name>.Value``, returning *None* on failure."""
try:
return _safe_float(getattr(obj, name).Value)
except Exception:
return None
def _prop_raw(obj: Any, name: str) -> Any:
"""Safely read ``obj.<name>``, returning *None* on failure."""
try:
return getattr(obj, name)
except Exception:
return None
def _link_name(obj: Any, name: str) -> Optional[str]:
"""Return the ``.Name`` of a linked object property, or *None*."""
try:
link = getattr(obj, name)
if isinstance(link, (list, tuple)):
link = link[0]
return link.Name if link is not None else None
except Exception:
return None
def _collect_inputs(obj: Any) -> Dict[str, Any]:
"""Extract the parametric inputs that affect *obj*'s geometry.
The returned dict is JSON-serialized and hashed to produce the
``properties_hash`` for the DAG node.
"""
tid = obj.TypeId
inputs: Dict[str, Any] = {"_type": tid}
# --- Sketch ---
if tid == "Sketcher::SketchObject":
inputs["geometry_count"] = _prop_raw(obj, "GeometryCount")
inputs["constraint_count"] = _prop_raw(obj, "ConstraintCount")
try:
inputs["geometry"] = obj.Shape.exportBrepToString()
except Exception:
pass
return inputs
# --- Extrude (Pad / Pocket) ---
if tid in ("PartDesign::Pad", "PartDesign::Pocket"):
inputs["length"] = _prop_value(obj, "Length")
inputs["type"] = str(_prop_raw(obj, "Type") or "")
inputs["reversed"] = _prop_raw(obj, "Reversed")
inputs["sketch"] = _link_name(obj, "Profile")
return inputs
# --- Revolution / Groove ---
if tid in ("PartDesign::Revolution", "PartDesign::Groove"):
inputs["angle"] = _prop_value(obj, "Angle")
inputs["type"] = str(_prop_raw(obj, "Type") or "")
inputs["reversed"] = _prop_raw(obj, "Reversed")
inputs["sketch"] = _link_name(obj, "Profile")
return inputs
# --- Hole ---
if tid == "PartDesign::Hole":
inputs["diameter"] = _prop_value(obj, "Diameter")
inputs["depth"] = _prop_value(obj, "Depth")
inputs["threaded"] = _prop_raw(obj, "Threaded")
inputs["thread_type"] = str(_prop_raw(obj, "ThreadType") or "")
inputs["depth_type"] = str(_prop_raw(obj, "DepthType") or "")
inputs["sketch"] = _link_name(obj, "Profile")
return inputs
# --- Pipe / Loft / Helix (additive + subtractive) ---
if tid in (
"PartDesign::AdditivePipe",
"PartDesign::SubtractivePipe",
"PartDesign::AdditiveLoft",
"PartDesign::SubtractiveLoft",
"PartDesign::AdditiveHelix",
"PartDesign::SubtractiveHelix",
):
inputs["sketch"] = _link_name(obj, "Profile")
inputs["spine"] = _link_name(obj, "Spine")
return inputs
# --- Fillet ---
if tid == "PartDesign::Fillet":
inputs["radius"] = _prop_value(obj, "Radius")
return inputs
# --- Chamfer ---
if tid == "PartDesign::Chamfer":
inputs["chamfer_type"] = str(_prop_raw(obj, "ChamferType") or "")
inputs["size"] = _prop_value(obj, "Size")
inputs["size2"] = _prop_value(obj, "Size2")
inputs["angle"] = _prop_value(obj, "Angle")
return inputs
# --- Draft ---
if tid == "PartDesign::Draft":
inputs["angle"] = _prop_value(obj, "Angle")
inputs["reversed"] = _prop_raw(obj, "Reversed")
return inputs
# --- Thickness ---
if tid == "PartDesign::Thickness":
inputs["value"] = _prop_value(obj, "Value")
inputs["reversed"] = _prop_raw(obj, "Reversed")
inputs["mode"] = str(_prop_raw(obj, "Mode") or "")
inputs["join"] = str(_prop_raw(obj, "Join") or "")
return inputs
# --- Mirrored ---
if tid == "PartDesign::Mirrored":
inputs["mirror_plane"] = _link_name(obj, "MirrorPlane")
return inputs
# --- LinearPattern ---
if tid == "PartDesign::LinearPattern":
inputs["direction"] = _link_name(obj, "Direction")
inputs["reversed"] = _prop_raw(obj, "Reversed")
inputs["length"] = _prop_value(obj, "Length")
inputs["occurrences"] = _prop_value(obj, "Occurrences")
return inputs
# --- PolarPattern ---
if tid == "PartDesign::PolarPattern":
inputs["axis"] = _link_name(obj, "Axis")
inputs["reversed"] = _prop_raw(obj, "Reversed")
inputs["angle"] = _prop_value(obj, "Angle")
inputs["occurrences"] = _prop_value(obj, "Occurrences")
return inputs
# --- Scaled ---
if tid == "PartDesign::Scaled":
inputs["factor"] = _prop_value(obj, "Factor")
inputs["occurrences"] = _prop_value(obj, "Occurrences")
return inputs
# --- MultiTransform ---
if tid == "PartDesign::MultiTransform":
try:
inputs["transform_count"] = len(obj.Transformations)
except Exception:
pass
return inputs
# --- Boolean ---
if tid == "PartDesign::Boolean":
inputs["type"] = str(_prop_raw(obj, "Type") or "")
return inputs
# --- Primitives (additive) ---
if tid in (
"PartDesign::AdditiveBox",
"PartDesign::SubtractiveBox",
):
inputs["length"] = _prop_value(obj, "Length")
inputs["width"] = _prop_value(obj, "Width")
inputs["height"] = _prop_value(obj, "Height")
return inputs
if tid in (
"PartDesign::AdditiveCylinder",
"PartDesign::SubtractiveCylinder",
):
inputs["radius"] = _prop_value(obj, "Radius")
inputs["height"] = _prop_value(obj, "Height")
inputs["angle"] = _prop_value(obj, "Angle")
return inputs
if tid in (
"PartDesign::AdditiveSphere",
"PartDesign::SubtractiveSphere",
):
inputs["radius"] = _prop_value(obj, "Radius")
return inputs
if tid in (
"PartDesign::AdditiveCone",
"PartDesign::SubtractiveCone",
):
inputs["radius1"] = _prop_value(obj, "Radius1")
inputs["radius2"] = _prop_value(obj, "Radius2")
inputs["height"] = _prop_value(obj, "Height")
return inputs
if tid in (
"PartDesign::AdditiveEllipsoid",
"PartDesign::SubtractiveEllipsoid",
):
inputs["radius1"] = _prop_value(obj, "Radius1")
inputs["radius2"] = _prop_value(obj, "Radius2")
inputs["radius3"] = _prop_value(obj, "Radius3")
return inputs
if tid in (
"PartDesign::AdditiveTorus",
"PartDesign::SubtractiveTorus",
):
inputs["radius1"] = _prop_value(obj, "Radius1")
inputs["radius2"] = _prop_value(obj, "Radius2")
return inputs
if tid in (
"PartDesign::AdditivePrism",
"PartDesign::SubtractivePrism",
):
inputs["polygon"] = _prop_raw(obj, "Polygon")
inputs["circumradius"] = _prop_value(obj, "Circumradius")
inputs["height"] = _prop_value(obj, "Height")
return inputs
if tid in (
"PartDesign::AdditiveWedge",
"PartDesign::SubtractiveWedge",
):
for dim in (
"Xmin",
"Ymin",
"Zmin",
"X2min",
"Z2min",
"Xmax",
"Ymax",
"Zmax",
"X2max",
"Z2max",
):
inputs[dim.lower()] = _prop_value(obj, dim)
return inputs
# --- Datum / ShapeBinder ---
if tid in (
"PartDesign::Point",
"PartDesign::Line",
"PartDesign::Plane",
"PartDesign::CoordinateSystem",
"PartDesign::ShapeBinder",
"PartDesign::SubShapeBinder",
):
try:
p = obj.Placement
inputs["position"] = {
"x": _safe_float(p.Base.x),
"y": _safe_float(p.Base.y),
"z": _safe_float(p.Base.z),
}
inputs["rotation"] = {
"axis_x": _safe_float(p.Rotation.Axis.x),
"axis_y": _safe_float(p.Rotation.Axis.y),
"axis_z": _safe_float(p.Rotation.Axis.z),
"angle": _safe_float(p.Rotation.Angle),
}
except Exception:
pass
return inputs
# --- Body / Part (containers) ---
if tid in ("PartDesign::Body", "App::Part", "Part::Feature"):
try:
inputs["child_count"] = len(obj.Group)
except Exception:
inputs["child_count"] = 0
return inputs
# --- Fallback ---
inputs["label"] = obj.Label
return inputs
def compute_properties_hash(obj: Any) -> str:
"""Return a SHA-256 hex digest of *obj*'s parametric inputs.
The hash is used for memoization -- if a node's inputs haven't
changed since the last validation run, re-validation can be skipped.
"""
inputs = _collect_inputs(obj)
canonical = json.dumps(inputs, sort_keys=True, default=str)
return hashlib.sha256(canonical.encode()).hexdigest()
# ---------------------------------------------------------------------------
# DAG extraction
# ---------------------------------------------------------------------------
def extract_dag(
doc: Any,
) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""Walk a FreeCAD document and return ``(nodes, edges)``.
*nodes* is a list of dicts matching the Silo ``PUT /dag`` payload
schema. *edges* connects dependencies (source) to dependents
(target).
Only objects whose TypeId is recognized by :func:`classify_type`
are included. Edges are limited to pairs where **both** endpoints
are included, preventing dangling references to internal objects
such as ``App::Origin``.
"""
# Pass 1 -- identify included objects
included: Set[str] = set()
classified: Dict[str, str] = {} # obj.Name -> node_type
for obj in doc.Objects:
if not hasattr(obj, "TypeId"):
continue
node_type = classify_type(obj.TypeId)
if node_type is not None:
included.add(obj.Name)
classified[obj.Name] = node_type
# Pass 2 -- build nodes and edges
nodes: List[Dict[str, Any]] = []
edges: List[Dict[str, Any]] = []
seen_edges: Set[Tuple[str, str]] = set()
for obj in doc.Objects:
if obj.Name not in included:
continue
nodes.append(
{
"node_key": obj.Name,
"node_type": classified[obj.Name],
"properties_hash": compute_properties_hash(obj),
"metadata": {
"label": obj.Label,
"type_id": obj.TypeId,
},
}
)
# Walk dependencies: OutList contains objects this one depends on
try:
out_list = obj.OutList
except Exception:
continue
for dep in out_list:
if not hasattr(dep, "Name"):
continue
if dep.Name not in included:
continue
edge_key = (dep.Name, obj.Name)
if edge_key in seen_edges:
continue
seen_edges.add(edge_key)
edges.append(
{
"source_key": dep.Name,
"target_key": obj.Name,
"edge_type": "depends_on",
}
)
return nodes, edges

191
freecad/open_search.py Normal file
View File

@@ -0,0 +1,191 @@
"""Search-and-open widget for Kindred Create.
Provides :class:`OpenItemWidget`, a plain ``QWidget`` that can be
embedded in an MDI sub-window. Searches both the Silo database and
local CAD files, presenting results in a unified table. Emits
``item_selected`` when the user picks an item and ``cancelled`` when
the user clicks Cancel.
"""
import FreeCAD
from PySide import QtCore, QtWidgets
class OpenItemWidget(QtWidgets.QWidget):
"""Search-and-open widget for embedding in an MDI subwindow.
Parameters
----------
client : SiloClient
Authenticated Silo API client instance.
search_local_fn : callable
Function that accepts a search term string and returns an
iterable of dicts with keys ``part_number``, ``description``,
``path``, ``modified``.
parent : QWidget, optional
Parent widget.
Signals
-------
item_selected(dict)
Emitted when the user selects an item. The dict contains
keys: *part_number*, *description*, *item_type*, *source*
(``"database"``, ``"local"``, or ``"both"``), *modified*,
and *path* (str or ``None``).
cancelled()
Emitted when the user clicks Cancel.
"""
item_selected = QtCore.Signal(dict)
cancelled = QtCore.Signal()
def __init__(self, client, search_local_fn, parent=None):
super().__init__(parent)
self._client = client
self._search_local = search_local_fn
self._results_data = []
self.setMinimumWidth(700)
self.setMinimumHeight(500)
self._build_ui()
# Debounced search timer (500 ms)
self._search_timer = QtCore.QTimer(self)
self._search_timer.setSingleShot(True)
self._search_timer.setInterval(500)
self._search_timer.timeout.connect(self._do_search)
# Populate on first display
QtCore.QTimer.singleShot(0, self._do_search)
# ---- UI construction ---------------------------------------------------
def _build_ui(self):
layout = QtWidgets.QVBoxLayout(self)
layout.setSpacing(8)
# Search row
self._search_input = QtWidgets.QLineEdit()
self._search_input.setPlaceholderText("Search by part number or description...")
self._search_input.textChanged.connect(self._on_search_changed)
layout.addWidget(self._search_input)
# Filter checkboxes
filter_layout = QtWidgets.QHBoxLayout()
self._db_checkbox = QtWidgets.QCheckBox("Database")
self._db_checkbox.setChecked(True)
self._local_checkbox = QtWidgets.QCheckBox("Local Files")
self._local_checkbox.setChecked(True)
self._db_checkbox.toggled.connect(self._on_filter_changed)
self._local_checkbox.toggled.connect(self._on_filter_changed)
filter_layout.addWidget(self._db_checkbox)
filter_layout.addWidget(self._local_checkbox)
filter_layout.addStretch()
layout.addLayout(filter_layout)
# Results table
self._results_table = QtWidgets.QTableWidget()
self._results_table.setColumnCount(5)
self._results_table.setHorizontalHeaderLabels(
["Part Number", "Description", "Type", "Source", "Modified"]
)
self._results_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self._results_table.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self._results_table.horizontalHeader().setStretchLastSection(True)
self._results_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self._results_table.doubleClicked.connect(self._open_selected)
layout.addWidget(self._results_table, 1)
# Buttons
btn_layout = QtWidgets.QHBoxLayout()
btn_layout.addStretch()
open_btn = QtWidgets.QPushButton("Open")
open_btn.clicked.connect(self._open_selected)
cancel_btn = QtWidgets.QPushButton("Cancel")
cancel_btn.clicked.connect(self.cancelled.emit)
btn_layout.addWidget(open_btn)
btn_layout.addWidget(cancel_btn)
layout.addLayout(btn_layout)
# ---- Search logic ------------------------------------------------------
def _on_search_changed(self, _text):
"""Restart debounce timer on each keystroke."""
self._search_timer.start()
def _on_filter_changed(self, _checked):
"""Re-run search immediately when filter checkboxes change."""
self._do_search()
def _do_search(self):
"""Execute search against database and/or local files."""
search_term = self._search_input.text().strip()
self._results_data = []
if self._db_checkbox.isChecked():
try:
for item in self._client.list_items(search=search_term):
self._results_data.append(
{
"part_number": item.get("part_number", ""),
"description": item.get("description", ""),
"item_type": item.get("item_type", ""),
"source": "database",
"modified": item.get("updated_at", "")[:10]
if item.get("updated_at")
else "",
"path": None,
}
)
except Exception as e:
FreeCAD.Console.PrintWarning(f"DB search failed: {e}\n")
if self._local_checkbox.isChecked():
try:
for item in self._search_local(search_term):
existing = next(
(r for r in self._results_data if r["part_number"] == item["part_number"]),
None,
)
if existing:
existing["source"] = "both"
existing["path"] = item.get("path")
else:
self._results_data.append(
{
"part_number": item.get("part_number", ""),
"description": item.get("description", ""),
"item_type": "",
"source": "local",
"modified": item.get("modified", "")[:10]
if item.get("modified")
else "",
"path": item.get("path"),
}
)
except Exception as e:
FreeCAD.Console.PrintWarning(f"Local search failed: {e}\n")
self._populate_table()
def _populate_table(self):
"""Refresh the results table from ``_results_data``."""
self._results_table.setRowCount(len(self._results_data))
for row, data in enumerate(self._results_data):
self._results_table.setItem(row, 0, QtWidgets.QTableWidgetItem(data["part_number"]))
self._results_table.setItem(row, 1, QtWidgets.QTableWidgetItem(data["description"]))
self._results_table.setItem(row, 2, QtWidgets.QTableWidgetItem(data["item_type"]))
self._results_table.setItem(row, 3, QtWidgets.QTableWidgetItem(data["source"]))
self._results_table.setItem(row, 4, QtWidgets.QTableWidgetItem(data["modified"]))
self._results_table.resizeColumnsToContents()
# ---- Selection ---------------------------------------------------------
def _open_selected(self):
"""Emit ``item_selected`` with the data from the selected row."""
selected = self._results_table.selectedItems()
if not selected:
return
row = selected[0].row()
self.item_selected.emit(dict(self._results_data[row]))

156
freecad/runner.py Normal file
View File

@@ -0,0 +1,156 @@
"""Headless runner entry points for silorunner compute jobs.
These functions are invoked via ``create --console -e`` by the
silorunner binary. They must work without a display server.
Entry Points
------------
dag_extract(input_path, output_path)
Extract feature DAG and write JSON.
validate(input_path, output_path)
Rebuild all features and report pass/fail per node.
export(input_path, output_path, format='step')
Export geometry to STEP, IGES, STL, or OBJ.
"""
import json
import FreeCAD
def dag_extract(input_path, output_path):
"""Extract the feature DAG from a Create file.
Parameters
----------
input_path : str
Path to the ``.kc`` or ``.FCStd`` file.
output_path : str
Path to write the JSON output.
Output JSON::
{"nodes": [...], "edges": [...]}
"""
from dag import extract_dag
doc = FreeCAD.openDocument(input_path)
try:
nodes, edges = extract_dag(doc)
with open(output_path, "w") as f:
json.dump({"nodes": nodes, "edges": edges}, f)
FreeCAD.Console.PrintMessage(
f"DAG extracted: {len(nodes)} nodes, {len(edges)} edges -> {output_path}\n"
)
finally:
FreeCAD.closeDocument(doc.Name)
def validate(input_path, output_path):
"""Validate a Create file by rebuilding all features.
Parameters
----------
input_path : str
Path to the ``.kc`` or ``.FCStd`` file.
output_path : str
Path to write the JSON output.
Output JSON::
{
"valid": true/false,
"nodes": [
{"node_key": "Pad001", "state": "clean", "message": null, "properties_hash": "..."},
...
]
}
"""
from dag import classify_type, compute_properties_hash
doc = FreeCAD.openDocument(input_path)
try:
doc.recompute()
results = []
all_valid = True
for obj in doc.Objects:
if not hasattr(obj, "TypeId"):
continue
node_type = classify_type(obj.TypeId)
if node_type is None:
continue
state = "clean"
message = None
if hasattr(obj, "isValid") and not obj.isValid():
state = "failed"
message = f"Feature {obj.Label} failed to recompute"
all_valid = False
results.append(
{
"node_key": obj.Name,
"state": state,
"message": message,
"properties_hash": compute_properties_hash(obj),
}
)
with open(output_path, "w") as f:
json.dump({"valid": all_valid, "nodes": results}, f)
status = "PASS" if all_valid else "FAIL"
FreeCAD.Console.PrintMessage(
f"Validation {status}: {len(results)} nodes -> {output_path}\n"
)
finally:
FreeCAD.closeDocument(doc.Name)
def export(input_path, output_path, format="step"):
"""Export a Create file to an external geometry format.
Parameters
----------
input_path : str
Path to the ``.kc`` or ``.FCStd`` file.
output_path : str
Path to write the exported file.
format : str
One of ``step``, ``iges``, ``stl``, ``obj``.
"""
import Part
doc = FreeCAD.openDocument(input_path)
try:
shapes = [
obj.Shape for obj in doc.Objects if hasattr(obj, "Shape") and obj.Shape
]
if not shapes:
raise ValueError("No geometry found in document")
compound = Part.makeCompound(shapes)
format_lower = format.lower()
if format_lower == "step":
compound.exportStep(output_path)
elif format_lower == "iges":
compound.exportIges(output_path)
elif format_lower == "stl":
import Mesh
Mesh.export([compound], output_path)
elif format_lower == "obj":
import Mesh
Mesh.export([compound], output_path)
else:
raise ValueError(f"Unsupported format: {format}")
FreeCAD.Console.PrintMessage(
f"Exported {format_lower.upper()} -> {output_path}\n"
)
finally:
FreeCAD.closeDocument(doc.Name)

629
freecad/schema_form.py Normal file
View File

@@ -0,0 +1,629 @@
"""Schema-driven new-item form for Kindred Create.
Fetches schema data from the Silo REST API and builds a dynamic Qt form
that mirrors the React ``CreateItemPane`` — category picker, property
fields grouped by domain, live part number preview, and project tagging.
The primary widget is :class:`SchemaFormWidget` (a plain ``QWidget``)
which can be embedded in an MDI tab, dock panel, or wrapped in the
backward-compatible :class:`SchemaFormDialog` modal.
"""
import json
import urllib.error
import urllib.parse
import urllib.request
import FreeCAD
from PySide import QtCore, QtGui, QtWidgets
# ---------------------------------------------------------------------------
# Domain labels derived from the first character of category codes.
# ---------------------------------------------------------------------------
_DOMAIN_LABELS = {
"F": "Fasteners",
"C": "Fluid Fittings",
"R": "Motion Components",
"S": "Structural",
"E": "Electrical",
"M": "Mechanical",
"T": "Tooling",
"A": "Assemblies",
"P": "Purchased",
"X": "Custom Fabricated",
}
_ITEM_TYPES = ["part", "assembly", "consumable", "tool"]
_SOURCING_TYPES = ["manufactured", "purchased"]
# ---------------------------------------------------------------------------
# Collapsible group box
# ---------------------------------------------------------------------------
class _CollapsibleGroup(QtWidgets.QGroupBox):
"""A QGroupBox that can be collapsed by clicking its title."""
def __init__(self, title: str, parent=None, collapsed=False):
super().__init__(title, parent)
self.setCheckable(True)
self.setChecked(not collapsed)
self.toggled.connect(self._on_toggled)
self._content = QtWidgets.QWidget()
self._layout = QtWidgets.QFormLayout(self._content)
self._layout.setContentsMargins(8, 4, 8, 4)
self._layout.setSpacing(6)
outer = QtWidgets.QVBoxLayout(self)
outer.setContentsMargins(0, 4, 0, 0)
outer.addWidget(self._content)
if collapsed:
self._content.hide()
def form_layout(self) -> QtWidgets.QFormLayout:
return self._layout
def _on_toggled(self, checked: bool):
self._content.setVisible(checked)
# ---------------------------------------------------------------------------
# Category picker (domain combo → subcategory combo)
# ---------------------------------------------------------------------------
class _CategoryPicker(QtWidgets.QWidget):
"""Two chained combo boxes: domain group → subcategory within that group."""
category_changed = QtCore.Signal(str) # emits full code e.g. "F01"
def __init__(self, categories: dict, parent=None):
super().__init__(parent)
self._categories = categories # {code: description, ...}
# Group by first character
self._groups = {} # {prefix: [(code, desc), ...]}
for code, desc in sorted(categories.items()):
prefix = code[0] if code else "?"
self._groups.setdefault(prefix, []).append((code, desc))
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(8)
self._domain_combo = QtWidgets.QComboBox()
self._domain_combo.addItem("-- Select domain --", "")
for prefix in sorted(self._groups.keys()):
label = _DOMAIN_LABELS.get(prefix, prefix)
count = len(self._groups[prefix])
self._domain_combo.addItem(f"{prefix} \u2014 {label} ({count})", prefix)
self._domain_combo.currentIndexChanged.connect(self._on_domain_changed)
layout.addWidget(self._domain_combo, 1)
self._sub_combo = QtWidgets.QComboBox()
self._sub_combo.setEnabled(False)
self._sub_combo.currentIndexChanged.connect(self._on_sub_changed)
layout.addWidget(self._sub_combo, 1)
def selected_category(self) -> str:
return self._sub_combo.currentData() or ""
def _on_domain_changed(self, _index: int):
prefix = self._domain_combo.currentData()
self._sub_combo.clear()
if not prefix:
self._sub_combo.setEnabled(False)
self.category_changed.emit("")
return
self._sub_combo.setEnabled(True)
self._sub_combo.addItem("-- Select subcategory --", "")
for code, desc in self._groups.get(prefix, []):
self._sub_combo.addItem(f"{code} \u2014 {desc}", code)
def _on_sub_changed(self, _index: int):
code = self._sub_combo.currentData() or ""
self.category_changed.emit(code)
# ---------------------------------------------------------------------------
# Property field factory
# ---------------------------------------------------------------------------
def _make_field(prop_def: dict) -> QtWidgets.QWidget:
"""Create a Qt widget for a property definition.
``prop_def`` has keys: type, default, unit, description, required.
"""
ptype = prop_def.get("type", "string")
if ptype == "boolean":
cb = QtWidgets.QCheckBox()
default = prop_def.get("default")
if default is True:
cb.setChecked(True)
if prop_def.get("description"):
cb.setToolTip(prop_def["description"])
return cb
if ptype == "number":
container = QtWidgets.QWidget()
h = QtWidgets.QHBoxLayout(container)
h.setContentsMargins(0, 0, 0, 0)
h.setSpacing(4)
spin = QtWidgets.QDoubleSpinBox()
spin.setDecimals(4)
spin.setRange(-1e9, 1e9)
spin.setSpecialValueText("") # show empty when at minimum
default = prop_def.get("default")
if default is not None and default != "":
try:
spin.setValue(float(default))
except (ValueError, TypeError):
pass
else:
spin.clear()
if prop_def.get("description"):
spin.setToolTip(prop_def["description"])
h.addWidget(spin, 1)
unit = prop_def.get("unit", "")
if unit:
unit_label = QtWidgets.QLabel(unit)
unit_label.setFixedWidth(40)
h.addWidget(unit_label)
return container
# Default: string
le = QtWidgets.QLineEdit()
default = prop_def.get("default")
if default and isinstance(default, str):
le.setText(default)
if prop_def.get("description"):
le.setPlaceholderText(prop_def["description"])
le.setToolTip(prop_def["description"])
return le
def _read_field(widget: QtWidgets.QWidget, prop_def: dict):
"""Extract the value from a property widget, type-converted."""
ptype = prop_def.get("type", "string")
if ptype == "boolean":
return widget.isChecked()
if ptype == "number":
spin = widget.findChild(QtWidgets.QDoubleSpinBox)
if spin is None:
return None
text = spin.text().strip()
if not text:
return None
return spin.value()
# string
if isinstance(widget, QtWidgets.QLineEdit):
val = widget.text().strip()
return val if val else None
return None
# ---------------------------------------------------------------------------
# Embeddable form widget
# ---------------------------------------------------------------------------
class SchemaFormWidget(QtWidgets.QWidget):
"""Schema-driven new-item form widget.
A plain ``QWidget`` that can be embedded in an MDI subwindow, dock
panel, or dialog. Emits :pyqt:`item_created` on successful creation
and :pyqt:`cancelled` when the user clicks Cancel.
"""
item_created = QtCore.Signal(dict)
cancelled = QtCore.Signal()
def __init__(self, client, parent=None):
super().__init__(parent)
self._client = client
self._prop_widgets = {} # {key: (widget, prop_def)}
self._prop_groups = [] # list of _CollapsibleGroup to clear on category change
self._categories = {}
self._projects = []
self._load_schema_data()
self._build_ui()
# Part number preview debounce timer
self._pn_timer = QtCore.QTimer(self)
self._pn_timer.setSingleShot(True)
self._pn_timer.setInterval(500)
self._pn_timer.timeout.connect(self._update_pn_preview)
# -- data loading -------------------------------------------------------
def _load_schema_data(self):
"""Fetch categories and projects from the API."""
try:
schema = self._client.get_schema()
segments = schema.get("segments", [])
cat_segment = next((s for s in segments if s.get("name") == "category"), None)
if cat_segment and cat_segment.get("values"):
self._categories = cat_segment["values"]
except Exception as e:
FreeCAD.Console.PrintWarning(f"Schema form: failed to fetch schema: {e}\n")
try:
self._projects = self._client.get_projects() or []
except Exception:
self._projects = []
def _fetch_properties(self, category: str) -> dict:
"""Fetch merged property definitions for a category."""
from silo_commands import _get_api_url, _get_auth_headers, _get_ssl_context
api_url = _get_api_url().rstrip("/")
url = f"{api_url}/schemas/kindred-rd/properties?category={urllib.parse.quote(category)}"
req = urllib.request.Request(url, method="GET")
req.add_header("Accept", "application/json")
for k, v in _get_auth_headers().items():
req.add_header(k, v)
try:
resp = urllib.request.urlopen(req, context=_get_ssl_context(), timeout=5)
data = json.loads(resp.read().decode("utf-8"))
return data.get("properties", data)
except Exception as e:
FreeCAD.Console.PrintWarning(
f"Schema form: failed to fetch properties for {category}: {e}\n"
)
return {}
def _generate_pn_preview(self, category: str) -> str:
"""Call the server to preview the next part number."""
from silo_commands import _get_api_url, _get_auth_headers, _get_ssl_context
api_url = _get_api_url().rstrip("/")
url = f"{api_url}/generate-part-number"
payload = json.dumps({"schema": "kindred-rd", "category": category}).encode()
req = urllib.request.Request(url, data=payload, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("Accept", "application/json")
for k, v in _get_auth_headers().items():
req.add_header(k, v)
try:
resp = urllib.request.urlopen(req, context=_get_ssl_context(), timeout=5)
data = json.loads(resp.read().decode("utf-8"))
return data.get("part_number", "")
except Exception:
return ""
# -- UI construction ----------------------------------------------------
def _build_ui(self):
root = QtWidgets.QVBoxLayout(self)
root.setSpacing(8)
# Inline error label (hidden by default)
self._error_label = QtWidgets.QLabel()
self._error_label.setStyleSheet(
"background-color: #f38ba8; color: #1e1e2e; "
"padding: 8px; border-radius: 4px; font-weight: bold;"
)
self._error_label.setAlignment(QtCore.Qt.AlignCenter)
self._error_label.hide()
root.addWidget(self._error_label)
# Part number preview banner
self._pn_label = QtWidgets.QLabel("Part Number: \u2014")
self._pn_label.setStyleSheet("font-size: 16px; font-weight: bold; padding: 8px;")
self._pn_label.setAlignment(QtCore.Qt.AlignCenter)
root.addWidget(self._pn_label)
# Scroll area for form content
scroll = QtWidgets.QScrollArea()
scroll.setWidgetResizable(True)
scroll.setFrameShape(QtWidgets.QFrame.NoFrame)
scroll_content = QtWidgets.QWidget()
self._form_layout = QtWidgets.QVBoxLayout(scroll_content)
self._form_layout.setSpacing(8)
self._form_layout.setContentsMargins(8, 4, 8, 4)
scroll.setWidget(scroll_content)
root.addWidget(scroll, 1)
# --- Identity section ---
identity = _CollapsibleGroup("Identity")
fl = identity.form_layout()
self._type_combo = QtWidgets.QComboBox()
for t in _ITEM_TYPES:
self._type_combo.addItem(t.capitalize(), t)
fl.addRow("Type:", self._type_combo)
self._desc_edit = QtWidgets.QLineEdit()
self._desc_edit.setPlaceholderText("Item description")
fl.addRow("Description:", self._desc_edit)
self._cat_picker = _CategoryPicker(self._categories)
self._cat_picker.category_changed.connect(self._on_category_changed)
fl.addRow("Category:", self._cat_picker)
self._form_layout.addWidget(identity)
# --- Sourcing section ---
sourcing = _CollapsibleGroup("Sourcing", collapsed=True)
fl = sourcing.form_layout()
self._sourcing_combo = QtWidgets.QComboBox()
for s in _SOURCING_TYPES:
self._sourcing_combo.addItem(s.capitalize(), s)
fl.addRow("Sourcing:", self._sourcing_combo)
self._cost_spin = QtWidgets.QDoubleSpinBox()
self._cost_spin.setDecimals(2)
self._cost_spin.setRange(0, 1e9)
self._cost_spin.setPrefix("$ ")
self._cost_spin.setSpecialValueText("")
fl.addRow("Standard Cost:", self._cost_spin)
self._sourcing_url = QtWidgets.QLineEdit()
self._sourcing_url.setPlaceholderText("https://...")
fl.addRow("Sourcing URL:", self._sourcing_url)
self._form_layout.addWidget(sourcing)
# --- Details section ---
details = _CollapsibleGroup("Details", collapsed=True)
fl = details.form_layout()
self._long_desc = QtWidgets.QTextEdit()
self._long_desc.setMaximumHeight(80)
self._long_desc.setPlaceholderText("Detailed description...")
fl.addRow("Long Description:", self._long_desc)
# Project selection
self._project_list = QtWidgets.QListWidget()
self._project_list.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self._project_list.setMaximumHeight(100)
for proj in self._projects:
code = proj.get("code", "")
name = proj.get("name", "")
label = f"{code} \u2014 {name}" if name else code
item = QtWidgets.QListWidgetItem(label)
item.setData(QtCore.Qt.UserRole, code)
self._project_list.addItem(item)
if self._projects:
fl.addRow("Projects:", self._project_list)
self._form_layout.addWidget(details)
# --- Dynamic property groups (inserted here on category change) ---
self._prop_insert_index = self._form_layout.count()
# Spacer
self._form_layout.addStretch()
# --- Buttons ---
btn_layout = QtWidgets.QHBoxLayout()
btn_layout.addStretch()
self._cancel_btn = QtWidgets.QPushButton("Cancel")
self._cancel_btn.clicked.connect(self.cancelled.emit)
btn_layout.addWidget(self._cancel_btn)
self._create_btn = QtWidgets.QPushButton("Create")
self._create_btn.setEnabled(False)
self._create_btn.setDefault(True)
self._create_btn.clicked.connect(self._on_create)
btn_layout.addWidget(self._create_btn)
root.addLayout(btn_layout)
# -- category change ----------------------------------------------------
def _on_category_changed(self, category: str):
"""Rebuild property groups when category selection changes."""
self._create_btn.setEnabled(bool(category))
# Remove old property groups
for group in self._prop_groups:
self._form_layout.removeWidget(group)
group.deleteLater()
self._prop_groups.clear()
self._prop_widgets.clear()
if not category:
self._pn_label.setText("Part Number: \u2014")
return
# Trigger part number preview
self._pn_timer.start()
# Fetch properties
all_props = self._fetch_properties(category)
if not all_props:
return
# Separate into category-specific and common (default) properties.
# The server merges them but we can identify category-specific ones
# by checking what the schema defines for this category prefix.
prefix = category[0]
domain_label = _DOMAIN_LABELS.get(prefix, prefix)
# We fetch the raw schema to determine which keys are category-specific.
# For now, use a heuristic: keys that are NOT in the well-known defaults
# list are category-specific.
_KNOWN_DEFAULTS = {
"manufacturer",
"manufacturer_pn",
"supplier",
"supplier_pn",
"sourcing_link",
"standard_cost",
"lead_time_days",
"minimum_order_qty",
"lifecycle_status",
"rohs_compliant",
"country_of_origin",
"notes",
}
cat_specific = {}
common = {}
for key, pdef in sorted(all_props.items()):
if key in _KNOWN_DEFAULTS:
common[key] = pdef
else:
cat_specific[key] = pdef
insert_pos = self._prop_insert_index
# Category-specific properties group
if cat_specific:
group = _CollapsibleGroup(f"{domain_label} Properties")
fl = group.form_layout()
for key, pdef in cat_specific.items():
label = key.replace("_", " ").title()
widget = _make_field(pdef)
fl.addRow(f"{label}:", widget)
self._prop_widgets[key] = (widget, pdef)
self._form_layout.insertWidget(insert_pos, group)
self._prop_groups.append(group)
insert_pos += 1
# Common properties group (collapsed by default)
if common:
group = _CollapsibleGroup("Common Properties", collapsed=True)
fl = group.form_layout()
for key, pdef in common.items():
label = key.replace("_", " ").title()
widget = _make_field(pdef)
fl.addRow(f"{label}:", widget)
self._prop_widgets[key] = (widget, pdef)
self._form_layout.insertWidget(insert_pos, group)
self._prop_groups.append(group)
def _update_pn_preview(self):
"""Fetch and display the next part number preview."""
category = self._cat_picker.selected_category()
if not category:
self._pn_label.setText("Part Number: \u2014")
return
pn = self._generate_pn_preview(category)
if pn:
self._pn_label.setText(f"Part Number: {pn}")
self.setWindowTitle(f"New: {pn}")
else:
self._pn_label.setText(f"Part Number: {category}-????")
self.setWindowTitle(f"New: {category}-????")
# -- submission ---------------------------------------------------------
def _collect_form_data(self) -> dict:
"""Collect all form values into a dict suitable for create_item."""
category = self._cat_picker.selected_category()
description = self._desc_edit.text().strip()
item_type = self._type_combo.currentData()
sourcing_type = self._sourcing_combo.currentData()
cost_text = self._cost_spin.text().strip().lstrip("$ ")
standard_cost = float(cost_text) if cost_text else None
sourcing_link = self._sourcing_url.text().strip() or None
long_description = self._long_desc.toPlainText().strip() or None
# Projects
selected_projects = []
for item in self._project_list.selectedItems():
code = item.data(QtCore.Qt.UserRole)
if code:
selected_projects.append(code)
# Properties
properties = {}
for key, (widget, pdef) in self._prop_widgets.items():
val = _read_field(widget, pdef)
if val is not None and val != "":
properties[key] = val
return {
"category": category,
"description": description,
"item_type": item_type,
"sourcing_type": sourcing_type,
"standard_cost": standard_cost,
"sourcing_link": sourcing_link,
"long_description": long_description,
"projects": selected_projects if selected_projects else None,
"properties": properties if properties else None,
}
def _on_create(self):
"""Validate and submit the form."""
self._error_label.hide()
data = self._collect_form_data()
if not data["category"]:
self._error_label.setText("Category is required.")
self._error_label.show()
return
try:
result = self._client.create_item(
"kindred-rd",
data["category"],
data["description"],
projects=data["projects"],
)
result["_form_data"] = data
self.item_created.emit(result)
except Exception as e:
self._error_label.setText(f"Failed to create item: {e}")
self._error_label.show()
# ---------------------------------------------------------------------------
# Modal dialog wrapper (backward compatibility)
# ---------------------------------------------------------------------------
class SchemaFormDialog(QtWidgets.QDialog):
"""Modal dialog wrapper around :class:`SchemaFormWidget`.
Provides the same ``exec_and_create()`` API as the original
implementation for callers that still need blocking modal behavior.
"""
def __init__(self, client, parent=None):
super().__init__(parent)
self.setWindowTitle("New Item")
self.setMinimumSize(600, 500)
self.resize(680, 700)
self._result = None
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
self._form = SchemaFormWidget(client, parent=self)
self._form.item_created.connect(self._on_created)
self._form.cancelled.connect(self.reject)
layout.addWidget(self._form)
@property
def _desc_edit(self):
"""Expose description field for pre-fill by callers."""
return self._form._desc_edit
def _on_created(self, result):
self._result = result
self.accept()
def exec_and_create(self):
"""Show dialog and return the creation result, or None if cancelled."""
if self.exec_() == QtWidgets.QDialog.Accepted:
return self._result
return None

File diff suppressed because it is too large Load Diff

View File

@@ -399,7 +399,10 @@ class SiloOrigin:
return True
except Exception as e:
import traceback
FreeCAD.Console.PrintError(f"Silo save failed: {e}\n")
FreeCAD.Console.PrintError(traceback.format_exc())
return False
def saveDocumentAs(self, doc, newIdentity: str) -> bool: