This commit is contained in:
Andrej730
2024-05-06 17:48:25 +05:00
parent 54a3730c20
commit a9d785c28f
4 changed files with 47 additions and 27 deletions
+20 -12
View File
@@ -61,20 +61,20 @@ class IfcCsv:
def export( def export(
self, self,
ifc_file, ifc_file: ifcopenshell.file,
elements, elements: ifcopenshell.entity_instance,
attributes, attributes,
headers=None, headers=None,
output=None, output=None,
format=None, format=None,
should_preserve_existing=False, should_preserve_existing: bool = False,
include_global_id=True, include_global_id: bool = True,
delimiter=",", delimiter: str = ",",
null="-", null: str = "-",
empty="", empty: str = "",
bool_true="YES", bool_true: str = "YES",
bool_false="NO", bool_false: str = "NO",
concat=", ", concat: str = ", ",
sort=None, sort=None,
groups=None, groups=None,
summaries=None, summaries=None,
@@ -382,8 +382,16 @@ class IfcCsv:
return ["{}.{}".format(pset_qto_name, n) for n in results] return ["{}.{}".format(pset_qto_name, n) for n in results]
def Import( def Import(
self, ifc_file, table, attributes=None, delimiter=",", null="-", empty="", bool_true="YES", bool_false="NO" self,
): ifc_file: ifcopenshell.file,
table: str,
attributes: Optional[list[Union[str, None]]] = None,
delimiter: str = ",",
null: str = "-",
empty: str = "",
bool_true: str = "YES",
bool_false: str = "NO",
) -> None:
ext = table.split(".")[-1].lower() ext = table.split(".")[-1].lower()
if ext == "csv": if ext == "csv":
@@ -20,7 +20,7 @@ import ifcopenshell
import ifcopenshell.util.element import ifcopenshell.util.element
def remove_classification(file, classification=None) -> None: def remove_classification(file: ifcopenshell.entity_instance, classification: ifcopenshell.entity_instance) -> None:
"""Removes an IfcClassification from the project and all references """Removes an IfcClassification from the project and all references
The classification and all of its relationships, children references, The classification and all of its relationships, children references,
@@ -19,7 +19,7 @@
import ifcopenshell import ifcopenshell
def remove_context(file, context=None) -> None: def remove_context(file: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance) -> None:
"""Removes an IfcGeometricRepresentationContext """Removes an IfcGeometricRepresentationContext
Any representation geometry that is assigned to the context is also Any representation geometry that is assigned to the context is also
@@ -18,9 +18,17 @@
import ifcopenshell import ifcopenshell
import ifcopenshell.util.pset import ifcopenshell.util.pset
from typing import Optional, Any, Union
def edit_pset(file, pset=None, name=None, properties=None, pset_template=None, should_purge=False) -> None: def edit_pset(
file: ifcopenshell.entity_instance,
pset: ifcopenshell.entity_instance,
name: Optional[str] = None,
properties: Optional[dict[str, Any]] = None,
pset_template: Optional[ifcopenshell.entity_instance] = None,
should_purge: bool = False,
) -> None:
"""Edits a property set and its properties """Edits a property set and its properties
At its simplest usage, this may be used to edit the name of a property At its simplest usage, this may be used to edit the name of a property
@@ -68,7 +76,7 @@ def edit_pset(file, pset=None, name=None, properties=None, pset_template=None, s
:param pset_template: If a property set template is provided, this will :param pset_template: If a property set template is provided, this will
be used to determine data types. If no user-defined template is be used to determine data types. If no user-defined template is
provided, the built-in buildingSMART templates will be loaded. provided, the built-in buildingSMART templates will be loaded.
:type pset_template: ifcopenshell.entity_instance :type pset_template: ifcopenshell.entity_instance, optional
:param should_purge: If left as False, properties set to None will be :param should_purge: If left as False, properties set to None will be
left as None but not removed. If set to true, properties set to None left as None but not removed. If set to true, properties set to None
will actually be removed. will actually be removed.
@@ -158,18 +166,18 @@ def edit_pset(file, pset=None, name=None, properties=None, pset_template=None, s
class Usecase: class Usecase:
def execute(self): def execute(self) -> None:
self.update_pset_name() self.update_pset_name()
self.load_pset_template() self.load_pset_template()
existing_props = self.update_existing_properties() existing_props = self.update_existing_properties()
new_props = self.add_new_properties() new_props = self.add_new_properties()
self.assign_new_properties(existing_props + new_props) self.assign_new_properties(existing_props + new_props)
def update_pset_name(self): def update_pset_name(self) -> None:
if self.settings["name"]: if self.settings["name"]:
self.settings["pset"].Name = self.settings["name"] self.settings["pset"].Name = self.settings["name"]
def load_pset_template(self): def load_pset_template(self) -> None:
if self.settings["pset_template"]: if self.settings["pset_template"]:
self.pset_template = self.settings["pset_template"] self.pset_template = self.settings["pset_template"]
else: else:
@@ -177,13 +185,13 @@ class Usecase:
self.psetqto = ifcopenshell.util.pset.get_template(self.file.schema) self.psetqto = ifcopenshell.util.pset.get_template(self.file.schema)
self.pset_template = self.psetqto.get_by_name(self.settings["pset"].Name) self.pset_template = self.psetqto.get_by_name(self.settings["pset"].Name)
def _should_update_prop(self, prop) -> bool: def _should_update_prop(self, prop: ifcopenshell.entity_instance) -> bool:
""" """
Checks if the given property should be changed Checks if the given property should be changed
""" """
return prop.Name in self.settings["properties"] return prop.Name in self.settings["properties"]
def _try_purge(self, prop) -> bool: def _try_purge(self, prop: ifcopenshell.entity_instance) -> bool:
""" """
Tries to remove the property Tries to remove the property
if successful, returns True, otherwise False if successful, returns True, otherwise False
@@ -200,7 +208,7 @@ class Usecase:
# For example - IfcPropertyEnumeratedValue to # For example - IfcPropertyEnumeratedValue to
# IfcPropertySingleValue. Or maybe the user should # IfcPropertySingleValue. Or maybe the user should
# just delete the property first? - vulevukusej # just delete the property first? - vulevukusej
def update_existing_properties(self): def update_existing_properties(self) -> list[ifcopenshell.entity_instance]:
existing_props = [] existing_props = []
for prop in self.get_properties(): for prop in self.get_properties():
if not self._should_update_prop(prop): if not self._should_update_prop(prop):
@@ -222,7 +230,9 @@ class Usecase:
raise NotImplementedError(f"Updating '{prop.is_a()}' properties is not supported yet") raise NotImplementedError(f"Updating '{prop.is_a()}' properties is not supported yet")
return existing_props return existing_props
def update_existing_prop_enum(self, prop): def update_existing_prop_enum(
self, prop: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, None]:
""" """
NOTE: Assumes the prop exists NOTE: Assumes the prop exists
""" """
@@ -255,7 +265,9 @@ class Usecase:
del self.settings["properties"][prop.Name] del self.settings["properties"][prop.Name]
return prop return prop
def update_existing_prop_single_value(self, prop): def update_existing_prop_single_value(
self, prop: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, None]:
""" """
NOTE: Assumes the prop exists NOTE: Assumes the prop exists
""" """
@@ -278,7 +290,7 @@ class Usecase:
del self.settings["properties"][prop.Name] del self.settings["properties"][prop.Name]
return prop return prop
def add_new_properties(self): def add_new_properties(self) -> list[ifcopenshell.entity_instance]:
properties = [] properties = []
for name, value in self.settings["properties"].items(): for name, value in self.settings["properties"].items():
if value is None and self.settings["should_purge"]: if value is None and self.settings["should_purge"]:
@@ -358,13 +370,13 @@ class Usecase:
properties.append(self.file.create_entity("IfcPropertySingleValue", **args)) properties.append(self.file.create_entity("IfcPropertySingleValue", **args))
return properties return properties
def assign_new_properties(self, props): def assign_new_properties(self, props: ifcopenshell.entity_instance) -> None:
if hasattr(self.settings["pset"], "HasProperties"): if hasattr(self.settings["pset"], "HasProperties"):
self.settings["pset"].HasProperties = props self.settings["pset"].HasProperties = props
elif hasattr(self.settings["pset"], "Properties"): elif hasattr(self.settings["pset"], "Properties"):
self.settings["pset"].Properties = props self.settings["pset"].Properties = props
def get_properties(self): def get_properties(self) -> list[ifcopenshell.entity_instance]:
""" """
Returns list of existing properties Returns list of existing properties
""" """