Adding a Custom Tool

You can put the New() method and custom properties to work by adding a specialized tool with custom behavior. In the previous procedure, you created a command tool that launches the LINE command. The following procedure shows how to add a tool that uses ObjectARX functionality to create a line. It also uses the tool's properties to define the endpoints of the line.

Basic steps for adding this tool include

To add new properties

  1. Using Visual Studio's ATL wizard, add a new DOUBLE property named StartX that supports both get and put functions.
  2. In SimpleTool.h, add a public data member to the CSimpleTool coclass to represent the new property:
    double m_StartX;
  3. If the ATL wizard has not done so already, add get and put public method declarations to the coclass:
    STDMETHOD(get_StartX)(/*out, retval*/ DOUBLE* pVal);
    STDMETHOD(put_StartX)(/*in*/ DOUBLE newVal);
  4. In SimpleTool.cpp, implement get and put methods for your property, as shown below:
    STDMETHODIMP CSimpleTool:get_StartX(DOUBLE* pVal)
    {
        (*pVal) = m_StartX;
        return S_OK;
    }
    STDMETHODIMP CSimpleTool:put_StartX(DOUBLE newVal)
    {
        m_StartX = newVal;
        return S_OK;
    }
  5. Repeat steps 1 through 4 for each of the following properties, being careful to revise the new member variable name and get/put method names as appropriate:
    StartY
    StartZ
    EndX
    EndY
    EndZ

Now that new properties are available, you can use the corresponding member variables to implement the line creation routine.

To override the executeCallback() function

  1. In SimpleTool.cpp, add an empty implementation of executeCallback():
    STDMETHODIMP CSimpleTool::executeCallback()
    {
    }
  2. Above the return statement, add code that uses the tool's start point and endpoint property values to create AcGePoint3d instances:
    AcGePoint3d ptStart(m_StartX, m_StartY, m_StartZ);
    AcGePoint3d ptEnd(m_EndX, m_EndY, m_EndZ);
  3. Using the ptStart and ptEnd variables, construct a new AcDbLine object:
    AcDbLine* pLine = new AcDbLine(ptStart, ptEnd);
  4. Add code that uses pLine to create a new database-resident AcDbLine entity:
    AcDbBlockTable *pBlockTable;
    acdbHostApplicationServices()->workingDatabase()
        ->getSymbolTable(pBlockTable, AcDb::kForRead);
    AcDbBlockTableRecord *pBlockTableRecord;
    pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
        AcDb::kForWrite);
    pBlockTable->close();
    AcDbObjectId lineId;
    pBlockTableRecord->appendAcDbEntity(lineId, pLine);
    pBlockTableRecord->close();
    pLine->close();
  5. Return a value indicating success:
    return S_OK;
  6. Rebuild the application.

    Here is the finished executeCallback() function:

    STDMETHODIMP CSimpleTool::executeCallback()
    {
        AcGePoint3d ptStart(m_StartX, m_StartY, m_StartZ);
        AcGePoint3d ptEnd(m_EndX, m_EndY, m_EndZ);
        AcDbLine* pLine = new AcDbLine(ptStart, ptEnd);
        AcDbBlockTable *pBlockTable;
        acdbHostApplicationServices()->workingDatabase()
            ->getSymbolTable(pBlockTable, AcDb::kForRead);
        AcDbBlockTableRecord *pBlockTableRecord;
        pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
            AcDb::kForWrite);
        pBlockTable->close();
        AcDbObjectId lineId;
        pBlockTableRecord->appendAcDbEntity(lineId, pLine);
        pBlockTableRecord->close();
        pLine->close();
        return S_OK;
    }

To link the executeCallback() functionality to a tool, you call the CreateToolATC() method in the CREATESIMPLE command.

  1. In the SimpleTool project, go to your command handler for the CREATESIMPLE AutoCAD command.
  2. Add the following line to your command implementation immediately after the CreateCommandToolATC() function call:
    tool.CreateToolATC(pPalette);
  3. Rebuild your application and test it in a new instance of AutoCAD.

When your new tool appears in the Tool Palettes window, right-click it and select Properties. Notice that the newly added start point and endpoint properties appear. If you modify their values in this UI, the tool will use the revised values when it constructs its next line.