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.pywrites it fromtools/workflows/01_documents_and_properties.py. Edit the workflow, not this page;python3 tools/userdocs.py --checkfails 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 back | Value |
|---|---|
| Active document | Demo |
| 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 back | Value |
|---|---|
| part.Length | 10 mm |
| part.Finish | Anodised |
| 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 back | Value |
|---|---|
| Error | “Bogus” is not part of the enumeration |
| part.Finish is unchanged | Anodised |
4. Close the document
Closing unregisters the document from the application.
FreeCAD.closeDocument('Demo')
| Read back | Value |
|---|---|
| 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/.