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.