remove_work_schedule to unassign a work schedule from a work plan #4819

This commit is contained in:
Andrej730
2024-06-07 17:14:03 +05:00
parent 5c41baeaf1
commit 61c2a2c6c6
2 changed files with 69 additions and 0 deletions
@@ -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:
@@ -0,0 +1,62 @@
# 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 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