RevitPythonShell: Nathan's Sketchbook

Floor Rebuilder

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, 'create some levels and floors')
 
t.Start()
 
#CREATE SOME LEVELS FROM CSV FILE ----------------------------------------------------------------------
 
#set the file path
filepathA = 'C:/Users/Nathan Miller/Documents/## CASE/Interoperability/Rebuild Floors/LevelLocation.csv'
 
#create the reader
filereaderA = System.IO.StreamReader(filepathA)
v = 0
 
levelInc = 1
#do the following for each line until end of file.
while filereaderA.Peek() > -1:
    levelName = 'Level ' + str(levelInc)
 
    #read line
    line = filereaderA.ReadLine()
 
    levelElev = int(line)
 
    #create level
    level = doc.Create.NewLevel(levelElev)
    level.Name = levelName
    levelInc = levelInc + 1
 
filereaderA.Close()
 
#CREATE SOME FLOORS FROM CSV FILE -------------------------------------------------------------------------
 
#set the file path
filepathB = 'C:/Users/Nathan Miller/Documents/## CASE/Interoperability/Rebuild Floors/FloorEdge.csv'
 
#create the reader
filereaderB = System.IO.StreamReader(filepathB)
points = []
 
#set floor normal
normal = XYZ.BasisZ
 
#set floor type
floorTypes = doc.FloorTypes
iter = floorTypes.ForwardIterator()
while iter.MoveNext():
    floorType = iter.Current
 
#get level elements
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_Levels)
collector.OfClass(Level)
 
levelIter = collector.GetElementIterator()
levelIter.Reset()
 
#draw floors on levels
for item in levelIter:
 
    del points[:]
 
    level = item
    profile = CurveArray()
 
    #read a line
    line = filereaderB.ReadLine()
 
    #split the string where there is a ","
    strArr = line.Split(";")
    u = len(strArr)
 
    #fill points array    
    for i in range(0,u):
        ptString = strArr[i]
 
        ptArr = ptString.Split(",")
        x = float(ptArr[0])
        y = float(ptArr[1])
        z = float(ptArr[2])
        points.append(XYZ(x,y,z))
 
    #draw profile
    for j in range(0,u-1):
        pA = points[j]
        pB = points[j+1]
        profile.Append(app.Create.NewLineBound(pA,pB))
 
    floor = doc.Create.NewFloor(profile, floorType, level, True, normal)
 
filereaderB.Close()
 
t.Commit()
 
__window__.Close()

Adaptive Component Class

import clr
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
#A CUSTOM CLASS FOR PLACING ADAPTIVE COMPONENTS ----------------------------------
 
class AdaptiveComponent:
 
    #method for retrieving a family symbol
    def FamilySymbol(self, symbName):
        collector = FilteredElementCollector(doc)
        collector.OfCategory(BuiltInCategory.OST_GenericModel)
        collector.OfClass(FamilySymbol)
 
        famTypeIter = collector.GetElementIdIterator()
        famTypeIter.Reset()
 
        #Search Family Symbols in Document
        for item in famTypeIter:
            famTypeID = item
            famSymb = doc.get_Element(famTypeID)
 
            if famSymb.Family.Name == symbName:
                return famSymb        
 
    #method for positioning an adaptive point 
    def PositionPoint(self, adaptivePoints, pointIndex, xyzDestination):
        adaptivePoint = adaptivePoints[pointIndex]
        refPt = doc.get_Element(adaptivePoint)
        trans = xyzDestination.Subtract(refPt.Position)
        ElementTransformUtils.MoveElement(doc, adaptivePoint, trans)    
 
    #method for placing an adaptive component with two points 
    def TwoPointComponent(self, name, start, end):
        famSymb = self.FamilySymbol(name)
        adaptComp = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, famSymb)
        adptPoints = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(adaptComp)
 
        #position adaptive points
        self.PositionPoint(adptPoints, 0, start)
        self.PositionPoint(adptPoints, 1, end)
 
        #return the adaptive component
        return adaptComp
 
    #method for placing an adaptive component with three points    
    def ThreePointComponent(self, name, xyz1, xyz2, xyz3):
        famSymb = self.FamilySymbol(name)
        adaptComp = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, famSymb)
        adptPoints = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(adaptComp)
 
        #position adaptive points
        self.PositionPoint(adptPoints, 0, xyz1)
        self.PositionPoint(adptPoints, 1, xyz2)
        self.PositionPoint(adptPoints, 2, xyz3)
 
        #return the adaptive component
        return adaptComp
 
    #method for placing an adaptive component with four points
    def FourPointComponent(self, name, xyz1, xyz2, xyz3, xyz4):
        famSymb = self.FamilySymbol(name)
        adaptComp = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, famSymb)
        adptPoints = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(adaptComp)
 
        #position adaptive points
        self.PositionPoint(adptPoints, 0, xyz1)
        self.PositionPoint(adptPoints, 1, xyz2)
        self.PositionPoint(adptPoints, 2, xyz3)
        self.PositionPoint(adptPoints, 3, xyz4)
 
        #return the adaptive component
        return adaptComp
 
        pass
 
#MAIN CODE -------------------------------------------------------------------------
 
def main():
    t = Transaction(doc, 'Place an Adaptive Component.')
 
    t.Start()
 
    #Accessing the Excel applications.
    xlApp = System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')
 
    #Worksheet, Row, and Column parameters
    worksheet = 1
    rowStart = 2
    rowEnd = 945
 
    #Using a loop to read a range of values and print them to the console.
    for i in range(rowStart, rowEnd):
        #Worksheet object specifying the cell location to read.
        c1 = xlApp.Worksheets(worksheet).Cells(i, 1).Text
        c2 = xlApp.Worksheets(worksheet).Cells(i, 2).Text
        c3 = xlApp.Worksheets(worksheet).Cells(i, 3).Text
        c4 = xlApp.Worksheets(worksheet).Cells(i, 4).Text
 
        pt1 = c1.Split(",")
        pt2 = c2.Split(",")
        pt3 = c3.Split(",")
        pt4 = c4.Split(",")
 
        #place 2 point adaptive component
        adptName = 'Family2'
        pt1 = XYZ(float(pt1[0]),float(pt1[1]),float(pt1[2]))
        pt2 = XYZ(float(pt2[0]),float(pt2[1]),float(pt2[2]))
        pt3 = XYZ(float(pt3[0]),float(pt3[1]),float(pt3[2]))
        pt4 = XYZ(float(pt4[0]),float(pt4[1]),float(pt4[2]))
 
        adpt = AdaptiveComponent()
        adpt.FourPointComponent(adptName, pt1, pt2, pt3, pt4)
 
    t.Commit()
    __window__.Close()
 
if __name__=="__main__":
    main()

Structural Pipe from Excel

Generate Adaptive Component Pipes

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, 'Place an Adaptive Component.')
 
t.Start()
 
#Family symbol name to place.
symbName = 'StructuralPipe'
 
#create a filtered element collector set to Category OST_Mass and Class FamilySymbol 
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_GenericModel)
collector.OfClass(FamilySymbol)
 
famtypeitr = collector.GetElementIdIterator()
famtypeitr.Reset()
 
#Search Family Symbols in document.
for item in famtypeitr:
    famtypeID = item
    famsymb = doc.get_Element(famtypeID)
 
    #If the FamilySymbol is the name we are looking for, create a new instance.
    if famsymb.Family.Name == symbName:
 
        xlApp = System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')
 
        #do the following for each line until end of file.
        for i in range(2,1861):
 
            ptStartData = xlApp.Worksheets(1).Cells(i, 2).Text
            ptEndData = xlApp.Worksheets(1).Cells(i, 3).Text
 
            #split the string where there is a ","
            ptArr1 = ptStartData.Split(",")
            ptArr2 = ptEndData.Split(",")
 
            #assign string segements to variables
            pt1X = float(ptArr1[0])
            pt1Y = float(ptArr1[1])
            pt1Z = float(ptArr1[2])
 
            pt2X = float(ptArr2[0])
            pt2Y = float(ptArr2[1])
            pt2Z = float(ptArr2[2])
 
            adaptComp = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, famsymb)
            adptPoints = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(adaptComp)
 
            #Starting adaptive point locations.  get_Element returns a Reference Point
            aPt1 = doc.get_Element(adptPoints[0])
            aPt2 = doc.get_Element(adptPoints[1])
 
            #Desired Adaptive Point Locations
            loc1 = XYZ(pt1X,pt1Y,pt1Z)
            loc2 = XYZ(pt2X,pt2Y,pt2Z) 
 
            #Some vector math to get the translation for MoveElement()
            trans1 = loc1.Subtract(aPt1.Position)
            trans2 = loc2.Subtract(aPt2.Position)
 
            #Position Adaptive Component using MoveElement()
            ElementTransformUtils.MoveElement(doc, adptPoints[0], trans1)
            ElementTransformUtils.MoveElement(doc, adptPoints[1], trans2)
 
t.Commit()
 
__window__.Close()

Modify Pipe Size Parameters

#Modify parameters
instName = 'StructuralPipe'
 
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.GenericModel)
collector.OfClass(FamilyInstance)
 
famtypeitr = collector.GetElementIdIterator()
famtypeitr.Reset()
 
i = 2
 
for item in famtypeitr:
    famtypeID = item
    faminst = doc.get_Element(famtypeID)
 
    if faminst.Name == instName:
        xlApp = System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')
 
        sizeData = xlApp.Worksheets(1).Cells(i, 4).Text
 
        param = faminst.get_Parameter('radius')
        param.Set(2*inc)
 
        i = i + 1

Randomize Curtain Panel Type

import clr
import System
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
t = Transaction(doc, 'Set multiple family instances.')
 
t.Start()
 
symcollector = FilteredElementCollector(doc)
symcollector.OfCategory(BuiltInCategory.OST_CurtainWallPanels)
symcollector.OfClass(FamilySymbol)
 
typeitr = symcollector.GetElementIdIterator()
typeitr.Reset()
 
types = []
 
for item in typeitr:
    types.append(item)
 
print types
 
instName = 'HexPanel-B'
 
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_CurtainWallPanels)
collector.OfClass(FamilyInstance)
 
famtypeitr = collector.GetElementIdIterator()
famtypeitr.Reset()
 
panels = collector.ToElements
 
seed = 4
rand = System.Random(seed)
r1 = 0
r2 = 1
 
p1 = 0.1
 
for item in famtypeitr:
    famtypeID = item
    faminst = doc.get_Element(famtypeID)
    #print faminst
 
    if faminst.Name == instName:
        randVal = rand.NextDouble()
        #print randVal
 
        if randVal > 0.5:
            faminst.ChangeTypeId(types[0])
        else:
            faminst.ChangeTypeId(types[1])
 
t.Commit()
 
#__window__.Close()

Place Adaptive Component using Text File

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, 'Place an Adaptive Component.')
 
t.Start()
 
#Family symbol name to place.
symbName = 'AdaptiveComponentTest'
 
#create a filtered element collector set to Category OST_Mass and Class FamilySymbol 
collector = FilteredElementCollector(doc)
collector.OfCategory(BuiltInCategory.OST_GenericModel)
collector.OfClass(FamilySymbol)
 
famtypeitr = collector.GetElementIdIterator()
famtypeitr.Reset()
 
#Search Family Symbols in document.
for item in famtypeitr:
    famtypeID = item
    famsymb = doc.get_Element(famtypeID)
 
    #If the FamilySymbol is the name we are looking for, create a new instance.
    if famsymb.Family.Name == symbName:
 
        #set the file path
        filepath = 'C:/Users/nmiller/Documents/## Computation/## Vasari/Tutorials/TestAdaptiveShape.txt'
 
        #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
            pt1X = float(strArr[0])
            pt1Y = float(strArr[1])
            pt1Z = float(strArr[2])
 
            pt2X = float(strArr[3])
            pt2Y = float(strArr[4])
            pt2Z = float(strArr[5])
 
            pt3X = float(strArr[6])
            pt3Y = float(strArr[7])
            pt3Z = float(strArr[8])
 
            pt4X = float(strArr[9])
            pt4Y = float(strArr[10])
            pt4Z = float(strArr[11])
 
            adaptComp = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(doc, famsymb)
            adptPoints = AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(adaptComp)
 
            #Starting adaptive point locations.  get_Element returns a Reference Point
            aPt1 = doc.get_Element(adptPoints[0])
            aPt2 = doc.get_Element(adptPoints[1])
            aPt3 = doc.get_Element(adptPoints[2])
            aPt4 = doc.get_Element(adptPoints[3])
 
            #Desired Adaptive Point Locations
            loc1 = XYZ(pt1X,pt1Y,pt1Z)
            loc2 = XYZ(pt2X,pt2Y,pt2Z) 
            loc3 = XYZ(pt3X,pt3Y,pt3Z)
            loc4 = XYZ(pt4X,pt4Y,pt4Z)
 
            #print loc1
            #print loc2
            #print loc3
            #print loc4
 
            #Some vector math to get the translation for MoveElement()
            trans1 = loc1.Subtract(aPt1.Position)
            trans2 = loc2.Subtract(aPt2.Position)
            trans3 = loc3.Subtract(aPt3.Position)
            trans4 = loc4.Subtract(aPt4.Position)
 
            #Position Adaptive Component using MoveElement()
            ElementTransformUtils.MoveElement(doc, adptPoints[0], trans1)
            ElementTransformUtils.MoveElement(doc, adptPoints[1], trans2)
            ElementTransformUtils.MoveElement(doc, adptPoints[2], trans3)
            ElementTransformUtils.MoveElement(doc, adptPoints[3], trans4)
 
t.Commit()
 
__window__.Close()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License