Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Create a document and edit properties

Open a document, add a typed length and an enumeration, write a value through each, and see the error an invalid enumeration value raises.

Generated page. tools/userdocs.py writes it from tools/workflows/01_documents_and_properties.py. Edit the workflow, not this page; python3 tools/userdocs.py --check fails when the two disagree.

Requires: the FreeCAD facade; no workbench module and no GPU.

1. Create a document

newDocument registers the document with the application and makes it active. The registry lives in the Rust core, so the facade keeps no state of its own.

doc = FreeCAD.newDocument('Demo')
Read backValue
Active documentDemo
Open documents[“Demo”]

2. Add properties

A feature object is created by type name. Properties are added one at a time and carry a group and a description, which is what a property editor shows. App::PropertyLength is a quantity, so it accepts a unit string and formats a value back with a unit.

part = doc.addObject("App::FeaturePython", "Part")
part.addProperty("App::PropertyLength", "Length", "Base", "Overall length")
part.addProperty("App::PropertyEnumeration", "Finish", "Base", "Surface finish")
part.Finish = ["As machined", "Anodised"]
part.Length = "10 mm"
part.Finish = "Anodised"
Read backValue
part.Length10 mm
part.FinishAnodised
Property types[“Finish”, “Length”]

3. An invalid value is rejected

The enumeration is a closed set. Assigning a value outside it raises ValueError and leaves the property unchanged; a script can rely on that instead of validating the set itself.

try:
    part.Finish = "Bogus"
except ValueError as exc:
    rejected = str(exc)
Read backValue
Error“Bogus” is not part of the enumeration
part.Finish is unchangedAnodised

4. Close the document

Closing unregisters the document from the application.

FreeCAD.closeDocument('Demo')
Read backValue
Open documents[]

The values above are read from the live object, not from a fixture: this page is generated by running the script in tools/workflows/.