How to add your knowledge

Running Commands from the Toolbox

    Table of contents
    No headers

    The recommended method to expose a AutoCAD Civil 3D extension to users is to add it to the Toolbox tab in Toolspace, by creating a toolbox macro. The Toolbox handles loading the .NET assembly or ARX DLL containing the commands.

    There are two execution types that a toolbox macro can have:

    1. CMD - the command name is sent to the command line to execute. This is the recommended execution type for both .NET and ARX commands.
    2. .NET - a method name is located, via Reflection, in the assembly, and is executed directly. No attribute flags are read and the code is always run in application context. (A command executed from the command line runs in the drawing context by default). Therefore, code run as as a .NET execute type must always be a static method, and must handle its own document locking.
    Note

    It is safe to explicitly lock a document, even if the code might be run in document context.

    Here is an example of how to handle document locking:

    static void setPrecision()
    {
        using (Autodesk.AutoCAD.ApplicationServices.DocumentLock locker = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
        {
            // perform any document / database modifications here
            CivilApplication.ActiveDocument.Settings.DrawingSettings.AmbientSettings.Station.Precision.Value = 2;
        }
    }
    

    To create a toolbox macro for a compiled command using the Toolbox Editor

    1. Click the Toolbox tab in Toolspace.
    2. Click to open the Toolbox Editor.
    3. Right-click Miscellaneous Utilities and click New Category.
    4. Right-click the new category, and click New Tool.
    5. Select the new tool, and enter its name.
    6. For Execute Type, click the drop-down and select CMD or .NET.
      Note

      CMD is the recommended execution type in most cases, because you do not need to explicitly handle document locking. See the discussion above.

    7. For Execute File, browse to the .NET assembly or ARX DLL that contains the command.
    8. For Macro Name, enter:
      • Name of the command to run, if the execute type is CMD
      • Name of the method to run, if the execute type is .NET.
    9. Optionally, enter a help file and help topic for the command.
    10. Click to apply the changes and close the editor.

    After a command has been set up, it can be run by right-clicking it and clicking Execute.