Creating an AutoCAD Command

ObjectARX applications typically define one or more commands that the user can call from the AutoCAD command prompt. For your tool application, you need only a single command to create tool catalogs, tool palettes, and tools. This command is used only once, when your application is first loaded. After your command has successfully created the tool components, AutoCAD saves their information to ATC files, along with the path to your ObjectARX module. Thereafter, the Tool Palette framework loads your application when it initializes the Tool Palettes window. Your tools and palettes become part of the Tool Palettes window's default configuration until they are removed.

For this project, you create a new AutoCAD command named CREATESIMPLE. You can define this command by clicking the ObjectARX wizard's ObjectARX Commands button, and then choosing New from the right-click menu in the ARX command list grid. Alternatively, if you did not use the ObjectARX wizard, you may create a registered command as described in ObjectARX Application Basics.

To create tool instances with AcadToolImpl

  1. Create a new AutoCAD command named CREATESIMPLE. If you started your project with the ObjectARX wizard, use its toolbar to create this command. Otherwise, use standard ObjectARX procedures for defining and registering AutoCAD commands.
  2. Provide the following code in your CREATESIMPLE command handler to create ATC content for a stock tool, a tool palette, and a command tool:
    CComObject<CSimpleTool> tool;
    // Don't redefine the stock tool if it's already in the catalog
    if (AcTcGetManager()->FindStockTool(tool.GetObjectCLSID()) !=
            NULL)
    return;
    AcTcCatalog* pCatalog =
        tool.CreateStockToolATC(_T(“SimpleCatalog”));
    AcTcPalette* pPalette = tool.CreatePaletteATC(pCatalog,
        _T(“SimplePalette”));
    tool.CreateCommandToolATC(pPalette, “Line”, “IDB_TOOL1”,
        “_LINE “);
  3. After the calls to create the tool and tool palette ATC content, add the following call to refresh the Tool Palettes window's display:
    AcTcGetManager()->LoadCatalogs(); // Refresh the palette in
                                      // AutoCAD.
  4. Make sure that the SimpleTool.h file is included in the file where you define the command handler.
  5. Rebuild your application.
  6. Test the CREATESIMPLE command in AutoCAD and inspect your new tool palette.

You now have a working tool palette with a single tool that starts the LINE command. To add more tools, simply make additional calls to any of the following methods:

To use CreateFlyoutToolATC(), you must have created a shape catalog. You can do this by calling CreateShapeCatalogATC(). See the Adding Flyouts section for information on using these functions.

The CreateToolATC() function provides an optional parameter, szToolImageOverride, that lets you override your stock tool's default icon image setting by providing an image filename. The CreateFlyoutToolATC() function's optional szToolNameOverride parameter allows you to override the tool's default name.

Because this sample application hasn't used your COM object's properties, you don't encounter problems with uninitialized variables. However, most Tool Palette applications create tools that rely on custom data. To initialize the member variables that contain custom tool data, your coclass needs to override the AcadToolImpl::New() method. You should do the initialization in the New() method, rather than in the coclass constructor, because the constructor is used only to create the stock tool.

To override the New() method

  1. In the SimpleTool.h file, declare a standard override of New() in the public section of your coclass declaration:
    STDMETHOD(New)()
    {
    }
  2. Implement this method, adding initializations for your property member variables. The New() method for this project is defined inline in the class declaration, as follows:
    STDMETHOD(New)()
    {
        tcscpy(m_tcCmdName, _T("_LINE"));
        // Add other property variable initializations here...
        return S_OK;
    }
  3. In your implementation of the CREATESIMPLE AutoCAD command, call New() on your new tool before creating catalog, palette, and tool ATC entries.

The following listing shows the finished command handler code:

CComObject<CSimpleTool> tool;
// Don't redefine the stock tool if it's already in the catalog
if (AcTcGetManager()->FindStockTool(tool.GetObjectCLSID()) !=
        NULL)
return;
if (SUCCEEDED(tool.New())) {
    AcTcCatalog* pCatalog =         tool.CreateStockToolATC(_T(“SimpleCatalog”));     AcTcPalette* pPalette = tool.CreatePaletteATC(pCatalog,         _T(“SimplePalette”));     tool.CreateCommandToolATC(pPalette, “Line”, “IDB_TOOL1”,         “_LINE “);     AcTcGetManager()->LoadCatalogs(); // Refresh the palette in                                       // AutoCAD
}

By calling New(), you set the tool's properties to their default values. You can then reset some or all of those properties to specialized values before creating tools.