You are on page 1of 12

11/29/2006 - 8:00 am - 9:30 am Room:Delfino - 4001 (PTD Campus)

.NET Command School


Doug Goforth - National MultiTech LTD, CadPLUS

CP21-2 Learn to make your custom VB or C# .NET commands look and feel just like "regular" AutoCAD. This
class is all about prompts, dialogs, and interaction with the users using the 2007 .NET API. We will
take a detailed look at creating looping command prompts for AutoCAD. You will learn how to present
the user with a choice of geometry and keyword responses, perform various actions depending on the
input, and then repeat the prompt for the next input.

About the Speaker:


Doug has been involved in the evolution of CAD for the facilities market for more than 20 years. He has
led teams in converting and creating tens of millions of square feet of drawings between various CAD
platforms. Doug has created and implemented CAD and space standards and procedures for major
international companies working across sites and continents. He is consultant and programmer for
process and CAD managers at IBM, BellSouth, and many familiar engineering firms in the U.S.,
Canada, Europe, Latin America, and Australia. Doug has long since lost count of the number of
programs written for AutoCAD using .NET, VB6, LISP, and ObjectARX.
doug.goforth@nationalmultitech.com

Stay Connect to AU all year at www.autodesk.com/AUOnline


.NET Command School

Can the Average User Tell the Difference between Your Commands and AutoCAD’s?
AutoCAD follows standard conventions when interacting with the user. Autodesk has many years
of observation and feedback that has helped in determining how to prompt for user input. Novice
and experienced users alike will expect to be presented with prompts and dialogs in the
conventions that they are accustomed to. You can gain a great advantage in usability for your
application by anticipating what the user will expect when prompted for input while using AutoCAD.

Prompts and Dialog Parts You Should Emulate


Select objects:
Specify a point:
Specify first point:
Specify height <0.200>:
Specify base point or [Displacement] <Displacement>:
Enter block name or [?]:
Enter an option [Next/Previous/Go/eXit] <N>:

.NET Managed Code Reference Requirements


1. acdbmdg.dll (ObjectDBX .NET Managed Wrapper)
2. acmgd.dll (AutoCAD .NET Managed Wrapper)

Autodesk Namespaces You Will Want to Include

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices
Imports DbTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager
Imports Autodesk.AutoCAD.Geometry

2
.NET Command School

Defining your Command with CommandMethod


CommandMethod is a member of Autodesk.AutoCAD.Runtime. It makes your command available
from the AutoCAD Command: prompt.

<CommandMethod("AUSCHOOL")>Public Sub myAuschoolCommand()


‘Some lines of code
End Sub

Anatomy of a Prompt
Prompts in AutoCAD follow a set format:

Setting up the Prompt Options


Getting the<default> to
Prompting for a point with keywords
come at the end of the
prompt
Dim prPtOpts As New PromptPointOptions(vbLf + _
"Please do something now “ + _
“[First thing/Second thing] <F>:") Some objects have methods
prPtOpts.Keywords.Add("First thing") defined for DefaultValue:
prPtOpts.Keywords.Add("Second thing") PromptAngleOptions
prPtOpts.AllowNone = True 'Allow ENTER only PromptDistanceOptions
prPtOpts.AppendKeywordsToMessage = False
PromptDoubleOptions
Prompting for a number with keywords and default value PromptIntegerOptions
PromptStringOptions
Dim prIntOpts As New PromptIntegerOptions(vbLf + _
"Please type a number:") These objects allow you to use
prIntOpts.Keywords.Add("First thing") AppendKeywordsToMessage
prIntOpts.Keywords.Add("Second thing") and DefaultValue to build the
prIntOpts.AppendKeywordsToMessage = True
prIntOpts.DefaultValue = 1 prompt in the correct order.
For all other objects you will
have to build the entire prompt
yourself if you plan to use
keywords with a default value.

3
.NET Command School

Displaying the Prompt


Use the Editor object to display the prompt. Each GETxxx method must be matched with its
corresponding Prompt Result Type. This will be displayed by VS2005’s intellisense feature.
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim prPtResult As PromptPointResult
prPtResult = ed.GetPoint(prPtOpts)

Getting the User Response


Four ways the user can respond to a command line prompt:

1. _____________________________________________
2. _____________________________________________
3. _____________________________________________
4. _____________________________________________
Select Case prPtResult.Status
Case PromptStatus.OK 'got expected response
Case PromptStatus.Keyword 'keyword was entered
Case PromptStatus.None 'user pressed ENTER
Case PromptStatus.Cancel 'user cancelled command
Case Else
End Select

Entity Prompting and Responding


GetEntity will return an ObjectID (Try/Catch blocks have been omitted for clarity):
Dim prEntOpts As New PromptEntityOptions("Pick an object: ")
Dim prEntResult As PromptEntityResult
prEntResult = ed.GetEntity(prEntOpts)
If prEntResult.Status <> PromptStatus.Error Then
Dim entid As ObjectId = prEntResult.ObjectId
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As _
Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim myT As Transaction = tm.StartTransaction()
Dim entity As _
Entity = CType(tm.GetObject(entid, OpenMode.ForRead, True), Entity)
ed.WriteMessage("You selected: " + entity.GetType().FullName)
myT.Commit()
End If

4
.NET Command School

A Word about Using the Transaction Manager

The Transaction Manager provides a clean way to access the AutoCAD database and manage the
manipulation of AutoCAD objects in a drawing. The Transaction Manager makes AutoCAD “aware”
of your transaction, makes possible the “Undoing” of your commands, and facilitates safe memory
management.
Use the Transaction Manager in the following sequence:
1. Initialize the Transaction Manager
2. Use the TM to operate on the entity (query, create, modify)
3. Commit the Transaction
Using the Transaction Manager to get the selected object as an Entity type insures that a proper
object will be operated on without crashing the AutoCAD session.

Getting String Input from a Dialog


Use Modal dialogs to constrain user attention to the form:

Set form properties as shown below to eliminate confusion and erroneous navigation when
displayed. Modal dialogs should not be minimizable since this would prevent the user from
interacting with AutoCAD.

Also, set the DialogResult property of the OK button to DialogResult.OK


Dim strName As String = ""
Dim dlgName As New frmName
dlgname.StartPosition=Windows.Forms.FormStartPosition.CenterParent
Application.ShowModalDialog(dlgName)
If dlgName.DialogResult = Windows.Forms.DialogResult.OK Then
strName = dlgName.TextBox1.Text
End If
dlgName.Dispose()
ed.WriteMessage("You entered " + strName)
A StartPosition of CenterParent will automatically center the form in the center of the AutoCAD
window.

5
.NET Command School

Getting Drawing Input from a Dialog


Use a Modal dialog for drawing selections. You must hide the form when interacting with AutoCAD.

This dialog is designed to emulate the Pick Point feature on the Block Definition dialog in AutoCAD.

6
.NET Command School

Making the Prompt Loop until User Exit


Always exit looping on ESC, an error, or expected end of recurring prompt (Try/Catch blocks and
Case Else have been omitted for clarity and space).

Question: Why is prPtOpts.Message redefined inside the loop?


_________________________________________________________
_________________________________________________________

7
.NET Command School

Launching by Prompt or Dialog


One AutoCAD convention you see a lot of is being able to launch a command with either a prompt
or a dialog depending on whether or not you use a dash in front of the command.
You can accomplish this by defining two commands in you application:
‘Start command with a dialog
<CommandMethod("MYCOMMAND")> Public Function myCommandDialog()

-and-
‘Start command with command line prompts
<CommandMethod("-MYCOMMAND")> Public Function myComand()

Each command method can be used to call common bits of code.

Making your Command Transparent (and other optional items)


You command can be made transparent by specifying a command flag to the CommandMethod
function:

<CommandMethod("AUSCHOOL", CommandFlags.Transparent)> _
Public Sub myAuschoolCommand()

In addition to transparent commands, you can limit you application to SDI, only ModelSpace and
more.

8
.NET Command School

A Command to Load a Custom Toolbar (CUI file)


This example shows how to load a CUI file from a command. The file path must be explicitly
specified, or it must exist in the search path.
This command does double duty:
1. If the CUI file is not loaded, it loads the CUI file
2. If the toolbar is loaded, but turned off, it turns it on.

9
.NET Command School

A Listing of Getxxx Methods for AutoCAD acgmd.dll


Use the Object Browser (F2) in Visual Studio to display a listing of Editor Members defined in
acmgd.dll.

10

You might also like