Tuesday, October 30, 2012

Revit API Programming Templates 2

In this post I am adding two more Revit API programming Templates to our database for Revit API. I hope that this templates help you to be able to do a good job in your projects.

The first template is Revit API Selection Template. This template helps you to be able to select one object, many objects, many objects using selection window, point, and other types of selection. After selecting this objects you can have access to their properties and methods in your script. The template is self explanatory and has lots of comments which help you understand the process.

The second templates is Revit API Family Forms Template. This template gives you some hints on creating forms like lines, curves, and more in Revit Family. In this template you learn how to create a simple point inside Revit using your code. Then it goes to higher level of forms like lines, splines, circle, box, helix, butterfly curve, and surface. You can use this template as a base to make your own code to edit or create something in Revit Family for your project.

Please put a comment if you have any questions.

Thursday, October 25, 2012

Revit API Programming Templates 1

As I have promised I am uploading Revit API programming templates. For this post I am uploading two templates.

First one is an empty template which can be used a s base for any type of Revit API programming. You can use this template and copy your code inside the provided spaces and build your code. This template has all the required preparations for Revit API programming that we have talked about and you do not need to do any preparation. Just copy your code and build it and enjoy the result. 


The second template is RoomDoorWindow-Revit 2013 template (RoomDoorWindow-Revit 2012 template for those who use Revit 2012) that helps you to get parameters of Room, Window, and Door in Revit using API programming and make some calculations and change the value of the parameters.

Since some people had problems with the second template, I have edited this post and put two different versions RoomDoorWindow template for those who use Revit-2013 and Revit-2012. The difference between these two templates is on GET ELEMENT command. In Revit 2012 you need to use doc.get_Element(id) but in Revit 2013 you have to use doc.GetElement(id). So you will have line of code like the following:


Revit 2012  ==>   FamilyInstance doorInstance = doc.get_Element(id) as FamilyInstance;

Revit 2013  ==>  FamilyInstance doorInstance = doc.GetElement(id) as FamilyInstance;

Please put a comment if you have any problem running your code.
 

Monday, October 22, 2012

Debugging Revit API Program

In order to debug your Revit API program using visual studio 2010 (for debugging with C# express see here) you need to follow these steps:

  • Go to your code in Microsoft Visual Studio 2010. In the Solution Explorer window, right click on the project name and choose properties of your C# project.

  • In the properties window select debug tab. In this tab, under Start Action part select "Start External Program". Use browse button and find Revit.exe where you have installed Revit. It should be in path like this:
                                  C:\Program Files\Autodesk\Revit 2013\Program\Revit.exe


  •  Go back to your code and add some Break Points beside the lines that you want the debugger to stop in your code. You can do this in two ways: 1) By clicking at the Gray area beside the line of the code you want the debugger to stop there. 2) Go to the line of code you want the debugger stop there, and from debug tab select Toggle Breakpoints(F9). Your code will look like this after adding break points.

  •  Now you are ready to debug your code. Click on the debug button (F5-Start Debugging). It makes Revit to open up, and you can open the project you want to work on in Revit now. Also, you can create a new Revit Project. 
  • In your Revit project, go to Adds-In tab and run your plug-in. It goes through your code step by step and stops at the lines that you have your first break point. Now you have the option to have a look on your code step by step and check the parameters and solve the problems you have. You can use Step Into, Step Over, and Step Out buttons to go through your code.

Wednesday, October 17, 2012

Start Revit API Programming - First Plug-in

After having an introduction on programming with C#, from today we officially start working on Revit API programming. Meanwhile, I will try to add more posts on programming with C# and provide you with some useful links.

In order to start Revit API programming you need to have the following software installed:
  1. Autodesk Revit 2013 - You can download the student version of this software here.
  2. Microsoft Visual Studio 2010 - How to Download Microsoft Visual Studio Free for Students Post illustrates the steps to download visual studio 2010 free for students. If you are not a student, you can download C# express instead here.
  3. Revit 2013 SDK - You can download Revit 2013 SDK from Autodesk Developer Network (ADN) . You can find more detail on how to install Revit 2013 SDK later in this post.

The best place to start learning on API programming is ADN website. After opening this page, first watch the video under My First Plug-in Title. This video gives you some ideas about how a plug-in works and what you can do with a very simple revit plug-in.

Then scroll down to the bottom of the page and download the Revit 2013 SDK from Tools. Then run the file you have downloaded and install the Revit 2013 SDK. This software adds a new tab to your Revit interface named Add-Inns. You will use this tab to run the plug-ins that you create later. Also,It will creates a folder with the same name in you Local Disk (C:). Go to this folder and browse all the files and folders in it.You can see what folders and files should be in your SDK folder in the following image.


Between these files and folders you will use the following more than others:
  1. Add-In Manager - Go to this folder and run the executive file named Add-In Manager for Autodesk Revit 2013. this program gives you an interface to manage your plug-ins. It will help you a lot and make your work much easier. We will come back to this topic later.
  2.  Samples - This folder contains a huge amount of API samples that you can go through and learn more about API programming. I would recommend you to find a sample close to what you want to do and use it as base for your projects.  In the samples folder, there are some .html files. These files help you to get more information on samples such as their difficulty.
 Let's go back to ADN website and create the first plug-in. Read the overview and then go to lesson 1: The Basic Plug-in. In this lesson you will create your very first basic Autodesk Revit plug-in for copying groups selected by the user to a specified location. Follow all the steps other than 7,8, and 9 from Steps To Create First Plug-in. You do not need to do these steps, since Revit 2013 can interact with .NET Framework 4.  Also, Do not do the Writing an AddIn Manifest. You will use add-in manager that you have installed in the previous step instead.

There are some changes in the Running the Plug-in too. When you go to Revit, in the Add-Ins tab in the external tools you will have Add-In managers tool as shown in the following.

 


Choose the Add-In Manager (Manual Mode). A new window will pop up. Select the load botton on this window and locate your dll file.


When you load the .dll file, you will see your class and .dll file in the Loaded Commands tab  of this window.

Select the class name and hit Run button. Then follow step 4 and 5 of the Running the Plug-In of the ADN website.

Then read Lesson 2 and Lesson 3 to learn more about API programming. 

How to Download of Microsoft Visual Studio free for Students:

In the very first step of learning API, you need to download Microsoft Visual Studio 2010 (NOT 2012) which is provided for free to students. Microsoft Visual Studio is an Integrated Development Environment (IDE). An IDE provides powerful tools for writing software, such as syntax checking, assistance in typing names of variables and routines, management of multiple files, access to prewritten routines, and debugging. The following part explains how you can download Visual Studio.

Step 1:

Go to DreamSpark at (https://www.dreamspark.com/Default.aspx). The DreamSpark is a Microsoft managed site that provides professional-level developer and design tools to students and educators around the world at no charge. In DreamSpark page, go to students tab. Then create an account (if you already have an account you can skip this step).

Fill in the required information. For E-mail address you need to use your academic E-mail address. In “Get Verified Now” page, select the first option which is “Get verified through my school” and type in the school name. It may pop up a window which requires you to provide your NetID and password.


When you have received the message that says you have successfully verified your academic status, you can go to the next step and download any software that you need free of charge.

Step 2:
When you are logged in, go to “Software Catalog” tab and find Visual Studio 2010 Professional and select it. A new window will come up that says get your software in 4 easy steps. Follow the steps in order.

 I would suggest downloading the software at school; otherwise it may take about 1 hour to download it at home. After finishing the downloading process, Launch Visual Studio. It will take you to the place that the file is stored (or you can go to that folder yourself). You need to unzip the file that you have downloaded and then run the autorun.exe file. It will start installing Visual Studio 2010 on your computer, which takes about 30-45 minutes. Beware that you will need several gigabytes of file space on your hard disk.


 Finish up everything and you are ready to start working with Visual Studio 2010.

Monday, October 15, 2012

First Code in C# to Get Prepared for API programming:



In the program.cs box, there are some existing lines of code. The first group of lines is showing the .Net toolkits that your program is using. Let’s just assume that they are needed for the coding part for now. We will talk about them later in detail. 



I will introduce namespace and class as a grouping tool that is used to group your code for now and later we will talk about them more. Main is the first method (and the only method here in this code right now) of your class. The specific thing about main is that the debugger starts your code from the first line of Main function and proceeds line by line. Main name is reserved by the program and you cannot name other methods in your code as main.



In the following you will see some lines of codes that clarify the definitions of data types, operator, method, and control structures.

 

Step1:

In this step you will learn how to define variables, define their type, and assign a value to them. Then you will see how to use operators to do some actions on your variables. In the following code, the “numMoCredits” and “numMarkCredits” are your symbols or variables. The type of these variables is defined as integer using “int” before them. Console.writeline and console.write help you to print out the result on the computer display (historically called a console).






Step 2:

In this step you will learn how to use a method or function to do the same process as the previous step. The function name is “caculateSum” and it does not get any parameters and does not return any values. The Main() function invokes the calculateSum() function.


The result would be the same as the previous code.

 

Step 3:

In this step a function that accepts two integer variables as input parameters is added to the code. Have a look to the result window and see how it has been changed.





 

Step 4:

In this step a method that accepts two integer values and two string values as inputs has been added to the previous codes.





Step5:

In this new code, a function is defined in a way that accepts two input values and returns an integer as an output.






Step6:

This is the last step of this post. In this step, it is asked from the user to enter two names. Then the program prints out those two names and provides the sum of the credits of those two students.








Here is the link to a video on Introduction to the C# Programming Language on MSDN website. Please watch the video and try to do the same thing and submit the executive file to eLearning.