📄 createshape.cs
字号:
using System;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
namespace Cstest1.Class
{
/// <summary>
/// CreateShape 的摘要说明。
/// </summary>
public class CreateShape : ITool,ICommand
{
ESRI.ArcGIS.MapControl.AxMapControl m_MapControl; //设置地图控件对象
ESRI.ArcGIS.Carto.ILayer m_CurrentLayer; //获取当前编辑图层
private IPoint m_PointStop;
private IDisplayFeedback m_Feedback; //移动图形的拖放对象
private bool m_bInUse; //画点线面时的使用状态,画线面时为真
public CreateShape(ILayer pCurrentLayer)
{
//
// TODO: 在此处添加构造函数逻辑
m_CurrentLayer=pCurrentLayer;
//
}
#region ITool 成员
public void OnMouseDown(int button, int shift, int x, int y)
{
// TODO: 添加 CreateShape.OnMouseDown 实现
if(button==1)
{
IFeatureLayer m_FeatureLayer = (IFeatureLayer)m_CurrentLayer;
if (m_FeatureLayer.FeatureClass == null) return;
IActiveView m_ActiveView = m_MapControl.ActiveView;
IPoint m_PointMousedown = m_ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x,y);
m_PointStop=m_PointMousedown;
if (!m_bInUse)
{
switch(m_FeatureLayer.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint: //点类型
CreateFeature(m_PointMousedown);
m_MapControl.ActiveView.Refresh();
break;
case esriGeometryType.esriGeometryPolyline: //线类型
m_bInUse = true;
m_Feedback = new NewLineFeedback();
INewLineFeedback m_LineFeed = (INewLineFeedback)m_Feedback;
m_LineFeed.Start(m_PointMousedown);
break;
case esriGeometryType.esriGeometryPolygon: //多边形类型
m_bInUse = true;
m_Feedback = new NewPolygonFeedback();
INewPolygonFeedback m_PolyFeed = (INewPolygonFeedback)m_Feedback;
m_PolyFeed.Start(m_PointMousedown);
break;
}
if (m_Feedback != null)
m_Feedback.Display = m_ActiveView.ScreenDisplay;
}
else
{
if (m_Feedback is INewLineFeedback)
{
INewLineFeedback m_LineFeed = (INewLineFeedback)m_Feedback;
m_LineFeed.AddPoint(m_PointMousedown);
}
else if (m_Feedback is INewPolygonFeedback)
{
INewPolygonFeedback m_PolyFeed = (INewPolygonFeedback)m_Feedback;
m_PolyFeed.AddPoint(m_PointMousedown);
}
}
}
}
public void OnMouseMove(int button, int shift, int x, int y)
{
// TODO: 添加 CreateShape.OnMouseMove 实现
// if(button==1)
// {
if (!m_bInUse || m_Feedback == null) return;
// 移动鼠标形成线、面的节点
IActiveView m_ActiveView = m_MapControl.ActiveView;
m_Feedback.MoveTo(m_ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y));
// m_Point = m_ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x,y);
//}
}
public void OnMouseUp(int button, int shift, int x, int y)
{
// TODO: 添加 CreateShape.OnMouseUp 实现
}
public void OnKeyDown(int keyCode, int shift)
{
// TODO: 添加 CreateShape.OnKeyDown 实现
}
public void OnKeyUp(int keyCode, int shift)
{
// TODO: 添加 CreateShape.OnKeyUp 实现
}
public int Cursor
{
get
{
// TODO: 添加 CreateShape.Cursor getter 实现
return 0;
}
}
public bool OnContextMenu(int x, int y)
{
// TODO: 添加 CreateShape.OnContextMenu 实现
return false;
}
public bool Deactivate()
{
// TODO: 添加 CreateShape.Deactivate 实现
return true; //返回true,保证对象注销时不报错
}
public void Refresh(int hdc)
{
// TODO: 添加 CreateShape.Refresh 实现
}
public void OnDblClick()
{
// TODO: 添加 CreateShape.OnDblClick 实现
EndSketch();
}
#endregion
#region ICommand 成员
public void OnClick()
{
// TODO: 添加 CreateShape.OnClick 实现
}
public string Message
{
get
{
// TODO: 添加 CreateShape.Message getter 实现
return null;
}
}
public int Bitmap
{
get
{
// TODO: 添加 CreateShape.Bitmap getter 实现
return 0;
}
}
public void OnCreate(object hook)
{
// TODO: 添加 CreateShape.OnCreate 实现
m_MapControl=hook as AxMapControl; //工具创建时,设置地图控件对象
m_bInUse=false;
}
public string Caption
{
get
{
// TODO: 添加 CreateShape.Caption getter 实现
return null;
}
}
public string Tooltip
{
get
{
// TODO: 添加 CreateShape.Tooltip getter 实现
return null;
}
}
public int HelpContextID
{
get
{
// TODO: 添加 CreateShape.HelpContextID getter 实现
return 0;
}
}
public string Name
{
get
{
// TODO: 添加 CreateShape.Name getter 实现
return null;
}
}
public bool Checked
{
get
{
// TODO: 添加 CreateShape.Checked getter 实现
return false;
}
}
public bool Enabled
{
get
{
// TODO: 添加 CreateShape.Enabled getter 实现
return false;
}
}
public string HelpFile
{
get
{
// TODO: 添加 CreateShape.HelpFile getter 实现
return null;
}
}
public string Category
{
get
{
// TODO: 添加 CreateShape.Category getter 实现
return null;
}
}
#endregion
private void EndSketch() //结束一次画图操作
{
IGeometry m_Geometry = null;
IPointCollection m_PointCollection = null;
if (m_Feedback is INewLineFeedback) //线对象有效
{
INewLineFeedback m_LineFeed = (INewLineFeedback)m_Feedback;
m_LineFeed.AddPoint(m_PointStop);
IPolyline m_PolyLine = m_LineFeed.Stop();
m_PointCollection = (IPointCollection)m_PolyLine;
if (m_PointCollection.PointCount <2)
{
MessageBox.Show("需要两个点才能生成一条线!", "未能生成线", MessageBoxButtons.OK);
return;
}
else
{
m_Geometry = (IGeometry)m_PointCollection;
}
}
else if (m_Feedback is INewPolygonFeedback) //多边形对象有效
{
INewPolygonFeedback m_PolyFeed = (INewPolygonFeedback)m_Feedback;
m_PolyFeed.AddPoint(m_PointStop);
IPolygon m_Polygon = m_PolyFeed.Stop();
if (m_Polygon != null)
m_PointCollection = (IPointCollection)m_Polygon;
if (m_PointCollection.PointCount < 3)
{
MessageBox.Show("需要三个点才能生成一个面!", "未能生成面", MessageBoxButtons.OK);
return;
}
else
{
m_Geometry = (IGeometry)m_PointCollection;
}
}
CreateFeature(m_Geometry);
m_MapControl.ActiveView.Refresh();
// m_Point = null;
m_Feedback = null;
m_bInUse = false;
}
private void CreateFeature(IGeometry m_Geometry) //创建要素
{
if (m_Geometry == null) return;
if (m_CurrentLayer == null) return;
IWorkspaceEdit m_WorkspaceEdit = GetWorkspaceEdit();
IFeatureLayer m_FeatureLayer = (IFeatureLayer)m_CurrentLayer;
IFeatureClass m_FeatureClass = m_FeatureLayer.FeatureClass;
m_WorkspaceEdit.StartEditOperation(); //使用WorkspaceEdit接口新建要素
IFeature m_Feature = m_FeatureClass.CreateFeature();
m_Feature.Shape = m_Geometry;
m_Feature.Store();
m_WorkspaceEdit.StopEditOperation();
// 以一定缓冲范围刷新视图
IActiveView m_ActiveView = m_MapControl.ActiveView;
if (m_Geometry.GeometryType == esriGeometryType.esriGeometryPoint)
{
double Length;
Length = ConvertPixelsToMapUnits(m_ActiveView, 30);
ITopologicalOperator m_Topo = (ITopologicalOperator)m_Geometry;
IGeometry m_Buffer = m_Topo.Buffer(Length);
m_ActiveView.PartialRefresh((esriViewDrawPhase)(esriDrawPhase.esriDPGeography |
esriDrawPhase.esriDPSelection), m_CurrentLayer, m_Buffer.Envelope);
}
else
m_ActiveView.PartialRefresh((esriViewDrawPhase)(esriDrawPhase.esriDPGeography |
esriDrawPhase.esriDPSelection), m_CurrentLayer, m_Geometry.Envelope);
}
private IWorkspaceEdit GetWorkspaceEdit() //获取当前编辑空间
{
if (m_CurrentLayer == null) return null;
IFeatureLayer m_FeatureLayer = (IFeatureLayer)m_CurrentLayer;
IFeatureClass m_FeatureClass = m_FeatureLayer.FeatureClass;
IDataset m_Dataset = (IDataset)m_FeatureClass;
if ( m_Dataset == null) return null;
return (IWorkspaceEdit)m_Dataset.Workspace;
}
private double ConvertPixelsToMapUnits(IActiveView pActiveView , double pixelUnits)
{
// 依据当前视图,将屏幕像素转换成地图单位
IPoint Point1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft;
IPoint Point2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight;
int x1, x2, y1, y2;
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(Point1, out x1, out y1);
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(Point2, out x2, out y2);
double pixelExtent = x2 - x1;
double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -