From d71b9c9c8cf667bf4b65e487c9e4505dfcdae4bf Mon Sep 17 00:00:00 2001 From: Ryan Schultz Date: Sat, 28 Mar 2026 09:43:51 -0500 Subject: [PATCH] Auto-create body context when adding drawing If the Model/Body/MODEL_VIEW context does not exist in the IFC file, get_body_context now creates it (and the Model parent if also missing) rather than returning None and crashing with an AssertionError. Generated with the assistance of an AI coding tool. --- src/bonsai/bonsai/tool/drawing.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/bonsai/bonsai/tool/drawing.py b/src/bonsai/bonsai/tool/drawing.py index 86113b9c95..eb51abfbd7 100644 --- a/src/bonsai/bonsai/tool/drawing.py +++ b/src/bonsai/bonsai/tool/drawing.py @@ -670,7 +670,19 @@ class Drawing(bonsai.core.tool.Drawing): @classmethod def get_body_context(cls) -> ifcopenshell.entity_instance: - return ifcopenshell.util.representation.get_context(tool.Ifc.get(), "Model", "Body", "MODEL_VIEW") + context = ifcopenshell.util.representation.get_context(tool.Ifc.get(), "Model", "Body", "MODEL_VIEW") + if context: + return context + parent = ifcopenshell.util.representation.get_context(tool.Ifc.get(), "Model") + if not parent: + parent = ifcopenshell.api.context.add_context(tool.Ifc.get(), context_type="Model") + return ifcopenshell.api.context.add_context( + tool.Ifc.get(), + context_type="Model", + context_identifier="Body", + target_view="MODEL_VIEW", + parent=parent, + ) @classmethod def get_document_uri(