Setting up the coding file Class1.cs
The actual coding of your Addin will be taking place in separate *.cs files. In the default template Class1.cs is already predefined. Class1.cs file can contain all code for a complete piece of software. This is probably best if you have a simple piece of software, but in more complex solutions this will become very confusing. In this case, you can create multiple *.cs files for different parts of the software.
So, start with renaming the default Class1.cs to a more suitable name, in this case I renamed it to TestClass.cs. I am told to always use the naming format DescriptionClass, so you can later see that it’s a Class file. Apparently there are also other types of cs-files, but I haven’t come across them just yet… (except off course the AssemblyInfo.cs, but this is in a different section of the Solution Explorer).
After this there are some other default settings that have to be applied.
The first four lines state which parts or containers of the .NET is going to be used. Default parts are the standard system containers. Since we are creating a Addin for Revit, we will also be using containers from the Revit API.
So, place the cursor after the fourth line, hit enter a few times to free up some space and start typing:
using Autodesk.Revit.UI (this allows you to access Revit commands such as move, etc)
using Autodesk.Revit.DB (this allows you to access the Revit Database)
using Autodesk.Revit.Creation (this allows you to automatically and place create Revit Components)
Creating the extra Revit based Executive Commands
As you type you will notice that VS presents possible textstrings. This is very helpful in selecting the proper code. When the previews are shown, you can use the arrowkeys to get to the right one and hit Enter. This will automatically fill in the text in the coding screen, thus preventing a lot of typing mistakes.
End the line with a semicolon. It’s always needed to end a line with a punctuation mark. This is usually the semicolon, except when there is a bracket of any kind.
Besides this: keep in mind that these textstrings are case sensitive.
Depending on the type of Addin you want to create there will probably a need to also add some of the system containers too:
Using System.IO; (which allows you to read and write files)
Using System.Windows.Forms; (which allows you to make screens, forms and with this create a user interface for your Addin).
When typing the Using System.Windows.Forms you probably noticed that there was NO automated fill-in of the commands by VS. This happened because the Reference with the .NET Class was not yet created for this command. To make this work, you will also need to go to the Reference subscreen, right click on Reference and select Add Reference.
Now go the .NET tab and select System.Windows.Forms to add it to the References.
Adding System.Windows.Forms to the References.
Last remark before actually starting the programming
So, after this we’re kind of good to go. In the next parts of this weblog I’ll start trying to compile an Addin which allows you to automatically place Revit Components based on a point file.
Last tip I got before starting the actual work: when starting to program there’s a very useful viewsetting. Go to the View menu and click Error List. This opens up another subscreen which lists all coding errors (no errors does NOT mean the program will work, it just means there are no syntax errors in your code).
VS with Error List opened.
In this example the semicolon after the line “using System.Windows.Forms” is missing. You can double click on the error to jump to the text line to which it applies.