Compare commits
25 Commits
feat/runne
...
f67d9a0422
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f67d9a0422 | ||
| af98994a53 | |||
|
|
d266bfb653 | ||
| a92174e0b9 | |||
|
|
edbaf65923 | ||
| 80f8ec27a0 | |||
|
|
6b3e8b7518 | ||
| b3fe98c696 | |||
|
|
c537e2f08f | ||
| 29b1f32fd9 | |||
| dca6380199 | |||
| 27f0cc0f34 | |||
| a5eff534b5 | |||
|
|
1001424b16 | ||
| 7e3127498a | |||
| 82d8741059 | |||
|
|
3fe43710fa | ||
|
|
8cbd872e5c | ||
|
|
e31321ac95 | ||
|
|
dc64a66f0f | ||
|
|
3d38e4b4c3 | ||
| 0f407360ed | |||
| fa4f3145c6 | |||
| d3e27010d8 | |||
|
|
d7c6066030 |
@@ -45,6 +45,7 @@ class SiloWorkbench(FreeCADGui.Workbench):
|
||||
"Separator",
|
||||
"Silo_Info",
|
||||
"Silo_BOM",
|
||||
"Silo_Jobs",
|
||||
]
|
||||
self.appendToolbar("Silo Origin", self.silo_toolbar_commands, "Unavailable")
|
||||
|
||||
@@ -52,12 +53,14 @@ class SiloWorkbench(FreeCADGui.Workbench):
|
||||
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",
|
||||
]
|
||||
@@ -103,7 +106,9 @@ def _register_silo_overlay():
|
||||
return False
|
||||
|
||||
try:
|
||||
FreeCADGui.registerEditingOverlay(
|
||||
from kindred_sdk import register_overlay
|
||||
|
||||
register_overlay(
|
||||
"silo", # overlay id
|
||||
["Silo Origin"], # toolbar names to append
|
||||
_silo_overlay_match, # match function
|
||||
|
||||
BIN
freecad/__pycache__/silo_commands.cpython-313.pyc
Normal file
BIN
freecad/__pycache__/silo_commands.cpython-313.pyc
Normal file
Binary file not shown.
BIN
freecad/__pycache__/silo_origin.cpython-313.pyc
Normal file
BIN
freecad/__pycache__/silo_origin.cpython-313.pyc
Normal file
Binary file not shown.
BIN
freecad/__pycache__/silo_start.cpython-313.pyc
Normal file
BIN
freecad/__pycache__/silo_start.cpython-313.pyc
Normal file
Binary file not shown.
317
freecad/bom_sync.py
Normal file
317
freecad/bom_sync.py
Normal file
@@ -0,0 +1,317 @@
|
||||
"""BOM extraction engine for FreeCAD Assembly documents.
|
||||
|
||||
Extracts cross-document ``App::Link`` components from an Assembly,
|
||||
resolves Silo UUIDs to part numbers, diffs against the server BOM,
|
||||
and applies adds/quantity updates via individual API calls.
|
||||
|
||||
No GUI dependencies -- usable in both desktop and headless mode.
|
||||
|
||||
Public API
|
||||
----------
|
||||
extract_bom_entries(doc) -> List[BomEntry]
|
||||
resolve_entries(entries, client) -> (resolved, unresolved)
|
||||
diff_bom(local_entries, remote_entries) -> BomDiff
|
||||
apply_bom_diff(diff, parent_pn, client) -> BomResult
|
||||
sync_bom_after_upload(doc, part_number, client) -> Optional[BomResult]
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import FreeCAD
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data structures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@dataclass
|
||||
class BomEntry:
|
||||
"""A single BOM line extracted from an Assembly."""
|
||||
|
||||
silo_item_id: Optional[str] # UUID from SiloItemId property
|
||||
part_number: Optional[str] # resolved via get_item_by_uuid()
|
||||
label: str # FreeCAD Label of the linked object
|
||||
doc_path: str # FileName of the linked document
|
||||
quantity: int # summed from ElementCount + individual links
|
||||
consolidation_warning: bool = False # multiple individual links to same source
|
||||
|
||||
|
||||
@dataclass
|
||||
class BomDiff:
|
||||
"""Result of diffing local assembly BOM against server BOM."""
|
||||
|
||||
added: List[Dict[str, Any]] = field(default_factory=list)
|
||||
removed: List[Dict[str, Any]] = field(default_factory=list)
|
||||
quantity_changed: List[Dict[str, Any]] = field(default_factory=list)
|
||||
unchanged: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BomResult:
|
||||
"""Summary of a BOM sync operation."""
|
||||
|
||||
added_count: int = 0
|
||||
updated_count: int = 0
|
||||
unreferenced_count: int = 0
|
||||
unresolved_count: int = 0
|
||||
errors: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Assembly detection helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _find_assembly(doc):
|
||||
"""Return the first ``Assembly::AssemblyObject`` in *doc*, or ``None``."""
|
||||
for obj in doc.Objects:
|
||||
if obj.isDerivedFrom("Assembly::AssemblyObject"):
|
||||
return obj
|
||||
return None
|
||||
|
||||
|
||||
def _get_silo_item_id(doc):
|
||||
"""Read ``SiloItemId`` from the tracked object in *doc*.
|
||||
|
||||
Returns ``None`` if the document has no tracked object or no UUID.
|
||||
"""
|
||||
for obj in doc.Objects:
|
||||
if hasattr(obj, "SiloItemId") and obj.SiloItemId:
|
||||
return obj.SiloItemId
|
||||
return None
|
||||
|
||||
|
||||
def _link_count(link_obj) -> int:
|
||||
"""Return the instance count for an ``App::Link``.
|
||||
|
||||
``ElementCount > 0`` means link array; otherwise single link (qty 1).
|
||||
"""
|
||||
element_count = getattr(link_obj, "ElementCount", 0)
|
||||
return element_count if element_count > 0 else 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Extraction
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def extract_bom_entries(doc) -> List[BomEntry]:
|
||||
"""Walk the Assembly in *doc* and collect cross-document link entries.
|
||||
|
||||
Returns an empty list if the document has no Assembly or no
|
||||
cross-document links. Only first-level children are extracted;
|
||||
sub-assemblies commit their own BOMs separately.
|
||||
"""
|
||||
assembly = _find_assembly(doc)
|
||||
if assembly is None:
|
||||
return []
|
||||
|
||||
# Key by linked document path to merge duplicates.
|
||||
entries: Dict[str, BomEntry] = {}
|
||||
|
||||
for obj in assembly.Group:
|
||||
# Accept App::Link (single or array) and App::LinkElement.
|
||||
if not (
|
||||
obj.isDerivedFrom("App::Link") or obj.isDerivedFrom("App::LinkElement")
|
||||
):
|
||||
continue
|
||||
|
||||
linked = obj.getLinkedObject()
|
||||
if linked is None:
|
||||
continue
|
||||
|
||||
# Skip in-document links (construction / layout geometry).
|
||||
if linked.Document == doc:
|
||||
continue
|
||||
|
||||
linked_doc = linked.Document
|
||||
doc_path = linked_doc.FileName or linked_doc.Name
|
||||
|
||||
qty = _link_count(obj)
|
||||
|
||||
if doc_path in entries:
|
||||
entries[doc_path].quantity += qty
|
||||
entries[doc_path].consolidation_warning = True
|
||||
else:
|
||||
entries[doc_path] = BomEntry(
|
||||
silo_item_id=_get_silo_item_id(linked_doc),
|
||||
part_number=None,
|
||||
label=linked.Label,
|
||||
doc_path=doc_path,
|
||||
quantity=qty,
|
||||
)
|
||||
|
||||
return list(entries.values())
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Resolution
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def resolve_entries(
|
||||
entries: List[BomEntry], client
|
||||
) -> Tuple[List[BomEntry], List[BomEntry]]:
|
||||
"""Resolve ``SiloItemId`` UUIDs to part numbers via the API.
|
||||
|
||||
Returns ``(resolved, unresolved)``. Unresolved entries have no
|
||||
``SiloItemId`` or the UUID lookup failed.
|
||||
"""
|
||||
resolved: List[BomEntry] = []
|
||||
unresolved: List[BomEntry] = []
|
||||
|
||||
for entry in entries:
|
||||
if not entry.silo_item_id:
|
||||
unresolved.append(entry)
|
||||
continue
|
||||
|
||||
try:
|
||||
item = client.get_item_by_uuid(entry.silo_item_id)
|
||||
entry.part_number = item["part_number"]
|
||||
resolved.append(entry)
|
||||
except Exception:
|
||||
unresolved.append(entry)
|
||||
|
||||
return resolved, unresolved
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Diff
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def diff_bom(
|
||||
local_entries: List[BomEntry],
|
||||
remote_entries: List[Dict[str, Any]],
|
||||
) -> BomDiff:
|
||||
"""Diff local assembly BOM against server BOM.
|
||||
|
||||
*local_entries*: resolved ``BomEntry`` list.
|
||||
*remote_entries*: raw dicts from ``client.get_bom()`` with keys
|
||||
``child_part_number`` and ``quantity``.
|
||||
"""
|
||||
local_map = {e.part_number: e.quantity for e in local_entries}
|
||||
remote_map = {e["child_part_number"]: e.get("quantity", 1) for e in remote_entries}
|
||||
|
||||
diff = BomDiff()
|
||||
|
||||
for pn, qty in local_map.items():
|
||||
if pn not in remote_map:
|
||||
diff.added.append({"part_number": pn, "quantity": qty})
|
||||
elif remote_map[pn] != qty:
|
||||
diff.quantity_changed.append(
|
||||
{
|
||||
"part_number": pn,
|
||||
"local_quantity": qty,
|
||||
"remote_quantity": remote_map[pn],
|
||||
}
|
||||
)
|
||||
else:
|
||||
diff.unchanged.append({"part_number": pn, "quantity": qty})
|
||||
|
||||
for pn, qty in remote_map.items():
|
||||
if pn not in local_map:
|
||||
diff.removed.append({"part_number": pn, "quantity": qty})
|
||||
|
||||
return diff
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Apply
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def apply_bom_diff(diff: BomDiff, parent_pn: str, client) -> BomResult:
|
||||
"""Apply adds and quantity updates to the server BOM.
|
||||
|
||||
Uses individual CRUD calls (Phase 1 fallback). Phase 2 replaces
|
||||
this with a single ``POST /items/{pn}/bom/merge`` call.
|
||||
|
||||
Removed entries are NEVER deleted -- only warned about.
|
||||
Each call is individually wrapped so one failure does not block others.
|
||||
"""
|
||||
result = BomResult()
|
||||
|
||||
for entry in diff.added:
|
||||
try:
|
||||
client.add_bom_entry(
|
||||
parent_pn,
|
||||
entry["part_number"],
|
||||
quantity=entry["quantity"],
|
||||
rel_type="component",
|
||||
)
|
||||
result.added_count += 1
|
||||
except Exception as e:
|
||||
result.errors.append(f"add {entry['part_number']}: {e}")
|
||||
|
||||
for entry in diff.quantity_changed:
|
||||
try:
|
||||
client.update_bom_entry(
|
||||
parent_pn,
|
||||
entry["part_number"],
|
||||
quantity=entry["local_quantity"],
|
||||
)
|
||||
result.updated_count += 1
|
||||
except Exception as e:
|
||||
result.errors.append(f"update {entry['part_number']}: {e}")
|
||||
|
||||
result.unreferenced_count = len(diff.removed)
|
||||
if diff.removed:
|
||||
pns = ", ".join(e["part_number"] for e in diff.removed)
|
||||
FreeCAD.Console.PrintWarning(
|
||||
f"BOM sync: {result.unreferenced_count} server entries "
|
||||
f"not in assembly (not deleted): {pns}\n"
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Top-level orchestrator
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def sync_bom_after_upload(doc, part_number: str, client) -> Optional[BomResult]:
|
||||
"""Full BOM sync pipeline: extract, resolve, diff, apply.
|
||||
|
||||
Returns ``None`` if *doc* is not an assembly or has no cross-document
|
||||
links. Returns a ``BomResult`` with summary counts otherwise.
|
||||
"""
|
||||
entries = extract_bom_entries(doc)
|
||||
if not entries:
|
||||
return None
|
||||
|
||||
resolved, unresolved = resolve_entries(entries, client)
|
||||
|
||||
# Log consolidation warnings.
|
||||
for entry in entries:
|
||||
if entry.consolidation_warning:
|
||||
FreeCAD.Console.PrintWarning(
|
||||
f"BOM sync: {entry.label} ({entry.doc_path}) has multiple "
|
||||
f"individual links. Consider using a link array "
|
||||
f"(ElementCount) for cleaner assembly management.\n"
|
||||
)
|
||||
|
||||
# Log unresolved components.
|
||||
for entry in unresolved:
|
||||
FreeCAD.Console.PrintWarning(
|
||||
f"BOM sync: {entry.label} ({entry.doc_path}) has no Silo "
|
||||
f"part number -- excluded from BOM.\n"
|
||||
)
|
||||
|
||||
if not resolved:
|
||||
result = BomResult()
|
||||
result.unresolved_count = len(unresolved)
|
||||
return result
|
||||
|
||||
# Fetch current server BOM.
|
||||
try:
|
||||
remote = client.get_bom(part_number)
|
||||
except Exception:
|
||||
remote = []
|
||||
|
||||
diff = diff_bom(resolved, remote)
|
||||
result = apply_bom_diff(diff, part_number, client)
|
||||
result.unresolved_count = len(unresolved)
|
||||
return result
|
||||
@@ -12,4 +12,17 @@
|
||||
<subdirectory>./</subdirectory>
|
||||
</workbench>
|
||||
</content>
|
||||
|
||||
<!-- Kindred Create extensions -->
|
||||
<kindred>
|
||||
<min_create_version>0.1.0</min_create_version>
|
||||
<load_priority>60</load_priority>
|
||||
<pure_python>true</pure_python>
|
||||
<dependencies>
|
||||
<dependency>sdk</dependency>
|
||||
</dependencies>
|
||||
<contexts>
|
||||
<context id="*" action="overlay"/>
|
||||
</contexts>
|
||||
</kindred>
|
||||
</package>
|
||||
|
||||
@@ -10,9 +10,6 @@ backward-compatible :class:`SchemaFormDialog` modal.
|
||||
"""
|
||||
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
import FreeCAD
|
||||
from PySide import QtCore, QtGui, QtWidgets
|
||||
@@ -267,17 +264,8 @@ class SchemaFormWidget(QtWidgets.QWidget):
|
||||
|
||||
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"))
|
||||
data = self._client.get_property_schema(category=category)
|
||||
return data.get("properties", data)
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(
|
||||
@@ -287,19 +275,10 @@ class SchemaFormWidget(QtWidgets.QWidget):
|
||||
|
||||
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
|
||||
from silo_commands import _get_schema_name
|
||||
|
||||
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"))
|
||||
data = self._client.generate_part_number(_get_schema_name(), category)
|
||||
return data.get("part_number", "")
|
||||
except Exception:
|
||||
return ""
|
||||
@@ -574,8 +553,10 @@ class SchemaFormWidget(QtWidgets.QWidget):
|
||||
return
|
||||
|
||||
try:
|
||||
from silo_commands import _get_schema_name
|
||||
|
||||
result = self._client.create_item(
|
||||
"kindred-rd",
|
||||
_get_schema_name(),
|
||||
data["category"],
|
||||
data["description"],
|
||||
projects=data["projects"],
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -30,7 +30,7 @@ class SiloOrigin:
|
||||
Key behaviors:
|
||||
- Documents are always stored locally (hybrid local-remote model)
|
||||
- Database tracks metadata, part numbers, and revision history
|
||||
- MinIO stores revision snapshots for sync/backup
|
||||
- Server stores revision files for sync/backup
|
||||
- Identity is tracked by UUID (SiloItemId), displayed as part number
|
||||
"""
|
||||
|
||||
@@ -388,9 +388,7 @@ class SiloOrigin:
|
||||
|
||||
# Upload to Silo
|
||||
properties = collect_document_properties(doc)
|
||||
_client._upload_file(
|
||||
obj.SiloPartNumber, str(file_path), properties, comment=""
|
||||
)
|
||||
_client._upload_file(obj.SiloPartNumber, str(file_path), properties, comment="")
|
||||
|
||||
# Clear modified flag (Modified is on Gui.Document, not App.Document)
|
||||
gui_doc = FreeCADGui.getDocument(doc.Name)
|
||||
@@ -567,12 +565,9 @@ def register_silo_origin():
|
||||
This should be called during workbench initialization to make
|
||||
Silo available as a file origin.
|
||||
"""
|
||||
origin = get_silo_origin()
|
||||
try:
|
||||
FreeCADGui.addOrigin(origin)
|
||||
FreeCAD.Console.PrintLog("Registered Silo origin\n")
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(f"Could not register Silo origin: {e}\n")
|
||||
from kindred_sdk import register_origin
|
||||
|
||||
register_origin(get_silo_origin())
|
||||
|
||||
|
||||
def unregister_silo_origin():
|
||||
@@ -582,9 +577,7 @@ def unregister_silo_origin():
|
||||
"""
|
||||
global _silo_origin
|
||||
if _silo_origin:
|
||||
try:
|
||||
FreeCADGui.removeOrigin(_silo_origin)
|
||||
FreeCAD.Console.PrintLog("Unregistered Silo origin\n")
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(f"Could not unregister Silo origin: {e}\n")
|
||||
from kindred_sdk import unregister_origin
|
||||
|
||||
unregister_origin(_silo_origin)
|
||||
_silo_origin = None
|
||||
|
||||
@@ -19,23 +19,10 @@ from PySide import QtCore, QtGui, QtWidgets
|
||||
# ---------------------------------------------------------------------------
|
||||
# Catppuccin Mocha palette
|
||||
# ---------------------------------------------------------------------------
|
||||
_MOCHA = {
|
||||
"base": "#1e1e2e",
|
||||
"mantle": "#181825",
|
||||
"crust": "#11111b",
|
||||
"surface0": "#313244",
|
||||
"surface1": "#45475a",
|
||||
"surface2": "#585b70",
|
||||
"text": "#cdd6f4",
|
||||
"subtext0": "#a6adc8",
|
||||
"subtext1": "#bac2de",
|
||||
"blue": "#89b4fa",
|
||||
"green": "#a6e3a1",
|
||||
"red": "#f38ba8",
|
||||
"peach": "#fab387",
|
||||
"lavender": "#b4befe",
|
||||
"overlay0": "#6c7086",
|
||||
}
|
||||
# Catppuccin Mocha palette — sourced from kindred-addon-sdk
|
||||
from kindred_sdk.theme import get_theme_tokens
|
||||
|
||||
_MOCHA = get_theme_tokens()
|
||||
|
||||
_PREF_GROUP = "User parameter:BaseApp/Preferences/Mod/KindredSilo"
|
||||
|
||||
|
||||
Submodule silo-client updated: fb658c5a24...9d07de1bca
Reference in New Issue
Block a user