Skip to content

freecad_context

freecad.datamanager_wb.ports.freecad_context

FreeCAD runtime context abstraction.

This module provides small Protocol interfaces for the parts of the FreeCAD API used by this workbench, plus a FreeCadContext wrapper that can be injected into non-UI code for testing.

Use get_runtime_context() to obtain the real runtime bindings.

AppLike

Minimal Protocol for the FreeCAD application module (App).

ActiveDocument instance-attribute

Console instance-attribute

Qt instance-attribute

ConsoleLike

Minimal Protocol for FreeCAD's console output API (App.Console).

PrintMessage(text)

Print a message to the FreeCAD report view / console.

Source code in freecad/datamanager_wb/ports/freecad_context.py
def PrintMessage(self, text: str) -> None:
    """Print a message to the FreeCAD report view / console."""
    ...

DocumentLike

Minimal Protocol for the FreeCAD active document.

Objects instance-attribute

getObject(name)

Return a document object by name, or None if it does not exist.

Source code in freecad/datamanager_wb/ports/freecad_context.py
def getObject(self, name: str) -> object | None:
    """Return a document object by name, or None if it does not exist."""
    ...

FreeCadContext(app, gui=None) dataclass

Bundle of runtime bindings used by this workbench.

This wrapper allows code to be written against Protocols and enables unit tests to provide a fake context without importing FreeCAD.

app instance-attribute

gui = None class-attribute instance-attribute

GuiLike

Minimal Protocol for the FreeCAD GUI module (Gui).

QtLike

Minimal Protocol for FreeCAD's translation API (App.Qt).

translate(context, text)

Translate the given text in the provided translation context.

Source code in freecad/datamanager_wb/ports/freecad_context.py
def translate(self, context: str, text: str) -> str:
    """Translate the given text in the provided translation context."""
    ...

get_runtime_context()

Return a context wired to the real FreeCAD runtime modules.

Source code in freecad/datamanager_wb/ports/freecad_context.py
def get_runtime_context() -> FreeCadContext:
    """Return a context wired to the real FreeCAD runtime modules."""
    import FreeCAD as App

    try:
        import FreeCADGui as Gui
    except Exception:  # pylint: disable=broad-exception-caught
        Gui = None

    return FreeCadContext(app=App, gui=Gui)