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-05-11 11:35:47 +10:00
|
|
|
import datetime
|
2024-04-22 17:01:28 +05:00
|
|
|
import ifcopenshell.util.constraint
|
2021-04-20 17:11:06 +10:00
|
|
|
import ifcopenshell.util.date
|
2021-05-11 11:35:47 +10:00
|
|
|
import ifcopenshell.util.sequence
|
2024-04-22 17:01:28 +05:00
|
|
|
from typing import Any, Optional
|
2021-04-20 17:11:06 +10:00
|
|
|
|
|
|
|
|
|
2021-04-20 05:12:13 +00:00
|
|
|
class Usecase:
|
2024-04-22 17:01:28 +05:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
file: ifcopenshell.file,
|
|
|
|
|
task_time: ifcopenshell.entity_instance,
|
|
|
|
|
attributes: Optional[dict[str, Any]] = None,
|
|
|
|
|
):
|
2023-01-03 17:31:48 +11:00
|
|
|
"""Edits the attributes of an IfcTaskTime
|
|
|
|
|
|
|
|
|
|
For more information about the attributes and data types of an
|
|
|
|
|
IfcTaskTime, consult the IFC documentation.
|
|
|
|
|
|
|
|
|
|
:param task_time: The IfcTaskTime entity you want to edit
|
2024-05-06 08:39:41 +10:00
|
|
|
:type task_time: ifcopenshell.entity_instance
|
2023-01-03 17:31:48 +11:00
|
|
|
:param attributes: a dictionary of attribute names and values.
|
|
|
|
|
:type attributes: dict, optional
|
|
|
|
|
:return: None
|
|
|
|
|
:rtype: None
|
|
|
|
|
|
2023-01-10 10:16:28 +11:00
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
2023-01-03 17:31:48 +11:00
|
|
|
|
|
|
|
|
# Let's imagine we are creating a construction schedule. All tasks
|
|
|
|
|
# need to be part of a work schedule.
|
|
|
|
|
schedule = ifcopenshell.api.run("sequence.add_work_schedule", model, name="Construction Schedule A")
|
|
|
|
|
|
|
|
|
|
# Create a task to do formwork
|
|
|
|
|
task = ifcopenshell.api.run("sequence.add_task", model,
|
|
|
|
|
work_schedule=schedule, name="Formwork", identification="A")
|
|
|
|
|
|
|
|
|
|
# Let's say it takes 2 days and starts on the 1st of January, 2000
|
|
|
|
|
time = ifcopenshell.api.run("sequence.add_task_time", model, task=formwork)
|
|
|
|
|
ifcopenshell.api.run("sequence.edit_task_time", model,
|
|
|
|
|
task_time=time, attributes={"ScheduleStart": "2000-01-01", "ScheduleDuration": "P2D"})
|
|
|
|
|
"""
|
2021-04-20 05:12:13 +00:00
|
|
|
self.file = file
|
2023-01-03 17:31:48 +11:00
|
|
|
self.settings = {"task_time": task_time, "attributes": attributes or {}}
|
2021-04-20 05:12:13 +00:00
|
|
|
|
2024-04-22 17:01:28 +05:00
|
|
|
def execute(self) -> None:
|
2021-06-17 13:23:09 +10:00
|
|
|
self.task = self.get_task()
|
|
|
|
|
self.calendar = ifcopenshell.util.sequence.derive_calendar(self.task)
|
|
|
|
|
|
2021-05-11 11:35:47 +10:00
|
|
|
# If the user specifies both an end date and a duration, the duration takes priority
|
2021-06-04 15:21:48 +10:00
|
|
|
if (
|
2021-08-10 10:55:01 +10:00
|
|
|
self.settings["attributes"].get("ScheduleDuration", None)
|
2021-06-04 15:21:48 +10:00
|
|
|
and "ScheduleFinish" in self.settings["attributes"].keys()
|
|
|
|
|
):
|
2021-05-15 02:04:43 +00:00
|
|
|
del self.settings["attributes"]["ScheduleFinish"]
|
2021-05-11 11:35:47 +10:00
|
|
|
|
2022-09-09 02:25:20 +01:00
|
|
|
duration_type = self.settings["attributes"].get(
|
|
|
|
|
"DurationType", self.settings["task_time"].DurationType
|
|
|
|
|
)
|
2022-03-25 15:53:05 +11:00
|
|
|
finish = self.settings["attributes"].get("ScheduleFinish", None)
|
|
|
|
|
if finish:
|
|
|
|
|
if isinstance(finish, str):
|
|
|
|
|
finish = datetime.datetime.fromisoformat(finish)
|
|
|
|
|
self.settings["attributes"]["ScheduleFinish"] = datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
ifcopenshell.util.sequence.get_soonest_working_day(
|
|
|
|
|
finish, duration_type, self.calendar
|
|
|
|
|
),
|
2022-03-25 15:53:05 +11:00
|
|
|
datetime.time(17),
|
2021-06-04 15:21:48 +10:00
|
|
|
)
|
2022-03-25 15:53:05 +11:00
|
|
|
start = self.settings["attributes"].get("ScheduleStart", None)
|
|
|
|
|
if start:
|
|
|
|
|
if isinstance(start, str):
|
|
|
|
|
start = datetime.datetime.fromisoformat(start)
|
|
|
|
|
self.settings["attributes"]["ScheduleStart"] = datetime.datetime.combine(
|
2022-09-09 02:25:20 +01:00
|
|
|
ifcopenshell.util.sequence.get_soonest_working_day(
|
|
|
|
|
start, duration_type, self.calendar
|
|
|
|
|
),
|
2022-03-25 15:53:05 +11:00
|
|
|
datetime.time(9),
|
2021-06-04 15:21:48 +10:00
|
|
|
)
|
|
|
|
|
|
2021-04-20 05:12:13 +00:00
|
|
|
for name, value in self.settings["attributes"].items():
|
2022-01-21 18:52:40 +11:00
|
|
|
if value is not None:
|
2021-05-06 17:14:28 +10:00
|
|
|
if "Start" in name or "Finish" in name or name == "StatusTime":
|
2021-04-20 17:11:06 +10:00
|
|
|
value = ifcopenshell.util.date.datetime2ifc(value, "IfcDateTime")
|
2022-09-09 02:25:20 +01:00
|
|
|
elif (
|
|
|
|
|
name == "ScheduleDuration"
|
|
|
|
|
or name == "ActualDuration"
|
|
|
|
|
or name == "RemainingTime"
|
|
|
|
|
):
|
2021-05-04 00:27:53 +00:00
|
|
|
value = ifcopenshell.util.date.datetime2ifc(value, "IfcDuration")
|
2021-04-20 17:11:06 +10:00
|
|
|
setattr(self.settings["task_time"], name, value)
|
2021-05-11 11:35:47 +10:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
"ScheduleDuration" in self.settings["attributes"].keys()
|
|
|
|
|
and self.settings["task_time"].ScheduleDuration
|
|
|
|
|
and self.settings["task_time"].ScheduleStart
|
|
|
|
|
):
|
2021-05-31 12:42:02 +10:00
|
|
|
self.calculate_finish()
|
2022-09-09 02:25:20 +01:00
|
|
|
elif (
|
|
|
|
|
self.settings["attributes"].get("ScheduleStart", None)
|
|
|
|
|
and self.settings["task_time"].ScheduleDuration
|
|
|
|
|
):
|
2021-05-31 12:42:02 +10:00
|
|
|
self.calculate_finish()
|
2022-09-09 02:25:20 +01:00
|
|
|
elif (
|
|
|
|
|
self.settings["attributes"].get("ScheduleFinish", None)
|
|
|
|
|
and self.settings["task_time"].ScheduleStart
|
|
|
|
|
):
|
2021-05-31 12:42:02 +10:00
|
|
|
self.calculate_duration()
|
|
|
|
|
|
2021-09-09 21:27:23 +10:00
|
|
|
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()
|
2021-06-17 13:23:09 +10:00
|
|
|
):
|
|
|
|
|
ifcopenshell.api.run("sequence.cascade_schedule", self.file, task=self.task)
|
2023-08-24 15:51:33 +01:00
|
|
|
if self.settings["task_time"].ScheduleDuration:
|
|
|
|
|
self.handle_resource_calculation()
|
2021-06-17 13:23:09 +10:00
|
|
|
|
2021-05-31 12:42:02 +10:00
|
|
|
def calculate_finish(self):
|
2022-03-25 15:53:05 +11:00
|
|
|
finish = ifcopenshell.util.sequence.get_start_or_finish_date(
|
2022-09-09 02:25:20 +01:00
|
|
|
ifcopenshell.util.date.ifc2datetime(
|
|
|
|
|
self.settings["task_time"].ScheduleStart
|
|
|
|
|
),
|
|
|
|
|
ifcopenshell.util.date.ifc2datetime(
|
|
|
|
|
self.settings["task_time"].ScheduleDuration
|
|
|
|
|
),
|
2021-06-04 15:21:48 +10:00
|
|
|
self.settings["task_time"].DurationType,
|
2021-06-17 13:23:09 +10:00
|
|
|
self.calendar,
|
2022-03-25 15:53:05 +11:00
|
|
|
date_type="FINISH",
|
2021-06-04 15:21:48 +10:00
|
|
|
)
|
2022-09-09 02:25:20 +01:00
|
|
|
self.settings["task_time"].ScheduleFinish = ifcopenshell.util.date.datetime2ifc(
|
|
|
|
|
finish, "IfcDateTime"
|
|
|
|
|
)
|
2021-05-31 12:42:02 +10:00
|
|
|
|
|
|
|
|
def calculate_duration(self):
|
2022-09-09 02:25:20 +01:00
|
|
|
start = ifcopenshell.util.date.ifc2datetime(
|
|
|
|
|
self.settings["task_time"].ScheduleStart
|
|
|
|
|
)
|
|
|
|
|
finish = ifcopenshell.util.date.ifc2datetime(
|
|
|
|
|
self.settings["task_time"].ScheduleFinish
|
|
|
|
|
)
|
2021-05-31 12:42:02 +10:00
|
|
|
current_date = datetime.date(start.year, start.month, start.day)
|
|
|
|
|
finish_date = datetime.date(finish.year, finish.month, finish.day)
|
2022-03-25 15:53:05 +11:00
|
|
|
duration = datetime.timedelta(days=1)
|
2021-05-31 12:42:02 +10:00
|
|
|
while current_date < finish_date:
|
2022-09-09 02:25:20 +01:00
|
|
|
if (
|
|
|
|
|
self.settings["task_time"].DurationType == "ELAPSEDTIME"
|
|
|
|
|
or not self.calendar
|
|
|
|
|
):
|
2021-05-31 12:42:02 +10:00
|
|
|
duration += datetime.timedelta(days=1)
|
2021-06-17 13:23:09 +10:00
|
|
|
elif ifcopenshell.util.sequence.is_working_day(current_date, self.calendar):
|
2021-05-31 12:42:02 +10:00
|
|
|
duration += datetime.timedelta(days=1)
|
|
|
|
|
current_date += datetime.timedelta(days=1)
|
2022-09-09 02:25:20 +01:00
|
|
|
self.settings[
|
|
|
|
|
"task_time"
|
|
|
|
|
].ScheduleDuration = ifcopenshell.util.date.datetime2ifc(
|
|
|
|
|
duration, "IfcDuration"
|
|
|
|
|
)
|
2021-05-31 12:42:02 +10:00
|
|
|
|
2024-04-22 17:01:28 +05:00
|
|
|
def get_task(self) -> ifcopenshell.entity_instance:
|
|
|
|
|
return next(
|
2022-09-09 02:25:20 +01:00
|
|
|
e
|
|
|
|
|
for e in self.file.get_inverse(self.settings["task_time"])
|
|
|
|
|
if e.is_a("IfcTask")
|
2024-04-22 17:01:28 +05:00
|
|
|
)
|
2023-08-24 15:51:33 +01:00
|
|
|
|
|
|
|
|
def handle_resource_calculation(self):
|
|
|
|
|
resources = ifcopenshell.util.sequence.get_task_resources(self.task, is_deep=False)
|
|
|
|
|
for resource in resources:
|
|
|
|
|
if ifcopenshell.util.constraint.is_attribute_locked(resource, "Usage.ScheduleWork"):
|
|
|
|
|
ifcopenshell.api.run("resource.calculate_resource_usage", self.file, resource=resource)
|
|
|
|
|
#TODO: If the duration changes, this implies the productivity rate must change to accomModate the new Schedule Work to be calculated.
|
|
|
|
|
# elif ifcopenshell.util.constraint.is_attribute_locked(resource, "Usage.ScheduleUsage"):
|
|
|
|
|
# ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource)
|