ArcEngine 面积测量实现

更新时间:2023-09-10 07:00:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

ArcEngine 距离测量和面积测量实现

上一篇 / 下一篇 2010-08-13 17:08:13 / 个人分类:ArcEngine

查看( 171 ) / 评论( 0 ) / 评分( 0 / 0 )

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chyocean/archive/2008/04/28/2337310.aspx 觉得这个自定义ITool实现的很完整,所以就借鉴过来了。 面积测量中最主要的接口就是INewPolygonFeedback。

下面就是AreaMeasure.cs的全部内容,这是将实现和调用分开的。 //自定义画多边形,测面积

ToolbarControl.AddItem(new AreaMeasure(), -1, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); 添加面积测量的功能。 AreaMeasure.cs的实现如下: using System;

using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.ADF.CATIDs; using System.Runtime.InteropServices; namespace com.san30.wjcg.Commands {

[ClassInterface(ClassInterfaceType.None)]

[Guid(\ ///

/// 测量距离功能

///

public class AreaMeasure : ICommand, ITool {

#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 ///

/// Required method for ArcGIS Component Category registration - /// Do not modify the contents of this method with the code editor. ///

private static void ArcGISCategoryRegistration(Type registerType)

{

string regKey = string.Format(\ ControlsCommands.Register(regKey); }

///

/// Required method for ArcGIS Component Category unregistration - /// Do not modify the contents of this method with the code editor. ///

private static void ArcGISCategoryUnregistration(Type registerType) {

string regKey = string.Format(\ ControlsCommands.Unregister(regKey); }

#endregion #endregion

[DllImport(\

static extern bool DeleteObject(IntPtr hObject);//删除对象 private System.Drawing.Bitmap m_bitmap;//工具显示图标 private IntPtr m_hBitmap;//用于表示指针或者句柄的平台特殊类型 private IHookHelper m_pHookHelper;//句柄

private System.Windows.Forms.Cursor m_areaMeasureCur;//光标 private bool m_enabled;//是否可用 private bool m_check;//是否选中

private bool m_isMouseDown;//鼠标是否按下

private INewPolygonFeedback m_pNewPolyFeedback;//INewPolygonFeedback,控制新的多边形显示反馈

private IActiveView pActiveView;//视图 ///

///构造函数

///

public AreaMeasure() {

//Load resources

string[] res = GetType().Assembly.GetManifestResourceNames(); if (res.GetLength(0) > 0) {

m_bitmap = new

System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), \ if (m_bitmap != null) {

m_bitmap.MakeTransparent(m_bitmap.GetPixel(1, 1)); m_hBitmap = m_bitmap.GetHbitmap(); } }

m_pHookHelper = new HookHelperClass(); }

///

/// 释构函数 /// ~AreaMeasure() {

if (m_hBitmap.ToInt32() != 0) DeleteObject(m_hBitmap); m_pHookHelper = null; m_check = false; //m_measureCur = null; }

#region ICommand Members

//点击

public void OnClick() { }

//消息 属性

public string Message { get {

return \测量多边形面积\ } }

//图标 属性 public int Bitmap { get {

return m_hBitmap.ToInt32(); } }

///

/// 创建工具命令 ///

/// public void OnCreate(object hook) {

m_pHookHelper.Hook = hook; m_enabled = true; m_check = false;

/// /// ///

public void OnMouseUp(int button, int shift, int x, int y) {

// TODO: Add AreaMeasure.OnMouseUp implementation }

///

/// 键盘按下事件 ///

/// /// public void OnKeyDown(int keyCode, int shift) {

if (m_isMouseDown) {

if (keyCode == 27) {

m_isMouseDown = false; m_pNewPolyFeedback = null;

m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null); } } }

///

/// 键盘放开事件 ///

/// /// public void OnKeyUp(int keyCode, int shift)

{

if (m_isMouseDown) {

if (keyCode == 27) {

m_isMouseDown = false; m_pNewPolyFeedback = null;

m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null); } } }

///

/// 光标 /// public int Cursor { get {

return m_areaMeasureCur.Handle.ToInt32(); } }

///

/// 上下文菜单事件 ///

/// /// /// bool

public bool OnContextMenu(int x, int y) {

// TODO: Add AreaMeasure.OnContextMenu implementation return false; }

///

/// 解除事件 ///

/// bool public bool Deactivate() {

return true; }

///

/// 刷新 ///

///

//Get a reference to the ActiveView if (m_pHookHelper != null) {

pActiveView = (IActiveView)m_pHookHelper.FocusMap; } }

///

/// 双击事件 ///

public void OnDblClick() { //图形

IGeometry pGeompoly = null;

//Get the geometry (Polygon) returned from the feedback if (m_pNewPolyFeedback != null) {

pGeompoly = m_pNewPolyFeedback.Stop(); }

//' If it is valid then draw a Geompoly on the ActiveView using the // DrawGeompoly procedure if (pGeompoly != null) {

//AddCreateElement(pGeompoly, pActiveView);

if (pGeompoly.GeometryType.Equals(esriGeometryType.esriGeometryPolygon)) {

DrawGeompoly(pGeompoly, pActiveView); }

//pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); }

//' Set the feedback to nothing for the next use m_pNewPolyFeedback = null; }

///

/// 画多边形 ///

///

public void DrawGeompoly(IGeometry pGeompoly, IActiveView pAV) {

if ((pGeompoly == null) || (pAV == null)) { return; }

//Takes an IGeometry and IActiveView

//in the ActiveView's BasicGraphicsLayer //填充样式

ISimpleFillSymbol pSFillSym; //文本样式

ITextSymbol pTextSymbol; //填充颜色

IRgbColor pFillRGB; //文本颜色

IRgbColor pTextRGB; //多边形

IPolygon pPolygon = pGeompoly as IPolygon; //多边形区域

IArea pArea = pPolygon as IArea; //多边形中心点

IPoint pTextPoint = pArea.Centroid; //多边形面积

double dArea = Math.Round(Math.Abs(pArea.Area),2); //格式化

string strArea = string.Format(\ strArea = strArea + \平方米\

//Create a new RGBColor pFillRGB = new RgbColorClass(); pFillRGB.Red = 110; pFillRGB.Green = 40; pFillRGB.Blue = 255; //Create a new RGBColor

pTextRGB = new RgbColorClass(); pTextRGB.Red = 60; pTextRGB.Green = 120;

pTextRGB.Blue = 230;

//Create a new SimpleFillSymbol and set its Color and Style pSFillSym = new SimpleFillSymbolClass(); pSFillSym.Color = pFillRGB;

pSFillSym.Style. = esriSimpleFillStyle.esriSFSSolid; //Text Symbol

pTextSymbol = new ESRI.ArcGIS.Display.TextSymbolClass();

pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter; pTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter; pTextSymbol.Size = 16; pTextSymbol.Color = pTextRGB; //pSymbol = pTextSymbol as ISymbol;

//pSymbol.ROP2 = esriRasterOpCode.esriROPBlack; pTextSymbol.Text = strArea;

pAV.ScreenDisplay.StartDrawing(pAV.ScreenDisplay.hDC, -1); //Use existing symbols and draw existing text and Polygon pAV.ScreenDisplay.SetSymbol(pSFillSym as ISymbol); pAV.ScreenDisplay.DrawPolygon(pGeompoly as IPolygon); pActiveView.ScreenDisplay.SetSymbol(pTextSymbol as ISymbol); pActiveView.ScreenDisplay.DrawText(pTextPoint, pTextSymbol.Text); pAV.ScreenDisplay.FinishDrawing(); }

#endregion } }

pTextRGB.Blue = 230;

//Create a new SimpleFillSymbol and set its Color and Style pSFillSym = new SimpleFillSymbolClass(); pSFillSym.Color = pFillRGB;

pSFillSym.Style. = esriSimpleFillStyle.esriSFSSolid; //Text Symbol

pTextSymbol = new ESRI.ArcGIS.Display.TextSymbolClass();

pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter; pTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter; pTextSymbol.Size = 16; pTextSymbol.Color = pTextRGB; //pSymbol = pTextSymbol as ISymbol;

//pSymbol.ROP2 = esriRasterOpCode.esriROPBlack; pTextSymbol.Text = strArea;

pAV.ScreenDisplay.StartDrawing(pAV.ScreenDisplay.hDC, -1); //Use existing symbols and draw existing text and Polygon pAV.ScreenDisplay.SetSymbol(pSFillSym as ISymbol); pAV.ScreenDisplay.DrawPolygon(pGeompoly as IPolygon); pActiveView.ScreenDisplay.SetSymbol(pTextSymbol as ISymbol); pActiveView.ScreenDisplay.DrawText(pTextPoint, pTextSymbol.Text); pAV.ScreenDisplay.FinishDrawing(); }

#endregion } }

本文来源:https://www.bwwdw.com/article/b9qh.html

Top