snapping in custom tools (ArcGIS Engine C#.net)

1747
2
09-26-2012 06:51 PM
TomaCasa
New Contributor III
Hi,
I'm kindof new to ArcObjects development but i'm working in the ArcGIS Engine environment (C#.net) and
trying to create a custom tool with snapping. currently having a problem getting the snapping extension..

in the documentation it says to use IHookHelper2    -> however I don't seem to be able to CAST the hook into the IHookHelper2..
comes back with a null...

m_hookHelper2 = (IHookHelper2)hook;   <- does not work...

I also tried to get the IApplication / mapDocument from the hook... which also did not work 😞


(this is the documentation code..... need to know where / how to get the m_hookHelper2)

[C#]
IExtensionManager extensionManager = m_hookHelper2.ExtensionManager;
if (extensionManager != null)
{
    UID guid = new UIDClass();
    guid.Value = "{E07B4C52-C894-4558-B8D4-D4050018D1DA}"; //Snapping extension.
    IExtension extension = extensionManager.FindExtension(guid);
    m_SnappingEnvironment = extension as ISnappingEnvironment;
}
0 Kudos
2 Replies
JohnHauck
Occasional Contributor II
For your tool did you start with one of the ArcGIS templates as discussed here in Creating commands and tools? If not using one of these templates will stub out the appropriate code for establishing the hook for you. For example with a standard Map Control tool implementation you would see the following for OnCreate:

        private IHookHelper m_hookHelper = null;

        public override void OnCreate(object hook)
        {
            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;

                if (m_hookHelper.ActiveView == null)
                {
                    m_hookHelper = null;
                }
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }


Note that HookHelperClass is first instantiated then the hook is passed to the IHookHelper.Hook rather than casting the hook directly as IHookHelper as you were doing in your code. With the code above you should be able to cast m_hookHelper as IHookHelper2 without any issues as long as your initial hook is occurring at an appropriate time such as OnCreate for a tool.
0 Kudos
TomaCasa
New Contributor III
yeah i was using the ArcGIS templates for command and tools...
ok thanks for that... got it working by casting the m_hookHelper into IHookHelper2..

m_hookHelper2 = (IHookHelper2)m_hookHelper;

seems to grab the snapping extension now 🙂

will post back if i have any more issues...
0 Kudos