OpenSite/Guides/Native property formulas

From BathAfrica Documentation
Jump to navigation Jump to search
OpenSite 0.0.1 · DevelopmentSource scope and availability


Make one property depend on another with OpenSite’s restricted formula language.

Stable identifier
opensite.help.native-property-formulas

Prerequisites

Embedded Python with the development host for the rectangular-section fixture. No Python-expression consent is needed for a native formula.

Steps

Create the fixture

Start with a width of 250 mm and height of 500 mm.

Store a formula instead of a literal

Set height to object(id).width * 2 with language="formula". The object ID must be the actual ID returned by this document.

Change the source value

Set width to 300 mm. The native dependency system recomputes height as 600 mm.

Inspect the stored source

Read doc.expression(id, "height"). A label is a display name; use the document’s stable references rather than inventing an ID.

Expected result

Height changes from 0.50 m to 0.60 m after editing width. The formula source remains stored on height.

Limitations

The formula language is not arbitrary Python. A dimension mismatch or dependency cycle is rejected. Writing a literal to an expression-bound property replaces that expression.

Complete example

import opensite

created = opensite.execute("document.create_section", {
    "label": "FormulaBeam",
    "width": opensite.QuantityArgument(opensite.Q("250 mm"), "mm"),
    "height": opensite.QuantityArgument(opensite.Q("500 mm"), "mm"),
})
doc, object_id = created["document"], created["object"]
opensite.execute("document.set_expression", {
    "document": doc, "object": object_id, "property": "height",
    "expression": f"object({object_id}).width * 2", "language": "formula",
})
beam = doc.get_object(object_id)
assert abs(beam.height.si_value - 0.50) < 1e-12
beam.set_property("width", opensite.Q("300 mm"))
assert abs(beam.height.si_value - 0.60) < 1e-12
print("Formula:", doc.expression(object_id, "height"))
print("Height:", beam.height.value_in("mm"), "mm")