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/>.
|
|
|
|
|
|
2024-06-28 12:36:31 +10:00
|
|
|
import ifcopenshell.api.owner
|
|
|
|
|
import ifcopenshell.api.geometry
|
2021-11-05 15:14:10 +11:00
|
|
|
import ifcopenshell.util.element
|
2024-09-03 15:17:17 +05:00
|
|
|
from typing import Any
|
2021-03-26 10:39:07 +11:00
|
|
|
|
|
|
|
|
|
2024-05-15 10:55:29 +05:00
|
|
|
def assign_representation(
|
|
|
|
|
file: ifcopenshell.file, product: ifcopenshell.entity_instance, representation: ifcopenshell.entity_instance
|
|
|
|
|
) -> None:
|
2024-05-06 14:35:39 +10:00
|
|
|
usecase = Usecase()
|
|
|
|
|
usecase.file = file
|
2025-07-24 19:14:26 +05:00
|
|
|
return usecase.execute(product, representation)
|
2024-05-06 14:35:39 +10:00
|
|
|
|
2021-01-03 19:09:01 +11:00
|
|
|
|
2024-05-06 14:35:39 +10:00
|
|
|
class Usecase:
|
2024-09-03 15:17:17 +05:00
|
|
|
file: ifcopenshell.file
|
|
|
|
|
settings: dict[str, Any]
|
|
|
|
|
|
2025-07-24 19:14:26 +05:00
|
|
|
def execute(self, product: ifcopenshell.entity_instance, representation: ifcopenshell.entity_instance) -> None:
|
|
|
|
|
if product.is_a("IfcProduct"):
|
|
|
|
|
product_type = ifcopenshell.util.element.get_type(product)
|
2021-11-05 15:14:10 +11:00
|
|
|
if (
|
|
|
|
|
product_type
|
|
|
|
|
and product_type.RepresentationMaps
|
2025-07-24 19:14:26 +05:00
|
|
|
and representation.RepresentationType != "MappedRepresentation"
|
2021-11-05 15:14:10 +11:00
|
|
|
):
|
2025-07-24 19:14:26 +05:00
|
|
|
product = product_type
|
2021-11-05 15:14:10 +11:00
|
|
|
|
2025-07-24 19:14:26 +05:00
|
|
|
if product.is_a("IfcProduct"):
|
|
|
|
|
self.assign_product_representation(product, representation)
|
|
|
|
|
elif product.is_a("IfcTypeProduct"):
|
|
|
|
|
if product.RepresentationMaps:
|
|
|
|
|
maps = list(product.RepresentationMaps)
|
2021-01-27 15:28:29 +11:00
|
|
|
else:
|
|
|
|
|
maps = []
|
|
|
|
|
self.zero = self.file.createIfcCartesianPoint((0.0, 0.0, 0.0))
|
|
|
|
|
self.x_axis = self.file.createIfcDirection((1.0, 0.0, 0.0))
|
|
|
|
|
self.z_axis = self.file.createIfcDirection((0.0, 0.0, 1.0))
|
|
|
|
|
maps.append(
|
|
|
|
|
self.file.create_entity(
|
|
|
|
|
"IfcRepresentationMap",
|
|
|
|
|
**{
|
|
|
|
|
"MappingOrigin": self.file.createIfcAxis2Placement3D(self.zero, self.z_axis, self.x_axis),
|
2025-07-24 19:14:26 +05:00
|
|
|
"MappedRepresentation": representation,
|
2021-01-27 15:28:29 +11:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-07-24 19:14:26 +05:00
|
|
|
product.RepresentationMaps = maps
|
2021-05-18 16:41:30 +10:00
|
|
|
if self.file.schema == "IFC2X3":
|
2025-07-24 19:14:26 +05:00
|
|
|
types = product.ObjectTypeOf
|
2021-05-18 16:41:30 +10:00
|
|
|
else:
|
2025-07-24 19:14:26 +05:00
|
|
|
types = product.Types
|
2021-05-18 16:41:30 +10:00
|
|
|
if types:
|
|
|
|
|
for element in types[0].RelatedObjects:
|
2024-06-28 12:36:31 +10:00
|
|
|
mapped_representation = ifcopenshell.api.geometry.map_representation(
|
2025-07-24 19:14:26 +05:00
|
|
|
self.file, representation=representation
|
2021-05-18 16:41:30 +10:00
|
|
|
)
|
2021-11-05 15:14:10 +11:00
|
|
|
self.assign_product_representation(element, mapped_representation)
|
2025-07-24 19:14:26 +05:00
|
|
|
ifcopenshell.api.owner.update_owner_history(self.file, element=product)
|
2021-11-05 15:14:10 +11:00
|
|
|
|
2024-09-03 15:17:17 +05:00
|
|
|
def assign_product_representation(
|
|
|
|
|
self, product: ifcopenshell.entity_instance, representation: ifcopenshell.entity_instance
|
|
|
|
|
) -> None:
|
2021-11-05 15:14:10 +11:00
|
|
|
definition = product.Representation
|
|
|
|
|
if not definition:
|
|
|
|
|
definition = self.file.createIfcProductDefinitionShape()
|
|
|
|
|
product.Representation = definition
|
|
|
|
|
representations = list(definition.Representations) if definition.Representations else []
|
|
|
|
|
representations.append(representation)
|
|
|
|
|
definition.Representations = representations
|