Kean Walmsley

July 2009

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  

Twitter Updates

    follow me on Twitter



    « 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:

    Comments

    So much talk about LISP lately I wonder about progress on making the LISP interface UNICODE aware? Any updates?

    What is your main concern with the way LISP is working with UNICODE? I know that functions that call through to ActiveX/COM Automation don't support the passing of UNICODE strings (when setting text in a drawing to a UNICODE value, for example).

    Please let me know what specifically the concern is, and I'll try to get you a response. There are no current plans to improve the level of UNICODE support in LISP (even with respect to COM/ActiveX support), so if there are major issues it would be good to understand them and pass that feedback on to Engineering.

    Regards,

    Kean

    hai plse send me if any .net programs for cad customisation if u had i just want to know these

    I am developing an autolisp application which requires creation of a text entity that includes the greek "DELTA" symbol. I would like to know how to acomplish this task. Maybe using \u+nnnn?
    Thanks

    A little off topic, but anyway... [In future, I'd suggest posting this type of question to the discussion groups or contacting ADN support.]

    The Character Map system tool in Windows will tell you the code (at least, that's the way I do it). \U+0394 will get you capital Delta and \U+03B4 will get you lowercase Delta.

    Regards,

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search