From 61c2a2c6c6e3b853981a5a56ccbacdaf6269d34f Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 7 Jun 2024 17:14:03 +0500 Subject: [PATCH] remove_work_schedule to unassign a work schedule from a work plan #4819 --- .../api/sequence/remove_work_schedule.py | 7 +++ .../api/sequence/test_remove_work_schedule.py | 62 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/ifcopenshell-python/test/api/sequence/test_remove_work_schedule.py diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/remove_work_schedule.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/remove_work_schedule.py index c648cfc576..1a6d8b8e2b 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/sequence/remove_work_schedule.py +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/remove_work_schedule.py @@ -18,6 +18,7 @@ import ifcopenshell import ifcopenshell.api +import ifcopenshell.api.aggregate import ifcopenshell.util.element @@ -54,6 +55,7 @@ def remove_work_schedule(file: ifcopenshell.file, work_schedule: ifcopenshell.en definitions=[settings["work_schedule"]], relating_context=file.by_type("IfcContext")[0], ) + if settings["work_schedule"].Declares: for rel in settings["work_schedule"].Declares: for work_schedule in rel.RelatedObjects: @@ -62,6 +64,11 @@ def remove_work_schedule(file: ifcopenshell.file, work_schedule: ifcopenshell.en file, work_schedule=work_schedule, ) + + # Unassign from work plans. + if settings["work_schedule"].Decomposes: + ifcopenshell.api.aggregate.unassign_object(file, [settings["work_schedule"]]) + for inverse in file.get_inverse(settings["work_schedule"]): if inverse.is_a("IfcRelDefinesByObject"): if inverse.RelatingObject == settings["work_schedule"] or len(inverse.RelatedObjects) == 1: diff --git a/src/ifcopenshell-python/test/api/sequence/test_remove_work_schedule.py b/src/ifcopenshell-python/test/api/sequence/test_remove_work_schedule.py new file mode 100644 index 0000000000..c4a3bd2d25 --- /dev/null +++ b/src/ifcopenshell-python/test/api/sequence/test_remove_work_schedule.py @@ -0,0 +1,62 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Dion Moult +# +# 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 . + +import pytest +import test.bootstrap +import ifcopenshell.api +import ifcopenshell.api.sequence + + +def declared_objects(ifc_file: ifcopenshell.file) -> set[ifcopenshell.entity_instance]: + project = ifc_file.by_type("IfcProject")[0] + declared = {obj for rel in project.Declares for obj in rel.RelatedDefinitions} + return declared + + +# NOTE: sequence module features relies on entities introduced in IFC4 +# therefore no IFC2X3 tests +class TestRemoveWorkSchedule(test.bootstrap.IFC4): + def test_remove_work_schedule(self): + self.file.create_entity("IfcProject") + work_schedule = ifcopenshell.api.sequence.add_work_schedule(self.file) + + work_plan = ifcopenshell.api.sequence.add_work_plan(self.file) + ifcopenshell.api.sequence.assign_workplan(self.file, work_schedule, work_plan) + + work_schedule1 = ifcopenshell.api.sequence.add_work_schedule(self.file) + rel = self.file.create_entity("IfcRelDefinesByObject") + rel.RelatingObject = work_schedule + rel.RelatedObjects = [work_schedule1] + + ifcopenshell.api.sequence.add_task(self.file, work_schedule=work_schedule) + + ifcopenshell.api.sequence.remove_work_schedule(self.file, work_schedule=work_schedule) + # Remove workschedule and subschedules. + assert len(self.file.by_type("IfcWorkSchedule")) == 0 + assert len(self.file.by_type("IfcRelDefinesByObject")) == 0 + # Unassign from a work plan. + assert len(self.file.by_type("IfcRelAggregates")) == 0 + # Remove related IfcTasks. + assert len(self.file.by_type("IfcTask")) == 0 + assert len(self.file.by_type("IfcRelAssignsToControl")) == 0 + # Unassign from a project. + assert declared_objects(self.file) == {work_plan} + + +class TestRemoveWorkScheduleIFC4X3(test.bootstrap.IFC4X3, TestRemoveWorkSchedule): + pass