small optimization

This commit is contained in:
Andrej730
2024-04-19 18:46:44 +05:00
parent 589b98053e
commit 9e79499532
3 changed files with 33 additions and 30 deletions
+13 -13
View File
@@ -1627,27 +1627,27 @@ class IfcImporter:
if self.ifc_import_settings.has_filter: if self.ifc_import_settings.has_filter:
rel_aggregates = set() rel_aggregates = set()
for element in self.elements: for element in self.elements:
if element.IsDecomposedBy: if decomposed_by := element.IsDecomposedBy:
rel_aggregates.add(element.IsDecomposedBy[0]) rel_aggregates.add(decomposed_by[0])
elif element.Decomposes: elif decomposes := element.Decomposes:
rel_aggregates.add(element.Decomposes[0]) rel_aggregates.add(decomposes[0])
elif getattr(element, "IsNestedBy", []): # IFC2X3 does not have IsNestedBy elif nested_by := getattr(element, "IsNestedBy", []): # IFC2X3 does not have IsNestedBy
if [e for e in element.IsNestedBy[0].RelatedObjects if not e.is_a("IfcPort")]: if next((e for e in nested_by[0].RelatedObjects if not e.is_a("IfcPort")), None):
rel_aggregates.add(element.IsNestedBy[0]) rel_aggregates.add(nested_by[0])
elif getattr(element, "Nests", []): elif nests := getattr(element, "Nests", []):
rel_aggregates.add(element.Nests[0]) rel_aggregates.add(nests[0])
else: else:
rel_aggregates = [ rel_aggregates = [
r r
for r in self.file.by_type("IfcRelAggregates") for r in self.file.by_type("IfcRelAggregates")
if r.RelatingObject.is_a("IfcElement") or r.RelatingObject.is_a("IfcElementType") if (relating_obj := r.RelatingObject).is_a("IfcElement") or relating_obj.is_a("IfcElementType")
] + [ ] + [
r r
for r in self.file.by_type("IfcRelNests") for r in self.file.by_type("IfcRelNests")
if ( if (
r.RelatingObject.is_a("IfcElement") (relating_obj := r.RelatingObject).is_a("IfcElement")
or r.RelatingObject.is_a("IfcElementType") or relating_obj.is_a("IfcElementType")
or (r.RelatingObject.is_a("IfcPositioningElement") and not r.RelatingObject.is_a("IfcGrid")) or (relating_obj.is_a("IfcPositioningElement") and not relating_obj.is_a("IfcGrid"))
) )
and [e for e in r.RelatedObjects if not e.is_a("IfcPort")] and [e for e in r.RelatedObjects if not e.is_a("IfcPort")]
] ]
@@ -121,10 +121,10 @@ def get_local_placement(placement: ifcopenshell.entity_instance) -> MatrixType:
""" """
if placement is None: if placement is None:
return np.eye(4) return np.eye(4)
if placement.PlacementRelTo is None: if (rel_to := placement.PlacementRelTo) is None:
parent = np.eye(4) parent = np.eye(4)
else: else:
parent = get_local_placement(placement.PlacementRelTo) parent = get_local_placement(rel_to)
return np.dot(parent, get_axis2placement(placement.RelativePlacement)) return np.dot(parent, get_axis2placement(placement.RelativePlacement))
@@ -181,14 +181,16 @@ def is_day_in_work_time(day, work_time):
is_day_in_work_time = True is_day_in_work_time = True
if isinstance(day, datetime.datetime): if isinstance(day, datetime.datetime):
day = datetime.date(day.year, day.month, day.day) day = datetime.date(day.year, day.month, day.day)
if work_time[4]: # 4 IfcWorktime Start
start = ifcopenshell.util.date.ifc2datetime(work_time[4]) if start := work_time[4]:
start = ifcopenshell.util.date.ifc2datetime(start)
if day > start: if day > start:
is_day_in_work_time = True is_day_in_work_time = True
else: else:
is_day_in_work_time = False is_day_in_work_time = False
if work_time[5]: # 5 IfcWorktime Finish
finish = ifcopenshell.util.date.ifc2datetime(work_time[5]) if finish := work_time[5]:
finish = ifcopenshell.util.date.ifc2datetime(finish)
if day < finish: if day < finish:
is_day_in_work_time = True is_day_in_work_time = True
else: else:
@@ -205,36 +207,39 @@ def is_work_time_applicable_to_day(work_time, day):
if isinstance(day, datetime.datetime): if isinstance(day, datetime.datetime):
day = datetime.date(day.year, day.month, day.day) day = datetime.date(day.year, day.month, day.day)
recurrence = work_time.RecurrencePattern recurrence = work_time.RecurrencePattern
if recurrence.RecurrenceType == "DAILY": recurrence_type: RECURRENCE_TYPE = recurrence.RecurrenceType
if recurrence_type == "DAILY":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return True return True
# 4 IfcWorktime Start
if not work_time[4]: if not work_time[4]:
return False return False
return False # TODO return False # TODO
elif recurrence.RecurrenceType == "WEEKLY": elif recurrence_type == "WEEKLY":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return (day.weekday() + 1) in recurrence.WeekdayComponent return (day.weekday() + 1) in recurrence.WeekdayComponent
# 4 IfcWorktime Start
if not work_time[4]: if not work_time[4]:
return False return False
return False # TODO return False # TODO
elif recurrence.RecurrenceType == "MONTHLY_BY_DAY_OF_MONTH": elif recurrence_type == "MONTHLY_BY_DAY_OF_MONTH":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return day.day in recurrence.DayComponent return day.day in recurrence.DayComponent
return False # TODO return False # TODO
elif recurrence.RecurrenceType == "MONTHLY_BY_POSITION": elif recurrence_type == "MONTHLY_BY_POSITION":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return (day.weekday() + 1) in recurrence.WeekdayComponent and floor( return (day.weekday() + 1) in recurrence.WeekdayComponent and floor(
day.day / 7 day.day / 7
) + 1 == recurrence["Position"] ) + 1 == recurrence["Position"]
return False # TODO return False # TODO
elif recurrence.RecurrenceType == "YEARLY_BY_DAY_OF_MONTH": elif recurrence_type == "YEARLY_BY_DAY_OF_MONTH":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return ( return (
day.month in recurrence.MonthComponent day.month in recurrence.MonthComponent
and day.day in recurrence.DayComponent and day.day in recurrence.DayComponent
) )
return False # TODO return False # TODO
elif recurrence.RecurrenceType == "YEARLY_BY_POSITION": elif recurrence_type == "YEARLY_BY_POSITION":
if not recurrence.Interval and not recurrence.Occurrences: if not recurrence.Interval and not recurrence.Occurrences:
return ( return (
day.month in recurrence.MonthComponent day.month in recurrence.MonthComponent
@@ -262,11 +267,9 @@ def get_nested_tasks(task):
def get_parent_task(task): def get_parent_task(task):
return ( nests = task.Nests
task.Nests[0].RelatingObject if nests and (obj := nests[0].RelatingObject).is_a("IfcTask"):
if task.Nests and task.Nests[0].RelatingObject.is_a("IfcTask") return obj
else None
)
def get_all_nested_tasks(task): def get_all_nested_tasks(task):