如何使用C#在ArcGIS中选择要素和突出显示

时间:2013-11-05 15:44:01

标签: c# arcgis esri arcobjects

嗨我有以下代码,根据属性放大到arcGIS对象,现在我只需要能够使用选择功能突出显示该区域(右键单击地图上的区域的功能)并选择功能)。

目前我有一个可以进行缩放的事件。我想将此选择添加到同一个属性中。

提前谢谢!!!

ESRI.ArcGIS.Carto.ILayer layer = GetLayersClass.GetFieldBoundaryLayer;
if (layer is ESRI.ArcGIS.Carto.IGroupLayer)
{
    ESRI.ArcGIS.Carto.IGroupLayer groupLayer = layer as ESRI.ArcGIS.Carto.IGroupLayer;
    ICompositeLayer pCompositeLayer = layer as ICompositeLayer;
    int layers = pCompositeLayer.Count;
    ILayer pLayer = pCompositeLayer.Layer[0];

    IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;

    IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    IQueryFilter pFilter = new QueryFilterClass();
    pFilter.WhereClause = "RightID = '" + selectedRightID.ToString() + "'";

    IFeatureCursor pFeatureCursor = pFeatureClass.Search(pFilter, false);
    IFeature pFeature = pFeatureCursor.NextFeature();

    if (pFeature == null)
    {
        System.Windows.Forms.MessageBox.Show("This section doesn't exist");
        return;
    }

    IApplication m_application = ArcMap.Application;
    IMxDocument pMxDoc = (IMxDocument)m_application.Document;
    IActiveView pActiveView = (IActiveView)pMxDoc.FocusMap;

    IEnvelope pEnv = pFeature.Shape.Envelope;
    pEnv.Expand(1.1, 1.1, true);

    pActiveView.Extent = pEnv;

    pActiveView.Refresh();

我尝试添加此代码,我认为这将为选择添加特定功能。 但也没有运气。

IFeatureSelection pfeatSelect = pFeatureLayer as IFeatureSelection;
pfeatSelect.Add(pFeature);

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需要:

IFeatureSelection featSelect = pFeatureLayer as IFeatureSelection;
featSelect.SelectFeatures(pFilter, esriSelectionResultEnum.esriSelectionResultNew, false);

这将选择与您的过滤器匹配的所有功能。

答案 1 :(得分:0)

此解决方案对我有用

            IFeatureLayer PFeaLayer = (IFeatureLayer)pLayer;
            IFeatureSelection PFeaSel = (IFeatureSelection)PFeaLayer;
            int OIDIndex = PFeature.Fields.FindField("OBJECTID");
            PFeaSel.SelectionSet.Add(Convert.ToInt32(PFeature.get_Value(OIDIndex)));
            ISelectionSet PFeaSelSet = PFeaSel.SelectionSet;
            IEnumGeometry pEnumGeom = new EnumFeatureGeometryClass();
            IEnumGeometryBind pEnumGeomBind = (IEnumGeometryBind)pEnumGeom;
            pEnumGeomBind.BindGeometrySource(null, PFeaSelSet);
            IGeometryFactory pGeomFactory = new GeometryEnvironmentClass();
            IGeometry pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom);
            IMxDocument pMxDoc = ArcMap.Document as IMxDocument;
            IActiveView activeView = pMxDoc.ActiveView;
            double midX = (pGeom.Envelope.XMax + pGeom.Envelope.XMin) / 2;
            double midY = (pGeom.Envelope.YMax + pGeom.Envelope.YMin) / 2;
            IPoint pPoint = new PointClass();
            pPoint.SpatialReference = pGeom.Envelope.SpatialReference;
            pPoint.PutCoords(midX, midY);
            pPoint.Project(activeView.Extent.SpatialReference);
            IEnvelope pCurrentEnvelope = activeView.Extent;
            pCurrentEnvelope.CenterAt(pPoint);
            activeView.Extent = pCurrentEnvelope;
            activeView.Refresh();