RevitPythonShell: Using Text Files

<<< Return to Nathan's Revit API Notebook

Introduction

This page contains methods for using external data with the Revit/Vasari API. With these capabilities, it is possible to establish workflows with other design systems by sharing parameters and native geometry reconstruction.

Text Files (CSV)

Text files can be read using conventional .NET methods within IronPython. A common text format used to share data is the comma separated value file (*.csv). CSV uses a simple structure where columns of data are defined by a comma "," and rows are read by line.

Reading

Stream Reader

Reading text files requires a .NET type known as a StreamReader(). A path is supplied which will point the program to the file for reading.

#set the file path
filepath = 'C:/Users/nmiller/Documents/sometext.csv'
 
#create the reader
filereader = System.IO.StreamReader(filepath)
Reading Lines

We can use a while loop to read each line in the text file.

while filereader.Peek() > -1:
 
    #Read the current line
    line = filereader.ReadLine()
 
    #Print the line to the console.
    print line
Parsing CSV

A CSV file can be parsed by splitting each line where a "," exists. This will create an array of values.

In this case, a string is split which results in an array of 3 values. Each value is then assigned to a string variable.

    #split the string where there is a ","
    strArr = line.Split(",")
 
    #assign string segements to variables
    var1 = strArr[0]
    var2 = strArr[1]
    var3 = strArr[2]
Summary

The final code looks like this…

import clr
import System
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
t = Transaction(doc, 'Read a text file.')
 
t.Start()
 
#set the file path
filepath = 'C:/Users/nmiller/Documents/sometext.csv'
 
#create the reader
filereader = System.IO.StreamReader(filepath)
 
#do the following for each line until end of file.
while filereader.Peek() > -1:
 
    #read a line
    line = filereader.ReadLine()
 
    #split the string where there is a ","
    strArr = line.Split(",")
 
    #assign string segements to variables
    var1 = strArr[0]
    var2 = strArr[1]
    var3 = strArr[2]
 
    #printing variables to the console
    print var1
    print var2
    print var3
 
t.Commit()

Writing

Files can be created and written using the .NET StreamWriter() class. This process is useful for getting parameters and saving them to an external location.

Stream Writer

Stream Writer lets us create a file in a directory location.

#File path
filepath = 'C:/Users/nmiller/Documents/sometext.txt'
 
#Create the file
file = System.IO.StreamWriter(filepath)
Writing Lines

The following will write some lines to the open Stream Writer. Two lines will be in the file 'hello' and 'world'. We can then close the file with .Close()

#Write some things to the file
file.WriteLine('hello')
file.WriteLine('world')
 
#Close the StreamWriter
file.Close()
Checking Existing Files

In some cases, you may need to check if the file you are creating already exists. The following will check for the existence of the file and delete it.

#Delete the file if it exists.
if (System.IO.File.Exists(filepath) == True):
    System.IO.File.Delete(filepath)
Summary

All together, your code may be structured like this…

import clr
import System
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
t = Transaction(doc, 'Write an external file.')
 
t.Start()
 
#Set the file path
filepath = 'C:/Users/nmiller/Documents/sometext.txt'
 
#Delete the file if it exists.
if (System.IO.File.Exists(filepath) == True):
    System.IO.File.Delete(filepath)
 
#Create the file
file = System.IO.StreamWriter(filepath)
 
#Write some things to the file
file.WriteLine('hello')
file.WriteLine('world')
 
#Close the StreamWriter
file.Close()
 
t.Commit()
 
__window__.Close()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License