Getting Started with Revit Macros Using C#
Transcript of Getting Started with Revit Macros Using C#
Revit Macros
Class outline• What is a macro?• Fundamentals of the C# coding language & the Revit API• Using SharpDevelop• Create macro to create sheets from views• Next steps
Revit Macros
The Basics• What is a macro?• What is C#• How do you write macros? • Where are macros saved?
Revit Macros
Why bother with macros?• Automate Revit
– Save time– Reduce errors
• Skills in high demand• Help lots of people• Become the Revit rock
star in your office!
Revit Macros
Let’s get started
• Download Revit SDK• Install on hard drive• Revit API help file• Macro samples
www.autodesk.com\developrevit
Revit Macros
Subs, Functions and Variables• Subroutines do something
– “Please close the door”• Functions do something and return something back
– “Please bring me a glass of water”• Both subroutines and functions can accept arguments
– “Please bring me a glass of water using this glass”• Variables store values
– Typed vs dynamic variables
Revit Macros
Which model are you working on?• Must specify which model file you are working on
- the macro’s model fileor
- the active model file• For macros, specify the active document
Revit Macros
Element ID• Unique ID for every object in
the project• ID is not unique across projects• ID can change
Revit Macros
Let’s write a macro!7 Steps1. Start with a new Revit file2. Create the module and macro3. Write some code4. Build macro and test5. Step through the code6. Run the macro7. Determine next steps
Revit Macros
Create Plan Sheets from ViewsProblem• Create new sheets• Rename and renumber sheets• Add existing views• Can do this manually but...
– Takes time– Easy to make mistakes– Views may not be in same location
Revit Macros
Create Plan Sheets from ViewsSolutionWrite macro that creates sheets and adds views• Get all floor plan views• Create sheets from floor plan views• Rename and renumber sheets• Add correct view to sheet • Position view in specific location
Revit Macros
Collecting Revit Elements• Use FilteredElementCollector• Can specify ByCategory or ByClass• ByCategory can reference Revit’s
built-in categories
Revit Macros
Loops• Execute the same code repeatedly
– Specific group of elements– Specific # of times– While condition exists– Until condition occurs
Revit Macros
Conditional Statements• Evaluates a condition as true
or false• IF … ELSE statement• Use ELSEIF (ELIF) for
additional evaluations • Can use AND, OR or NOT as
part of statement
Revit Macros
Transactions• Needed anytime you make a
change to the Revit model• Error if you try to change model
without one• Need to commit changes after
change is made• Location determines undo point
Revit Macros
Debugging• Step into macro• Print to the Output window• Use breakpoints• Comment your code!• Handle exceptions
www.xkcd.com
Revit Macros
Next steps. . .• Add multiple views to sheets• Center view of sheet based on title block dimensions• Use different title block for different sheets• Update additional sheet instance parameters
Revit Macros
Mastering Revit Macros with C#• 5 week course + coaching• Starts July 17• Weekly training sessions
– Conducted entirely online– Video lesson on Monday– Practice activity– Coaching session each Thursday – Private forum for students
• Cost is $347 USD (or 3 x $125)
Revit Macros
• Week 1: Getting started• Week 2: Import from Excel• Week 3: Working with views• Week 4: Creating revit elements• Week 5: Inserting families• Bonus: Building a user interface• Bonus: Convert macro to addin
Mastering Revit Macros with C#