2022-01-19 12:18:33 +11:00
|
|
|
# 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
|
|
|
|
|
|
2021-03-05 15:37:40 +11:00
|
|
|
import ifcopenshell
|
2024-06-28 12:36:31 +10:00
|
|
|
import ifcopenshell.api.owner
|
2024-05-08 17:21:00 +05:00
|
|
|
import ifcopenshell.guid
|
2021-03-05 15:37:40 +11:00
|
|
|
|
|
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
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
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
If a product is already assigned to the group, it will not be assigned
|
|
|
|
|
twice.
|
2022-12-06 16:42:47 +11:00
|
|
|
|
2024-05-06 14:35:39 +10: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
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
Example:
|
2023-01-10 10:16:28 +11:00
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
.. code:: python
|
2022-12-06 16:42:47 +11:00
|
|
|
|
2024-06-28 12:36:31 +10:00
|
|
|
group = ifcopenshell.api.group.add_group(model, name="Furniture")
|
|
|
|
|
ifcopenshell.api.group.assign_group(model,
|
2024-05-06 14:35:39 +10:00
|
|
|
products=model.by_type("IfcFurniture"), group=group)
|
|
|
|
|
"""
|
2025-02-18 15:18:23 +05:00
|
|
|
if not products:
|
2024-05-06 14:35:39 +10:00
|
|
|
return
|
2024-04-11 15:54:06 +05:00
|
|
|
|
2025-02-18 15:18:23 +05:00
|
|
|
is_grouped_by: tuple[ifcopenshell.entity_instance, ...]
|
|
|
|
|
if not (is_grouped_by := group.IsGroupedBy):
|
2024-05-06 14:35:39 +10:00
|
|
|
return file.create_entity(
|
|
|
|
|
"IfcRelAssignsToGroup",
|
|
|
|
|
**{
|
|
|
|
|
"GlobalId": ifcopenshell.guid.new(),
|
2024-06-28 12:36:31 +10:00
|
|
|
"OwnerHistory": ifcopenshell.api.owner.create_owner_history(file),
|
2025-02-18 15:18:23 +05:00
|
|
|
"RelatedObjects": products,
|
|
|
|
|
"RelatingGroup": group,
|
|
|
|
|
},
|
2024-05-06 14:35:39 +10:00
|
|
|
)
|
2025-02-18 15:18:23 +05:00
|
|
|
rel = is_grouped_by[0]
|
2024-05-06 14:35:39 +10:00
|
|
|
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)
|
2025-06-10 11:02:54 +05:00
|
|
|
ifcopenshell.api.owner.update_owner_history(file, element=rel)
|
2024-05-06 14:35:39 +10:00
|
|
|
return rel
|