October 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  










« Calling AutoCAD commands from .NET | Main | Custom objects in .NET »

September 01, 2006

Passing arguments to .NET-defined commands

Another case of planets aligning: two different people have asked me this question in two days...

It's quite common for commands to require users to provide additional input during execution. This information might be passed in via a script or a (command) from LISP, or it may simply be typed manually or pasted in by the user.

The fact is, though, that commands don't actually take arguments. It may seem like they do, but they don't. What they do is ask for user input using dialogs or the command-line.

Here are a few tips on how to support passing of information into your commands...

Define a version of your command that asks for input via the command-line

It's very easy to define beautiful UIs in .NET applications. You absolutely should do so. But it's also helpful to provide an alternative that can be called via the command-line and from scripts. I'd suggest a couple of things here for your commands:

  • Define a standard version (e.g. "MYCOMMAND") and a command-line version ("-MYCOMMAND"). It's common to prefix command-line versions of commands in AutoCAD with a hyphen (see -PURGE vs. PURGE, for instance).
  • An alternative is to check for the values of FILEDIA and CMDDIA system variables - see the AutoCAD documentation on these commands to understand what effect they're meant to have on commands.

When implementing a command-line version of your command, you simply use the standard user input functions (GetXXX()) available in the Autodesk.AutoCAD.EditorInput namespace. Here's some VB.NET code showing this:

    <CommandMethod("TST")> _

    Public Sub Test()

        Dim ed As Editor

        ed = Application.DocumentManager.MdiActiveDocument.Editor

        Dim name As PromptResult = ed.GetString(vbCr + "Enter name: ")

        ed.WriteMessage("You entered: " + name.StringResult)

    End Sub

When it's run, you get this (typed text in red):

Command: tst

Enter name: Hello

You entered: Hello

Command:

By the way, I tend to put a vbCr (or "\n" in ObjectARX) in front of prompts being used in GetXXX() functions, as it's also possible to terminate text input with a space in AutoCAD: this means that even is a space is entered to launch the command, the prompt string displays on a new line.

Define a LISP version of your command

The <LispFunction> attribute allows you to declare .NET functions as LISP-callable. A very good technique is to separate the guts of your command into a separate function that is then called both by your command (after it has asked the user for the necessary input) and by the LISP-registered function, which unpackages the arguments passed into it.

To understand more about how to implement LISP-callable functions in .NET, see this previous post.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83452464869e200d834ad69e153ef

Listed below are links to weblogs that reference Passing arguments to .NET-defined commands:

blog comments powered by Disqus

Feed/Share

10 Random Posts