-
Notifications
You must be signed in to change notification settings - Fork 16
05: exporting and importing information
jakob-beetz edited this page Dec 21, 2016
·
1 revision
Python ships with a large library of handy modules, including the creation of CSV files, which is about the most simple structured file format possible and can easily opened in spreadsheet applications such as OpenOffice Calc or MS Excel.
To achieve this, we need to import the CSV module from the Python standard library, which allows us to read and write CSVs.
###########################################################################
# A simple script that iterates over all IfcPropertySets of the currently #
# selected object and writes them to a CSV file #
###########################################################################
import csv
sel_set = viewer.get_selection_set(model)
#get the IfcProduct that is stored in the global variable 'selection'
with open(r'D:\test1.csv', 'wb') as testfile:
for p in sel_set:
csv_writer = csv.writer(testfile, delimiter=';')
for relDefinesByProperties in p.IsDefinedBy:
print("[{0}]".format(relDefinesByProperties.RelatingPropertyDefinition.Name))
for prop in relDefinesByProperties.RelatingPropertyDefinition.HasProperties:
print ("{:<20} :{}".format(prop.Name,prop.NominalValue.wrappedValue))
print ("\n")
csv_writer.writerow([p.GlobalId, p.Name, relDefinesByProperties.RelatingPropertyDefinition.Name,prop.Name,prop.NominalValue.wrappedValue])
Similarly to the above example to export information, we can quickly read information from external files (again, CSV to the rescue) and process it within our program. We can e.g. write a simple model checker, to check the minimum heights of all doors etc.
###########################################################################
# Minimalistic Model checker script
# A simple to use external information defined in e.g. Excel to check
# values in an IFC file. Create a minimal CSV like:
#
# Element;Attribute;Min_height
# IfcDoor;OverallHeight;2.1
# IfcWindow;OverallHeight;1
#
# and save it to a convient location
###########################################################################
import csv
#open the file
with open(r'D:\simple_check.csv') as checkfile:
# create a reader, that reads the columns into a dictionary
# using the first row as keys ("Element", "Attribute", "Min_height"
reader = csv.DictReader(checkfile,delimiter=";")
# iterate over all rows
for row in reader:
print(row['Element'], row["Attribute"],row['Min_height'])
# get all elements
for element in model.by_type(row["Element"]):
attribute_value=element[row["Attribute"]]
if float(attribute_value) < float(row['Min_height']):
print ("Element {} violates the minimum height requirement with {}".format(element.GlobalId,attribute_value))