TitlePages

Thursday 15 January 2015

Modifying the Vertices of a Polyline in ArcMap using ArcObjects

Modifying the Vertices of a Polyline using ArcObjects


1.Start the Editing Mode in the Editor Toolbar

2.This will create an edit session

3.Using the editor tool select the polyline feature you want to edit

4.Click on our Custom tool

5.The vertices of our polyline will be visible

6.Save the edits

7.Stop editing and complete the session

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


namespace ToolTest
{
    public class poli : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        public static Boolean mousedown = false;
        public static IEditor3 meditor=null;
        IEditSketch3 editsketch = meditor as IEditSketch3;

        public poli()
        {
        }

        protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            if (mousedown == false)
            {

                meditor = GetEditorFromArcMap(ArcMap.ThisApplication) as IEditor3;
                //IWorkspace workspace = (GetFeatureLayerFromLayerIndexNumber((ArcMap.Application.Document as IActiveView), 1).FeatureClass as IWorkspace);
                //if(meditor.EditState!=esriEditState.esriStateEditing)
                //meditor.StartEditing(workspace);






                IEditSketch3 editsketch = meditor as IEditSketch3;
                editsketch.GeometryType = esriGeometryType.esriGeometryPolyline;


                 IEditTaskSearch editTaskSearch = meditor as IEditTaskSearch;
 IEditTask editTask = editTaskSearch.get_TaskByUniqueName("GarciaUI_ModifyFeatureTask");

  //Set the task returned to the current edit task.
 if (editTask != null)
 {
     meditor.CurrentTask = editTask;
 }
                ISketchOperation sk = new SketchOperationClass();
                sk.Start(meditor);

             

             
            }
            if(mousedown==true)
            {
                if(editsketch.Geometry==null)
                {
                    IPolyline poly;
                    poly = editsketch as IPolyline;
                    editsketch.FinishSketch();

                }
                else
                {
                    mousedown=false;
                }


            }
       

         
        }


        protected override void OnMouseUp(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
         
        }

public ESRI.ArcGIS.Editor.IEditor2 GetEditorFromArcMap(ESRI.ArcGIS.ArcMapUI.IMxApplication mxApplication)
{
  if(mxApplication == null)
  {
    return null;
  }
  ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
  uid.Value = "{F8842F20-BB23-11D0-802B-0000F8037368}";
  ESRI.ArcGIS.Framework.IApplication application = mxApplication as ESRI.ArcGIS.Framework.IApplication; // Dynamic Cast
  ESRI.ArcGIS.esriSystem.IExtension extension = application.FindExtensionByCLSID(uid);
  ESRI.ArcGIS.Editor.IEditor2 editor2 = extension as ESRI.ArcGIS.Editor.IEditor2; // Dynamic Cast

  return editor2;
}




public ESRI.ArcGIS.Carto.IFeatureLayer GetFeatureLayerFromLayerIndexNumber(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 layerIndex)
{
    if (activeView == null || layerIndex < 0)
    {
        return null;
    }
    ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
    if (layerIndex < map.LayerCount && map.get_Layer(layerIndex) is ESRI.ArcGIS.Carto.IFeatureLayer)
    {
        return (ESRI.ArcGIS.Carto.IFeatureLayer)activeView.FocusMap.get_Layer(layerIndex); // Explicit Cast
    }
    else
    {
        return null;
    }
}
public ESRI.ArcGIS.Geometry.IPointCollection4 modifyFirstVertexOfAPolyline
(ESRI.ArcGIS.Geometry.IGeometryCollection geometryCollection_Polyline,
System.Double searchRadius, System.Double offsetX, System.Double offsetY)
{
    ESRI.ArcGIS.Geometry.IPolyline polyline = (ESRI.ArcGIS.Geometry.IPolyline)
        geometryCollection_Polyline;
    ESRI.ArcGIS.Geometry.IPoint queryPoint = polyline.FromPoint;

    ESRI.ArcGIS.Geometry.IPoint hitPoint = new ESRI.ArcGIS.Geometry.PointClass();

    //Define and initialize the variables that will get populated from the .HitTest() method.
    System.Double hitDistance = 0;
    System.Int32 hitPartIndex = 0;
    System.Int32 hitSegmentIndex = 0;
    System.Boolean rightSide = false;

    ESRI.ArcGIS.Geometry.IHitTest hitTest = (ESRI.ArcGIS.Geometry.IHitTest)
        geometryCollection_Polyline;
    System.Boolean foundGeometry = hitTest.HitTest(queryPoint, searchRadius,
        ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex,
        hitPoint, ref hitDistance, ref hitPartIndex, ref hitSegmentIndex, ref
        rightSide);

    if (foundGeometry == true)
    {
        ESRI.ArcGIS.Geometry.IGeometry geometry =
            geometryCollection_Polyline.get_Geometry(hitPartIndex);
        ESRI.ArcGIS.Geometry.IPointCollection4 pointCollection =
            (ESRI.ArcGIS.Geometry.IPointCollection4)geometry;
        ESRI.ArcGIS.Geometry.IPoint transformPoint = pointCollection.get_Point
            (hitSegmentIndex);

        ESRI.ArcGIS.Geometry.ITransform2D transform2D =
            (ESRI.ArcGIS.Geometry.ITransform2D)transformPoint;
        transform2D.Move(offsetX, offsetY);

        ESRI.ArcGIS.Geometry.IPoint afterMovePoint = (ESRI.ArcGIS.Geometry.IPoint)
            transform2D;

        //The point is not updated in the polyline until the next line is called.
        pointCollection.UpdatePoint(hitSegmentIndex, (ESRI.ArcGIS.Geometry.IPoint)
            transform2D);

        return pointCollection;
    }

    return null;

}

        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }
    }

}

No comments: