AnsweredAssumed Answered

Looping through layers in C#

Question asked by agehlot on Jul 5, 2016


Hello! This is probably a relatively simple question, but I am brand new to using the C# programming language and using ArcObjects.

 

I am attempting to change the default construction tool for many layers at once. I ran across the following code, which will update the construction tool for all classes within a layer, but I am trying to iterate through all of the active layers in my document. Are there any modifications I can make to do this? If so, how do I create an add-in button from this code? Thanks.

 

using System;

using ESRI.ArcGIS.Editor;

using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Framework;

 

 

namespace SetTemplateProperty

{

  public class SetDefaultTemplateTool : ESRI.ArcGIS.Desktop.AddIns.Button

  {

    IEditor3 m_editor;

   

    public SetDefaultTemplateTool()

    {

      m_editor = ArcMap.Application.FindExtensionByName("esriEditor.Editor") as IEditor3;

    }

 

 

    protected override void OnClick()

    {

      //get the current template and ArcMap tool

      IEditTemplate currentTemplate = m_editor.CurrentTemplate;

      ICommandItem currentTool = ArcMap.Application.CurrentTool;

     

      //loop through the templates in the edit session

      for (int i = 0; i < m_editor.TemplateCount - 1; i++)

      {

        IEditTemplate editTemplate = m_editor.get_Template(i);

        if (editTemplate.Layer.Name == currentTemplate.Layer.Name)

        {

          //set the default tool to the current ArcMap tool

          Guid g = new Guid(currentTool.ID.Value.ToString());

          editTemplate.Tool = g;

        }

      }

    }

    protected override void OnUpdate()

    {

      this.Enabled = (m_editor.EditState == esriEditState.esriStateEditing);

    }

  }

 

 

}

Outcomes