ArcObjects Library Reference  

ElementFeatureAssociation

About the Implementing associations between GIS features and schematic features Sample

[C#]

ElementFeatureAssociation.cs

using ESRI.ArcGIS.Schematic;
using ESRI.ArcGIS.Geodatabase;

namespace LinkerManagerCS
{
	public class ElementFeatureAssociation : ESRI.ArcGIS.Desktop.AddIns.Extension
	{
		private SchematicDatasetManager m_schemDatasetMgr;
		// Attribute names that specified the ClassName and OBJECTID of the feature related to each schematic element
		private const string AttClassNameName = "RelatedFeatureCN";
		private const string AttObjectIdName = "RelatedFeatureOID";

		protected override void OnStartup()
		{
			m_schemDatasetMgr = new SchematicDatasetManager();
			m_schemDatasetMgr.AfterLoadDiagram += new ISchematicDatasetEvents_AfterLoadDiagramEventHandler(OnAfterLoadDiagram);
			m_schemDatasetMgr.AfterRefreshDiagram += new ISchematicDatasetEvents_AfterRefreshDiagramEventHandler(OnAfterRefreshDiagram);
		}

		protected override void OnShutdown()
		{
			m_schemDatasetMgr.AfterRefreshDiagram -= OnAfterRefreshDiagram;
			m_schemDatasetMgr.AfterLoadDiagram -= OnAfterLoadDiagram;
			m_schemDatasetMgr = null;

			base.OnShutdown();
		}

		public void OnAfterLoadDiagram(ISchematicInMemoryDiagram inMemoryDiagram)
		{
			// if add-in is not enabled then quit
			if (State != ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled) return;

			IEnumSchematicInMemoryFeature enumSchemElements;
			ISchematicInMemoryFeature schemElement;
			string nameRelatedCN = "";
			int numRelatedOID = 0;
			IFeatureClass esriFeatureClass;
			IFeature esriFeature;
			ISchematicInMemoryFeatureLinkerEdit schemFeatureLinkerEdit = (ISchematicInMemoryFeatureLinkerEdit)new SchematicLinker();
			ISchematicObjectClass myObjectType;
			ISchematicAttribute schemAttributeCID;
			ISchematicAttribute schemAttributeOID;
			ISchematicAttributeContainer schemAttributeContainer;

			IWorkspace esriWorkspace;
			Microsoft.VisualBasic.Collection colFCByName;

			colFCByName = new Microsoft.VisualBasic.Collection();
			esriWorkspace = inMemoryDiagram.SchematicDiagramClass.SchematicDataset.SchematicWorkspace.Workspace;

			// Retrieve all schematic element of the diagram

			enumSchemElements = (IEnumSchematicInMemoryFeature)inMemoryDiagram.SchematicInMemoryFeatures;
			enumSchemElements.Reset();

			schemElement = enumSchemElements.Next();
			while (schemElement != null)
			{
				// retrieve attribute
				myObjectType = schemElement.SchematicElementClass;
				schemAttributeContainer = (ISchematicAttributeContainer)schemElement.SchematicElementClass;
				schemAttributeCID = schemAttributeContainer.GetSchematicAttribute(AttClassNameName, false);
				schemAttributeOID = schemAttributeContainer.GetSchematicAttribute(AttObjectIdName, false);
				if (schemAttributeCID != null && schemAttributeOID != null)
				{
					// get value of attribute
					nameRelatedCN = schemAttributeCID.GetValue((ISchematicObject)schemElement).ToString();
					numRelatedOID = System.Convert.ToInt32(schemAttributeOID.GetValue((ISchematicObject)schemElement));

					// get feature from geodatabase
					esriFeatureClass = FindFeatureClassByName(esriWorkspace, nameRelatedCN, colFCByName);
					if (esriFeatureClass != null)
					{
						// get feature from FeatureClass
						esriFeature = esriFeatureClass.GetFeature(numRelatedOID);
						if (esriFeature != null)
						{
							// Associate geographical feature with schematic feature
							schemFeatureLinkerEdit.Associate(schemElement, esriFeature);
						}
					}
				}
				schemElement = enumSchemElements.Next();
			}
			colFCByName.Clear();
			colFCByName = null;
		}

		public void OnAfterRefreshDiagram(ESRI.ArcGIS.Schematic.ISchematicInMemoryDiagram InMemoryDiagram)
		{
			OnAfterLoadDiagram(InMemoryDiagram);
		}

		private IFeatureClass FindFeatureClassByName(IWorkspace workspace, string name, Microsoft.VisualBasic.Collection colFCByName)
		{
			IFeatureClass esriFeatureClass = null;

			try
			{
				// try to retrieve FeatureClass in collection
				esriFeatureClass = (IFeatureClass)colFCByName[name];
				return esriFeatureClass;
			}
			catch { }

			IEnumDataset enumDatasets;
			IDataset esriDataset;
			IFeatureClassContainer featContainer;

			// get datasets
			enumDatasets = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
			enumDatasets.Reset();
			esriDataset = enumDatasets.Next();
			while (esriDataset != null)
			{
				// try to find class in dataset
				try
				{
					featContainer = (IFeatureClassContainer)esriDataset;
					// get FeatureClass from current dataset
					esriFeatureClass = featContainer.get_ClassByName(name);
					if (esriFeatureClass != null)
					{
						// if exists add to collection and quit
						colFCByName.Add(esriFeatureClass, name, null, null);
						return esriFeatureClass;
					}
				}
				catch { }
				// try another dataset
				esriDataset = enumDatasets.Next();
			}

			try
			{
				// try to find FeatureClass in workspace
				featContainer = (IFeatureClassContainer)workspace;
				esriFeatureClass = featContainer.get_ClassByName(name);
				if (esriFeatureClass != null)
				{
					// if exists add to collection and quit
					colFCByName.Add(esriFeatureClass, name, null, null);
					return esriFeatureClass;
				}
			}
			catch { }
			return null;
		}
	}
}

[Visual Basic .NET]

ElementFeatureAssociation.vb

Imports ESRI.ArcGIS.Schematic
Imports ESRI.ArcGIS.Geodatabase

Public Class ElementFeatureAssociation
	Inherits ESRI.ArcGIS.Desktop.AddIns.Extension

	'The OnAfterLoadDiagram procedure is used to specify the associations 
	'between schematic elements and features.
	'The diagram contains schematic elements for which particular attributes have been created. 
	'These attributes specify the ClassID and the ObjectID the schematic element is related to.
	'The ISchematicInMemoryFeatureLinkerEdit Associate method is used to specify 
	'that a schematic element is associated with a feature.

	Private m_schemDatasetMgr As SchematicDatasetManager

	' Attribute names that specified the ClassName and OBJECTID of the feature related to each schematic element
	Private Const AttClassNameName As String = "RelatedFeatureCN"
	Private Const AttObjectIdName As String = "RelatedFeatureOID"

	Protected Overrides Sub OnStartup()
		m_schemDatasetMgr = New SchematicDatasetManager
		AddHandler m_schemDatasetMgr.AfterLoadDiagram, AddressOf OnAfterLoadDiagram
	End Sub

	Protected Overrides Sub OnShutdown()
		RemoveHandler m_schemDatasetMgr.AfterLoadDiagram, AddressOf OnAfterLoadDiagram
		m_schemDatasetMgr = Nothing
	End Sub

	Public Sub OnAfterLoadDiagram(ByVal inMemoryDiagram As ESRI.ArcGIS.Schematic.ISchematicInMemoryDiagram)
        ' if add-in is not enabled then quit
		If State <> ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then Return

		Dim enumSchemElements As IEnumSchematicInMemoryFeature
		Dim schemElement As ISchematicInMemoryFeature
		Dim nameRelatedCN As String
		Dim numRelatedOID As Long
		Dim esriFeatureClass As IFeatureClass
		Dim esriFeature As IFeature
		Dim schemFeatureLinkerEdit As ISchematicInMemoryFeatureLinkerEdit = New SchematicLinker
		Dim schemObjectType As ISchematicObjectClass
		Dim schemAttributeCID As ISchematicAttribute
		Dim schemAttributeOID As ISchematicAttribute
		Dim schemAttributeContainer As ISchematicAttributeContainer
		Dim esriWorkspace As IWorkspace
		Dim colFCByName As Collection

		colFCByName = New Collection
		esriWorkspace = inMemoryDiagram.SchematicDiagramClass.SchematicDataset.SchematicWorkspace.Workspace

		' Retrieve all schematic element of the diagram
		enumSchemElements = CType(inMemoryDiagram.SchematicInMemoryFeatures, IEnumSchematicInMemoryFeature)
		enumSchemElements.Reset()

		schemElement = enumSchemElements.Next()
		While (schemElement IsNot Nothing)
			schemObjectType = schemElement.SchematicElementClass

			' get Attributes
			schemAttributeContainer = CType(schemElement.SchematicElementClass, ISchematicAttributeContainer)
			schemAttributeCID = schemAttributeContainer.GetSchematicAttribute(AttClassNameName)
			schemAttributeOID = schemAttributeContainer.GetSchematicAttribute(AttObjectIdName)

			If (schemAttributeCID IsNot Nothing) AndAlso (schemAttributeOID IsNot Nothing) Then
				' get value of attribute
				nameRelatedCN = schemAttributeCID.GetValue(schemElement)
				numRelatedOID = schemAttributeOID.GetValue(schemElement)

				' get feature from geodatabase
				esriFeatureClass = FindFeatureClassByName(esriWorkspace, nameRelatedCN, colFCByName)
				If esriFeatureClass IsNot Nothing Then
					' get feature from FeatureClass
					esriFeature = esriFeatureClass.GetFeature(numRelatedOID)
					If esriFeature IsNot Nothing Then
						' Associate geographical feature with schematic feature
						schemFeatureLinkerEdit.Associate(schemElement, esriFeature)
					End If
				End If
			End If
			schemElement = enumSchemElements.Next()
		End While
		colFCByName.Clear()
		colFCByName = Nothing
	End Sub

	Public Sub OnAfterRefreshDiagram(ByVal InMemoryDiagram As ESRI.ArcGIS.Schematic.ISchematicInMemoryDiagram)
		OnAfterLoadDiagram(InMemoryDiagram)
	End Sub

	Private Function FindFeatureClassByName(ByVal workspace As IWorkspace, ByVal name As String, ByVal colFCByName As Collection) As IFeatureClass
		Dim esriFeatureClass As IFeatureClass

		Try
			' try to retrieve FeatureClass in collection
			esriFeatureClass = CType(colFCByName(name), IFeatureClass)
			Return esriFeatureClass
		Catch
		End Try

		Dim enumDatasets As IEnumDataset
		Dim esriDataset As IDataset
		Dim featContainer As IFeatureClassContainer

		' get datasets
		enumDatasets = workspace.Datasets(esriDatasetType.esriDTFeatureDataset)
		enumDatasets.Reset()
		esriDataset = enumDatasets.Next()
		While esriDataset IsNot Nothing
			' try to find class in dataset
			Try
				featContainer = CType(esriDataset, IFeatureClassContainer)
				' get FeatureClass from current dataset
				esriFeatureClass = featContainer.ClassByName(name)
				If esriFeatureClass IsNot Nothing Then
					' if exists add to collection and quit
					colFCByName.Add(esriFeatureClass, name)
					Return esriFeatureClass
				End If
			Catch
			End Try
			' try another dataset
			esriDataset = enumDatasets.Next()
		End While

		Try
			' try to find FeatureClass in workspace
			featContainer = CType(workspace, IFeatureClassContainer)
			esriFeatureClass = featContainer.ClassByName(name)
			If esriFeatureClass IsNot Nothing Then
				' if exists add to collection and quit
				colFCByName.Add(esriFeatureClass, name)
				Return esriFeatureClass
			End If
		Catch
		End Try
		Return Nothing
	End Function
End Class