用于循环范围(Python 2.7 + Ifc)

时间:2015-01-22 21:27:48

标签: python-2.7 for-loop range ifc

我对编程很陌生,这是我在本网站上的第一个问题之一。我现在已经学习了Pyhon超过三个星期了。

对于一个项目,我使用一个名为IfcOpenShell的模块,它允许轻松检索ifc文件中指定的数据。 Ifc文件是一种基于表达的语言,用于描述建筑数据。

现在,我想从ifc文件中检索适合照明分析的所有相关材料数据,并将它们转换为Radiance格式。 Radiance是开源照明分析软件

到目前为止,我已经编写了这个脚本:

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

我发现ifc文件中指定了100种不同的材料。当我运行此代码时,它会输出:

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

这正是我想做的事情,但是,当我改变时:

for x in range(0):

for x in range(0,100)

它只输出范围的最后一个材料:

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

我不明白我在哪里犯了错误,或者我是否正在使用正确的功能。我的目的是输出所有100种不同的材料定义

1 个答案:

答案 0 :(得分:0)

缩进显示哪些行是for循环的一部分。 如果你想让print语句发生100次,你也必须缩进它们。

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