Skip to content

Commit

Permalink
Wrapped ifcopenshell api and automated project modified flag
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Jun 6, 2023
1 parent 1156225 commit 2c5ebae
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
8 changes: 4 additions & 4 deletions ifc_geometry.py
Expand Up @@ -144,7 +144,7 @@ def set_attribute(ifcfile, element, prop, value):
"""Sets an attribute. Returns True if the attribute was changed"""

if value != getattr(element, prop):
ifcopenshell.api.run(
ifc_tools.api_run(
"attribute.edit_attributes",
ifcfile,
product=element,
Expand Down Expand Up @@ -219,7 +219,7 @@ def set_geom_property(obj, prop):
points = getattr(obj, prop)
if len(points) > len(elem_points):
for i in range(len(points) - len(elem_points)):
p = ifcopenshell.api.run(
p = ifc_tools.api_run(
"root.create_entity",
ifcfile,
ifc_class="IfcCartesianPoint",
Expand All @@ -232,14 +232,14 @@ def set_geom_property(obj, prop):
rest.append(elem_points.pop())
elem.Points = elem_points
for r in rest:
ifcopenshell.api.run(
ifc_tools.api_run(
"root.remove_product", ifcfile, product=r
)
if len(points) == len(elem_points):
for i in range(len(points)):
v = FreeCAD.Vector(points[i]).multiply(scaling)
coord = tuple(v)[:psize]
ifcopenshell.api.run(
ifc_tools.api_run(
"attribute.edit_attributes",
ifcfile,
product=elem_points[i],
Expand Down
15 changes: 2 additions & 13 deletions ifc_objects.py
Expand Up @@ -127,8 +127,6 @@ def edit_attribute(self, obj, attribute, value=None):
if elt:
result = ifc_tools.set_attribute(ifcfile, elt, attribute, value)
if result:
proj = ifc_tools.get_project(obj)
proj.Modified = True
if hasattr(result, "id") and (result.id() != obj.StepId):
obj.StepId = result.id()

Expand All @@ -141,8 +139,6 @@ def edit_geometry(self, obj, prop):
result = ifc_geometry.set_geom_property(obj, prop)
if result:
obj.touch()
proj = ifc_tools.get_project(obj)
proj.Modified = True

def set_schema(self, obj, schema):
"""Changes the schema of an IFC document"""
Expand All @@ -161,7 +157,6 @@ def set_schema(self, obj, schema):
return
ifcfile, migration_table = ifc_tools.migrate_schema(ifcfile, schema)
self.ifcfile = ifcfile
obj.Modified = True
for old_id, new_id in migration_table.items():
child = [
o
Expand All @@ -176,17 +171,11 @@ def edit_placement(self, obj):

import ifc_tools # lazy import

result = ifc_tools.set_placement(obj)
if result:
proj = ifc_tools.get_project(obj)
proj.Modified = True
ifc_tools.set_placement(obj)

def edit_pset(self, obj, prop):
"""Edits a Pset value"""

import ifc_tools # lazy import

result = ifc_tools.edit_pset(obj, prop)
if result:
proj = ifc_tools.get_project(obj)
proj.Modified = True
ifc_tools.edit_pset(obj, prop)
3 changes: 1 addition & 2 deletions ifc_observer.py
Expand Up @@ -93,5 +93,4 @@ def slotDeletedObject(self, obj):
return
if getattr(obj.Proxy, "nodelete", False):
return
if ifc_tools.remove_ifc_element(obj):
proj.Modified = True
ifc_tools.remove_ifc_element(obj)
56 changes: 30 additions & 26 deletions ifc_tools.py
Expand Up @@ -133,6 +133,19 @@ def create_ifcfile():
return ifcfile


def api_run(*args, **kwargs):
"""Runs an IfcOpenShell API call and flags the ifcfile as modified"""

ifcopenshell.api.run(*args, **kwargs)
# *args are command, ifcfile
ifcfile = args[1]
for d in FreeCAD.listDocuments().values():
for o in d.Objects:
if hasattr(o, "Proxy") and hasattr(o.Proxy, "ifcfile"):
if o.Proxy.ifcfile == ifcfile:
o.Modified = True


def create_object(ifcentity, document, ifcfile, shapemode=0):
"""Creates a FreeCAD object from an IFC entity"""

Expand Down Expand Up @@ -906,9 +919,7 @@ def set_attribute(ifcfile, element, attribute, value):
+ str(value)
+ "\n"
)
product = ifcopenshell.api.run(
cmd, ifcfile, product=element, ifc_class=value
)
product = api_run(cmd, ifcfile, product=element, ifc_class=value)
# TODO fix attributes
return product
cmd = "attribute.edit_attributes"
Expand All @@ -922,7 +933,7 @@ def set_attribute(ifcfile, element, attribute, value):
+ str(value)
+ "\n"
)
ifcopenshell.api.run(cmd, ifcfile, product=element, attributes=attribs)
api_run(cmd, ifcfile, product=element, attributes=attribs)
return True
return False

Expand Down Expand Up @@ -1044,9 +1055,7 @@ def set_placement(obj):
+ "\n"
)
api = "geometry.edit_object_placement"
ifcopenshell.api.run(
api, ifcfile, product=element, matrix=new_matrix, is_si=False
)
api_run(api, ifcfile, product=element, matrix=new_matrix, is_si=False)
return True
return False

Expand Down Expand Up @@ -1106,6 +1115,7 @@ def aggregate(obj, parent):
if new and delete:
obj.Document.removeObject(obj.Name)
newobj.Label = label # to avoid 001-ing the Label...
# TODO the line below should be done automatically when using the api to create products
proj.Modified = True
return newobj

Expand All @@ -1117,7 +1127,7 @@ def deaggregate(obj, parent):
element = get_ifc_element(obj)
if not element:
return
ifcopenshell.api.run("aggregate.unassign_object", ifcfile, product=element)
api_run("aggregate.unassign_object", ifcfile, product=element)
parent.Proxy.removeObject(parent, obj)


Expand Down Expand Up @@ -1226,8 +1236,8 @@ def create_relationship(old_obj, obj, parent, element, ifcfile):
parent_element = get_ifc_element(parent)
# case 1: element inside spatiual structure
if parent_element.is_a("IfcSpatialStructureElement") and element.is_a("IfcElement"):
ifcopenshell.api.run("spatial.unassign_container", ifcfile, product=element)
uprel = ifcopenshell.api.run(
api_run("spatial.unassign_container", ifcfile, product=element)
uprel = api_run(
"spatial.assign_container",
ifcfile,
product=element,
Expand All @@ -1245,34 +1255,32 @@ def create_relationship(old_obj, obj, parent, element, ifcfile):
old_obj.Document.removeObject(tempobj.Name)
if tempface:
old_obj.Document.removeObject(tempface.Name)
ifcopenshell.api.run(
api_run(
"void.add_opening", ifcfile, opening=opening, element=parent_element
)
ifcopenshell.api.run(
"void.add_filling", ifcfile, opening=opening, element=element
)
api_run("void.add_filling", ifcfile, opening=opening, element=element)
# windows must also be part of a spatial container
ifcopenshell.api.run("spatial.unassign_container", ifcfile, product=element)
api_run("spatial.unassign_container", ifcfile, product=element)
if parent_element.ContainedInStructure:
container = parent_element.ContainedInStructure[0].RelatingStructure
uprel = ifcopenshell.api.run(
uprel = api_run(
"spatial.assign_container",
ifcfile,
product=element,
relating_structure=container,
)
elif parent_element.Decomposes:
container = parent_element.Decomposes[0].RelatingObject
uprel = ifcopenshell.api.run(
uprel = api_run(
"aggregate.assign_object",
ifcfile,
product=element,
relating_object=container,
)
# case 3: element aggregated inside other element
else:
ifcopenshell.api.run("aggregate.unassign_object", ifcfile, product=element)
uprel = ifcopenshell.api.run(
api_run("aggregate.unassign_object", ifcfile, product=element)
uprel = api_run(
"aggregate.assign_object",
ifcfile,
product=element,
Expand Down Expand Up @@ -1343,7 +1351,7 @@ def remove_ifc_element(obj):
ifcfile = get_ifcfile(obj)
element = get_ifc_element(obj)
if ifcfile and element:
ifcopenshell.api.run("root.remove_product", ifcfile, product=element)
api_run("root.remove_product", ifcfile, product=element)
return True
return False

Expand Down Expand Up @@ -1556,14 +1564,10 @@ def edit_pset(obj, prop, value=None):
)
pset = get_pset(pset, element)
else:
pset = ifcopenshell.api.run(
"pset.add_pset", ifcfile, product=element, name=pset
)
pset = api_run("pset.add_pset", ifcfile, product=element, name=pset)
if not target_prop:
target_prop = prop
ifcopenshell.api.run(
"pset.edit_pset", ifcfile, pset=pset, properties={target_prop: value}
)
api_run("pset.edit_pset", ifcfile, pset=pset, properties={target_prop: value})
# TODO manage quantities
return True

Expand Down
2 changes: 0 additions & 2 deletions ifc_viewproviders.py
Expand Up @@ -263,8 +263,6 @@ def dropObject(self, vobj, incoming_object):
ifc_tools.aggregate(incoming_object, parent)
if self.hasChildren(parent):
self.expandChildren(parent)
proj = ifc_tools.get_project(parent)
proj.Modified = True


class ifc_vp_document(ifc_vp_object):
Expand Down

0 comments on commit 2c5ebae

Please sign in to comment.