Where did the Sketch tool go?

872
9
09-13-2010 05:45 PM
ITSupport
New Contributor
Hi

I am trying to convert my ArcObjects 9.3.1 code to 10. I have a simple button that when clicked creates a new shapefile & adds it to the TOC. It then starts the editor & selects the Sketch Tool.
The user then draws a polygon and the OnSketchEventHanlder then calculates the selected area the user just drew on the new shapefile. This is simple stuff but it wont work in ArcMap 10. Now I have to contend with what looks like templates & shapeconstructors. Ive managed to create a new template & add it to the template array, but I have no idea how to specifiy the editor to use the polygon construtor tool. The editor starts but the new shapefile is not selected & the polygon tools is not selected. How do I get it to use my template & use the polygon tool? Do i even need to do it this way with version 10? I see you can create a temporary feature within a template so I may not even need to create the new shapefile to use for editing???
Here's what I have so far

internal static IEditor get_Editor(IApplication application)
{
    IEditor editor = null;
    UID uidEditor = new UIDClass();
    uidEditor.Value = "esriEditor.Editor";
    editor =  application.FindExtensionByCLSID(uidEditor) as IEditor;
    return editor;
}

internal static void StartEditing(IApplication application, string layerName)
{
        IEditor3 editor = get_Editor(application) as IEditor3;

        //find the layer to edit
        IMxDocument mxDocument = (IMxDocument)application.Document;
        IFeatureLayer featureLayer = (IFeatureLayer)get_Layer(mxDocument, layerName);

        //check for existing templates & remove
        editor.RemoveAllTemplatesInLayer(featureLayer);

        //create a single template for the editable layer
        IEditTemplateFactory editTemplateFactory = new EditTemplateFactoryClass();
        IEditTemplate editTemplate = editTemplateFactory.Create("PolygonAreaTemplate", featureLayer);

        //Add the template
        IArray templateArray = new ArrayClass();
        templateArray.Add(editTemplate);
        editor.AddTemplates(templateArray);

       //Need some more code here use my template & use the polygon constructor tool

       IDataset dataset = (IDataset)featureLayer.FeatureClass;
       editor.StartEditing(dataset.Workspace);
}

Cheers
Tony
0 Kudos
9 Replies
JohnHauck
Occasional Contributor II
0 Kudos
ITSupport
New Contributor
Hi John

yep, I read all that but it still hasn't helped. I can't find any examples either.
I'll just have to wait until others have the same problem which I'm guessing wont take
long as the editor workflow is just so different now as to what it was in 9.3.

Cheers
Tony
0 Kudos
JohnHauck
Occasional Contributor II
Try the following:

Using feature templates
0 Kudos
ITSupport
New Contributor
I have been able to create an editTemplate and start the editor but have not for the life of me been able to get the polygon contruction tool to start. On the editor toolbar I have to manually click the "Create Features" button which shows my editTemplate, I then have to manually click my editTemplate which then selects the Polygon construction tool. As to how you do this in code I'm unsure. I have tried IEditSketch & the StraightConstructor but still no joy.

IEditSketch3 editSketch3 = editor as IEditSketch3;
editSketch3.GeometryType = esriGeometryType.esriGeometryPolygon;           

IShapeConstructor shapeConstructor = new StraightConstructorClass();
shapeConstructor.Initialize(editor);               
editSketch3.ShapeConstructor = shapeConstructor;   //This line throws an error "Item not found in collection"        
shapeConstructor.Activate();
0 Kudos
by Anonymous User
Not applicable
Hi Tony,

Your original code was on the right track, i'll try and fill in the blanks.

Once you have created the template you may want to set some default attributes for it via the methods on IEditTemplate.
You can also set the default tool this template uses via IEditTemplate.Tool

Setting the current template for the editor is done through IEditor3.CurrentTemplate.
If the template has a default tool set then arcmap will activate that tool, otherwise you can always set it later via IApplication.CurrentTool.
Once a construction tool is active (presumably esriEditor.PolygonFeatureTool aka {8F79967B-66A0-4A1C-B884-F44BC7E26921}) you can activate a different shape constructor if you dont want the straight constructor by default.

This can all be done as an add-in button. Hope this helps.
0 Kudos
ITSupport
New Contributor
Thanks Sean that helped a lot. I have finally got the editor going.
Here's my code to help anyone else.

IEditor3 editor = get_Editor(application) as IEditor3;

IEditTemplateFactory editTemplateFactory = new EditTemplateFactoryClass();
IEditTemplate editTemplate = editTemplateFactory.Create("PolygonAreaTemplate", featureLayer);
Guid polygonFeatureTool = new Guid("{8F79967B-66A0-4A1C-B884-F44BC7E26921}");
editTemplate.Tool = polygonFeatureTool;

IArray templateArray = new ArrayClass();
templateArray.Add(editTemplate);
editor.AddTemplates(templateArray); 

editor.StartEditing(dataset.Workspace);
editor.CurrentTemplate = editTemplate;
0 Kudos
ITSupport
New Contributor
I can't see anywhere to edit my post so I will reply to my own thread.

A tip: You need to set the current template AFTER you start the editor.
If you set the current template before starting the editor the editTemplate tool will not activate.

//FAIL: the tool will not activate
editor.CurrentTemplate = editTemplate;
editor.StartEditing(dataset.Workspace);

//PASS: the tool will activate
editor.StartEditing(dataset.Workspace);
editor.CurrentTemplate = editTemplate;

I hope this helps others, I was setting the current template before starting the editor & the tool would not activate, I was pulling my hair out all due to the order of my code!
0 Kudos
JonHall
Occasional Contributor II

Here's my code to help anyone else.


Thanks for the follow-up on your post, Tony!
0 Kudos
DuncanHornby
MVP Notable Contributor
Just to add my experience to this thread I tried to set the current tool to the default constructor but every time I tried to set the UID.Value property to  "{8F79967B-66A0-4A1C-B884-F44BC7E26921}" it kept saying it was a value out of range. I am using ArcGIS 10.2.1. I do not know if this is a bug but I got around it by using the following code:

Dim pUID As New UID
pUID.Value = "esriEditor.PolygonFeatureTool"
m_App.CurrentTool = m_App.Document.CommandBars.Find(pUID, True)

0 Kudos