Change Current Tool In ArcMap

2919
3
Jump to solution
12-26-2013 11:14 AM
BrendanDwyer
Occasional Contributor
While in an edit session, I want to change the current tool to the Straight Segment tool (aka esriEditor.SketchStraightCommand). 

I created a toolbar with two buttons.  When the first button is clicked it starts the edit session, gets a line feature class in the TOC, makes it the current edit layer and edit template. 

When the next button is clicked, I try to set the current tool to the Straight Segment tool using the following code:

editTemplate = btn1.editor.CurrentTemplate;
try
{
                IApplication app = ArcMap.Application;
                ESRI.ArcGIS.Framework.ICommandBars commandBars = app.Document.CommandBars;
                ESRI.ArcGIS.esriSystem.UID commandID = new UIDClass();
                commandID.Value = "esriEditor.SketchStraightCommand";
                ICommandItem commandItem = commandBars.Find(commandID, false, false);
                if (commandItem != null)
                {
                    app.CurrentTool = commandItem; //this is the line that sets off the error
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

The error message I get is: "System.ArgumentException: Value does not fall within the expected range."

Digging a little deeper in the debugger, I find this in the commandItem Locals window:
commandItem.Action threw and exception of type 'System.Runtime.InteropServices.COMException.'  {"This method cannot be called on built in commands."}

I've been digging around but I'm not sure what this means.  Is it impossible to set the Straight Segment tool as the current tool from an Add-In button or command? 

Thanks,

Brendan
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Brendan,
The shape constructors (straight, arc, trace etc) are commands that set the mode on the edit sketch for the current construction tool (line, polygon etc). In your example you are trying to set a command as the current tool and hence the error.

You can fix this by calling execute on the commanditem to 'click' the button.

      if (commandItem != null)       {         //app.CurrentTool = commandItem; //this is the line that sets off the error         commandItem.Execute();       }

View solution in original post

0 Kudos
3 Replies
by Anonymous User
Not applicable
Brendan,
The shape constructors (straight, arc, trace etc) are commands that set the mode on the edit sketch for the current construction tool (line, polygon etc). In your example you are trying to set a command as the current tool and hence the error.

You can fix this by calling execute on the commanditem to 'click' the button.

      if (commandItem != null)       {         //app.CurrentTool = commandItem; //this is the line that sets off the error         commandItem.Execute();       }
0 Kudos
BrendanDwyer
Occasional Contributor
Sean,
Thanks for your reply.  I sorted it out with the infromation you posted.  I'll post the working code shortly.
0 Kudos
BrendanDwyer
Occasional Contributor
Here's the c# code.

When a user selects a layer to edit, I assign a default tool to edit with.  If the layer is a line, use the straight sketch command:

IEditLayers editLayers = toolExtension.editor as IEditLayers;
                    editLayers.SetCurrentLayer(selectedLayer, subtype_number);
                    IEditTemplateFactory editTemplateFactory = new EditTemplateFactoryClass();
                    editTemplate = editTemplateFactory.Create(layer.Name, layer);
                    IFeatureClass s_featureClass = selectedLayer.FeatureClass;
                    if (s_featureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                    {
                        Guid g = new Guid("{69DAD63E-3B9B-401D-84AE-27A84F720BB2}");// this is the esriEditor.SketchStraightCommand
                        editTemplate.Tool = g;
                    }

When the user wants to start adding features, get the geometry type and set the ICommandItem accordingly.

app = ArcMap.Application;
            ICommandBars documentBars = app.Document.CommandBars;
            UID cmdID = new UIDClass();
            cmdID.Value = "esriEditor.PointFeatureTool";
            editTemplate = toolExtension.editor.CurrentTemplate;
            IFeatureLayer m_flayer = editTemplate.Layer as IFeatureLayer;
            IFeatureClass m_fclass = m_flayer.FeatureClass;
            if (m_fclass.ShapeType == esriGeometryType.esriGeometryPolyline)
            {
                cmdID.Value = "esriEditor.LineFeatureTool";
            }
            if (m_fclass.ShapeType == esriGeometryType.esriGeometryPoint)
            {
                cmdID.Value = "esriEditor.PointFeatureTool";
            }
            ICommandItem cmdItem = documentBars.Find(cmdID, false, false);
            app.CurrentTool = cmdItem;
0 Kudos