Fix bug where adding objects relying on missing contexts would fail

This commit is contained in:
Dion Moult
2025-04-01 14:20:56 +11:00
parent f0b5e93737
commit b6115a6d71
3 changed files with 56 additions and 3 deletions
+5 -2
View File
@@ -456,11 +456,14 @@ class Drawing(bonsai.core.tool.Drawing):
"PLAN_LEVEL",
):
parent = ifcopenshell.util.representation.get_context(tool.Ifc.get(), "Plan")
if not parent:
parent = ifcopenshell.api.context.add_context(tool.Ifc.get(), context_type="Plan")
else:
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.run(
"context.add_context",
return ifcopenshell.api.context.add_context(
tool.Ifc.get(),
context_type=parent.ContextType,
context_identifier="Annotation",
@@ -134,3 +134,34 @@ Scenario: Remove drawing - deleting active drawing
When the object "IfcAnnotation/PLAN_VIEW" is selected
And I delete the selected objects
Then the collection "IfcAnnotation/PLAN_VIEW" does not exist
Scenario: Add annotation - text
Given an empty IFC project
And I add a cube
And the object "Cube" is selected
And I save sample test files
And I look at the "Drawings" panel
And I click "IMPORT"
And I click "ADD"
And I press "bim.expand_target_view(target_view='PLAN_VIEW')"
And I select the "PLAN_VIEW" item in the "BIM_UL_drawinglist" list
And I click "OUTLINER_OB_CAMERA"
When I press "bim.add_annotation"
Then the object "IfcAnnotation/TEXT" is selected
Scenario: Add annotation - auto create context if it doesn't exist
Given an empty IFC project
And I add a cube
And the object "Cube" is selected
And I save sample test files
And I look at the "Geometric Representation Contexts" panel
And I see "Plan"
And I click the "X" after the text "Plan"
And I look at the "Drawings" panel
And I click "IMPORT"
And I click "ADD"
And I press "bim.expand_target_view(target_view='PLAN_VIEW')"
And I select the "PLAN_VIEW" item in the "BIM_UL_drawinglist" list
And I click "OUTLINER_OB_CAMERA"
When I press "bim.add_annotation"
Then the object "IfcAnnotation/TEXT" is selected
+20 -1
View File
@@ -134,7 +134,10 @@ class PanelSpy:
icon = kwargs.get("icon", None)
if text:
self.spied_labels.append(text)
spied_operator = {"operator": operator, "icon": icon, "text": text, "kwargs": {}}
after = ""
if self.spied_labels:
after = self.spied_labels[-1]
spied_operator = {"operator": operator, "icon": icon, "text": text, "kwargs": {}, "after": after}
self.spied_operators.append(spied_operator)
return OperatorSpy(spied_operator)
else:
@@ -570,6 +573,22 @@ def i_click_button(button):
assert False, f"Could not find {button}:\n{debug}"
@given(parsers.parse('I click the "{button}" after the text "{text}"'))
@when(parsers.parse('I click the "{button}" after the text "{text}"'))
@then(parsers.parse('I click the "{button}" after the text "{text}"'))
def i_click_the_button_after_the_text_text(button, text):
panel_spy.refresh_spy()
for spied_operator in panel_spy.spied_operators:
if spied_operator["after"] == text:
if spied_operator["text"] == button or spied_operator["icon"] == button:
spied_operator["operator"]("INVOKE_DEFAULT", **spied_operator["kwargs"])
panel_spy.is_spy_dirty = True
return
debug = "\n".join([f"{i} {v}" for i, v in enumerate(panel_spy.spied_operators)])
debug += f"\nHere is the text we see: {panel_spy.spied_labels}"
assert False, f"Could not find {button}:\n{debug}"
@given(parsers.parse('I click "{button}" and expect error "{error_msg}"'))
@when(parsers.parse('I click "{button}" and expect error "{error_msg}"'))
def i_click_button_and_expect_error_error_msg(button, error_msg):