Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, December 31, 2013

Revit Python Script Clean useless Reference Planes


It is always difficult to clean reference planes in a file that has been touched by many people. They are everywhere. One rule is to name the one you want to keep. and the unnamed ones can be removed to decease file size or visual discomfort. See script below:
 
#this revit 2013 python is to delete all useless sections and
#unnamed reference planes
import clr

import math
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *
from Autodesk.Revit.UI.Selection import *
from Autodesk.Revit.UI import *
from System import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
#selection = list(__revit__.ActiveUIDocument.Selection.Elements)
uidoc = __revit__.ActiveUIDocument
selection = __revit__.ActiveUIDocument.Selection.Elements


#methods
def cleanReferencePlane():
    # grab all reference planes
    rpInstancesFilter = ElementClassFilter(clr.GetClrType(ReferencePlane))
    collector = FilteredElementCollector(doc)
    rPlaneId = collector.WherePasses(rpInstancesFilter).ToElementIds()
    # go through ids delete unnamed ones.
    for id in rPlaneId:
        s = doc.get_Element(id).Name
        if s.Equals("Reference Plane", StringComparison.Ordinal):
            doc.Delete(id)
def cleanSection():
    print ("to be done...")
#define a transaction variable and describe the transaction
t = Transaction(doc,'Clean Section ReferencePlane')

#start a transaction in the Revit database
t.Start()

#************ ADD YOUR OWN CODES HERE.******************
#get all filled regions

y=int(raw_input("clean sections and reference planes: 1.clean both 2.clean sections 3. clean reference planes: "))
if y==1:
    print "clean sections and reference planes..."
    cleanReferencePlane()
    cleanSection()
elif y==2:
    print "clean sections only..."
    cleanSection()
elif y==3:
    print "clean reference plane only..."
    cleanReferencePlane()
   


#print "end"
   

#commit the transaction to the Revit database
t.Commit()

#close the script window
raw_input("Enter to Quit.....")
#__window__.Hide()
__window__.Close()
#__window__.Show()

Saturday, March 9, 2013

Revit Python Script Code Example - automatic rename view number in a sheet

hi, I rewrote the code in python script for revit 2012/2013, see how it works in video



the code is below, copy and save the code in a file named "sortSheetViews.py" or whatever. then follow the video to add this script to revit ribbon

import clr
import math
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *
from Autodesk.Revit.UI.Selection import *
from Autodesk.Revit.UI import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
#selection = list(__revit__.ActiveUIDocument.Selection.Elements)
uidoc = __revit__.ActiveUIDocument
#selection = __revit__.ActiveUIDocument.Selection.Elements
selelementsid=[]
#############################method###################################
#get views in a list
def viewSelectInList():
    sel = uidoc.Selection
    sel.Elements.Clear()
    loopSwitch = 1
    while loopSwitch==1:

        pickedOne = sel.PickObject(ObjectType.Element,"Pick view one by one, pick title block to finish")
        if pickedOne!= None :
            #revit 2013 use the statement below
            e = doc.GetElement(pickedOne.ElementId)
            #revit 2012 use the statement below
            #e = doc.GetElement(pickedOne)
#            TaskDialog.Show("Revit", e.Category.Name)
            if e.Category.Name.Equals("Title Blocks"):
#                TaskDialog.Show("Revit", e.Category.Name)
                loopSwitch = 0
            else:
                selelementsid.append(e.Id)
#                TaskDialog.Show("Revit", e.Category.Name)
        else:
            break
def reNumber (startInt):
    currentNo = startInt
    for eId in selelementsid:
        setViewPortNumber(eId,currentNo)
        currentNo += 1
def getViewByDetailNumber (vs,i):
    for v in vs.Views:
        if v.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsString() != None:
            if i == int(v.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsString()):
                return v
       
def setViewPortNumber (id, number):
    vs = doc.ActiveView
    stringOldNumber=doc.get_Element(id).get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).AsString()
#    print "viewport detail number: ",stringOldNumber
    if int(stringOldNumber) != number:
        if getViewByDetailNumber(vs, number) != None:
            #*****************to be finished here *****************
            getViewByDetailNumber(vs, number).get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set("999")
            doc.get_Element(id).get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set(str(number))
            getViewByDetailNumber(vs, 999).get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set(stringOldNumber)
        else:
            doc.get_Element(id).get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set(str(number))
#define a transaction variable and describe the transaction
t = Transaction(doc,'Sort Views in Sheet')

#start a transaction in the Revit database
t.Start()

#************ ADD YOUR OWN CODES HERE.******************
x = int(raw_input ("number start from: "))
viewSelectInList()
reNumber ( x )


#print "finished"
   
#************** END OF YOUR MAJOR CODES HERE.*********************
#commit the transaction to the Revit database
t.Commit()

#close the script window
__window__.Close()

the above video teachs how to add such scripts to revit ribbon