Modifications to scheduled dates can now cascade through the task tree

This commit is contained in:
Dion Moult
2021-06-17 13:23:09 +10:00
parent 97d0286b57
commit 50608d9459
5 changed files with 192 additions and 17 deletions
@@ -565,12 +565,15 @@ class AssignPredecessor(bpy.types.Operator):
def execute(self, context):
props = context.scene.BIMWorkScheduleProperties
self.file = IfcStore.get_file()
ifcopenshell.api.run(
rel = ifcopenshell.api.run(
"sequence.assign_sequence",
self.file,
relating_process=IfcStore.get_file().by_id(self.task),
related_process=IfcStore.get_file().by_id(props.active_task_id),
)
ifcopenshell.api.run(
"sequence.edit_sequence", self.file, rel_sequence=rel, attributes={"SequenceType": "FINISH_START"}
)
Data.load(self.file)
bpy.ops.bim.load_task_properties(task=self.task)
return {"FINISHED"}
@@ -584,12 +587,15 @@ class AssignSuccessor(bpy.types.Operator):
def execute(self, context):
props = context.scene.BIMWorkScheduleProperties
self.file = IfcStore.get_file()
ifcopenshell.api.run(
rel = ifcopenshell.api.run(
"sequence.assign_sequence",
self.file,
relating_process=IfcStore.get_file().by_id(props.active_task_id),
related_process=IfcStore.get_file().by_id(self.task),
)
ifcopenshell.api.run(
"sequence.edit_sequence", self.file, rel_sequence=rel, attributes={"SequenceType": "FINISH_START"}
)
Data.load(self.file)
bpy.ops.bim.load_task_properties(task=self.task)
return {"FINISHED"}
@@ -0,0 +1,162 @@
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 = {}
self.cascade_task(self.settings["task"])
def cascade_task(self, task):
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
if rel.SequenceType == "FINISH_START":
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
starts.append(self.offset_date(finish, days, duration_type, self.get_calendar(task)))
starts.append(self.offset_date(finish, days, duration_type, self.get_calendar(predecessor)))
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
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)))
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
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)))
else:
finishes.append(finish)
elif rel.SequenceType == "START_FINISH":
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
finishes.append(self.offset_date(start, days, duration_type, self.get_calendar(task)))
finishes.append(self.offset_date(start, days, duration_type, self.get_calendar(predecessor)))
else:
finishes.append(start)
else:
return
if starts and finishes:
start = max(starts)
finish = max(finishes)
potential_finish = ifcopenshell.util.sequence.get_finish_date(
start,
duration,
task.TaskTime.DurationType,
self.get_calendar(task),
)
if potential_finish > finish:
start_ifc = ifcopenshell.util.date.datetime2ifc(start, "IfcDateTime")
if task.TaskTime.ScheduleStart == start_ifc:
return
task.TaskTime.ScheduleStart = start_ifc
task.TaskTime.ScheduleFinish = ifcopenshell.util.date.datetime2ifc(potential_finish, "IfcDateTime")
else:
finish_ifc = ifcopenshell.util.date.datetime2ifc(finish, "IfcDateTime")
if task.TaskTime.ScheduleFinish == finish_ifc:
return
task.TaskTime.ScheduleFinish = finish_ifc
task.TaskTime.ScheduleStart = ifcopenshell.util.date.datetime2ifc(
ifcopenshell.util.sequence.get_finish_date(
finish,
-duration,
task.TaskTime.DurationType,
self.get_calendar(task),
),
"IfcDateTime",
)
elif finishes:
finish = max(finishes)
finish_ifc = ifcopenshell.util.date.datetime2ifc(finish, "IfcDateTime")
if task.TaskTime.ScheduleFinish == finish_ifc:
return
task.TaskTime.ScheduleFinish = finish_ifc
task.TaskTime.ScheduleStart = ifcopenshell.util.date.datetime2ifc(
ifcopenshell.util.sequence.get_finish_date(
finish,
-duration,
task.TaskTime.DurationType,
self.get_calendar(task),
),
"IfcDateTime",
)
elif starts:
start = max(starts)
start_ifc = ifcopenshell.util.date.datetime2ifc(start, "IfcDateTime")
if task.TaskTime.ScheduleStart == start_ifc:
return
task.TaskTime.ScheduleStart = start_ifc
task.TaskTime.ScheduleFinish = ifcopenshell.util.date.datetime2ifc(
ifcopenshell.util.sequence.get_finish_date(
start,
duration,
task.TaskTime.DurationType,
self.get_calendar(task),
),
"IfcDateTime",
)
else:
return
for rel in task.IsPredecessorTo:
self.cascade_task(rel.RelatedProcess)
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:
self.calendar_cache[task.id()] = ifcopenshell.util.sequence.derive_calendar(task)
return self.calendar_cache[task.id()]
def offset_date(self, date, days, duration_type, calendar):
return datetime.datetime.combine(
ifcopenshell.util.sequence.get_finish_date(date, datetime.timedelta(days=days), duration_type, calendar),
datetime.datetime.min.time(),
)
def get_task_time_attribute(self, task, attribute):
if task.TaskTime:
value = getattr(task.TaskTime, attribute)
if value:
return ifcopenshell.util.date.ifc2datetime(value)
@@ -11,6 +11,9 @@ class Usecase:
self.settings[key] = value
def execute(self):
self.task = self.get_task()
self.calendar = ifcopenshell.util.sequence.derive_calendar(self.task)
# If the user specifies both an end date and a duration, the duration takes priority
if (
"ScheduleDuration" in self.settings["attributes"].keys()
@@ -26,11 +29,11 @@ class Usecase:
duration_type = self.settings["attributes"].get("DurationType", self.settings["task_time"].DurationType)
if "ScheduleFinish" in self.settings["attributes"]:
self.settings["attributes"]["ScheduleFinish"] = ifcopenshell.util.sequence.get_soonest_working_day(
self.settings["attributes"]["ScheduleFinish"], duration_type, self.get_calendar()
self.settings["attributes"]["ScheduleFinish"], duration_type, self.calendar
)
if "ScheduleStart" in self.settings["attributes"]:
self.settings["attributes"]["ScheduleStart"] = ifcopenshell.util.sequence.get_soonest_working_day(
self.settings["attributes"]["ScheduleStart"], duration_type, self.get_calendar()
self.settings["attributes"]["ScheduleStart"], duration_type, self.calendar
)
for name, value in self.settings["attributes"].items():
@@ -52,12 +55,22 @@ class Usecase:
elif "ScheduleFinish" in self.settings["attributes"].keys() and self.settings["task_time"].ScheduleStart:
self.calculate_duration()
if (
self.settings["task_time"].ScheduleDuration
and (
"ScheduleStart" in self.settings["attributes"].keys()
or "ScheduleFinish" in self.settings["attributes"].keys()
or "ScheduleDuration" in self.settings["attributes"].keys()
)
):
ifcopenshell.api.run("sequence.cascade_schedule", self.file, task=self.task)
def calculate_finish(self):
finish_date = ifcopenshell.util.sequence.get_finish_date(
ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleStart),
ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleDuration),
self.settings["task_time"].DurationType,
self.get_calendar(),
self.calendar,
)
self.settings["task_time"].ScheduleFinish = ifcopenshell.util.date.datetime2ifc(finish_date, "IfcDateTime")
@@ -66,20 +79,14 @@ class Usecase:
finish = ifcopenshell.util.date.ifc2datetime(self.settings["task_time"].ScheduleFinish)
current_date = datetime.date(start.year, start.month, start.day)
finish_date = datetime.date(finish.year, finish.month, finish.day)
calendar = self.get_calendar()
duration = datetime.timedelta()
while current_date < finish_date:
if self.settings["task_time"].DurationType == "ELAPSEDTIME" or not calendar:
if self.settings["task_time"].DurationType == "ELAPSEDTIME" or not self.calendar:
duration += datetime.timedelta(days=1)
elif ifcopenshell.util.sequence.is_working_day(current_date, calendar):
elif ifcopenshell.util.sequence.is_working_day(current_date, self.calendar):
duration += datetime.timedelta(days=1)
current_date += datetime.timedelta(days=1)
self.settings["task_time"].ScheduleDuration = ifcopenshell.util.date.datetime2ifc(duration, "IfcDuration")
def get_calendar(self):
task = [e for e in self.file.get_inverse(self.settings["task_time"]) if e.is_a("IfcTask")]
if not task:
return
else:
task = task[0]
return ifcopenshell.util.sequence.derive_calendar(task)
def get_task(self):
return [e for e in self.file.get_inverse(self.settings["task_time"]) if e.is_a("IfcTask")][0]
@@ -84,7 +84,7 @@ class Usecase:
rel.RelatedProcess.id(),
{
"lag_time": 0
if not rel.TimeLag or not rel.TimeLag.LagValue
if not rel.TimeLag
else ifcopenshell.util.date.ifc2datetime(rel.TimeLag.LagValue.wrappedValue).days,
"type": self.sequence_type_map[rel.SequenceType],
},
@@ -27,7 +27,7 @@ def ifc2datetime(element):
if isinstance(duration, datetime.timedelta):
return timedelta2duration(duration)
return duration
elif isinstance(element, str) and element[2] == ":": # IfcTime
elif isinstance(element, str) and len(element) > 3 and element[2] == ":": # IfcTime
return datetime.time.fromisoformat(element)
elif isinstance(element, str) and ":" in element: # IfcDateTime
return datetime.datetime.fromisoformat(element)