ifcopenshell.api.style.assign_material_style

Module Contents

class ifcopenshell.api.style.assign_material_style.Usecase
create_new_definition_representation()
create_styled_item(reuse_item=None)
create_styled_representation()
execute()
get_styled_representation(definition_representation)
has_proposed_style(styled_item: ifcopenshell.entity_instance) bool
has_same_style_type(styled_item: ifcopenshell.entity_instance) bool
modify_existing_definition_representation()
ifcopenshell.api.style.assign_material_style.assign_material_style(file: ifcopenshell.file, material: ifcopenshell.entity_instance, style: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance, should_use_presentation_style_assignment: bool = False) None

Assigns a style to a material

A style may either be assigned directly to an object’s representation, or to a material which is then associated with the object. If both exist, then the style assigned directly to the object’s representation takes precedence. It is recommended to use materials and assign styles to materials. This API function provides that capability.

Parameters:
  • material (ifcopenshell.entity_instance) – The IfcMaterial which you want to assign the style to.

  • style (ifcopenshell.entity_instance) – The IfcPresentationStyle (typically IfcSurfaceStyle) that you want to assign to the material. This will then be applied to all objects that have that material.

  • context (ifcopenshell.entity_instance) – The IfcGeometricRepresentationSubContext at which this style should be used. Typically this is the Model BODY context.

  • should_use_presentation_style_assignment (bool) – This is a technical detail to accomodate a bug in Revit. This should always be left as the default of False, unless you are finding that colours aren’t showing up in Revit. In that case, set it to True, but keep in mind that this is no longer a valid IFC. Blame Autodesk.

Returns:

None

Return type:

None

Example:

# A model context is needed to store 3D geometry
model3d = ifcopenshell.api.context.add_context(model, context_type="Model")

# Specifically, we want to store body geometry
body = ifcopenshell.api.context.add_context(model,
    context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model3d)

# Let's create a new wall. The wall does not have any geometry yet.
wall = ifcopenshell.api.root.create_entity(model, ifc_class="IfcWall")

# Let's use the "3D Body" representation we created earlier to add a
# new wall-like body geometry, 5 meters long, 3 meters high, and
# 200mm thick
representation = ifcopenshell.api.geometry.add_wall_representation(model,
    context=body, length=5, height=3, thickness=0.2)

# Assign our new body geometry back to our wall
ifcopenshell.api.geometry.assign_representation(model,
    product=wall, representation=representation)

# Place our wall at the origin
ifcopenshell.api.geometry.edit_object_placement(model, product=wall)

# Let's prepare a concrete material. Note that our concrete material
# does not have any colours (styles) at this point.
concrete = ifcopenshell.api.material.add_material(model, name="CON01", category="concrete")

# Assign our concrete material to our wall
ifcopenshell.api.material.assign_material(model,
    products=[wall], type="IfcMaterial", material=concrete)

# Create a new surface style
style = ifcopenshell.api.style.add_style(model)

# Create a simple grey shading colour and transparency.
ifcopenshell.api.style.add_surface_style(model,
    style=style, ifc_class="IfcSurfaceStyleShading", attributes={
        "SurfaceColour": { "Name": None, "Red": 0.5, "Green": 0.5, "Blue": 0.5 },
        "Transparency": 0., # 0 is opaque, 1 is transparent
    })

# Now any element (like our wall) with a concrete material will have
# a grey colour applied.
ifcopenshell.api.style.assign_material_style(model, material=concrete, style=style, context=body)