Hi everyone,
I am new to GIS programming.I wanted to write a code in which we will be having two polygon layers and we need to select the second layer polygons that are intersecting with the polygons in first layer.I have been able to cop up with the code upto some extent.but still it is not selecting the polygons.I am getting the resultant polygons through ispatial filter but unable to highlight them.below is the code..plz help its urgent...i have tried several methods such as selectionset.add,selectionset.addlist but its not working.
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Geodatabase;
namespace try1
{
[Guid("571a1393-83e1-429d-b48b-27de18e8416f")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("try1")]
public class custom : ICommand
{
public ILayer l1;
public ILayer l2;
public IMxApplication mxApplication; //to create application display object
IApplication m_application;
private IntPtr m_hBitmap;
public IApplication m_app;
public IMxDocument m_mxDoc;
public MxDocument m_mxDocument;
public Map m_map;
public PageLayout m_pageLayout;
public IFeatureClass featureClass;
// Needed to clear up the Hbitmap unmanaged resource
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
static extern bool DeleteObject(IntPtr hObject);
#region "Component Category Registration"
[ComRegisterFunction()]
static void Reg(string regKey)
{
MxCommands.Register(regKey);
}
[ComUnregisterFunction()]
static void Unreg(string regKey)
{
MxCommands.Unregister(regKey);
}
#endregion
#region ICommand Members
public string Message
{
get
{
// Set the message string that appears in the statusbar of the
// application when the mouse passes over the command.
return "first tool";
//return null;
}
}
public int HelpContextID
{
get
{
return 0;
}
}
public string Name
{
get
{
return "sample tool";
}
}
public void OnClick()
{
l1 = m_map.get_Layer(0);
l2 = m_map.get_Layer(1);
IFeatureCursor featureCursor1;
IFeatureLayer pfl = (IFeatureLayer)m_map.get_Layer(0);
IFeatureLayer pfl1 = (IFeatureLayer)m_map.get_Layer(1);
IFeatureClass featureClass = pfl.FeatureClass;
IFeatureClass featureClass1 = pfl1.FeatureClass;
IFeatureCursor featureCursor = featureClass.Search(null, true);
IFeature Feat = featureCursor.NextFeature();
ISpatialFilter spatialFilter = new SpatialFilterClass();
IFeatureSelection sel = Feat as IFeatureSelection;
IActiveView activeView = m_mxDoc.ActiveView;
while (Feat != null)
{
spatialFilter.GeometryField = featureClass1.ShapeFieldName;
spatialFilter.Geometry = Feat.Shape;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
featureCursor1 = featureClass1.Search(spatialFilter, false);
while (Feat1 != null) //to check the result of the ispatialquery
{
MessageBox.Show("FEAT " + Feat1.OID.ToString());
Feat1 = featureCursor1.NextFeature();
}
Feat = featureCursor.NextFeature();
}
sel.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false);
activeView.PartialRefresh(esriViewDrawPhase.esriVi ewGeoSelection, null, null);
}
public int Bitmap
{
get
{
return m_hBitmap.ToInt32();
}
}
public void OnCreate(object hook)
{
m_app = hook as IApplication;
m_mxDoc = m_app.Document as IMxDocument;
m_mxDocument = m_app.Document as MxDocument;
m_map = m_mxDoc.FocusMap as Map;
}
public string Caption
{
get
{
return "MyTool in CSharp";
//return null;
}
}
public string Tooltip
{
get
{
// Set the string that appears in the screen tip.
return "MyTool in CSharp";
//return null;
}
}
public bool Checked
{
get
{
return false;
}
}
public bool Enabled
{
get
{
// Add some logic here to specify when the command should
// be enabled. In this example, the command is always enabled.
return true;
}
}
public string HelpFile
{
get
{
return null;
}
}
public string Category
{
get
{
// Set the category of this command. This determines where the
// command appears in the Commands panel of the Customize dialog.
return "test tool";
//return null;
}
}
#endregion
}
}