ArcGIS Engine开发Geodatabase代码(五)——Relationship Class

时间:2013-4-25    作者:悬浮的青春    分类: gis二次开发


/**/

  ESRI Developer Summit 2009

 
Developer's Guide to the Geodatabase

  Code Samples

 
6 April 2009

/**/



开发环境:

  • ArcGIS Engine9.3/9.3.1
  • VS2008



关于版本的接口差别参考:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Type_changes_between_9_3_and_10/000100000408000000/


以下示例主要是使用代码创建关系类


  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.InteropServices;  
  4. using ESRI.ArcGIS.DataSourcesGDB;  
  5. using ESRI.ArcGIS.esriSystem;  
  6. using ESRI.ArcGIS.Geodatabase;  
  7.   
  8. namespace EditingDemo  
  9. {  
  10.     public class EditingDemo  
  11.     {  
  12.         public static void Main(string[] args)  
  13.         {  
  14.             #region Licensing  
  15.             // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.  
  16.             // You will need to adjust this code if using ArcEngine or ArcEditor.  
  17.             IAoInitialize aoInitialize = new AoInitializeClass();  
  18.             esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);  
  19.             if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)  
  20.             {  
  21.                 Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);  
  22.                 return;  
  23.             }  
  24.             #endregion  
  25.  
  26.             #region Data Setup  
  27.             // If any data is leftover from previous runs, delete it.  
  28.             if (Directory.Exists("Riverside.gdb"))  
  29.             {  
  30.                 Directory.Delete("Riverside.gdb"true);  
  31.             }  
  32.   
  33.             // Copy the geodatabase from the data directory to this directory.  
  34.             Type tempFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");  
  35.             IWorkspaceFactory tempWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(tempFactoryType);  
  36.             IWorkspaceName sourceWorkspace = new WorkspaceNameClass  
  37.             {  
  38.                 PathName = @"..\..\..\Data\Riverside.gdb",  
  39.                 WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"  
  40.             };  
  41.             IWorkspaceName copiedWorkspace = null;  
  42.             tempWorkspaceFactory.Copy(sourceWorkspace, Environment.CurrentDirectory, out copiedWorkspace);  
  43.             #endregion  
  44.   
  45.             try  
  46.             {  
  47.                 // Open the test data.  
  48.                 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");  
  49.                 IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);  
  50.                 IWorkspace workspace = workspaceFactory.OpenFromFile("Riverside.gdb", 0);  
  51.                 IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;  
  52.   
  53.                 // Open the feature classes that will participate in the relationship class.  
  54.                 IFeatureClass polesFeatureClass = featureWorkspace.OpenFeatureClass("Utility_Poles");  
  55.                 IFeatureClass transformersFeatureClass = featureWorkspace.OpenFeatureClass("Transformers");  
  56.   
  57.                 // Open the feature dataset where the relationship class will be stored.  
  58.                 IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Electric");  
  59.                 IRelationshipClassContainer relClassContainer = (IRelationshipClassContainer)featureDataset;  
  60.   
  61.                 // Create a composite relationship class.  
  62.                 IRelationshipClass relationshipClass = relClassContainer.CreateRelationshipClass("PolesToTransformers",  
  63.                     polesFeatureClass, transformersFeatureClass, "supports""is located on",  
  64.                     esriRelCardinality.esriRelCardinalityOneToMany, esriRelNotification.esriRelNotificationForward,  
  65.                     truefalsenull"Pole_ID""""Pole_ID""");  
  66.   
  67.                 // Set the first relationship class rule.  
  68.                 // One Pole (subtype: Wood) can hold a minimum of 0 and a maximum of 3 transformers.  
  69.                 // A Transformer must be related to one and only one Utility Pole.  
  70.                 IRelationshipRule relationshipRule = new RelationshipRuleClass  
  71.                 {  
  72.                     OriginClassID = polesFeatureClass.ObjectClassID,  
  73.                     OriginSubtypeCode = 1, // Wooden pole subtype  
  74.                     OriginMaximumCardinality = 1,  
  75.                     OriginMinimumCardinality = 1,  
  76.                     DestinationClassID = transformersFeatureClass.ObjectClassID,  
  77.                     DestinationSubtypeCode = 0,  
  78.                     DestinationMaximumCardinality = 3,  
  79.                     DestinationMinimumCardinality = 0  
  80.                 };  
  81.   
  82.                 // Add the rule to the relationship class.  
  83.                 relationshipClass.AddRelationshipRule(relationshipRule);  
  84.   
  85.                 // Set the second relationship class rule.  
  86.                 // One Pole (subtype: Steel) can hold a minimum of 0 and a maximum of 5 transformers,  
  87.                 // a Transformer must be related to one and only one Utility Pole.  
  88.                 IRelationshipRule relationshipRule2 = new RelationshipRuleClass  
  89.                 {  
  90.                     OriginClassID = polesFeatureClass.ObjectClassID,  
  91.                     OriginSubtypeCode = 2, // Steel pole subtype  
  92.                     OriginMaximumCardinality = 1,  
  93.                     OriginMinimumCardinality = 1,  
  94.                     DestinationClassID = transformersFeatureClass.ObjectClassID,  
  95.                     DestinationSubtypeCode = 0,  
  96.                     DestinationMaximumCardinality = 5,  
  97.                     DestinationMinimumCardinality = 0  
  98.                 };  
  99.   
  100.                 relationshipClass.AddRelationshipRule(relationshipRule2);  
  101.   
  102.                 // Validate the relationship rules to identify invalid features.  
  103.                 IValidation validation = (IValidation)polesFeatureClass;  
  104.                 ISelectionSet selectionSet = validation.Validate(nullnull);  
  105.                 Console.WriteLine("There are {0} features that are violating the relationship rules.", selectionSet.Count);  
  106.   
  107.                 // Display the Object IDs of poles that are violating the relationship rules.  
  108.                 IEnumIDs enumIDs = selectionSet.IDs;  
  109.                 enumIDs.Reset();  
  110.                 int oid = -1;  
  111.                 while ((oid = enumIDs.Next()) != -1)  
  112.                 {  
  113.                     Console.WriteLine("Invalid Pole OID = {0}", oid);  
  114.                 }  
  115.             }  
  116.             catch (COMException ComEx)  
  117.             {  
  118.                 Console.WriteLine("An error occurred: {0}, Error Code: {1}", ComEx.Message, ComEx.ErrorCode);  
  119.             }  
  120.             catch (Exception exc)  
  121.             {  
  122.                 Console.WriteLine("An error occurred: {0}", exc.Message);  
  123.             }  
  124.             finally  
  125.             {  
  126.                 Console.WriteLine("Done.");  
  127.             }  
  128.   
  129.             aoInitialize.Shutdown();  
  130.         }  
  131.     }  
  132. }  

标签: arcgis二次开发 arcgis Geodatabase

WRITTEN BY

avatar