Search code examples
python-2.7for-looprangeifc

For loop over a range (Python 2.7 + Ifc)


I am quite new to programming, and this is one of my first questions on this site. I've been learning Pyhon for over three weeks now.

For a project I use a module called IfcOpenShell which allow for easy retrieving the data specified in an ifc file. An Ifc file is an express based language to describe building data.

Now I want to retrieve all the relevant material data fit for lighting analyses from an ifc file and convert them to a Radiance format. Radiance is open source lighting analyses software

So far I have written this script:

import ifcopenshell

ifc_file = ifcopenshell.open('Example_A.ifc')

materials = ifc_file.by_type('IfcMaterial')

print 'There are ' + str(len(materials)) + ' different types of  materials in the file:' + '\n'

for x in range(0):
    materials = ifc_file.by_type('IfcMaterial')[x] 
    colour = ifc_file.by_type('IfcColourRgb')[x]
    styles = ifc_file.by_type('IfcStyledItem')[x]
    surfacestyles = ifc_file.by_type('IfcSurfaceStyleRendering')[x]

#DECLARING THE VARIABLES FOR RADIANCE DEFINTION
rad_material = materials.Name
rad_red = colour.Red
rad_green = colour.Green
rad_blue = colour.Blue
rad_specular = surfacestyles.SpecularColour
rad_roughness = 0.1 #<- Roughness is not defined in IFC, the value of 0.1 is  merely put there as an example


print 'RETRIEVES DATA FROM IFC FILE PRINTS THEM ACCORDING TO A RADIANCE READABLE DEFINITION'
print ''

print '#material name: ' + rad_material
print '#material type: ' + 'No definition found in the specified ifc file'
print 'void ' + ' plastic ' + rad_material
print '0'
print '0'
print rad_red , rad_green, rad_blue,rad_specular[0], rad_roughness

I found there were exactly 100 different materials specified in the ifc file. And when I run this code, it outputs :

There are 100 different types of  materials in the file:

RETRIEVES DATA FROM IFC FILE PRINTS THEM ACCORDING TO A RADIANCE READABLE DEFINITION

#material name: SH_resin Floor
#material type: No definition found in the specified ifc file
void  plastic SH_resin Floor
0
0
1.0 1.0 1.0 0.5 0.1

This is exactly what I wanted to do, however, when I change:

for x in range(0):

to

for x in range(0,100)

it only outputs the last material of the range:

There are 100 different types of  materials in the file:

RETRIEVES DATA FROM IFC FILE PRINTS THEM ACCORDING TO A RADIANCE READABLE DEFINITION

#material name: Juice
#material type: No definition found in the specified ifc file
void  plastic Juice
0
0
0.956862745098 0.956862745098 0.956862745098 0.5 0.1

I do not understand where I'm making a mistake, or if I'm using the right functions for what I want. My intent was to output all the 100 different material definitions


Solution

  • The indentation shows which lines is part of the for loop. If you want the print statements to happen 100 times, you have to indent them as well.

    import ifcopenshell
    
    ifc_file = ifcopenshell.open('Example_A.ifc')
    
    materials = ifc_file.by_type('IfcMaterial')
    
    print 'There are ' + str(len(materials)) + ' different types of  materials in the file:' + '\n'
    
    for x in range(0):
        materials = ifc_file.by_type('IfcMaterial')[x] 
        colour = ifc_file.by_type('IfcColourRgb')[x]
        styles = ifc_file.by_type('IfcStyledItem')[x]
        surfacestyles = ifc_file.by_type('IfcSurfaceStyleRendering')[x]
    
        #DECLARING THE VARIABLES FOR RADIANCE DEFINTION
        rad_material = materials.Name
        rad_red = colour.Red
        rad_green = colour.Green
        rad_blue = colour.Blue
        rad_specular = surfacestyles.SpecularColour
        rad_roughness = 0.1 #<- Roughness is not defined in IFC, the value of 0.1 is  merely put there as an example
    
    
        print 'RETRIEVES DATA FROM IFC FILE PRINTS THEM ACCORDING TO A RADIANCE READABLE DEFINITION'
        print ''
    
        print '#material name: ' + rad_material
        print '#material type: ' + 'No definition found in the specified ifc file'
        print 'void ' + ' plastic ' + rad_material
        print '0'
        print '0'
        print rad_red , rad_green, rad_blue,rad_specular[0], rad_roughness