Autodesk - My First Plug-In Training - Lesson 1 - The Basic Plug-In PDF
Autodesk - My First Plug-In Training - Lesson 1 - The Basic Plug-In PDF
Products
Solutions
Purchase
Support
Community
Company
Contact Us
Store
Partners
United States
Worldwide Sites
Share
Video: A Demonstration of
Lesson 1 Steps to Create your
First Plug-in
lesson1_revit_2012_and_earlier_project_files.zip
(zip - 7283Kb)
Lesson Downloads
Visual C# Express will create a default code project for you and display the code in the code window.
3. Save the project:
5. Click the Browse tab in the Add Reference dialog and browse to the Revit product installation sub-folder.
(The sub-folder path depends on where you have installed Revit Architecture 201x. The default path is
C:\Program Files\Autodesk\Revit Architecture 201x\Program*).
* The path may vary depending on the flavour of Autodesk Revit you are using.
You will add two reference files from this folder. Select RevitAPI.dll, hold the Ctrl key and select
RevitAPIUI.dll, and then click OK. Now the two interface DLL files are referenced in your project. All the
Revit APIs are exposed by these interface files and your project can use all of those available APIs from
them.
6. Set the referenced files' Copy Local property value:
In the Solution Explorer window you saw in step 5, click RevitAPI under Reference node. In the
Properties window, click Copy Local property, and then click the drop-down list, select False. Repeat the
same steps to change RevitAPIUI's Copy Local property value to False.
7. Set target .NET Framework:
Attention: Autodesk Revit 2011 supports the use of .NET Framework 3.5. Autodesk Revit 2012 and
higher supports the use of .NET Framework 4.0, which Visual C# 2010 Express uses by default. The
following step is needed in order to use the correct version. If the Revit Architecture version that you are
using supports .NET Framework 4.0, you can skip step 7, 8 and 9.
In the Solution Explorer window, right-click Lab1PlaceGroup project, and click Property.
8. In the displaying Project Property form, click Application tab on the left-hand side of the window, click the
drop-down list below Target framework, and then click the .NET Framework 3.5 option in the list.
9. The following message box appears to ask your confirmation. Click Yes to confirm the change.
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class Lab1PlaceGroup : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
//Get application and document objects
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
//Define a Reference object to accept the pick result.
Reference pickedRef = null;
//Pick a group
Selection sel = uiApp.ActiveUIDocument.Selection;
pickedRef = sel.PickObject(ObjectType.Element, "Please select a group");
Element elem = pickedRef.Element;
Group group = elem as Group;
//Pick a point
XYZ point = sel.PickPoint("Please pick a point to place group");
//Place the group
Transaction trans = new Transaction(doc);
trans.Start("Lab");
doc.Create.PlaceGroup(point, group.GroupType);
trans.Commit();
return Result.Succeeded;
}
}
Don't worry about the details of the code for now, you'll come back to this shortly in the next couple of
lessons.
11. Save the file:
On the File menu, click Save All.
12. Build the project:
The code you have written is in human readable form. To make the code readable by a computer, you will
need to translate it or build it.
Inside Visual C# Express, in the Debug menu, click Build Solution to compile and build your plug-in. Build
Success message shows in status bar of the Visual C# Express window if the code is successfully built.
Note: If you are working with Revit 2012 API, you will see a warning stating that
'Autodesk.Revit.DB.Reference.Element' is obsolete. At this point, don't worry about this warning. We will
address what the code needs to be changed to, in Lesson 3.
If you are working with Revit 2013 and higher API, you will need to replace the following line of code:
Depending on what version you are using you may need to change the path here to match your
Lab1PlaceGroup.dll location on your computer:
C:\test\Lab1PlaceGroup\Lab1PlaceGroup\bin\Release\Lab1PlaceGroup.dll
2. Save the file:
On Notepads File menu, click Save. Enter MyFirstPlugin.addin in the File name box. Change Save as
type to the All Files option (the file name is up to you; however the file extension must be .addin). Browse
to the following subfolder, and then click the Save button.
For Windows XP* - C:\Documents and Settings\All Users\Application Data\Autodesk\Revit\Addins\2011\
For Windows Vista/Windows 7* - C:\ProgramData\Autodesk\Revit\Addins\2011\ (The ProgramData
folder is hidden by default)
* The path may vary depending on the flavour of Autodesk Revit you are using.
For example, here is the setting in Save As dialog in Windows 7 for Revit Architecture 2011.
5. Pick a point in another room, for example in Room 2. You should see the group copied to this location. The
center of the new group is the point you selected.
Congratulations! You have just written your first plug-in for Autodesk Revit. You will be reviewing the code in
detail in Lesson 3.
Before you move on to the next lessons, let us go back to some of the things we skipped over earlier, starting
with basics concept about programming, and the benefits it can bring to your day-to-day work.
Additional Topics
Introduction to Programming
The C# code you have just executed to copy a group is only 30 lines long. Here you see a small amount of code
working in a similar way to the internal Revit command, Create Similar. Software programming allows you to
capture the logic of a particular functionality once and then reap the benefits over and over again, every time
you want to perform this functionality.
What is Programming?
A simple answer to this question is: Computer programming is the process of creating a sequence of instructions
to tell the computer to do something. You can look at your program as a sequence of instructions. During the
course of the upcoming lessons, you will look at the various lines and blocks of code and look at them all in the
context of being instructions for a computer.
If you were to explain what computers are to a young child, you might say: a computer is a tool which follows
instructions you provide. Programming is one way of giving instructions to the computer. Internally, a computer
sees these instructions encoded as a series of numbers (also called machine code). The human-readable
instructions you saw at the beginning of this lesson is called source code and the computer converts these
instructions to machine code which it can then read and execute. A sequence of such instructions (or code),
written to perform a specific task, is called a program and a collection of such programs and related data is
called a software. Autodesk Revit is one such software product.
Source code can be written in different languages, just as humans use different languages to communicate
between ourselves. The language you will be using in this guide is called C# (pronounced C-Sharp).
What is an API?
API is the acronym for Application Programming Interface: the way a software programmer can communicate with
a software product. For instance, the Revit API is the way programmers can work with Revit, and it establishes
what functionality a software programmer can use within Revit. Such as the Revit API allows you to write
instructions for Revit to execute one after the other.
To put this slightly differently: commercial software companies, such as Autodesk, often distribute a set of
libraries which you can use in your own program to interact with a particular software product, such as Autodesk
Revit, and extend its functionality. This set of libraries is known as the software products API.
The type of program you write to interact with a software product and extend its functionality will depend upon
how the API has been designed and what has been exposed (through APIs) for you to work with.
What is a Plug-in?
A software plug-in is a type of program module (or file) that adds functionality to a software product, usually in
the form of a command automating a task or some customization of the products behavior. When you talk about
a plug-in for Revit and you will also hear the term Add-In used for this product we mean a module containing
code that makes use of the Revit API. Revit loads such plug-ins and uses them to adjust its behavior under
certain conditions, such as when a particular command is executed by the user of the plug-in.
< Return to Overview
Careers
Mobile Site
Copyright 2013 Autodesk, Inc. All rights reserved. Privacy Legal Notices & Trademarks Report Noncompliance Site Map