You can now edit attributes of contexts and subcontexts. Fix bug where 2D contexts were not created with the right dimensionality.

This commit is contained in:
Dion Moult
2021-09-30 18:28:50 +10:00
parent 9753ed07a0
commit b86b8c1b5a
19 changed files with 573 additions and 62 deletions
@@ -2,30 +2,26 @@ class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"context": None,
"subcontext": None,
"context_type": None,
"parent": None,
"context_identifier": None,
"target_view": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
parent = [
c
for c in self.file.by_type("IfcGeometricRepresentationContext")
if c.ContextType == self.settings["context"]
]
if not parent:
self.create_origin()
if self.settings["context"] == "Plan":
if not self.settings["parent"]:
if self.settings["context_type"] == "Plan":
self.create_2d_origin()
context = self.file.createIfcGeometricRepresentationContext(None, "Plan", 2, 1.0e-05, self.origin)
else:
context = self.file.createIfcGeometricRepresentationContext(None, "Model", 3, 1.0e-05, self.origin)
self.create_3d_origin()
context = self.file.createIfcGeometricRepresentationContext(
None, self.settings["context_type"], 3, 1.0e-05, self.origin
)
if self.file.schema == "IFC2X3":
project = self.file.by_type("IfcProject")[0]
else:
project = self.file.by_type("IfcContext")[0]
project = self.file.by_type("IfcProject")[0]
if project.RepresentationContexts:
contexts = list(project.RepresentationContexts)
@@ -34,20 +30,25 @@ class Usecase:
contexts.append(context)
project.RepresentationContexts = contexts
return context
parent = parent[0]
return self.file.create_entity(
"IfcGeometricRepresentationSubContext",
**{
"ContextIdentifier": self.settings["subcontext"],
"ContextType": self.settings["context"],
"ParentContext": parent,
"ContextIdentifier": self.settings["context_identifier"],
"ContextType": self.settings["context_type"],
"ParentContext": self.settings["parent"],
"TargetView": self.settings["target_view"],
}
)
def create_origin(self):
def create_3d_origin(self):
self.origin = self.file.createIfcAxis2Placement3D(
self.file.createIfcCartesianPoint((0.0, 0.0, 0.0)),
self.file.createIfcDirection((0.0, 0.0, 1.0)),
self.file.createIfcDirection((1.0, 0.0, 0.0)),
)
def create_2d_origin(self):
self.origin = self.file.createIfcAxis2Placement2D(
self.file.createIfcCartesianPoint((0.0, 0.0, 0.0)),
self.file.createIfcDirection((1.0, 0.0, 0.0)),
)
@@ -0,0 +1,10 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"context": None, "attributes": {}}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["context"], name, value)
@@ -0,0 +1,59 @@
import test.bootstrap
import ifcopenshell.api
class TestAddContext(test.bootstrap.IFC4):
def test_adding_a_3d_context(self):
self.file.createIfcProject()
context = ifcopenshell.api.run("context.add_context", self.file, context_type="Model")
assert context.ContextType == "Model"
assert context.is_a() == "IfcGeometricRepresentationContext"
assert context.WorldCoordinateSystem.is_a() == "IfcAxis2Placement3D"
assert context.WorldCoordinateSystem.Location.Coordinates == (0, 0, 0)
assert context.WorldCoordinateSystem.Axis.DirectionRatios == (0, 0, 1)
assert context.WorldCoordinateSystem.RefDirection.DirectionRatios == (1, 0, 0)
assert context.CoordinateSpaceDimension == 3
def test_adding_a_2d_context(self):
self.file.createIfcProject()
context = ifcopenshell.api.run("context.add_context", self.file, context_type="Plan")
assert context.ContextType == "Plan"
assert context.is_a() == "IfcGeometricRepresentationContext"
assert context.WorldCoordinateSystem.is_a() == "IfcAxis2Placement2D"
assert context.WorldCoordinateSystem.Location.Coordinates == (0, 0, 0)
assert context.WorldCoordinateSystem.RefDirection.DirectionRatios == (1, 0, 0)
assert context.CoordinateSpaceDimension == 2
def test_defaulting_to_3d_with_an_unknown_context_type(self):
self.file.createIfcProject()
context = ifcopenshell.api.run("context.add_context", self.file)
assert context.ContextType is None
assert context.is_a() == "IfcGeometricRepresentationContext"
assert context.WorldCoordinateSystem.is_a() == "IfcAxis2Placement3D"
assert context.WorldCoordinateSystem.Location.Coordinates == (0, 0, 0)
assert context.WorldCoordinateSystem.Axis.DirectionRatios == (0, 0, 1)
assert context.WorldCoordinateSystem.RefDirection.DirectionRatios == (1, 0, 0)
assert context.CoordinateSpaceDimension == 3
def test_adding_a_subcontext(self):
self.test_adding_a_3d_context()
context = self.file.by_type("IfcGeometricRepresentationContext")[0]
subcontext = ifcopenshell.api.run("context.add_context", self.file, parent=context, target_view="NOTDEFINED")
assert subcontext.is_a("IfcGeometricRepresentationSubContext")
assert subcontext.TargetView == "NOTDEFINED"
def test_adding_a_subcontext_with_an_optional_identifier_specified(self):
self.test_adding_a_3d_context()
context = self.file.by_type("IfcGeometricRepresentationContext")[0]
subcontext = ifcopenshell.api.run(
"context.add_context", self.file, parent=context, target_view="NOTDEFINED", context_identifier="Body"
)
assert subcontext.is_a("IfcGeometricRepresentationSubContext")
assert subcontext.TargetView == "NOTDEFINED"
assert subcontext.ContextIdentifier == "Body"
def test_adding_two_contexts(self):
self.test_adding_a_3d_context()
self.test_adding_a_2d_context()
project = self.file.by_type("IfcProject")[0]
assert len(project.RepresentationContexts) == 2