Getting AutoCAD’s command-line arguments using .NET

I'm starting to get ready for a trip to the US: I'm heading out on Monday and will be home on Thursday of the following week. While I expect I'll have time to write a few posts during the trip – thank you, jetlag! – I'm going to write a couple of very simple ones today and tomorrow, just to free up time for preparations.

Both posts came out of an internal email discussion on trying to determine the language of the AutoCAD product your application is running in. The initial question was focused on getting this information from the command-line arguments passed into AutoCAD, hence the topic of this first post.

Adam Nagy, from DevTech EMEA, suggested the following approach while acknowledging there might be issues if choosing to use the same object to get the command-line arguments themselves.

Here are two C# commands to get the command-line information from System.Environment:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

 

namespace CommandLineArgs

{

  public class Commands

  {

    [CommandMethod("CL")]

    public static void CommandLine()

    {

      Application.DocumentManager.MdiActiveDocument.Editor.

        WriteMessage(

          "\nCommand-line at AutoCAD launch was [{0}].",

          System.Environment.CommandLine

        );

    }

 

    [CommandMethod("CLA")]

    public static void CommandLineArguments()

    {

      var ed = Application.DocumentManager.MdiActiveDocument.Editor;

      var args = System.Environment.GetCommandLineArgs();

      ed.WriteMessage(

        "\nCommand-line arguments at AutoCAD launch were "

      );

      foreach (var arg in args)

      {

        ed.WriteMessage("[{0}] ", arg);

      }

    }

  }

}

If we run the first one we can see the full command-line, while the second chunks it up into separate arguments. Which you use is ultimately up to you, but you should bear in mind that – as explained in the StackOverflow discussion Adam mentioned – due to GetCommandLineArgs()'s use of CommandLineToArgvW(), you might get somewhat bizarre escaping behaviour especially if the command-line arguments contain backslashes.

Command: CL

Command-line at AutoCAD launch was ["C:\Program Files\Autodesk\AutoCAD 2014\acad.exe" 
/product "ACAD" /language "en-US"].

Command: CLA

Command-line arguments at AutoCAD launch were [C:\Program Files\Autodesk\AutoCAD 2014\acad.exe] [/product] [ACAD] [/language] [en-US]

As you can see, the results are OK for standard AutoCAD usage. That said, for the particular scenario of wanting to determining the language of your AutoCAD product, there is a better way. More on that tomorrow.

4 responses to “Getting AutoCAD’s command-line arguments using .NET”

  1. wow, I gotta look at that Environment object more. Helpful post.

  2. Very interesting... I'm having an issue where I start AutoCAD 2014 via a shell command. I use the /product switch to specify ACAD. All of that works great. The only problem is I have Plant 3D 2014 installed on this machine as well. If Plant was the last 2014 application opened before I run the shell command, AutoCAD starts perfectly, but a Plant 3D license is pulled. This is a very exspensive issue... Any ideas?

    1. Kean Walmsley Avatar

      Hmm, I can see why that would be a problem. I don't know enough about how network licensing works between AutoCAD and its verticals, unfortunately. If you're an ADN member you might try pinging them, otherwise I suggest posting to the appropriate discussion group or raising this through your usual support channel.

      Kean

      1. Thanks for the quick reply! I've raised the issue with support. Just waiting on a reply.
        Steve

Leave a Reply to Kean Walmsley Cancel reply

Your email address will not be published. Required fields are marked *