diff --git a/src/blenderbim/blenderbim/bim/module/sequence/operator.py b/src/blenderbim/blenderbim/bim/module/sequence/operator.py index 1415f4f049..60c4dd2e1d 100644 --- a/src/blenderbim/blenderbim/bim/module/sequence/operator.py +++ b/src/blenderbim/blenderbim/bim/module/sequence/operator.py @@ -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"} diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/cascade_schedule.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/cascade_schedule.py new file mode 100644 index 0000000000..9e0d598dbd --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/cascade_schedule.py @@ -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) diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py index 976442ab75..f332265eb9 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_task_time.py @@ -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] diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/recalculate_schedule.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/recalculate_schedule.py index c284e72ba2..820595ee65 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/sequence/recalculate_schedule.py +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/recalculate_schedule.py @@ -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], }, diff --git a/src/ifcopenshell-python/ifcopenshell/util/date.py b/src/ifcopenshell-python/ifcopenshell/util/date.py index 99c60f6d1d..c14fff1221 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/date.py +++ b/src/ifcopenshell-python/ifcopenshell/util/date.py @@ -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)