主要修改的地方为在绘制过程中或绘制结束后实现了结点的符号化,效果如图:
利用INewLineFeedback画线
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
namespace CHD.GIS.myCommands
{
public sealed class myAddPolyLine : BaseTool
{
private IHookHelper m_hookHelper;
private INewLineFeedback m_LineFeedback;
public myAddPolyLine()
{
//
// 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");
}
}
Overriden Class Methods#region Overriden 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;
// TODO: Add myAddLine.OnCreate implementation
}
/**//// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add myAddLine.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (Button == 2) //右键
return;
// TODO: Add myAddLine.OnMouseDown implementation
IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_LineFeedback == null)
{
m_LineFeedback = new NewLineFeedbackClass();
m_LineFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_LineFeedback.Start(pt);
}
else
{
m_LineFeedback.AddPoint(pt);
}
绘制结点的第一种方法,在绘制过程中添加结点符号,效果类似于画线编辑时的效果#region 绘制结点的第一种方法,在绘制过程中添加结点符号,效果类似于画线编辑时的效果
//设置结点符号
IRgbColor pRGB = new RgbColorClass();
pRGB.Red = 255;
pRGB.Green = 0;
pRGB.Blue = 0;
ISimpleMarkerSymbol pSimpleMarkSymbol = new SimpleMarkerSymbolClass();
pSimpleMarkSymbol.Color = pRGB as IColor;
pSimpleMarkSymbol.Size = 4;
pSimpleMarkSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;
IMarkerElement pMarkerElement = new MarkerElementClass();
pMarkerElement.Symbol = pSimpleMarkSymbol as IMarkerSymbol;
IElement ele = pMarkerElement as IElement;
ele.Geometry = pt as IGeometry;
//获取结点element的范围,以便确定刷新范围
IEnvelope pEnvBounds=new EnvelopeClass();
ele.QueryBounds(m_hookHelper.ActiveView.ScreenDisplay as IDisplay,pEnvBounds);
IGraphicsContainer pGra = m_hookHelper.ActiveView as IGraphicsContainer;
pGra.AddElement(ele, 0);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,ele, pEnvBounds); //局部刷新,第二次参数必须是ele,如果是null,画线过程中线就没了,被刷新没了.
#endregion
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
// TODO: Add myAddLine.OnMouseMove implementation
if (m_LineFeedback == null)
return;
IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_LineFeedback.MoveTo(pt);
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
}
public override void OnDblClick()
{
IPolyline pPolyline=null;
IGroupElement pGroupElement = new GroupElementClass();
try
{
if (m_LineFeedback == null)
return;
pPolyline = m_LineFeedback.Stop();
if (pPolyline == null)
{
m_LineFeedback = null;
return;
}
//设置线型
IRgbColor pRGB = new RgbColorClass();
pRGB.Red = 0;
pRGB.Green = 120;
pRGB.Blue = 0;
ISimpleLineSymbol pSimplelineSymbol = new SimpleLineSymbolClass();
pSimplelineSymbol.Color = pRGB as IColor;
pSimplelineSymbol.Width = 1.5;
pSimplelineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
//m_LineFeedback.Symbol = pSimplelineSymbol as ISymbol;
ILineElement pLineElement = new LineElementClass();
pLineElement.Symbol = pSimplelineSymbol as ILineSymbol;
IElement element = pLineElement as IElement;
element.Geometry = pPolyline as IGeometry;
pGroupElement.AddElement(element);
绘制结点的第二种方法,全部结束时绘制结点#region 绘制结点的第二种方法,全部结束时绘制结点
/**/////设置结点符号
//pRGB = new RgbColorClass();
//pRGB.Red = 255;
//pRGB.Green = 0;
//pRGB.Blue = 0;
//ISimpleMarkerSymbol pSimpleMarkSymbol = new SimpleMarkerSymbolClass();
//pSimpleMarkSymbol.Color = pRGB as IColor;
//pSimpleMarkSymbol.Size = 4;
//pSimpleMarkSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;
/**/////获取得到结点
//IPointCollection pPointCol = (IPointCollection)pPolyline;
//for(int index =0;index<pPointCol.PointCount;index++)
//{
// IPoint pt = pPointCol.get_Point(index);
// IMarkerElement pMarkerElement = new MarkerElementClass();
// pMarkerElement.Symbol = pSimpleMarkSymbol as IMarkerSymbol;
// IElement ele = pMarkerElement as IElement;
// ele.Geometry = pt as IGeometry;
// pGroupElement.AddElement(ele);
//}
#endregion
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return;
}
finally
{
IGraphicsContainer pGraphicsContainer = m_hookHelper.ActiveView as IGraphicsContainer;
pGraphicsContainer.AddElement(pGroupElement as IElement, ((IPointCollection)pPolyline).PointCount);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pGroupElement,pPolyline.Envelope);
//释放变量
m_LineFeedback = null;
}
}
#endregion
}
}
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
namespace CHD.GIS.myCommands
{
public sealed class myAddPolyLine : BaseTool
{
private IHookHelper m_hookHelper;
private INewLineFeedback m_LineFeedback;
public myAddPolyLine()
{
//
// 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");
}
}
Overriden Class Methods#region Overriden 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;
// TODO: Add myAddLine.OnCreate implementation
}
/**//// <summary>
/// Occurs when this tool is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add myAddLine.OnClick implementation
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (Button == 2) //右键
return;
// TODO: Add myAddLine.OnMouseDown implementation
IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
if (m_LineFeedback == null)
{
m_LineFeedback = new NewLineFeedbackClass();
m_LineFeedback.Display = m_hookHelper.ActiveView.ScreenDisplay;
m_LineFeedback.Start(pt);
}
else
{
m_LineFeedback.AddPoint(pt);
}
绘制结点的第一种方法,在绘制过程中添加结点符号,效果类似于画线编辑时的效果#region 绘制结点的第一种方法,在绘制过程中添加结点符号,效果类似于画线编辑时的效果
//设置结点符号
IRgbColor pRGB = new RgbColorClass();
pRGB.Red = 255;
pRGB.Green = 0;
pRGB.Blue = 0;
ISimpleMarkerSymbol pSimpleMarkSymbol = new SimpleMarkerSymbolClass();
pSimpleMarkSymbol.Color = pRGB as IColor;
pSimpleMarkSymbol.Size = 4;
pSimpleMarkSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;
IMarkerElement pMarkerElement = new MarkerElementClass();
pMarkerElement.Symbol = pSimpleMarkSymbol as IMarkerSymbol;
IElement ele = pMarkerElement as IElement;
ele.Geometry = pt as IGeometry;
//获取结点element的范围,以便确定刷新范围
IEnvelope pEnvBounds=new EnvelopeClass();
ele.QueryBounds(m_hookHelper.ActiveView.ScreenDisplay as IDisplay,pEnvBounds);
IGraphicsContainer pGra = m_hookHelper.ActiveView as IGraphicsContainer;
pGra.AddElement(ele, 0);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,ele, pEnvBounds); //局部刷新,第二次参数必须是ele,如果是null,画线过程中线就没了,被刷新没了.
#endregion
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
// TODO: Add myAddLine.OnMouseMove implementation
if (m_LineFeedback == null)
return;
IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
m_LineFeedback.MoveTo(pt);
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
}
public override void OnDblClick()
{
IPolyline pPolyline=null;
IGroupElement pGroupElement = new GroupElementClass();
try
{
if (m_LineFeedback == null)
return;
pPolyline = m_LineFeedback.Stop();
if (pPolyline == null)
{
m_LineFeedback = null;
return;
}
//设置线型
IRgbColor pRGB = new RgbColorClass();
pRGB.Red = 0;
pRGB.Green = 120;
pRGB.Blue = 0;
ISimpleLineSymbol pSimplelineSymbol = new SimpleLineSymbolClass();
pSimplelineSymbol.Color = pRGB as IColor;
pSimplelineSymbol.Width = 1.5;
pSimplelineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
//m_LineFeedback.Symbol = pSimplelineSymbol as ISymbol;
ILineElement pLineElement = new LineElementClass();
pLineElement.Symbol = pSimplelineSymbol as ILineSymbol;
IElement element = pLineElement as IElement;
element.Geometry = pPolyline as IGeometry;
pGroupElement.AddElement(element);
绘制结点的第二种方法,全部结束时绘制结点#region 绘制结点的第二种方法,全部结束时绘制结点
/**/////设置结点符号
//pRGB = new RgbColorClass();
//pRGB.Red = 255;
//pRGB.Green = 0;
//pRGB.Blue = 0;
//ISimpleMarkerSymbol pSimpleMarkSymbol = new SimpleMarkerSymbolClass();
//pSimpleMarkSymbol.Color = pRGB as IColor;
//pSimpleMarkSymbol.Size = 4;
//pSimpleMarkSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;
/**/////获取得到结点
//IPointCollection pPointCol = (IPointCollection)pPolyline;
//for(int index =0;index<pPointCol.PointCount;index++)
//{
// IPoint pt = pPointCol.get_Point(index);
// IMarkerElement pMarkerElement = new MarkerElementClass();
// pMarkerElement.Symbol = pSimpleMarkSymbol as IMarkerSymbol;
// IElement ele = pMarkerElement as IElement;
// ele.Geometry = pt as IGeometry;
// pGroupElement.AddElement(ele);
//}
#endregion
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return;
}
finally
{
IGraphicsContainer pGraphicsContainer = m_hookHelper.ActiveView as IGraphicsContainer;
pGraphicsContainer.AddElement(pGroupElement as IElement, ((IPointCollection)pPolyline).PointCount);
m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pGroupElement,pPolyline.Envelope);
//释放变量
m_LineFeedback = null;
}
}
#endregion
}
}