Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/cost/copy_cost_item.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
4.8 KiB
Python
Raw Normal View History

# 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
2024-04-09 16:41:03 +05:00
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, cost_item=None):
"""Copies all cost items and related relationships
2024-04-09 16:44:26 +05:00
The following relationships are also duplicated:
2024-04-09 16:44:26 +05:00
* The copy will have the same attributes and property sets as the original cost item
* The copy will be assigned to the parent cost schedule
* The copy will have duplicated nested cost items
2024-04-09 16:44:26 +05:00
:param cost_item: The cost item to be duplicated
:type cost_item: ifcopenshell.entity_instance.entity_instance
:return: The duplicated cost item or the list of duplicated cost items if the latter has children
:rtype: ifcopenshell.entity_instance.entity_instance or list of ifcopenshell.entity_instance.entity_instance
2024-04-09 16:44:26 +05:00
Example:
.. code:: python
2024-04-09 16:44:26 +05:00
# We have a cost item
cost_item = CostItem(name="Design new feature", deadline="2023-03-01")
2024-04-09 16:44:26 +05:00
# And now we have two
2024-04-09 16:44:26 +05:00
duplicated_cost_item = project.duplicate_cost_item(cost_item)
"""
self.file = file
self.settings = {"cost_item": cost_item}
2024-04-09 16:44:26 +05:00
def execute(self):
self.new_cost_items = []
self.duplicate_cost_item(self.settings["cost_item"])
2024-04-09 16:44:26 +05:00
def duplicate_cost_item(self, cost_item):
new_cost_item = ifcopenshell.util.element.copy_deep(self.file, cost_item)
self.new_cost_items.append(new_cost_item)
self.copy_indirect_attributes(cost_item, new_cost_item)
return new_cost_item
def copy_indirect_attributes(self, from_element, to_element):
for inverse in self.file.get_inverse(from_element):
if inverse.is_a("IfcRelDefinesByProperties"):
inverse = ifcopenshell.util.element.copy(self.file, inverse)
inverse.RelatedObjects = [to_element]
2024-04-09 16:44:26 +05:00
pset = ifcopenshell.util.element.copy_deep(self.file, inverse.RelatingPropertyDefinition)
inverse.RelatingPropertyDefinition = pset
elif inverse.is_a("IfcRelNests") and inverse.RelatingObject == from_element:
nested_cost_items = [e for e in inverse.RelatedObjects]
if nested_cost_items:
new_cost_items = []
for cost_item in nested_cost_items:
new_cost_item = self.duplicate_cost_item(cost_item)
new_cost_items.append(new_cost_item)
inverse = ifcopenshell.util.element.copy(self.file, inverse)
inverse.RelatingObject = to_element
inverse.RelatedObjects = new_cost_items
ifcopenshell.api.run("nest.unassign_object", self.file, related_objects=new_cost_items)
ifcopenshell.api.run(
"nest.assign_object",
self.file,
related_objects=new_cost_items,
relating_object=to_element,
)
# elif inverse.is_a("IfcRelAssignsToProduct"):
# continue
# to_element.IsDefinedBy = inverse.RelatingOrder
# elif inverse.is_a("IfcRelAssignsToProcess"):
# to_element.IsDefinedBy = inverse.RelatingProcess
# elif inverse.is_a("IfcRelAssignsToResource"):
# continue
# elif inverse.is_a("IfcRelAssignsToControl"):
# continue
# elif inverse.is_a("IfcRelDefinesByObject"):
# continue
else:
for i, value in enumerate(inverse):
if value == from_element:
new_inverse = ifcopenshell.util.element.copy(self.file, inverse)
new_inverse[i] = to_element
elif isinstance(value, (tuple, list)) and from_element in value:
new_value = list(value)
new_value.append(to_element)
2024-04-09 16:41:34 +05:00
inverse[i] = new_value