2022-01-19 12:18:33 +11:00
|
|
|
# IfcOpenShell - IFC toolkit and geometry engine
|
|
|
|
|
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
|
|
|
|
|
#
|
|
|
|
|
# This file is part of IfcOpenShell.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# IfcOpenShell is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2021-06-17 13:23:09 +10:00
|
|
|
import datetime
|
|
|
|
|
import ifcopenshell.util.date
|
|
|
|
|
import ifcopenshell.util.sequence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Usecase:
|
|
|
|
|
def __init__(self, file, **settings):
|
|
|
|
|
self.file = file
|
|
|
|
|
self.settings = {"task": None}
|
|
|
|
|
for key, value in settings.items():
|
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
|
self.calendar_cache = {}
|
2021-08-10 12:35:07 +10:00
|
|
|
self.cascade_task(self.settings["task"], is_first_task=True)
|
2021-06-17 13:23:09 +10:00
|
|
|
|
2022-07-19 12:39:54 +10:00
|
|
|
def cascade_task(self, task, is_first_task=False, task_sequence=None):
|
|
|
|
|
if task_sequence is None:
|
|
|
|
|
task_sequence = []
|
|
|
|
|
|
|
|
|
|
if task in task_sequence:
|
|
|
|
|
print("Warning! Recursive sequence is as follows:")
|
|
|
|
|
for i, debug_task in enumerate(task_sequence):
|
|
|
|
|
if i == 0:
|
|
|
|
|
print("Starting at", debug_task)
|
|
|
|
|
else:
|
|
|
|
|
print("... is a predecessor to ...", debug_task)
|
|
|
|
|
print("... which is cyclically a predecessor to ...", task)
|
2022-07-26 10:33:40 +10:00
|
|
|
raise RecursionError("Recursive tasks found. Could not cascade schedule.")
|
2022-07-19 12:39:54 +10:00
|
|
|
|
2021-06-17 13:23:09 +10:00
|
|
|
if not task.TaskTime:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
duration = (
|
|
|
|
|
ifcopenshell.util.date.ifc2datetime(task.TaskTime.ScheduleDuration)
|
|
|
|
|
if task.TaskTime.ScheduleDuration
|
|
|
|
|
else datetime.timedelta()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
finishes = []
|
|
|
|
|
starts = []
|
|
|
|
|
|
|
|
|
|
for rel in task.IsSuccessorFrom:
|
|
|
|
|
predecessor = rel.RelatingProcess
|
2022-03-25 15:53:05 +11:00
|
|
|
predecessor_duration = (
|
2022-09-09 02:25:20 +01:00
|
|
|
ifcopenshell.util.date.ifc2datetime(
|
|
|
|
|
predecessor.TaskTime.ScheduleDuration
|
|
|
|
|
)
|
2022-08-29 21:55:37 +01:00
|
|
|
if predecessor.TaskTime and predecessor.TaskTime.ScheduleDuration
|
2022-03-25 15:53:05 +11:00
|
|
|
else datetime.timedelta()
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
if rel.SequenceType == "FINISH_START":
|
|
|
|
|
finish = self.get_task_time_attribute(predecessor, "ScheduleFinish")
|
|
|
|
|
if not finish:
|
|
|
|
|
continue
|
2022-03-25 15:53:05 +11:00
|
|
|
days = 0 if predecessor_duration.days == 0 else 1
|
|
|
|
|
duration_type = "WORKTIME"
|
2021-06-17 13:23:09 +10:00
|
|
|
if rel.TimeLag:
|
2022-03-25 15:53:05 +11:00
|
|
|
days += self.get_lag_time_days(rel.TimeLag)
|
2021-06-17 13:23:09 +10:00
|
|
|
duration_type = rel.TimeLag.DurationType
|
2022-03-25 15:53:05 +11:00
|
|
|
if days:
|
|
|
|
|
starts.append(
|
|
|
|
|
datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
self.offset_date(
|
|
|
|
|
finish, days, duration_type, self.get_calendar(task)
|
|
|
|
|
),
|
|
|
|
|
datetime.time(9),
|
2022-03-25 15:53:05 +11:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
starts.append(
|
|
|
|
|
datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
self.offset_date(
|
|
|
|
|
finish,
|
|
|
|
|
days,
|
|
|
|
|
duration_type,
|
|
|
|
|
self.get_calendar(predecessor),
|
|
|
|
|
),
|
2022-03-25 15:53:05 +11:00
|
|
|
datetime.time(9),
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
else:
|
|
|
|
|
starts.append(finish)
|
|
|
|
|
elif rel.SequenceType == "START_START":
|
|
|
|
|
start = self.get_task_time_attribute(predecessor, "ScheduleStart")
|
|
|
|
|
if not start:
|
|
|
|
|
continue
|
|
|
|
|
if rel.TimeLag:
|
|
|
|
|
days = self.get_lag_time_days(rel.TimeLag)
|
|
|
|
|
duration_type = rel.TimeLag.DurationType
|
2022-09-09 02:25:20 +01:00
|
|
|
starts.append(
|
|
|
|
|
self.offset_date(
|
|
|
|
|
start, days, duration_type, self.get_calendar(task)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
starts.append(
|
|
|
|
|
self.offset_date(
|
|
|
|
|
start, days, duration_type, self.get_calendar(predecessor)
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
else:
|
|
|
|
|
starts.append(start)
|
|
|
|
|
elif rel.SequenceType == "FINISH_FINISH":
|
|
|
|
|
finish = self.get_task_time_attribute(predecessor, "ScheduleFinish")
|
|
|
|
|
if not finish:
|
|
|
|
|
continue
|
|
|
|
|
if rel.TimeLag:
|
|
|
|
|
days = self.get_lag_time_days(rel.TimeLag)
|
|
|
|
|
duration_type = rel.TimeLag.DurationType
|
2022-09-09 02:25:20 +01:00
|
|
|
finishes.append(
|
|
|
|
|
self.offset_date(
|
|
|
|
|
finish, days, duration_type, self.get_calendar(task)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
finishes.append(
|
|
|
|
|
self.offset_date(
|
|
|
|
|
finish, days, duration_type, self.get_calendar(predecessor)
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
else:
|
|
|
|
|
finishes.append(finish)
|
|
|
|
|
elif rel.SequenceType == "START_FINISH":
|
|
|
|
|
start = self.get_task_time_attribute(predecessor, "ScheduleStart")
|
|
|
|
|
if not start:
|
|
|
|
|
continue
|
2022-03-25 15:53:05 +11:00
|
|
|
days = -1
|
|
|
|
|
duration_type = "WORKTIME"
|
2021-06-17 13:23:09 +10:00
|
|
|
if rel.TimeLag:
|
2022-03-25 15:53:05 +11:00
|
|
|
days += self.get_lag_time_days(rel.TimeLag)
|
2021-06-17 13:23:09 +10:00
|
|
|
duration_type = rel.TimeLag.DurationType
|
2022-03-25 15:53:05 +11:00
|
|
|
if days or rel.TimeLag:
|
|
|
|
|
finishes.append(
|
|
|
|
|
datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
self.offset_date(
|
|
|
|
|
start, days, duration_type, self.get_calendar(task)
|
|
|
|
|
),
|
|
|
|
|
datetime.time(17),
|
2022-03-25 15:53:05 +11:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
finishes.append(
|
|
|
|
|
datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
self.offset_date(
|
|
|
|
|
start,
|
|
|
|
|
days,
|
|
|
|
|
duration_type,
|
|
|
|
|
self.get_calendar(predecessor),
|
|
|
|
|
),
|
2022-03-25 15:53:05 +11:00
|
|
|
datetime.time(17),
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
else:
|
|
|
|
|
finishes.append(start)
|
|
|
|
|
|
|
|
|
|
if starts and finishes:
|
|
|
|
|
start = max(starts)
|
|
|
|
|
finish = max(finishes)
|
2022-03-25 15:53:05 +11:00
|
|
|
potential_finish = ifcopenshell.util.sequence.get_start_or_finish_date(
|
2022-09-09 02:25:20 +01:00
|
|
|
start,
|
|
|
|
|
duration,
|
|
|
|
|
task.TaskTime.DurationType,
|
|
|
|
|
self.get_calendar(task),
|
|
|
|
|
date_type="FINISH",
|
2021-06-17 13:23:09 +10:00
|
|
|
)
|
|
|
|
|
if potential_finish > finish:
|
|
|
|
|
start_ifc = ifcopenshell.util.date.datetime2ifc(start, "IfcDateTime")
|
2021-08-10 12:35:07 +10:00
|
|
|
if task.TaskTime.ScheduleStart == start_ifc and not is_first_task:
|
2021-06-17 13:23:09 +10:00
|
|
|
return
|
|
|
|
|
task.TaskTime.ScheduleStart = start_ifc
|
2022-09-09 02:25:20 +01:00
|
|
|
task.TaskTime.ScheduleFinish = ifcopenshell.util.date.datetime2ifc(
|
|
|
|
|
potential_finish, "IfcDateTime"
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
else:
|
|
|
|
|
finish_ifc = ifcopenshell.util.date.datetime2ifc(finish, "IfcDateTime")
|
2021-08-10 12:35:07 +10:00
|
|
|
if task.TaskTime.ScheduleFinish == finish_ifc and not is_first_task:
|
2021-06-17 13:23:09 +10:00
|
|
|
return
|
|
|
|
|
task.TaskTime.ScheduleFinish = finish_ifc
|
|
|
|
|
task.TaskTime.ScheduleStart = ifcopenshell.util.date.datetime2ifc(
|
2022-03-25 15:53:05 +11:00
|
|
|
ifcopenshell.util.sequence.get_start_or_finish_date(
|
2022-09-09 02:25:20 +01:00
|
|
|
finish,
|
|
|
|
|
duration,
|
|
|
|
|
task.TaskTime.DurationType,
|
|
|
|
|
self.get_calendar(task),
|
|
|
|
|
date_type="START",
|
2021-06-17 13:23:09 +10:00
|
|
|
),
|
|
|
|
|
"IfcDateTime",
|
|
|
|
|
)
|
|
|
|
|
elif finishes:
|
|
|
|
|
finish = max(finishes)
|
|
|
|
|
finish_ifc = ifcopenshell.util.date.datetime2ifc(finish, "IfcDateTime")
|
2021-08-10 12:35:07 +10:00
|
|
|
if task.TaskTime.ScheduleFinish == finish_ifc and not is_first_task:
|
2021-06-17 13:23:09 +10:00
|
|
|
return
|
|
|
|
|
task.TaskTime.ScheduleFinish = finish_ifc
|
|
|
|
|
task.TaskTime.ScheduleStart = ifcopenshell.util.date.datetime2ifc(
|
2022-03-25 15:53:05 +11:00
|
|
|
ifcopenshell.util.sequence.get_start_or_finish_date(
|
2022-09-09 02:25:20 +01:00
|
|
|
finish,
|
|
|
|
|
duration,
|
|
|
|
|
task.TaskTime.DurationType,
|
|
|
|
|
self.get_calendar(task),
|
|
|
|
|
date_type="START",
|
2021-06-17 13:23:09 +10:00
|
|
|
),
|
|
|
|
|
"IfcDateTime",
|
|
|
|
|
)
|
|
|
|
|
elif starts:
|
|
|
|
|
start = max(starts)
|
|
|
|
|
start_ifc = ifcopenshell.util.date.datetime2ifc(start, "IfcDateTime")
|
2021-08-10 12:35:07 +10:00
|
|
|
if task.TaskTime.ScheduleStart == start_ifc and not is_first_task:
|
2021-06-17 13:23:09 +10:00
|
|
|
return
|
|
|
|
|
task.TaskTime.ScheduleStart = start_ifc
|
|
|
|
|
task.TaskTime.ScheduleFinish = ifcopenshell.util.date.datetime2ifc(
|
2022-03-25 15:53:05 +11:00
|
|
|
ifcopenshell.util.sequence.get_start_or_finish_date(
|
2022-09-09 02:25:20 +01:00
|
|
|
start,
|
|
|
|
|
duration,
|
|
|
|
|
task.TaskTime.DurationType,
|
|
|
|
|
self.get_calendar(task),
|
|
|
|
|
date_type="FINISH",
|
2021-06-17 13:23:09 +10:00
|
|
|
),
|
|
|
|
|
"IfcDateTime",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for rel in task.IsPredecessorTo:
|
2022-07-19 12:39:54 +10:00
|
|
|
self.cascade_task(rel.RelatedProcess, task_sequence=task_sequence + [task])
|
2021-06-17 13:23:09 +10:00
|
|
|
|
|
|
|
|
def get_lag_time_days(self, lag_time):
|
|
|
|
|
return ifcopenshell.util.date.ifc2datetime(lag_time.LagValue.wrappedValue).days
|
|
|
|
|
|
|
|
|
|
def get_calendar(self, task):
|
|
|
|
|
if task.id() not in self.calendar_cache:
|
2022-09-09 02:25:20 +01:00
|
|
|
self.calendar_cache[task.id()] = ifcopenshell.util.sequence.derive_calendar(
|
|
|
|
|
task
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
return self.calendar_cache[task.id()]
|
|
|
|
|
|
|
|
|
|
def offset_date(self, date, days, duration_type, calendar):
|
2022-09-09 02:25:20 +01:00
|
|
|
return ifcopenshell.util.sequence.offset_date(
|
|
|
|
|
date, datetime.timedelta(days=days), duration_type, calendar
|
|
|
|
|
)
|
2021-06-17 13:23:09 +10:00
|
|
|
|
|
|
|
|
def get_task_time_attribute(self, task, attribute):
|
|
|
|
|
if task.TaskTime:
|
|
|
|
|
value = getattr(task.TaskTime, attribute)
|
|
|
|
|
if value:
|
|
|
|
|
return ifcopenshell.util.date.ifc2datetime(value)
|