Add a custom menu created in .NET to ArcGIS Desktop
[C#]
AddShapefile.cs
// Copyright 2008 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
//
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
namespace CustomMenus
{
/// <summary>
/// Summary description for AddShapefile.
/// </summary>
[Guid("6f8f48fe-bfbc-4b2a-b1e7-25eced548743")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CustomMenus.AddShapefile")]
public sealed class AddShapefile : 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);
}
/// <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);
}
#endregion
#endregion
private IApplication m_application;
public AddShapefile()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = "Add Shapefile"; //localizable text
base.m_message = ""; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = "CustomMenus_AddShapefile"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overriden 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_application = hook as IApplication;
//Disable if it is not ArcMap
if (hook is IMxApplication)
base.m_enabled = true;
else
base.m_enabled = false;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add AddShapefile.OnClick implementation
ESRI.ArcGIS.Carto.IActiveView activeView = GetActiveViewFromArcMap(m_application);
AddShapefileUsingOpenFileDialog(activeView);
}
#endregion
//#### ArcGIS Snippets ####
#region "Add Shapefile Using OpenFileDialog"
// ArcGIS Snippet Title:
// Add Shapefile Using OpenFileDialog
//
// Long Description:
// Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.
//
// Add the following references to the project:
// ESRI.ArcGIS.Carto
// ESRI.ArcGIS.DataSourcesFile
// ESRI.ArcGIS.Geodatabase
// System.Windows.Forms
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
// ArcGIS Engine
//
// Applicable ArcGIS Product Versions:
// 9.2
// 9.3
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
///<summary>Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///
///<remarks></remarks>
public void AddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveView activeView)
{
//parameter check
if (activeView == null)
{
return;
}
// Use the OpenFileDialog Class to choose which shapefile to load.
System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "Shapefiles (*.shp)|*.shp";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// The user chose a particular shapefile.
// The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp"
string shapefileLocation = openFileDialog.FileName;
if (shapefileLocation != "")
{
// Ensure the user chooses a shapefile
// Create a new ShapefileWorkspaceFactory CoClass to create a new workspace
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
// System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\"
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast
// System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities"
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));
ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
featureLayer.FeatureClass = featureClass;
featureLayer.Name = featureClass.AliasName;
featureLayer.Visible = true;
activeView.FocusMap.AddLayer(featureLayer);
// Zoom the display to the full extent of all layers in the map
activeView.Extent = activeView.FullExtent;
activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null);
}
else
{
// The user did not choose a shapefile.
// Do whatever remedial actions as necessary
// System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1",
// System.Windows.Forms.MessageBoxButtons.OK,
// System.Windows.Forms.MessageBoxIcon.Exclamation);
}
}
else
{
// The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button.
// Do whatever remedial actions as necessary.
// System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2",
// System.Windows.Forms.MessageBoxButtons.OK,
// System.Windows.Forms.MessageBoxIcon.Exclamation);
}
}
#endregion
#region "Get ActiveView from ArcMap"
// ArcGIS Snippet Title:
// Get ActiveView from ArcMap
//
// Long Description:
// Get ActiveView from ArcMap.
//
// Add the following references to the project:
// ESRI.ArcGIS.ArcMapUI
// ESRI.ArcGIS.Carto
// ESRI.ArcGIS.Framework
// ESRI.ArcGIS.System
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
//
// Applicable ArcGIS Product Versions:
// 9.2
// 9.3
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
///<summary>Get ActiveView from ArcMap</summary>
///
///<param name="application">An IApplication interface that is the ArcMap application.</param>
///
///<returns>An IActiveView interface.</returns>
///
///<remarks></remarks>
public ESRI.ArcGIS.Carto.IActiveView GetActiveViewFromArcMap(ESRI.ArcGIS.Framework.IApplication application)
{
if (application == null)
{
return null;
}
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument; // Dynamic Cast
ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;
return activeView;
}
#endregion
}
}
Add a custom menu created in .NET to ArcGIS Desktop
最新推荐文章于 2022-04-10 16:34:14 发布