4

I am trying to call ZoomToLayer from my custom code (shown below).

ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UID();
ICommandItem CmdItem;

//pUID.Value = "{18DF94D9-0F8A-11D2-94B1-080009EEBECB}";
pUID.Value = "esriArcMapUI.LayerContextMenuItems";
pUID.SubType = 7;

CmdItem = IApplication.Document.CommandBars.Find(pUID, true, false);

CmdItem.Execute();

Upon investigation I found that CommandItem.Action is throwing COMException:

'CmdItem.Action' threw an exception of type 'System.Runtime.InteropServices.COMException - This method cannot be called on built in commands.

I am trying to use it as a command and not tool...hence I cannot understand the exception.

Any ideas why it's happening?

  • In what version and ESRI product are you using the code? – MathiasWestin May 13 '11 at 7:04
  • I am using ESRI 9.2 – ujjwalesri May 16 '11 at 10:22
  • Here is a snippet ArcGIS 10, I guess it's the same as for 9.2: help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/… only difference I can see is that you are not using a recursive find. – MathiasWestin May 16 '11 at 13:53
  • @mathias I tried with recursive find as well, but same result. Basically the correct command item is being return, but it's action property is throwing exception...I dunno why :( – ujjwalesri May 17 '11 at 9:34
2

You also need to set the IMxDocument.ContextItem to the layer that you want to zoom to. See ESRI forum: Re: Zooming In On A Shapefile To Highlight Areas

  • Yohoooooo....it worked...! – ujjwalesri May 18 '11 at 4:48
2

If you have the Layer of interest in your table of contents you could go this way and not have to mess with ContextMenuItems by trying this:

        ILayer layer = null;
        for (int a = 0; a < layercount; a++)
        {
            layer = focusmap.get_Layer(a);
            if (layer.Name.Contains("YOURLAYERNAMEHERE"))
            {

                break;
            }
        }
        ArcMap.Document.FocusMap.ActiveView.Extent = layer.AreaofInterest;
        ArcMap.Document.FocusMap.ActiveView.Refresh();
  • Yes eventually I ended up doing it manualy :) – ujjwalesri May 18 '11 at 4:29
1

Ok, I have not done VB.NET, but you are calling

CmdItem = IApplication.Document.CommandBars.Find(pUID, true, false);

on an the interface definition of IApplication and not an instance of the coclass

  • Hi,The code is correct, but while posting it here a reference to another utility application was removed, hence your concern :) – ujjwalesri May 16 '11 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.