Creating A Command in AutoCAD With C#
Creating A Command in AutoCAD With C#
CP2774-L This hands-on lab will show you how to create a new project in Microsoft Visual Studio
and do setup for an AutoCAD plug-in. You will learn how to add code to create a simple command and
add a splash screen or other initialization code. After that, we will create a command that uses a dialog
to display a user selection.
Learning
Objectives
At the end of this class, you will be able to:
Start and set up a Visual Studio project as an AutoCAD plug-in
Create custom commands to be used within AutoCAD
Create and use dialogs with custom AutoCAD commands
Create initialization code for command-line prompts, splash screens, or other initial settings that are
required on loading
Email: [email protected]
Creating a Command in AutoCAD with C#
The first part of this Lab. will illustrate creating an AutoCAD plugin without using a template.
The first thing we will do is to create a very simple plugin without using the AutoCAD wizard project.
2
Creating a Command in AutoCAD with C#
2. In Visual Studio select the file menu, and then select New, then select Project...
3. In the New Project dialog select Class Library under Visual C# Language...
3
Creating a Command in AutoCAD with C#
4. At the Bottom of the New Project dialog enter the name of the project..
4
Creating a Command in AutoCAD with C#
5
Creating a Command in AutoCAD with C#
6. The next several steps will be to convert this project into an AutoCAD plugin.
The first requirement is to add references to the required AutoCAD DLLs .
C:\ObjectARX 2013\inc...
6
Creating a Command in AutoCAD with C#
On the properties page Select Start External Select the ... button
select the Debug Tab... Program... to browse to the
location of the
AutoCAD executable
file acad.exe...
7
Creating a Command in AutoCAD with C#
9. The next step is to add Using statements to add references to the namespaces
we will be using...
Adding the
CommandClassAttribute
definition is considered
Optional...
This custom attribute class is used to mark a type as the application's command class.
An application may designate one, and only one, type as its command class. AutoCAD
looks for an application's command methods on the type that bears this attribute.
8
Creating a Command in AutoCAD with C#
To add the Command you use the CommandMethod Attribute and at minimum specify the commands
global name. The following method will contain the code to process.
9
Creating a Command in AutoCAD with C#
OR:
.....Select Build
After Build:
10
Creating a Command in AutoCAD with C#
14. Configuration...
For debugging
the configuration
should be set to
Debug...
Change to Release
for deploying...
Check Project
property settings after
setting configuration
to Release...
Setting the Platform to Any CPU will allow your project to run on both 32 and 64 bit
systems...
11
Creating a Command in AutoCAD with C#
12
Creating a Command in AutoCAD with C#
18. To Debug...
13
Creating a Command in AutoCAD with C#
4. Under Visual Basic select Autodesk, then Select the AutoCAD 2013 CSharp
plug-in template...
5. Set the Name of the new project and location to save then click OK...
14
Creating a Command in AutoCAD with C#
7. The project is created with AutoCAD .NET DLL references, debug setup
and some example startup commands.
15
Creating a Command in AutoCAD with C#
In startup project and in the wizard project the CommandMethod attribute was used to specify the
command(s). There is also a LispFunction attribute to define AutoLisp functions from a .NET AutoCAD
plugin.
16
Creating a Command in AutoCAD with C#
Command Flags:
17
Creating a Command in AutoCAD with C#
Using the project we just created we will add a couple commands and
LispFunctions that use some of the properties and constructors...
1. Start Visual Studio and open the project created from the AutoCAD plugin
template...
3. Scroll to the bottom of the MyCommands.cs file and before the last 2 closing
} brackets add the following command...
[CommandMethod("FirstCommand",
"_FirstCommand",
CommandFlags.Modal)]
public
void
fCommand()
{
Document
doc
=
Application.DocumentManager.MdiActiveDocument;
Database
db
=
doc.Database;
Editor
ed
=
doc.Editor;
This command illustrates creating a command that uses the Modal flag, with a group name of FirstGroup
and a global name...
Run the with debug Starting AutoCAD then Netload the DLL and run the command.
18
Creating a Command in AutoCAD with C#
} 4. Try/Catch is used in
catch
(System.Exception
ex) case of errors.
{
ed.WriteMessage("Exception
:
"
+
ex.Message); 5. Notice the + used to add
} multiple flags...
}
19
Creating a Command in AutoCAD with C#
5. Add the following Command to Use the Undefined Undefined Command flag causes
Flag then run debug and Netload the DLL into command to be loaded as undefined.
AutoCAD...
[CommandMethod("undefinedCommand","_undefinedCommand",
CommandFlags.Modal
|
CommandFlags.Undefined)]
public
void
uCommand()
{
Document
doc
=
Application.DocumentManager.MdiActiveDocument;
Database
db
=
doc.Database;
Editor
ed
=
doc.Editor;
20
Creating a Command in AutoCAD with C#
return
resBufOut; Run debug and Netload the DLL, then type the following Lisp expression
} at the AutoCAD command line: (setq myList (returnlist))
21
Creating a Command in AutoCAD with C#
d. This creates the form, now use toolbox and add a button...
f. double click on the button and a new method will be created...in the
method type: close()
22
Creating a Command in AutoCAD with C#
The previous command created a Modeless dialog, change the command to:
Application.ShowModalDialog(Application.MainWindow.Handle, frm);
23
Creating a Command in AutoCAD with C#
Create initialization code for command line prompts, splash screen, or other
initial settings required on loading
The Autodesk Runtime IExtensionApplication Interface is used to do one time initialization when the
plugin application is loaded. This is where you would do command line initialization of your application
and display a splash screen.
1. In the plugin that was created with the AutoCAD plugin wizard a class named
myPlugin.cs was created when the project was created. Go to the Solution
Explorer and double-click on that class to open it.
This code will print the TEXT to the command line when the application is loaded, build and debug to test
this code.
This Will display the location path of the application, there are several properties available that could be
displayed on initialization as well as start up instructions.
4. To create a Splash screen we Add a new form like we did above for displaying a
dialog.
24
Creating a Command in AutoCAD with C#
6. A new form is created that we will use for the splash screen. In the new forms
properties change the border style to NONE.
This will turn off the title and all of the buttons,
leaving an empty container.
25
Creating a Command in AutoCAD with C#
This will display the splash screen in the center of the screen for a few seconds when the Application is
loaded.