1、前言
在ArcEngine
中,要素的批量旋转可以使用IFeatureEdit
实现,下面给出实现代码。
2、旋转要素
2.1、主界面代码
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Windows.Forms;
using WindowsFormsApplication1.Command;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
btnSelect.Enabled = false;
btnRotate.Enabled = false;
}
// 开始编辑
private void btnStartEditing_Click(object sender, EventArgs e)
{
ICommand command = new ControlsEditingStartCommand();
command.OnCreate(axMapControl1.Object);
command.OnClick();
btnStartEditing.Enabled = false;
btnStopEditing.Enabled = true;
btnSelect.Enabled = true;
btnRotate.Enabled = true;
}
// 结束编辑
private void btnStopEditing_Click(object sender, EventArgs e)
{
ICommand saveCommand = new ControlsEditingSaveCommand();
saveCommand.OnCreate(axMapControl1.Object);
saveCommand.OnClick();
ICommand clearSelectionCommand = new ControlsClearSelectionCommand();
clearSelectionCommand.OnCreate(axMapControl1.Object);
clearSelectionCommand.OnClick();
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
btnSelect.Enabled = false;
btnRotate.Enabled = false;
}
// 选择要素
private void btnSelect_Click(object sender, EventArgs e)
{
ICommand command = new ControlsSelectFeaturesTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 旋转要素
private void btnRotate_Click(object sender, EventArgs e)
{
ICommand command = new RotateFeaturesTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
}
}
2.2、旋转工具代码
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1.Command
{
/// <summary>
/// Summary description for RotateFeaturesTool.
/// </summary>
[Guid("adf70a8f-8f0e-4d28-a261-be552a023d96")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Command.RotateFeaturesTool")]
public sealed class RotateFeaturesTool : BaseTool
{
#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);
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);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
public RotateFeaturesTool()
{
//
// 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 = ""; //unique id, non-localizable (e.g. "MyCategory_MyTool")
try
{
//
// TODO: change resource name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this tool is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (m_hookHelper.FocusMap.SelectionCount == 0)
{
MessageBox.Show("请选择需要旋转的要素", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取被选择的要素
IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
pEnumFeature.Reset();
IFeature pFeature = pEnumFeature.Next();
ISet pSet = new Set();
while (pFeature != null)
{
pSet.Add(pFeature);
pFeature = pEnumFeature.Next();
}
// 批量旋转要素
IPoint pPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
pSet.Reset();
pFeature = pSet.Next() as IFeature;
while (pFeature != null)
{
IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
pFeatureEdit.RotateSet(pSet, pPoint, 45);
pFeature.Store();
pFeature = pSet.Next() as IFeature;
}
// 刷新地图
m_hookHelper.ActiveView.Refresh();
}
#endregion
}
}
运行结果如下图所示: