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.
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();
}
}