sequence.copy_work_schedule #6901

Similar to e50215ddc but for IfcWorkSchedules

Button location - https://files.catbox.moe/jfd1ol.png
This commit is contained in:
Andrej730
2025-07-24 19:25:36 +05:00
parent f5b28c0df8
commit b76b05fc71
11 changed files with 135 additions and 3 deletions
@@ -36,6 +36,7 @@ def copy_cost_schedule(
schedule = ifcopenshell.api.cost.add_cost_schedule(model)
new_schedule = ifcopenshell.api.cost.copy_cost_schedule(schedule)
"""
# Shared code logic with copy_work_schedule.
new_schedule = ifcopenshell.util.element.copy(file, cost_schedule)
for rel in cost_schedule.Controls:
@@ -38,6 +38,7 @@ from .assign_sequence import assign_sequence
from .assign_work_plan import assign_work_plan
from .calculate_task_duration import calculate_task_duration
from .cascade_schedule import cascade_schedule
from .copy_work_schedule import copy_work_schedule
from .create_baseline import create_baseline
from .duplicate_task import duplicate_task
from .edit_lag_time import edit_lag_time
@@ -84,6 +85,7 @@ __all__ = [
"assign_work_plan",
"calculate_task_duration",
"cascade_schedule",
"copy_work_schedule",
"create_baseline",
"duplicate_task",
"edit_lag_time",
@@ -0,0 +1,51 @@
# 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/>.
import ifcopenshell.api.control
import ifcopenshell.api.sequence
import ifcopenshell.util.element
def copy_work_schedule(
file: ifcopenshell.file,
work_schedule: ifcopenshell.entity_instance,
) -> ifcopenshell.entity_instance:
"""Copy a work schedule.
:param work_schedule: IfcWorkSchedule to copy.
:return: The duplicated IfcWorkSchedule entity.
Example:
.. code:: python
work_plan = ifcopenshell.api.sequence.add_work_plan(model, name="Construction")
schedule = ifcopenshell.api.sequence.add_work_schedule(model,
name="Construction Schedule A", work_plan=work_plan)
new_schedule = ifcopenshell.api.sequence.copy_work_schedule(model, schedule)
"""
# Shared code logic with copy_cost_schedule.
new_schedule = ifcopenshell.util.element.copy(file, work_schedule)
for rel in work_schedule.Controls:
for task in rel.RelatedObjects:
duplicated_tasks = ifcopenshell.api.sequence.duplicate_task(file, task)[1]
# All other nested items are not connected to the work schedule explicitly.
duplicated_task = duplicated_tasks[0]
ifcopenshell.api.control.assign_control(file, new_schedule, duplicated_task)
return new_schedule
@@ -27,6 +27,7 @@ import ifcopenshell.util.sequence
from typing import Union, Any
# TODO: inconsistent name with other copy_xxx api methods.
def duplicate_task(
file: ifcopenshell.file, task: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, list[ifcopenshell.entity_instance]]:
@@ -58,7 +59,7 @@ def duplicate_task(
class Usecase:
file: ifcopenshell.file
settings: dict[str, Any]
settings: dict[str, list[ifcopenshell.entity_instance]]
def execute(self):
self.tracker = {"current": [], "duplicate": []}
@@ -66,14 +67,16 @@ class Usecase:
self.copy_sequence_relationship(self.tracker["current"], self.tracker["duplicate"])
return self.tracker["current"], self.tracker["duplicate"]
def duplicate_task(self, task):
def duplicate_task(self, task: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance:
new_task = ifcopenshell.util.element.copy_deep(self.file, task)
self.tracker["current"].append(task)
self.tracker["duplicate"].append(new_task)
self.copy_indirect_attributes(task, new_task)
return new_task
def copy_indirect_attributes(self, from_element, to_element):
def copy_indirect_attributes(
self, from_element: ifcopenshell.entity_instance, to_element: ifcopenshell.entity_instance
) -> None:
for inverse in self.file.get_inverse(from_element):
if inverse.is_a("IfcRelDefinesByProperties"):
inverse = ifcopenshell.util.element.copy(self.file, inverse)
@@ -23,6 +23,7 @@ import test.bootstrap
class TestCopyCostSchedule(test.bootstrap.IFC4):
def test_run(self):
# Shared code logic with test_copy_work_schedule.
schedule = ifcopenshell.api.cost.add_cost_schedule(self.file, name="Foo")
item = ifcopenshell.api.cost.add_cost_item(self.file, cost_schedule=schedule)
ifcopenshell.api.cost.add_cost_item(self.file, cost_item=item) # Subitem.
@@ -0,0 +1,47 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2024 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/>.
import ifcopenshell.api.sequence
import ifcopenshell.util.sequence
import test.bootstrap
class TestCopyWorkSchedule(test.bootstrap.IFC4):
def test_run(self):
self.file.create_entity("IfcProject")
# Shared code logic with test_copy_cost_schedule.
work_plan = ifcopenshell.api.sequence.add_work_plan(self.file)
schedule = ifcopenshell.api.sequence.add_work_schedule(self.file, work_plan=work_plan)
task = ifcopenshell.api.sequence.add_task(self.file, work_schedule=schedule)
ifcopenshell.api.sequence.add_task(self.file, parent_task=task) # Subtask.
old_cost_items = set(self.file.by_type("IfcTask"))
new_schedule = ifcopenshell.api.sequence.copy_work_schedule(self.file, schedule)
# We don't check how well IfcTasks are copied,
# it should be tested separately in test_duplicate_task.
assert isinstance(new_schedule, ifcopenshell.entity_instance)
assert new_schedule != schedule
assert len(ifcopenshell.util.sequence.get_root_tasks(new_schedule)) == 1
new_tasks = set(ifcopenshell.util.sequence.get_work_schedule_tasks(new_schedule))
assert len(new_tasks) == 2
assert len(new_tasks.intersection(old_cost_items)) == 0
class TestCopyWorkScheduleIFC4X3(test.bootstrap.IFC4X3, TestCopyWorkSchedule):
pass