ArcGIS Engine 개발 Geodatabase 코드 (5) - Relationship Class

6225 단어
*********************************************************************************************************************************************************************************************************개발된 관련 코드 예시,개발 환경을 여러분과 빨리 공유하세요.
  • ArcGIS Engine9.3/9.3.1
  • VS2008

  • 설명: 이 코드는 ArcGIS Engine 초보자나 Geodatabase 개발에 관심이 있는 친구에게 적용됩니다. 만약에 Engine 버전이 9.3.1보다 높으면 관련 인터페이스가 바뀔 수 있습니다. 이것은 사용자 스스로 수정해야 하지만 똑똑한 사용자는 코드의 허용 여부에 국한되지 않습니다. 다른 사람의 인터페이스 사용 방법을 배울 수도 있습니다.모델을 개발하는 것이 가장 중요하다.
    버전 인터페이스 차이점 참조:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Type_changes_between_9_3_and_10/000100000408000000/
    다음 예는 주로 코드를 사용하여 관계 클래스를 만드는 것이다
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using ESRI.ArcGIS.DataSourcesGDB;
    using ESRI.ArcGIS.esriSystem;
    using ESRI.ArcGIS.Geodatabase;
    
    namespace EditingDemo
    {
    	public class EditingDemo
    	{
    		public static void Main(string[] args)
    		{
    			#region Licensing
    			// Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.
    			// You will need to adjust this code if using ArcEngine or ArcEditor.
    			IAoInitialize aoInitialize = new AoInitializeClass();
    			esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
    			if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
    			{
    				Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);
    				return;
    			}
    			#endregion
    
    			#region Data Setup
    			// If any data is leftover from previous runs, delete it.
    			if (Directory.Exists("Riverside.gdb"))
    			{
    				Directory.Delete("Riverside.gdb", true);
    			}
    
    			// Copy the geodatabase from the data directory to this directory.
    			Type tempFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
    			IWorkspaceFactory tempWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(tempFactoryType);
    			IWorkspaceName sourceWorkspace = new WorkspaceNameClass
    			{
    				PathName = @"..\..\..\Data\Riverside.gdb",
    				WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"
    			};
    			IWorkspaceName copiedWorkspace = null;
    			tempWorkspaceFactory.Copy(sourceWorkspace, Environment.CurrentDirectory, out copiedWorkspace);
    			#endregion
    
    			try
    			{
    				// Open the test data.
    				Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
    				IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
    				IWorkspace workspace = workspaceFactory.OpenFromFile("Riverside.gdb", 0);
    				IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
    
    				// Open the feature classes that will participate in the relationship class.
    				IFeatureClass polesFeatureClass = featureWorkspace.OpenFeatureClass("Utility_Poles");
    				IFeatureClass transformersFeatureClass = featureWorkspace.OpenFeatureClass("Transformers");
    
    				// Open the feature dataset where the relationship class will be stored.
    				IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Electric");
    				IRelationshipClassContainer relClassContainer = (IRelationshipClassContainer)featureDataset;
    
    				// Create a composite relationship class.
    				IRelationshipClass relationshipClass = relClassContainer.CreateRelationshipClass("PolesToTransformers",
    					polesFeatureClass, transformersFeatureClass, "supports", "is located on",
    					esriRelCardinality.esriRelCardinalityOneToMany, esriRelNotification.esriRelNotificationForward,
    					true, false, null, "Pole_ID", "", "Pole_ID", "");
    
    				// Set the first relationship class rule.
    				// One Pole (subtype: Wood) can hold a minimum of 0 and a maximum of 3 transformers.
    				// A Transformer must be related to one and only one Utility Pole.
    				IRelationshipRule relationshipRule = new RelationshipRuleClass
    				{
    					OriginClassID = polesFeatureClass.ObjectClassID,
    					OriginSubtypeCode = 1, // Wooden pole subtype
    					OriginMaximumCardinality = 1,
    					OriginMinimumCardinality = 1,
    					DestinationClassID = transformersFeatureClass.ObjectClassID,
    					DestinationSubtypeCode = 0,
    					DestinationMaximumCardinality = 3,
    					DestinationMinimumCardinality = 0
    				};
    
    				// Add the rule to the relationship class.
    				relationshipClass.AddRelationshipRule(relationshipRule);
    
    				// Set the second relationship class rule.
    				// One Pole (subtype: Steel) can hold a minimum of 0 and a maximum of 5 transformers,
    				// a Transformer must be related to one and only one Utility Pole.
    				IRelationshipRule relationshipRule2 = new RelationshipRuleClass
    				{
    					OriginClassID = polesFeatureClass.ObjectClassID,
    					OriginSubtypeCode = 2, // Steel pole subtype
    					OriginMaximumCardinality = 1,
    					OriginMinimumCardinality = 1,
    					DestinationClassID = transformersFeatureClass.ObjectClassID,
    					DestinationSubtypeCode = 0,
    					DestinationMaximumCardinality = 5,
    					DestinationMinimumCardinality = 0
    				};
    
    				relationshipClass.AddRelationshipRule(relationshipRule2);
    
    				// Validate the relationship rules to identify invalid features.
    				IValidation validation = (IValidation)polesFeatureClass;
    				ISelectionSet selectionSet = validation.Validate(null, null);
    				Console.WriteLine("There are {0} features that are violating the relationship rules.", selectionSet.Count);
    
    				// Display the Object IDs of poles that are violating the relationship rules.
    				IEnumIDs enumIDs = selectionSet.IDs;
    				enumIDs.Reset();
    				int oid = -1;
    				while ((oid = enumIDs.Next()) != -1)
    				{
    					Console.WriteLine("Invalid Pole OID = {0}", oid);
    				}
    			}
    			catch (COMException ComEx)
    			{
    				Console.WriteLine("An error occurred: {0}, Error Code: {1}", ComEx.Message, ComEx.ErrorCode);
    			}
    			catch (Exception exc)
    			{
    				Console.WriteLine("An error occurred: {0}", exc.Message);
    			}
    			finally
    			{
    				Console.WriteLine("Done.");
    			}
    
    			aoInitialize.Shutdown();
    		}
    	}
    }
    

    관련 소스 코드 및 테스트 데이터

    좋은 웹페이지 즐겨찾기