April 15, 2018

Revit API update and a few random things for beginners:

1. I don't want to discourage anyone else from using python to program revit, but I've almost completely switched to C#. There are more examples in C#, and I kept having problems with the line returns and tabs in Python. Maybe it was the way I copied code off the internet, and pasted it into the compiler, but every time I would try to build, I would get whitespace errors. Then I would have to delete the return at the end of every line, and put the returns and tabs back in.

The good news is that visual studio and the Sharp Develop macros both have Python and Ruby options for programming in the newer versions (I know VS 2015 and Revit 2017 both support Python. Not sure exactly when they added it offhand.)

2. If you aren't a professional programmer, I recommend going through a few C# books. You can find quite a few free ones online. I like Rob Miles C# yellow book. Sams Teach Yourself C# in 24 hours is good, and has an emphasis on using the "form designer". Those are both basic, beginner books. I also recommend reading at least part of a more advanced book, although it may have more detail than necessary.

4. Macros: I tend to do almost all my programming and debugging in the Sharp Development environment using an Application level Macro. With Visual Studio, you have to restart Revit every time you make a program change. WIth Sharp, you just rebuild and rerun. I know there are some things you can't do in Sharp (adding items to the ribbon panel, for example), but its much faster to do what you can in Sharp.

The changes to move main code body from sharp to VS are very minimal. The main difference is in the way you define the doc, uidoc, and active view. In Sharp, these are defined as:

UIDocument uidoc = ActiveUIDocument;

Document doc = ActiveUIDocument.Document;

Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView;

This should also work:

UIDocument uidoc = ActiveUIDocument;

Document doc = uidoc.Document;

Autodesk.Revit.DB.View pView = doc.ActiveView;

With a Visual Studio addin, use:

UIDocument uidoc = revit.Application.ActiveUIDocument;

Document doc = revit.Application.ActiveUIDocument.Document;

Autodesk.Revit.DB.View pView = revit.Application.ActiveUIDocument.Document.ActiveView;

5. Notice that many examples leave out the definitions for uidoc and doc listed above. They assume you know how and where to add those definitions. Also, most use uidoc and doc for the variables shown above. But occasionally you will see uidocument or document used for one of the variables. You will have to edit your program to stay consistent throughout the program.

6. Add-in GUID's: Revit Help explains the GUID add-in number that must be unique. But they don't explain how to get that number. You can just type in a random number (the letters can't be above "f" because its hexadecimal). The odds of it matching another GUID that someone else typed is at least as small as winning the lottery a couple of times. But if you want to do it right, just search online for a GUID generator. It will give you a number guaranteed to be unique even if your app is installed on millions of computers.

8. If you haven't already gone through this, you should do the "My first revit plug-in" tutorial at:

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=16777469

Don't just read it either. Do the actual program. You may be surprised how many errors you have to work your way through for something so short.

If you are just starting, and you don't do this tutorial, you probably aren't going to know what to do when you can't get something to work.

9. Revit SDK: Also make sure you get the revit Software Developers Kit here:

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=2484975

The Revit.chm file is the basic revit help file that explains all the available commands. There is also other useful links on that page.

10: C# vs. Python: If you move to C# from python like I did, a few main differences:

a. C# lines end with a semi-colon ; instead of a simple return.

b. Groups of code are enclosed in curly brackets: { }

c. Capitalization is very important: Document is not the same as document.

d. When defining a variable, you must declare the type of variable it is. For example, the following line creates a variable called "doc", and it is a variable of the type "Document", and it is assigned the value of the Revit file:

Document doc = revit.Application.ActiveUIDocument.Document;

Likewise, a string would be declared as:

String mystring = null;