Showing posts with label api. Show all posts
Showing posts with label api. 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()

Sunday, December 16, 2012

HOW TO INSTALL / COPY REVIT APPLICATION MACRO

Revit application macro modules usually saved in:
C:\ProgramData\Autodesk\Revit\Macros\2012\Architecture\VstaMacros\AppHookup\

to install application macros, you just need to copy your application macro module folder under it. You can also duplicate macro modules from this folder and copy onto different machines. The only disadvantage is macro contains source code. if you don't want to share source code with others. try to build add on or plug-ins.

revit 2012 Application macro set up tutorials

Revit application macro is easy to develop using vsta. And easy to debug. If you are not aiming to sell your program, and not care about sharing the source code, it is a good and time saving tool. To deploy it is as easy as copying you module file under your revit.
The main tricks for setting up revit application Macro is reference passing. In ThisApplication.cs , the module class, you need to pass 'this',which is the running application itself to your macro class.

In your macro class, you need to define 3 variables,
1. m_app point to running application
2. m_uidoc point to active uidocument , you can use it to get user selection, etc.
3. m_doc point to current active document, the opening revit file you want to modify.

now is the form class, the user interface to take user input if necessary. it is created as an object inside your macro class. just make the controls (elements) public for easy access from macro class. for example:
mainFrame.Textbox1.Text will return you the text input from textbox1(a text field)
also when create this form in macro class, you need to pass macro class itself to it using 'this' key word. the corresponding variable in form class is m_dataBuffer. if you want to access macro class, you can call m_dataBuffer.methodYouWant().

overall, you need 3 files, ThisApplication.cs is created for you by macro manager. ClassMacro.cs for put your business logic(thing to do, to interactive with revit file), Form.cs to interactive with user. put classmacro and form file in a folder for easy management in future.

 below are video tutorials: