public bool ExportGeodatabaseAnnotationFeatureClassToFileGeoDB(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace InputFeatureWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureClass InputFeatureClass, string OutputFeatureClassName, string OutputFileGeoDBPath, string OutputFileGeoDBName) { ESRI.ArcGIS.Geodatabase.IWorkspace inputWorkspace = null; ESRI.ArcGIS.esriSystem.IPropertySet inputPropertySet = null; System.Type inputFactoryType = null; System.Object inputFactoryObject = null; ESRI.ArcGIS.Geodatabase.IWorkspaceFactory inputWorkspaceFactory = null; ESRI.ArcGIS.Geodatabase.IWorkspaceName inputWorkspaceName = null; ESRI.ArcGIS.Geodatabase.IDataset inputDataset = null; ESRI.ArcGIS.esriSystem.VarArray geoprocessorVariantArray = null; PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities generalUtilities = null; ESRI.ArcGIS.Geoprocessing.GeoProcessor geoProcessor = null; try { // QI to the Input Workspace. inputWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)InputFeatureWorkspace; // Build a Property Set for the Current Workspace. inputPropertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass(); inputPropertySet = inputWorkspace.ConnectionProperties; // The Folks at ESRI were not smart enough to handle a Property Set that did not include a Server Name so if there is not one in this propertyset dummy one in. if (inputPropertySet.GetProperty("Instance").ToString().ToUpper().IndexOf("SDE:SQLSERVER:") != -1) { // Determine the server name from the "Instance" Property of the Property Set. string inputServerName = inputPropertySet.GetProperty("Instance").ToString(); while (inputServerName.IndexOf(@":") != -1) { // Strip the first character from the Input Server Name. inputServerName = inputServerName.Substring(1); } // If the Server Name Includes an Instance Value, strip that from the server name. while (inputServerName.IndexOf(@"\") != -1) { // Strip the last character from the Input Server Name. inputServerName = inputServerName.Substring(0, (inputServerName.Length - 1)); } // Add a Server Property to the Property Set. inputPropertySet.SetProperty("Server", inputServerName); } // Determine which directory the Temporary SDE Connection File should be created in. string temporaryDirectory = null; if (System.IO.Directory.Exists(@"D:\Temp")) { // Set the Temporary Directory to 'D:\TEMP\'. temporaryDirectory = @"D:\Temp\"; } else { // Check to see if there is a 'C:\TEMP' Directory. if (System.IO.Directory.Exists(@"C:\Temp")) { // Set the Temporary Directory to 'C:\Temp\' temporaryDirectory = @"C:\Temp\"; } else { // Set the Temporary Directory to 'C:\'. temporaryDirectory = @"C:\"; } } // Make sure the Output Temporary Connection File does not already exist before attempting to create a new one. if (System.IO.File.Exists(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde")) { // Delete the existing File. System.IO.File.Delete(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde"); } // Create the Temporary SDE Connection File that will be used to specify the Input Annotation Features for this export operation. inputFactoryType = System.Type.GetTypeFromProgID("esriDataSourcesGDB.SDEWorkspaceFactory"); inputFactoryObject = System.Activator.CreateInstance(inputFactoryType); inputWorkspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)inputFactoryObject; inputWorkspaceName = inputWorkspaceFactory.Create(temporaryDirectory, OutputFeatureClassName + "SDEConn.sde", inputPropertySet, 0); // Specify the parameters for the export of this Feature Class to the Output File Geodatabase. inputDataset = (ESRI.ArcGIS.Geodatabase.IDataset)InputFeatureClass; string processDatasetName = null; if (inputDataset.Name.IndexOf(inputWorkspaceName.ConnectionProperties.GetProperty("Database").ToString()) > -1) { // Drop the Server name from the Name before using it. processDatasetName = inputDataset.Name.Substring(inputWorkspaceName.ConnectionProperties.GetProperty("Database").ToString().Length + 1); } else { // Use the Name as is. processDatasetName = inputDataset.Name.ToString(); } // Create a Field Mapping for this export. string inputAnnotationFeatures = inputWorkspaceName.PathName.ToString() + @"\" + processDatasetName; ESRI.ArcGIS.Geoprocessing.IGPUtilities geoprocessingUtilities = new ESRI.ArcGIS.Geoprocessing.GPUtilitiesClass(); ESRI.ArcGIS.Geodatabase.IDETable inputTable = (ESRI.ArcGIS.Geodatabase.IDETable)geoprocessingUtilities.MakeDataElement(inputAnnotationFeatures, null, null); ESRI.ArcGIS.esriSystem.IArray inputTables = new ESRI.ArcGIS.esriSystem.ArrayClass(); inputTables.Add(inputTable); ESRI.ArcGIS.Geoprocessing.IGPFieldMapping fieldMapping = new ESRI.ArcGIS.Geoprocessing.GPFieldMappingClass(); // Go through the fields in the Input Table and add them to the Field Mapping. object missing = Type.Missing; for (int i = 0; i < inputTable.Fields.FieldCount; i++) { if ((inputTable.Fields.get_Field(i).Type != ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID) && (inputTable.Fields.get_Field(i).Type != ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry)) { ESRI.ArcGIS.Geoprocessing.IGPFieldMap currentFieldMap = new ESRI.ArcGIS.Geoprocessing.GPFieldMapClass(); currentFieldMap.AddInputField(inputTable, inputTable.Fields.get_Field(i), -1, -1); fieldMapping.AddFieldMap(currentFieldMap); currentFieldMap = null; } } // Build the Variant Array that will be used to pass the parameters necessary for the export to the toolbox object. geoprocessorVariantArray = new ESRI.ArcGIS.esriSystem.VarArrayClass(); geoprocessorVariantArray.Add(inputAnnotationFeatures); geoprocessorVariantArray.Add(System.IO.Path.Combine(OutputFileGeoDBPath, OutputFileGeoDBName + ".gdb")); geoprocessorVariantArray.Add(OutputFeatureClassName); geoprocessorVariantArray.Add(null); geoprocessorVariantArray.Add(fieldMapping); geoprocessorVariantArray.Add(null); // Determine the ArcGIS Install Path so that the Projector Toolbox can be opened to be used. generalUtilities = new PDX.BTS.DataMaintenance.MaintTools.GeneralUtilities(); string installPath = generalUtilities.DetermineArcGISDesktopInstallPath(); // Make sure the Install Path was determined successfully before moving on. if (System.String.IsNullOrEmpty(installPath)) { // Let the user know that the ArcGIS Desktop Install Path could not be determined. if (ErrorMessage != null) { ErrorMessage("Could not Determine the ArcGIS Desktop Install Path to Initialize the Projection Toolbox. The MaintTools.FeatureClassUtilities.ProjectFeatureClassHARNtoWGS() Method failed!"); } // Return FALSE to the calling method to indicate that this method failed. return false; } // Instantiate the Geoprocessing Object that will be used to export this Annotatioin Feature Class to the Output File Geodatabase. geoProcessor = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); geoProcessor.AddToolbox(System.IO.Path.Combine(installPath, @"ArcToolbox\Toolboxes\Conversion Tools.tbx")); // Perform the Export in a TRY Block so that any COM or IO Errors can be identified and handled. try { // Perform the export. geoProcessor.Execute("FeatureClassToFeatureClass_Conversion", geoprocessorVariantArray, null); // Write the messages from the Feature Class to Feature Class tool log file. int toolMessageCount = geoProcessor.MessageCount; int currentToolMessageIndex = 0; if (ProcessMessage != null) { ProcessMessage(" - Feature Class to Feature Class Operation Messages..."); } while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. if (ProcessMessage != null) { ProcessMessage(" + " + geoProcessor.GetMessage(currentToolMessageIndex)); } // Increment the Tool Message Index Counter. currentToolMessageIndex++; } } catch (System.IO.IOException ioException) { // Determine the Line Number from which the exception was thrown. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(ioException, true); System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); int lineNumber = stackFrame.GetFileLineNumber(); // Let the User know that the Dissolve Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Feature Class to Feature Class Operation in the MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method Failed with error message - " + ioException.Message + " (" + ioException.Source + " Line: " + lineNumber.ToString() + ")!"); } int toolMessageCount = geoProcessor.MessageCount; int currentToolMessageIndex = 0; if (ProcessMessage != null) { ProcessMessage("The information from the Geoprocessor is:"); } while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. if (ProcessMessage != null) { ProcessMessage(" + " + geoProcessor.GetMessage(currentToolMessageIndex)); } // Increment to Toold Message Index Counter. currentToolMessageIndex++; } // Return FALSE to the calling routine ito indicate that this process failed. return false; } catch (System.Runtime.InteropServices.COMException comException) { // Determine the Line Number from which the exception was thrown. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(comException, true); System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); int lineNumber = stackFrame.GetFileLineNumber(); // Let the User know that the Dissolve Operation Failed. if (ErrorMessage != null) { ErrorMessage("The Feature Class to Feature Class Operation in the MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method Failed with error message - " + comException.Message + " (" + comException.ErrorCode + " Line: " + lineNumber.ToString() + ")!"); } int toolMessageCount = geoProcessor.MessageCount; int currentToolMessageIndex = 0; if (ProcessMessage != null) { ProcessMessage("The information from the Geoprocessor is:"); } while (currentToolMessageIndex < toolMessageCount) { // Write the current message to the log file. if (ProcessMessage != null) { ProcessMessage(" + " + geoProcessor.GetMessage(currentToolMessageIndex)); } // Increment to Toold Message Index Counter. currentToolMessageIndex++; } // Return FALSE to the calling routine ito indicate that this process failed. return false; } // Delete the SDE Connection File since it is no longer needed. if (System.IO.File.Exists(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde")) { // Delete the existing File. System.IO.File.Delete(temporaryDirectory + OutputFeatureClassName + "SDEConn.sde"); } } catch (System.Exception caught) { // Determine the Line Number from which the exception was thrown. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true); System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); int lineNumber = stackFrame.GetFileLineNumber(); // Let the User know that this process failed. if (ErrorMessage != null) { ErrorMessage("The MaintTools.FeatureClassUtilities.ExportGeodatabaseAnnotationFeatureClassToFileGeoDB() Method failed with error message: " + caught.Message + " (Line: " + lineNumber.ToString() + ")!"); } // Return FALSE to the calling routine to indicate that this process failed. return false; } finally { // If the Input Workspace Object has been Instantiated, close it. if (inputWorkspace != null) { // Close the Input Workspace Object. inputWorkspace = null; } // If the Input Property Set Object was instantiated, close it. if (inputPropertySet != null) { inputPropertySet = null; } // If the Input Factory Type Object was instantiated, close it. if (inputFactoryType != null) { inputFactoryType = null; } // If the Input Factory Object was instantiated, close it. if (inputFactoryObject != null) { inputFactoryObject = null; } // If the Input Workspace Factory Object was instantiated, close it. if (inputWorkspaceFactory != null) { inputWorkspaceFactory = null; } // If the Input Workspace Name Object was instantiated, close it. if (inputWorkspaceName != null) { inputWorkspaceName = null; } // If the Input Dataset name Object was instantiated, close it. if (inputDataset != null) { inputDataset = null; } // If the Geoprocessor Variant Array Object was instantiated, close it. if (geoprocessorVariantArray != null) { geoprocessorVariantArray.RemoveAll(); geoprocessorVariantArray = null; } // If the General Utilities Object was instantiated, close it. if (generalUtilities != null) { generalUtilities = null; } // If the Geoprocessor Object was instantiated, close it. if (geoProcessor != null) { geoProcessor = null; } } // If the process made it to here it was successful so return TRUE to the calling method. return true; }
/// <summary> /// 字段匹配 /// </summary> /// <param name="sourceFeatClass"></param> /// <param name="targetFeatClass"></param> /// <param name="FieldPair"></param> /// <returns></returns> public static ESRI.ArcGIS.Geoprocessing.IGPFieldMapping GetGPFieldMapping(ESRI.ArcGIS.Geodatabase.IFeatureClass sourceFeatClass, ESRI.ArcGIS.Geodatabase.IFeatureClass targetFeatClass, string FieldPair) { ESRI.ArcGIS.Geodatabase.IDataset sourceDataset = sourceFeatClass as ESRI.ArcGIS.Geodatabase.IDataset; ESRI.ArcGIS.esriSystem.IName pName = sourceDataset.FullName; ESRI.ArcGIS.Geoprocessing.IGPUtilities gputilities = new ESRI.ArcGIS.Geoprocessing.GPUtilitiesClass(); ESRI.ArcGIS.Geodatabase.IDETable inputTableA = (ESRI.ArcGIS.Geodatabase.IDETable)gputilities.MakeDataElementFromNameObject(pName); ESRI.ArcGIS.esriSystem.IArray inputtables = new ESRI.ArcGIS.esriSystem.ArrayClass(); inputtables.Add(inputTableA); ESRI.ArcGIS.Geoprocessing.IGPFieldMapping pGPFieldMapping = new ESRI.ArcGIS.Geoprocessing.GPFieldMappingClass(); pGPFieldMapping.Initialize(inputtables, null); string[] sFieldPairs = FieldPair.Split(';'); if (sFieldPairs == null) { return(null); } foreach (string sFieldPair in sFieldPairs) { try { if (string.IsNullOrWhiteSpace(sFieldPair)) { continue; } string[] sourceAndtarget = sFieldPair.Split(','); if (sourceAndtarget == null || sourceAndtarget.Length <= 0) { continue; } string targetFieldName = sourceAndtarget[0]; string sourceFieldName = sourceAndtarget[1]; int sourceindex = sourceFeatClass.FindField(sourceFieldName); int targetindex = targetFeatClass.FindField(targetFieldName); if (sourceindex < 0 || targetindex < 0) { continue; } ESRI.ArcGIS.Geoprocessing.IGPFieldMap trackid = new ESRI.ArcGIS.Geoprocessing.GPFieldMapClass(); trackid.OutputField = targetFeatClass.Fields.get_Field(targetFeatClass.FindField(targetFieldName)); trackid.MergeRule = ESRI.ArcGIS.Geoprocessing.esriGPFieldMapMergeRule.esriGPFieldMapMergeRuleLast; int fieldmap_index = pGPFieldMapping.FindFieldMap(sourceFieldName); ESRI.ArcGIS.Geoprocessing.IGPFieldMap stfid_fieldmap = pGPFieldMapping.GetFieldMap(fieldmap_index); int field_index = stfid_fieldmap.FindInputField(inputTableA, sourceFieldName); ESRI.ArcGIS.Geodatabase.IField inputField = stfid_fieldmap.GetField(field_index); //if (inputField.Name == "STLLZ") //{ // ESRI.ArcGIS.Geodatabase.IField field = targetFeatClass.Fields.get_Field(targetFeatClass.FindField(targetFieldName)); //} trackid.AddInputField(inputTableA, inputField, -1, -1); pGPFieldMapping.AddFieldMap(trackid); } catch (Exception ex) { } } return(pGPFieldMapping); }