0

I am trying to highlight those features which are associated (via Relationship) to the selected feature in ArcObjects C#.

public void IRelationship_Example(IFeature feature)
{
    //assumes that only one RelationshipClass exists for the Origin feature class
    IEnumRelationshipClass enumRelClass = feature.Class.get_RelationshipClasses(esriRelRole.esriRelRoleAny);
    IRelationshipClass relClass = enumRelClass.Next();
    //if a feature with no Relationships established has been selected, exit
    if (relClass == null)
    {
        return;
    }
    ESRI.ArcGIS.esriSystem.ISet relSet = relClass.GetObjectsRelatedToObject((IObject)feature);
    relSet.Reset();
    //If an Attributed Relationship does not exist, exit
    if (relClass.IsAttributed != true)
    {
        return;
    }
    IFeature destinationFeature = (IFeature)relSet.Next();
    while (destinationFeature != null)
    {
        IRelationship relationship = relClass.GetRelationship((IObject)feature, (IObject)destinationFeature);
        IRow row = (IRow)relationship;
        object attributeValue;
        if (row.get_Value(0) == null)
        {
            attributeValue = "0";
        }
        else
        {
            attributeValue = row.get_Value(0);
        }
        MessageBox.Show("Destination OID: {0}  Origin OID:  {1}  Attribute value:  {2}", relationship.DestinationObject.get_Value(0), relationship.OriginObject.get_Value(0), attributeValue);
        destinationFeature = (IFeature)relSet.Next();
    }
} 

I am newbie in ArcGIS Products.

enter image description here

As shown in above image, once, I select the feature, I can able to highlight all associated features.

For selecting feature I am using following Code:

newselection = true;  //set newselection to true on every mouse click.
HighlightClickedFeature(Layer, 22, 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 SpatialFilter();
        spatialFilter.Geometry = geometry;
        spatialFilter.GeometryField = featureClass.ShapeFieldName;
        spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
        spatialFilter.WhereClause = "OBJECTID = " + featureOid;

        IFeatureSelection featureSelection = layer as IFeatureSelection;
        if (featureSelection != null)
        {
            if (newselection == true)
            {
                //featureSelection.Add(feature);
                //featureSelection.CombinationMethod = esriSelectionResultEnum.esriSelectionResultAdd;
                //featureSelection.SelectionSet.Combine(selectionSet, esriSetOperation.esriSetUnion, selectionSet2);
                featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
                newselection = false;
            }
            else
            {
                featureSelection.SelectFeatures(spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
            }
            activeView.Refresh();
        }
    }
1

Things like feature selection on the map and highlights happen at the layer level, not the feature class level. So while you can identify the features in the relationship class, you'll need to go to find the associated layers on your map and select the features through there. You can loop through the layers like this:

public IEnumerable<IFeatureLayer> GetLayers()
{
    var layer_enum = (ArcMap.Document as IMxDocument).FocusMap.Layers;
    ILayer layer;
    while ((layer = layer_enum.Next()) != null)
        if (layer.Valid && layer is IFeatureLayer)
            yield return layer as IFeatureLayer;
}

I'm not very familiar with relationship classes, but you'll want to at least look for a a feature layer that has the FeatureClass property as the feature class you're looking for.

Layers can also have definition queries that can limit which features are visible on that layer, such as a layer for only one particular subtype. You may need to check the definition query as well as that might filter out the features you're trying to highlight.

To then select the features you can cast the layer to IFeatureSelection and then use the Add or SelectFeatures functions to select all your features.

//remove previous selection
(ArcMap.Document as IMxDocument).FocusMap.ClearSelection();

//Add method
IFeature feature;
IFeatureLayer layer;
(layer as IFeatureSelection).Add(feature);

//SelectFeatures method
IFeatureLayer layer;
List<int> oids;
IQueryFilter filter = new QueryFilter();
filter.WhereClause = $"{layer.FeatureClass.OIDFieldName} IN ({String.Join(",", oids)})"
(layer as IFeatureSelection).SelectFeatures(filter, esriSelectionResultEnum.esriSelectionResultNew, false);
4
  • With few more modification, I get the the relationship features (that was my first task). Secondly, I am using the code provided by you for feature selection, but it only works for single feature, I have features from different featureaLayers and some times different DataSet. It is not highlighting, although the code is working fine for single feature selection. – Vikrant Jain May 19 at 6:05
  • You would need to update your question with the new code you're using to select multiple features. For the SelectFeatures method you'd need to change the second parameter and ensure the last parameter is false. – danielm May 19 at 14:43
  • 1
    I don't see where in your updated code you are highlighting the related fields. The WhereClause you have set will restrict the selection to a single feature because OBJECTIDs are unique to only one feature in a feature class. Because of this the spatial component of the search is doing nothing. – danielm May 20 at 15:50
  • Actually, the issue is with data. the feature class name is different. – Vikrant Jain May 21 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.