Tuesday, May 2, 2017

Code snippet: To highlight selected feature in ArcMap programmatically using ArcObjects

Code snippet to highlight selected feature in ArcMap programmatically using ArcObjects and return the STGeomFromWKB   string of selected feature.

To return the geometry you should import Microsoft.SqlServer.Types  from Nuget and SqlServerSpatial140.dll  from C:\Windows\System32

 using System;  
 using System.Collections.Generic;  
 using System.Data.SqlTypes;  
 using System.Linq;  
 using System.Runtime.InteropServices;  
 using System.Text;  
 using System.Windows;  
 using ESRI.ArcGIS.ArcMapUI;  
 using ESRI.ArcGIS.Carto;  
 using ESRI.ArcGIS.Catalog;  
 using ESRI.ArcGIS.CatalogUI;  
 using ESRI.ArcGIS.Desktop.AddIns;  
 using ESRI.ArcGIS.Display;  
 using ESRI.ArcGIS.esriSystem;  
 using ESRI.ArcGIS.Framework;  
 using ESRI.ArcGIS.Geodatabase;  
 using ESRI.ArcGIS.Geometry;  
 using Microsoft.SqlServer.Types;  
   
   
 namespace Test.Addin.ArcCommands  
 {  
   class SelectFeatureTool : ESRI.ArcGIS.Desktop.AddIns.Tool  
   {  
     protected override void OnMouseDown(MouseEventArgs arg)  
     {  
       IMxDocument mxDocument = ArcMap.Application.Document as IMxDocument;  
       IActiveView activeView = mxDocument.ActiveView;  
       IMap map = mxDocument.FocusMap;  
       ILayer layer = map.get_Layer(0); //Get 1st Layer  
       IFeatureLayer featureLayer = (IFeatureLayer) layer;  
   
       IPoint identifyPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);  
       ESRI.ArcGIS.Carto.IIdentify identifyLayer = (IIdentify)layer;  
       IArray array = identifyLayer.Identify(identifyPoint);  
         
       int oid = -1;  
       if (array != null)  
       {  
         object obj = array.get_Element(0);  
         IFeatureIdentifyObj fobj = obj as IFeatureIdentifyObj;  
         IRowIdentifyObject irow = fobj as IRowIdentifyObject;  
         IFeature feature = irow.Row as IFeature;  
         oid = feature.OID;  
       }  
       HighlightClickedFeature(featureLayer, oid, activeView);  
     }  
   
     private void HighlightClickedFeature(IFeatureLayer layer, int featureOid, IActiveView activeView)  
     {  
       IFeatureClass featureClass = layer.FeatureClass;  
       IFeature feature = featureClass.GetFeature(featureOid);  
       IGeometry geometry = feature.Shape;  
   
       ISpatialFilter spatialFilter = new SpatialFilterClass();  
       spatialFilter.Geometry = geometry;  
       spatialFilter.GeometryField = featureClass.ShapeFieldName;  
       spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;  
       spatialFilter.WhereClause = "FID = " + featureOid;  
   
       IFeatureSelection featureSelection = layer as IFeatureSelection;  
         
       if (featureSelection != null)  
       {  
         //featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd,false); //multiple selections  
         featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false); //single selection  
         activeView.Refresh();  
       }  
   
       IWkb wkbGeometry = geometry as IWkb;  
       byte[] wkbBytes = new byte[wkbGeometry.WkbSize];  
       int byteCount = wkbGeometry.WkbSize;  
       wkbGeometry.ExportToWkb(ref byteCount, out wkbBytes[0]);  
   
       int srid = feature.Shape.SpatialReference.FactoryCode; // .SpatialReference.FactoryCode;  
   
       SqlGeometry sqlGeometry = SqlGeometry.STGeomFromWKB(new SqlBytes(wkbBytes),srid);  
       string wktGeometry = sqlGeometry.ToString();  
   
       MessageBox.Show(wktGeometry);  
   
     }  
       
     protected override void OnUpdate()  
     {  
       Enabled = ArcMap.Application != null;  
     }  
       
   }  
 }  

arcobject , C#

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me