Files
create/mods/sdk/kindred_sdk/origin.py
forbes a6a6cefc16 feat(sdk): add status bar widget wrapper and origin query bindings (#356)
register_status_widget(): pure Python wrapper that adds a live widget
to the main window status bar with context menu discoverability.

Origin query bindings (kcsdk.list_origins, active_origin, get_origin,
set_active_origin): thin C++ forwarding to OriginManager with Python
wrappers using kcsdk-first routing.

IOriginProvider and IStatusBarProvider C++ interfaces dropped — existing
FileOrigin stack is already complete, and status bar widgets don't need
C++ lifecycle management.
2026-03-01 14:42:00 -06:00

146 lines
3.9 KiB
Python

"""FileOrigin registration and query wrappers.
Wraps ``FreeCADGui.addOrigin()`` / ``removeOrigin()`` with validation
and error handling. Addons implement the FileOrigin duck-typed
interface directly (see Silo's ``SiloOrigin`` for the full contract).
Query functions (``list_origins``, ``active_origin``, etc.) route
through the ``kcsdk`` C++ module when available, falling back to
``FreeCADGui.*`` for backwards compatibility.
"""
import FreeCAD
# Try to import the C++ SDK module; None if not yet built/installed.
try:
import kcsdk as _kcsdk
except ImportError:
_kcsdk = None
_REQUIRED_METHODS = ("id", "name", "type", "ownsDocument")
def _gui():
import FreeCADGui
return FreeCADGui
def register_origin(origin):
"""Register a FileOrigin with FreeCADGui.
*origin* must be a Python object implementing at least ``id()``,
``name()``, ``type()``, and ``ownsDocument(doc)`` methods.
"""
missing = [m for m in _REQUIRED_METHODS if not hasattr(origin, m)]
if missing:
raise TypeError(f"origin is missing required methods: {', '.join(missing)}")
try:
_gui().addOrigin(origin)
FreeCAD.Console.PrintLog(f"kindred_sdk: Registered origin '{origin.id()}'\n")
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: Failed to register origin: {e}\n")
def unregister_origin(origin):
"""Remove a previously registered FileOrigin."""
try:
_gui().removeOrigin(origin)
FreeCAD.Console.PrintLog(f"kindred_sdk: Unregistered origin '{origin.id()}'\n")
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: Failed to unregister origin: {e}\n")
def list_origins():
"""Return IDs of all registered origins.
Returns
-------
list[str]
Origin IDs (always includes ``"local"``).
"""
if _kcsdk is not None:
try:
return _kcsdk.list_origins()
except Exception:
pass
try:
return _gui().listOrigins()
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: list_origins failed: {e}\n")
return []
def active_origin():
"""Return info about the currently active origin.
Returns
-------
dict or None
Origin info dict with keys: ``id``, ``name``, ``nickname``,
``type``, ``tracksExternally``, ``requiresAuthentication``,
``supportsRevisions``, ``supportsBOM``, ``supportsPartNumbers``,
``connectionState``.
"""
if _kcsdk is not None:
try:
return _kcsdk.active_origin()
except Exception:
pass
try:
return _gui().activeOrigin()
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: active_origin failed: {e}\n")
return None
def set_active_origin(origin_id):
"""Set the active origin by ID.
Parameters
----------
origin_id : str
The origin ID to activate.
Returns
-------
bool
True if the origin was found and activated.
"""
if _kcsdk is not None:
try:
return _kcsdk.set_active_origin(origin_id)
except Exception:
pass
try:
return _gui().setActiveOrigin(origin_id)
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: set_active_origin failed: {e}\n")
return False
def get_origin(origin_id):
"""Get info about a specific origin by ID.
Parameters
----------
origin_id : str
The origin ID to look up.
Returns
-------
dict or None
Origin info dict, or None if not found.
"""
if _kcsdk is not None:
try:
return _kcsdk.get_origin(origin_id)
except Exception:
pass
try:
return _gui().getOrigin(origin_id)
except Exception as e:
FreeCAD.Console.PrintWarning(f"kindred_sdk: get_origin failed: {e}\n")
return None