I have found there are great references out there regarding setting up your project from the start, I will try to remember to add some links at the bottom of the document, but here is my checklist of things to setup:
Application - Set the Assembly name to LearningAPI, this will help you reference it later.
References - You have to include references to RevitAPI.dll & RevitAPIUI.dll, the reason for this is that there are methods and such within these dll files that you will need, this will enable you to have access to Revit commands in your program. Within these references you should set the Copy Local property to False, this means that the Revit dll's will not be copied to your Build folder when you build the program.
Debug - You should set the location of the program to use for testing, so set the external program to the Revit.exe of your choice, I have 7 to choose from on my machine, at least in 2013 it is a single application per year!
|
Adding the Revit Application to the Debug page |
Compile - Point the output path to a known location
You should also click Build Events... In the Build Events dialog box paste the following code into Post-build event command line:
copy "$(ProjectDir)LearningAPI.addin" "$(AppData)\Autodesk\REVIT\Addins\2012\LearningAPI.addin"
This will ensure that the addin sml file you create is copied to the program's addin folder
Add a Class - Right click in the Solution Browser and create a new Class via add. Select Class from the dialog that opens:
and Name this class AdskApplication.vb. repeat the process to create AdskCommand.vb.
Add a XML Module - In order to get the command to autoload you have to define an addin file, this is an xml file with certain properties, right click in the Solution Browser and create a new Module,
in the dialog box select XML File and name this LearningAPI.addin
Add some code - You then need to put a bunch of code into the Classes/Modules so that they can do something when required. Put the following into AdskApplication:
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Class AdskApplication
Implements IExternalApplication
Public Function
OnStartup(ByVal application As UIControlledApplication)
As Result Implements IExternalApplication.OnStartup
'TODO put some code here
'Must return some code
Return Result.Succeeded
End Function
Public Function
OnShutdown(ByVal application As UIControlledApplication)
As Result Implements IExternalApplication.OnShutdown
'TODO: Add your code here
'Must return
some code
Return Result.Succeeded
End Function
End Class
Put the following into AdskCommand:
Imports System
Imports System.Collections.Generic
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
<Transaction(TransactionMode.Manual)>
Public Class
AdskCommand
Implements IExternalCommand
Public Function
Execute(ByVal commandData As ExternalCommandData,
ByRef message As
String, ByVal
elements As ElementSet)
As Result Implements IExternalCommand.Execute
Dim uiapp As UIApplication = commandData.Application
Dim uidoc As UIDocument = uiapp.ActiveUIDocument
Dim app As Application = uiapp.Application
Dim doc As Document = uidoc.Document
Dim sel As Selection = uidoc.Selection
'TODO put code in here
'Must return some code
Return Result.Succeeded
End Function
End Class
Put the following into LearningAPI.addin:
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Command">
<Text>LearningAPI</Text>
<Description>Some description for LearningAPI</Description>
<Assembly>LOCATION DEFINED IN COMPILE OUTPUT
PATH\LearningAPI.dll</Assembly>
<FullClassName>LearningAPI.AdskCommand</FullClassName>
<ClientId>a6518a22-3138-433e-8ce0-144bd12fd520</ClientId>
<VendorId>TBC_</VendorId>
<VendorDescription>Drew's Revit Add-Ins</VendorDescription>
</AddIn>
<AddIn Type="Application">
<Name>Application LearningAPI</Name>
<Assembly>LOCATION DEFINED IN COMPILE OUTPUT
PATH\LearningAPI.dll</Assembly>
<ClientId>aaeff130-2e2d-40dc-85e3-f4029cc65d04</ClientId>
<FullClassName>LearningAPI.AdskApplication</FullClassName>
<VendorId>TBC_</VendorId>
<VendorDescription>Drew's Revit Add-Ins</VendorDescription>
</AddIn>
</RevitAddIns>
Add Hello World - replace the text 'TODO put code in here within AdskCommand with the following:
Msgbox("Hello World")
Build the program, then open Revit and run the addinn from the Add-Ins Ribbon -> External Tools, let me know what I messed up and I will improve the page!
Next time.... Selecting an object and getting some data from it...