Styles not working

Hi,

So, I have been working on a project. I want to create an IfcOpenShell code that changes the color of elements that contain pre-established criteria.
I tried to assign styles to the elements, but they aren't changing color when I open the file and am confused about it. Could it be because some of the elements I am trying to change already have a material assigned to it? Do styles override materials? Could someone help me with the code?
I will paste it here in case anyone wants to read what I've done so far:

#### Model path:
model = ifcopenshell.open("file_path")

#### Creating the color styles:
waste = ifcopenshell.api.run("style.add_style", model, name = 'Waste')
reclaim = ifcopenshell.api.run("style.add_style", model, name = 'Reclaim')
recycle = ifcopenshell.api.run("style.add_style", model, name = 'Recycle')
reuse = ifcopenshell.api.run("style.add_style", model, name = 'Reuse')

#### Giving the styles a surface color:
ifcopenshell.api.run("style.add_surface_style", model, style=waste, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 1, "Green": 0, "Blue": 0 }})
ifcopenshell.api.run("style.add_surface_style", model, style=reclaim, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 0, "Green": 0, "Blue": 1 }})
ifcopenshell.api.run("style.add_surface_style", model, style=recycle, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 1, "Green": 1, "Blue": 0 }})
ifcopenshell.api.run("style.add_surface_style", model, style=reuse, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 0, "Green": 1, "Blue": 0 }})


#### Assigning colors:
for i in model.by_type('IfcElement'):

    #### Filtering elements that have a DfR - Pset
    if 'DfR - Pset' in Element.get_psets(i):

        #### Filtering elements that have the attribute EoL assigned
        if 'Material' in Element.get_psets(i)['DfR - Pset']:
            representation = i.Representation

            #### Assigning the colors:
            if 'Material' == 'Waste':
                ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[waste])
            elif 'Material' == 'Reclaim':
                ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[reclaim])
            elif 'Material' == 'Recycle':
                ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[recycle])
            elif 'Material' == 'Reuse':
                ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[reuse])

model.write('file_path')

Comments

  • There might be other problems, but comparing a string to string, will always be false.
    if 'Material' == 'Waste':
    I'm a noobie.

  • @theoryshaw said:
    There might be other problems, but comparing a string to string, will always be false.
    if 'Material' == 'Waste':
    I'm a noobie.

    That's right. I fixed it. The 'Material' is supposed to be an attribute inside 'DfR - Pset'. But it is still not working for some reason. The code runs without error, the styles were "assigned" but the elements' color did not change.

    The updated code is:

    #### Model path:
    model = ifcopenshell.open("file_path")
    
    #### Creating the color styles:
    waste = ifcopenshell.api.run("style.add_style", model, name = 'Waste')
    reclaim = ifcopenshell.api.run("style.add_style", model, name = 'Reclaim')
    recycle = ifcopenshell.api.run("style.add_style", model, name = 'Recycle')
    reuse = ifcopenshell.api.run("style.add_style", model, name = 'Reuse')
    
    #### Giving the styles a surface color:
    ifcopenshell.api.run("style.add_surface_style", model, style=waste, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 1, "Green": 0, "Blue": 0 }})
    ifcopenshell.api.run("style.add_surface_style", model, style=reclaim, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 0, "Green": 0, "Blue": 1 }})
    ifcopenshell.api.run("style.add_surface_style", model, style=recycle, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 1, "Green": 1, "Blue": 0 }})
    ifcopenshell.api.run("style.add_surface_style", model, style=reuse, ifc_class="IfcSurfaceStyleShading", attributes={"SurfaceColour":{ "Name": None, "Red": 0, "Green": 1, "Blue": 0 }})
    
    
    #### Assigning colors:
    for i in model.by_type('IfcElement'):
    
        #### Filtering elements that have a DfR - Pset
        if 'DfR - Pset' in Element.get_psets(i):
    
            #### Filtering elements that have the attribute EoL assigned
            if 'Material' in Element.get_psets(i)['DfR - Pset']:
                representation = i.Representation
    
                #### Assigning the colors:
                if Element.get_psets(i)['DfR - Pset']['Material'] == 'Waste':
                    ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[waste])
                elif Element.get_psets(i)['DfR - Pset']['Material'] == 'Reclaim':
                    ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[reclaim])
                elif Element.get_psets(i)['DfR - Pset']['Material'] == 'Recycle':
                    ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[recycle])
                elif Element.get_psets(i)['DfR - Pset']['Material'] == 'Reuse':
                    ifcopenshell.api.run("style.assign_representation_styles", model, shape_representation=representation, styles=[reuse])
    
    model.write('file_path')
    
    bruno_perdigao
  • @guieliote I tryed your script and it worked when I opened with "Open IFC Viewer", but not with BlenderBIM. I don't know how to solve, but I guess the problem is related to your objects having another material assigned.

  • Hmm I see. Well, thanks Bruno, I will try to see if it also works on BIMvision!

Sign In or Register to comment.