0

I have a windows form which is launched with an ESRI AddIn button (ArcGIS 10.2 and Windows 7). On my form I have a button to pick a point from the Map. I have added an ESRI BaseTool class to the project, which has an OnMouseDown event.

The problem is that I cannot get the Tool to run. Note that the tool is not on the ArcGIS Command Bar (like the button is) but the tool is still found by the Find(uid) process.

When the Tool was added to the project (using the ArcGIS Add BaseTool process) it didn't update the .esriaddinx file. I had to do that manually.

My Addin file is:

<AddIn language="CLR4.0" library="HVLR_Processing.dll" namespace="HVLR_Processing">
<ArcMap>
  <Commands>
    <Button id="RMS_HVLR_Processing_clsHVLR_Processing" class="clsHVLR_Processing" ...
    <Tool id="HVLR_PickTool" class="clsMapPick" category="Add-In Controls" caption="" message="" tip="" image="" />
  </Commands>
</ArcMap>

The clsMapClick code contains the OnMouseDown event.

To start the process I have tried many methods. I can retrieve the Tool but when I execute it (or assign it to the CurrentTool) nothing happens.

UID pUID;
ICommandItem pCmdItem;
ICommand pCmd;
clsMapPick pPick;
ITool pTool;

try
{
   this.WindowState = FormWindowState.Minimized;

   m_pApp.CurrentTool = null;

   pUID = new UIDClass();
   pUID.Value = "HVLR_PickTool";

   pCmdItem = m_pApp.Document.CommandBars.Find(pUID, false, false);

   if (pCmdItem != null)
   {
       m_pApp.CurrentTool = pCmdItem; // Nothing happens
       m_pApp.CurrentTool.Execute(); // Nothing happens
       m_pApp.CurrentTool.Refresh();
   }
}
catch (Exception ex)

Can anyone tell me how to get this tool to execute?

John M
  • 53
  • 7

1 Answers1

0

OK. Big stuff-up. You can't add a BaseTool to an ESRI AddIn; it's a COM object. What has to be done is:

  1. Create a new ESRI Tool class.

  2. Add a boolean variable to the class to indicate the mousedown event has fired.

  3. In the OnUpdate method put some code to continue until the mousedown event has fired.

  4. Create an OnMouseDown event handler by starting to type protected void On... and itellisense will allow you to select the event you want to track.

  5. Put the code you want to run in the OnMouseDown event handler and also set the boolean value to true.

Code:

public class clsMapPick : ESRI.ArcGIS.Desktop.AddIns.Tool
{
    private bool m_bIsFinished = false;
    private int m_iXPixel = -1;
    private int m_iYPixel = -1;
    //private string m_sError = "";
    //private bool m_bSuccess = true;

    public clsMapPick()
    {
    }

    protected override void OnActivate()
    {
        base.OnActivate();
        return;
    }

    protected override void OnUpdate()
    {
        if (m_bIsFinished)
        {

            m_bIsFinished = false;
            frmHVLR.m_dX = m_iXPixel;
            frmHVLR.m_dX = m_iYPixel;

        }
    }

    protected override void OnMouseDown(MouseEventArgs arg)
    {
        base.OnMouseDown(arg);

        m_iXPixel = arg.X;
        m_iYPixel = arg.Y;
        m_bIsFinished = true;
    }
}

In the form where the button for clicking on the map is fired:

        string sError = "";
        dPickedX = 0;
        dPickedY = 0;
        UID pUID;
        ICommandItem pCmdItem;
        ICommandBars pCmdBars;
        ICommand pCmd;
        ITool pTool;

        try
        {
            this.WindowState = FormWindowState.Minimized;

            pCmdBars = m_pApp.Document.CommandBars;

            pUID = new UIDClass();
            pUID.Value = HVLR_Processing.ThisAddIn.IDs.clsMapPick;
            pCmdItem = pCmdBars.Find(pUID);

            if (pCmdItem != null)
            {
                m_pApp.CurrentTool = pCmdItem;
                //pCmdItem.Execute();

                dPickedX = m_pMxDoc.CurrentLocation.X;
                dPickedY = m_pMxDoc.CurrentLocation.Y;
            }




            return sError;
        }

This is working fine for me now, the Tool class is being called but the OnMouseDown event isn't being fired.

If you know why I'd appreciate it.

John M
  • 53
  • 7