Adding set items can now specify a name at creation for convenience

This commit is contained in:
Dion Moult
2025-02-01 16:00:43 +11:00
parent 8436817408
commit 9f93779ac2
3 changed files with 32 additions and 37 deletions
@@ -16,10 +16,14 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>. # along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell import ifcopenshell
from typing import Optional
def add_constituent( def add_constituent(
file: ifcopenshell.file, constituent_set: ifcopenshell.entity_instance, material: ifcopenshell.entity_instance file: ifcopenshell.file,
constituent_set: ifcopenshell.entity_instance,
material: ifcopenshell.entity_instance,
name: Optional[str] = None,
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
"""Adds a new constituent to a constituent set """Adds a new constituent to a constituent set
@@ -40,11 +44,9 @@ def add_constituent(
constituent is part of. The constituent set represents a group of constituent is part of. The constituent set represents a group of
constituents. See ifcopenshell.api.material.add_material_set for constituents. See ifcopenshell.api.material.add_material_set for
information on how to add a constituent set. information on how to add a constituent set.
:type constituent_set: ifcopenshell.entity_instance
:param material: The IfcMaterial that the constituent is made out of. :param material: The IfcMaterial that the constituent is made out of.
:type material: ifcopenshell.entity_instance :param name: An optional name of the constituent.
:return: The newly created IfcMaterialConstituent :return: The newly created IfcMaterialConstituent
:rtype: ifcopenshell.entity_instance
Example: Example:
@@ -71,9 +73,9 @@ def add_constituent(
# Now let's use those materials as two constituents in our set. # Now let's use those materials as two constituents in our set.
ifcopenshell.api.material.add_constituent(model, ifcopenshell.api.material.add_constituent(model,
constituent_set=material_set, material=aluminium) constituent_set=material_set, material=aluminium, name="Framing")
ifcopenshell.api.material.add_constituent(model, ifcopenshell.api.material.add_constituent(model,
constituent_set=material_set, material=glass) constituent_set=material_set, material=glass, name="Glazing")
# Great! Let's assign our material set to our window type. # Great! Let's assign our material set to our window type.
# We're technically not done here, we might want to add geometry to # We're technically not done here, we might want to add geometry to
@@ -82,10 +84,8 @@ def add_constituent(
# aluminium and glass. # aluminium and glass.
ifcopenshell.api.material.assign_material(model, products=[window_type], material=material_set) ifcopenshell.api.material.assign_material(model, products=[window_type], material=material_set)
""" """
settings = {"constituent_set": constituent_set, "material": material} constituents = list(constituent_set.MaterialConstituents or [])
constituent = file.create_entity("IfcMaterialConstituent", Material=material, Name=name)
constituents = list(settings["constituent_set"].MaterialConstituents or [])
constituent = file.create_entity("IfcMaterialConstituent", **{"Material": settings["material"]})
constituents.append(constituent) constituents.append(constituent)
settings["constituent_set"].MaterialConstituents = constituents constituent_set.MaterialConstituents = constituents
return constituent return constituent
@@ -17,10 +17,14 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>. # along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell import ifcopenshell
import ifcopenshell.util.unit import ifcopenshell.util.unit
from typing import Optional
def add_layer( def add_layer(
file: ifcopenshell.file, layer_set: ifcopenshell.entity_instance, material: ifcopenshell.entity_instance file: ifcopenshell.file,
layer_set: ifcopenshell.entity_instance,
material: ifcopenshell.entity_instance,
name: Optional[str] = None,
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
"""Adds a new layer to a layer set """Adds a new layer to a layer set
@@ -39,11 +43,9 @@ def add_layer(
layer set represents a group of layers. See layer set represents a group of layers. See
ifcopenshell.api.material.add_material_set for more information on ifcopenshell.api.material.add_material_set for more information on
how to add a layer set. how to add a layer set.
:type layer_set: ifcopenshell.entity_instance
:param material: The IfcMaterial that the layer is made out of. :param material: The IfcMaterial that the layer is made out of.
:type material: ifcopenshell.entity_instance :param name: An optional name of the layer.
:return: The newly created IfcMaterialLayer :return: The newly created IfcMaterialLayer
:rtype: ifcopenshell.entity_instance
Example: Example:
@@ -82,12 +84,8 @@ def add_layer(
ifcopenshell.api.material.assign_material(model, products=[wall_type], material=material_set) ifcopenshell.api.material.assign_material(model, products=[wall_type], material=material_set)
""" """
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file) unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
settings = {"layer_set": layer_set, "material": material} layers = list(layer_set.MaterialLayers or [])
layer = file.create_entity("IfcMaterialLayer", Material=material, LayerThickness=0.1 / unit_scale, Name=name)
layers = list(settings["layer_set"].MaterialLayers or [])
layer = file.create_entity(
"IfcMaterialLayer", **{"Material": settings["material"], "LayerThickness": 0.1 / unit_scale}
)
layers.append(layer) layers.append(layer)
settings["layer_set"].MaterialLayers = layers layer_set.MaterialLayers = layers
return layer return layer
@@ -24,6 +24,7 @@ def add_profile(
profile_set: ifcopenshell.entity_instance, profile_set: ifcopenshell.entity_instance,
material: Optional[ifcopenshell.entity_instance] = None, material: Optional[ifcopenshell.entity_instance] = None,
profile: Optional[ifcopenshell.entity_instance] = None, profile: Optional[ifcopenshell.entity_instance] = None,
name: Optional[str] = None,
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
"""Add a new profile item to a profile set """Add a new profile item to a profile set
@@ -48,14 +49,12 @@ def add_profile(
profile set represents a group of profile items. See profile set represents a group of profile items. See
ifcopenshell.api.material.add_material_set for more information on ifcopenshell.api.material.add_material_set for more information on
how to add a profile set. how to add a profile set.
:type profile_set: ifcopenshell.entity_instance
:param material: The IfcMaterial that the profile item is made out of. :param material: The IfcMaterial that the profile item is made out of.
:type material: ifcopenshell.entity_instance, optional
:param profile: The IfcProfileDef that represents the 2D cross section :param profile: The IfcProfileDef that represents the 2D cross section
of the the profile item. of the the profile item.
:type profile: ifcopenshell.entity_instance, optional :param name: An optional name of the material profile (not the geometric
profile).
:return: The newly created IfcMaterialProfile :return: The newly created IfcMaterialProfile
:rtype: ifcopenshell.entity_instance
Example: Example:
@@ -90,14 +89,12 @@ def add_profile(
# Great! Let's assign our material set to our beam type. # Great! Let's assign our material set to our beam type.
ifcopenshell.api.material.assign_material(model, products=[beam_type], material=material_set) ifcopenshell.api.material.assign_material(model, products=[beam_type], material=material_set)
""" """
settings = {"profile_set": profile_set, "material": material, "profile": profile} profiles = list(profile_set.MaterialProfiles or [])
mat_profile = file.create_entity("IfcMaterialProfile", Name=name)
profiles = list(settings["profile_set"].MaterialProfiles or []) if material:
profile = file.create_entity("IfcMaterialProfile") mat_profile.Material = material
if settings["material"]: if profile:
profile.Material = settings["material"] mat_profile.Profile = profile
if settings["profile"]: profiles.append(mat_profile)
profile.Profile = settings["profile"] profile_set.MaterialProfiles = profiles
profiles.append(profile) return mat_profile
settings["profile_set"].MaterialProfiles = profiles
return profile