Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/group/assign_group.py
T

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

69 lines
2.4 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/>.
2025-12-19 18:53:04 +05:00
from typing import Union
import ifcopenshell
import ifcopenshell.api.owner
2024-05-08 17:21:00 +05:00
import ifcopenshell.guid
def assign_group(
file: ifcopenshell.file, products: list[ifcopenshell.entity_instance], group: ifcopenshell.entity_instance
) -> Union[ifcopenshell.entity_instance, None]:
"""Assigns products to a group
2022-12-06 16:42:47 +11:00
If a product is already assigned to the group, it will not be assigned
twice.
2022-12-06 16:42:47 +11:00
:param products: A list of IfcProduct elements to assign to the group
:param group: The IfcGroup to assign the products to
:return: The IfcRelAssignsToGroup relationship
or `None` if `products` was empty list.
2022-12-06 16:42:47 +11:00
Example:
2023-01-10 10:16:28 +11:00
.. code:: python
2022-12-06 16:42:47 +11:00
group = ifcopenshell.api.group.add_group(model, name="Furniture")
ifcopenshell.api.group.assign_group(model,
products=model.by_type("IfcFurniture"), group=group)
"""
2025-02-18 15:18:23 +05:00
if not products:
return
2025-02-18 15:18:23 +05:00
is_grouped_by: tuple[ifcopenshell.entity_instance, ...]
if not (is_grouped_by := group.IsGroupedBy):
return file.create_entity(
"IfcRelAssignsToGroup",
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.owner.create_owner_history(file),
2025-02-18 15:18:23 +05:00
"RelatedObjects": products,
"RelatingGroup": group,
},
)
2025-02-18 15:18:23 +05:00
rel = is_grouped_by[0]
related_objects = set(rel.RelatedObjects) or set()
2025-02-18 15:18:23 +05:00
products_set = set(products)
if products_set.issubset(related_objects):
2024-04-05 14:06:10 +05:00
return rel
2025-02-18 15:18:23 +05:00
rel.RelatedObjects = list(related_objects | products_set)
ifcopenshell.api.owner.update_owner_history(file, element=rel)
return rel