GeoDataExport 請參考博文https://blog.csdn.net/RicardoMTan/article/details/102799403
/***************************
Write by Chn gzGISer Tst
CSDN blog Ricardo.M.Tan
**************************/
using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using Chn.SpatialDataProcessing.GeoDataHelper.Processing;
namespace Chn.SpatialDataProcessing.Command {
/// <summary>
/// Command that works in ArcMap/Map/PageLayout
/// </summary>
[Guid("29e28108-a50d-41b2-92dd-3055250afc34")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Chn.SpatialDataProcessing.Command.ExportSelectedFeatureCmd")]
public sealed class ExportSelectedFeatureCmd : BaseCommand {
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType) {
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType) {
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper = null;
IMapControl3 m_mapControl3 = null;
IFeatureLayer m_currentLayer = null;
/// <summary>
/// 導出所選圖層
/// </summary>
public ExportSelectedFeatureCmd() {
//
// TODO: Define values for the public properties
//
base.m_category = "通用工具"; //localizable text
base.m_caption = "導出所選要素"; //localizable text
base.m_message = "導出所選要素"; //localizable text
base.m_toolTip = "導出所選要素"; //localizable text
base.m_name = "ExportSelectedFeature"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try {
base.m_bitmap = Properties.Resources.LayerExportData16;
} catch (Exception ex) {
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook) {
if (hook == null)
return;
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.Hook is IMapControl3) {
m_mapControl3 = m_hookHelper.Hook as IMapControl3;
if (m_mapControl3.CustomProperty is IFeatureLayer) {
m_currentLayer = m_mapControl3.CustomProperty as IFeatureLayer;
IFeatureSelection pSelection = m_currentLayer as IFeatureSelection;
ISelectionSet pSelectionSet = pSelection.SelectionSet;
if (pSelectionSet.Count == 0) {
m_enabled = false;
} else {
m_enabled = true;
}
} else {
m_enabled = false;
}
} else {
m_enabled = false;
}
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick() {
FolderBrowserDialog folder = new FolderBrowserDialog() {
Description = "選擇導出路徑"
};
if (folder.ShowDialog() == DialogResult.OK) {
IWorkspaceFactory outputWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace outputWorkspace = outputWorkspaceFactory.OpenFromFile(folder.SelectedPath, 0);
m_currentLayer = m_mapControl3.CustomProperty as IFeatureLayer;
if (m_currentLayer == null) return;
GeoDataExport geodataExport = new GeoDataExport();
geodataExport.Export(m_currentLayer, outputWorkspace);
}
}
#endregion
}
}