0% found this document useful (0 votes)
6 views9 pages

Debugging Revit Plug-ins in Visual Studio

Uploaded by

sonor1981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Debugging Revit Plug-ins in Visual Studio

Uploaded by

sonor1981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

My First Revit Plug-in Overview >> Lesson

1 >> Lesson 2 >> Lesson 3>> Lesson 4 >> Lesson


5 >> Lesson 6 >> Lesson 7 >> Lesson 8
Lesson 4: Debugging your code.

Learn how to use the Visual Studio debugger to step through your code
line-by-line to follow the program execution; and to watch the values of
variables as they are changed by your code.

It’s rare that you will write a program that runs perfectly the first time. Even
if you haven’t made any mistakes that prevent Visual Studio from building
your plug-in, it’s quite likely that you made a mistake in your program logic
that causes it to produce different results from the ones you expected.
Errors in your code are called bugs, and the process of stepping through
your code examining the program execution flow and variable values is
called debugging.

To debug your plug-in code, you’re going to have to launch Revit from your
Visual Studio debugger and load your plug-in into Revit.

In order to be able to debug using Visual Studio, we have to make some


manual edits to the project files by implementing the following steps:

1. In the Solution Explorer window, right Click Lab1PlaceGroup and


select Properties.
2. In the Properties window, Go to Debug option and choose “Start
external program”.
3. Browse for “[Link]” file and normally the file will be present in this
path(C:\Program Files\Autodesk\Revit 20xx\[Link]).

(Edit the file path for [Link] if you’ve installed it in a non-default


location).

Thus, the Properties settings allow you to set the executable to


launch when you start the debugger:
Launching the debugger

If you closed the Visual Studio, launch it again now and open the project
you saved in Lesson 1 (called the Lab1PlaceGroup).

Before you launch the debugger, please ensure that the AddIn manifest
file for this lab has been created and exists at the location described in
the Writing an AddIn Manifest section in Lesson 1: The Basic Plug-in. Since
you will now be debugging the plug-in code, Visual Studio has created a
’debug’ version of your .NET plug-in DLL. And so for Revit to read this
‘debug’ DLL instead of the ‘release’ DLL that was created in Lesson 1, we
need to change the path of the DLL in the manifest to the following
(highlighted in bold) –

<?xml version="1.0" encoding="utf-8"?>


<RevitAddIns>
<AddIn Type="Command">
<Name>Lab1PlaceGroup</Name>
<FullClassName>Lab1PlaceGroup.Class1</FullClassName>
<Text>Lab1PlaceGroup</Text>
<Description>Places the Group at Particular Point</Description>
<VisibilityMode>AlwaysVisible</VisibilityMode>

<Assembly>C:\test\Lab1PlaceGroup\Lab1PlaceGroup\bin\Debug\Lab1placeGrou
[Link]</Assembly>
<AddInId>502fe383-2648-4e98-adf8-5e6047f9dc34</AddInId>
<VendorId>ADSK</VendorId>
<VendorDescription>Autodesk, Inc,
[Link]</VendorDescription>
</AddIn>
</RevitAddIns>

To start your debugging session, simply open the Debug menu and select
the Start Debugging option, or just hit F5. This will launch Revit from your
debugger. The flavor of Revit that gets launched will depend on the path
you have provided in the tag in the .csproj file that we edited manually at
the beginning of this lesson.

You have to launch Revit in this way so that your debugger can hook into
the Revit process to monitor the execution of the code you compiled into
your plug-in DLL. Because we’ve placed the AddIn manifest in the correct
location, Revit will automatically load our plug-in.

Remember that the DLL you loaded in Lesson 1 was in


the bin\Release folder. When you are building a ‘final’ version of your plug-
in DLL that you want to give to your users and customers, Visual Studio will
build a release version. Visual Studio makes various optimizations to the
compiled code in a release build so that it will run faster and take up less
memory. When you want to debug your code, Visual Studio will create
a debug version of your plug-in DLL. The debug version isn’t optimized for
speed/memory, and also includes additional information that the debugger
uses to tell you about what is happening when the code runs.

Please read more on this in this article: How to: Attach to a Running
Process ( [Link]
versions/visualstudio/visual-studio-2015/debugger/attach-to-running-
processes-with-the-visual-studio-debugger?view=vs-
2015&redirectedfrom=MSDN )

Now, open the [Link] Project file.

Breakpoints

Your plug-in DLL is now ready to debug. But before you run
your Lab1PlaceGroup command, you have to tell the debugger you want it
to stop when it is executing your code. You do this using a breakpoint.

In Visual Studio, double-click on [Link] in the Solution Explorer to


display your code and click anywhere in the line:
UIApplication uiapp = [Link];

Then select Toggle Breakpoint from the Debug menu (or hit F9).

That line will now be highlighted in red and have a red circle in the margin
next to it to indicate that you’ve set a breakpoint for this line:
Set a breakpoint for the PickPoint function in the same way:
//Pick point
XYZ point = [Link]("Please pick a point to place group");

When Revit calls these methods in your code, the debugger will stop at
these lines and wait for you to tell it what to do.

Stepping through your code

Now it’s time to invoke your command. Inside Revit on the Add-Ins ribbon
tab, click the External Tools drop-down list, then click Lab1PlaceGroup.
This will start your plug-in in Revit and Visual Studio should take control
and become your foreground application. (If it doesn’t, click on its icon in
your Windows taskbar to activate it.) The debugger is now stopped waiting
for you with the line of code it’s about to execute highlighted in yellow and
with a little yellow arrow in the margin next to it.
Now you’re ready to step through your code. The Visual Studio debug
menu gives you three ways to step through your code: Step Into; Step
Over; and Step Out. You’ll mostly be using Step Over – this executes the
next line of code (the line highlighted in yellow) in the debugger, and then
moves to the next line. If the line of code to be executed is a method call,
then Step Over executes the entire method. If you also want to execute the
called method a line at a time, you can use Step Into; and you can
use Step Out to move back up (out of the method) to the code you were
originally debugging.

As well as in the Debug menu, you should also see Step


Into, Step Over and Step Out icons on a toolbar, and each also has a
corresponding hot key (F11, F10, and Shift+F11).

Click on the Step Over icon on the toolbar now. The debugger moves to
the next line of code (it ignores comments).

You can now hover the mouse over the text uiApp and a tooltip is
displayed showing the value of the variable.
You can click on the sign to drill down to the various properties and check
their values.

To display variable values, you can also right click on the variable in your
code and select Add Watch. This displays the variable and its value in the
Watch window at the bottom of your IDE.

Keep stepping through the code, hovering over variable values and
properties to see how they change as the code executes. If at any point
you want to stop line-by-line debugging, just hit F5 to Continue to the next
breakpoint you had set – which in our case was set to
the [Link]() line.
Stepping through this line by clicking F10, makes Revit the foreground
application and prompts you to select the point where you want the
Furniture group to be copied to. Let us select the approximate center of the
adjacent room now. As soon as the point is selected, Visual Studio now
becomes the foreground application with the next line of code highlighted in
yellow with the debugger waiting for us to either check the value of the
selected point or continue line by line debugging or just jump to the next
breakpoint by clicking F5. At this point, we can check the XYZ value of the
selected point by just hovering the mouse over the point variable (which is
the container/variable we created to store the target point for the furniture
group to be copied to).

If you hit F5 now, you will find that the debugger steps through all the
remaining code, and makes Revit the foreground application again with the
copy operation successfully completed.

Once you’ve finished experimenting, select Stop Debugging from the


Debug menu to close Revit and end your debugging session.
Congratulations! You now know how to debug a project in Visual Studio.

My First Revit Plug-in Overview >> Lesson


1 >> Lesson 2 >> Lesson 3>> Lesson 4 >> Lesson
5 >> Lesson 6 >> Lesson 7 >> Lesson 8

Common questions

Powered by AI

The 'Watch' window in Visual Studio displays specified variables and their current values during debugging sessions, offering persistent monitoring of these variables as the code executes. It plays a critical role in analyzing program behavior by allowing developers to observe how variable values evolve, thus identifying potential issues or verifying expected behavior .

Visual Studio creates different DLL versions to optimize for different scenarios. The debug version includes extra information for debugging purposes and is not optimized, which aids in tracking code execution. The release version, however, is optimized for performance in speed and memory usage, making it suitable for deployment to users. These differences reflect the contrasting priorities of development (diagnostic capability) and production (efficiency) environments .

To verify the displacement of a 3D model element, such as a furniture group, set a breakpoint right before and after the operation in the code. Step through the breakpoint to the execution point where Revit becomes the foreground application for selection. After selection, return to Visual Studio when it regains focus, and examine the updated XYZ coordinates of the element by checking the variable storing these values. This confirms changes or calculations performed during the code execution .

Visual Studio debugger allows variable value inspection via hovering over variables to display tooltips showing their current values. The 'Add Watch' feature can be used to monitor variables continuously in the 'Watch' window. This functionality helps track how variables change throughout the program execution, enabling deeper insights into logic errors or unintended behavior .

Breakpoints are essential in debugging as they allow you to halt the execution of your program at specific lines of code, enabling you to investigate variable values and program flow at those points. In Visual Studio, you can set a breakpoint by opening the code file, double-clicking a line within it, and selecting 'Toggle Breakpoint' from the Debug menu or by pressing F9. The line, such as 'UIApplication uiapp = commandData.Application;', will be highlighted in red, indicating a breakpoint is set .

'Step Over' executes the next line of code in the debugger and moves to the next line, skipping over method internals. 'Step Into' dives into the method call to step through each line within the method. 'Step Out' continues execution until the current method returns, moving back to the code that called the method. These options allow the debugger to control the level of detail during code execution analysis .

The initial steps in Visual Studio to prepare for debugging a Revit plug-in include opening the Solution Explorer, right-clicking on Lab1PlaceGroup, and selecting Properties. Then, navigate to the Debug option and choose 'Start external program'. Afterward, browse and select the 'Revit.exe' file, which is typically located in the path C:\Program Files\Autodesk\Revit 20xx\Revit.exe. You must also ensure the AddIn manifest file is in the correct location as described in the Writing an AddIn Manifest section .

To launch a debugging session, first ensure that the AddIn manifest points to the correct debug DLL. Then, select the Debug menu in Visual Studio and choose 'Start Debugging' or press F5. This action will initiate Revit along with your plug-in. Visual Studio hooks into the Revit process, allowing you to step through your code using the attached debugger .

Launching Revit from the Visual Studio debugger is necessary because it enables Visual Studio to interact with the Revit process in real-time. This integration allows the developer to examine code execution flow, set breakpoints, and inspect variable states as they change within the Revit environment, facilitating thorough debugging and troubleshooting .

Modifying the AddIn manifest file is crucial for ensuring that Revit uses the debug version of the plug-in DLL instead of the release version. The manifest file specifies the path of the DLL, and during debugging, it should point to the 'bin\Debug' folder where the debug version resides. This allows the debugger to provide detailed feedback on code execution and variable states .

You might also like