Etichette

mercoledì 16 ottobre 2013

Revit API - Iexternal command template

In this article I would like to show a simple way to create and test your code using my basic template for Add-in avalaible here

Let's start opening visual studio and clicking on the Add-in template. You need to type a name for your solution as in the screenshot below.

I called my app "My first command"

Now we need to type some essentials strings:

 namespace My_first_command  
 {  
   [TransactionAttribute(TransactionMode.Manual)]  
   [RegenerationAttribute(RegenerationOption.Manual)]  
   public class MyCommand : IExternalCommand  
   {  
       public Result Execute(
              ExternalCommandData commandData,
              ref string message,
              ElementSet elements)
              {
                 //YOUR CODE HERE
              }
   }  
 }  

You just need to type the code for the function where I wrote "Your code here".
Let's start with a simple example....a Message with "Hello world!" showed clicking on a button in Revit.

 namespace My_first_command  
 {  
   [TransactionAttribute(TransactionMode.Manual)]  
   [RegenerationAttribute(RegenerationOption.Manual)]  
   public class MyCommand : IExternalCommand  
   {  
     public Result Execute(  
        ExternalCommandData commandData,  
        ref string message,  
        ElementSet elements)  
     {  
       TaskDialog.Show("Hello world!", "Hello");  
       return Result.Succeeded;  
     }  
   }  
 }  

Now you need to test the code.
In order to set your visual studio to start revit and to test the code you need to click on "Project" and to click on "My project properties" (in this case "My_first_command Properties").
You need to click on Debug and to set revit as external program, so click on "Start external program" and clicking on "..." you will be able to click on Revit.exe in C:\Programfiles\Autodesk\Revit2014\Revit.exe.

This is a screenshot which show the result:

Now you need to set the paths for your solution, because Revit will initialize only the addins located in some specific paths. 
Click on "Build" and set the path to C:\ProgramData\Autodesk\Revit\Addins\2014\
Click on save button.

Now you need to set your addin manifest. (THIS IS IMPORTANT)
Be sure that the name of the assembly is correct and also the class name for your application.

Download the source code at the link below to understand how to compile an addin manifest correctly.

You just need to click on "Start" in the toolbar and Revit will appear. Your command will be avalaible in the "Add-ins" ribbon tab clicking on external tools.







Nessun commento:

Posta un commento