利用INewLineFeedback画线

主要修改的地方为在绘制过程中或绘制结束后实现了结点的符号化,效果如图:

AE画线

 

ContractedBlock.gif ExpandedBlockStart.gif 利用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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public sealed class myAddPolyLine : BaseTool
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private IHookHelper m_hookHelper;
        
private INewLineFeedback m_LineFeedback;
        
public myAddPolyLine()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//
            
// 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
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//
                
// 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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Diagnostics.Trace.WriteLine(ex.Message, 
"Invalid Bitmap");
            }

        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
Overriden Class Methods#region Overriden Class Methods

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Occurs when this tool is created
        
/// </summary>
        
/// <param name="hook">Instance of the application</param>

        public override void OnCreate(object hook)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (m_hookHelper == null)
                m_hookHelper 
= new HookHelperClass();

            m_hookHelper.Hook 
= hook;
            
// TODO:  Add myAddLine.OnCreate implementation
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Occurs when this tool is clicked
        
/// </summary>

        public override void OnClick()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
// TODO: Add myAddLine.OnClick implementation
        }


        
public override void OnMouseDown(int Button, int Shift, int X, int Y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (Button == 2//右键
                return;
            
// TODO:  Add myAddLine.OnMouseDown implementation
           IPoint pt = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            
if (m_LineFeedback == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                m_LineFeedback 
= new NewLineFeedbackClass();
                m_LineFeedback.Display 
= m_hookHelper.ActiveView.ScreenDisplay;
                m_LineFeedback.Start(pt);
                
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                m_LineFeedback.AddPoint(pt);
            }


ContractedSubBlock.gifExpandedSubBlockStart.gif            
绘制结点的第一种方法,在绘制过程中添加结点符号,效果类似于画线编辑时的效果#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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
// 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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
        }

        
public override void OnDblClick()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IPolyline pPolyline
=null;
            IGroupElement pGroupElement 
= new GroupElementClass();
            
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (m_LineFeedback == null)
                    
return;
                pPolyline 
= m_LineFeedback.Stop();
                
if (pPolyline == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    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);

ContractedSubBlock.gifExpandedSubBlockStart.gif                
绘制结点的第二种方法,全部结束时绘制结点#region 绘制结点的第二种方法,全部结束时绘制结点
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**/////设置结点符号
                //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;

ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**/////获取得到结点
                //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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Windows.Forms.MessageBox.Show(ex.Message);
               
                
return;
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
                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

    }

}


 

 

转载于:https://www.cnblogs.com/JinDin/archive/2008/12/16/1356308.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
客户最近打电话过来,说我们的软件在量距离的时候不能平移地图。我们的软件是使用ArcEngine来开发的,在网上输入关键字“arcengine 距离测量”,可以搜索到一大堆内容基本相似的文章或代码,基本上都是采用INewLineFeedback来实现的,我们的软件也是使用此种方法。方法大致如下:写一个距离测量工具,继承自BaseTool,在该类中使用INewLineFeedback动态绘制多段折线,最后转化为Element添加到地图上。如果要增加平移功能,我们仿造ArcMap使用鼠标中键平移,在OnMouseDown、OnMouseMove、OnMouseUp事件中增加判断鼠标中键的语句,然后分别使用PanStart、PanMoveTo、PanStop实现地图的平移。 此时,出现了一个新问题,一旦移动地图后,之前使用INewLineFeedback绘制的线完全乱了方寸,有一部分线丢失了。之后又发现一个更可笑的问题,在绘制的过程中,如果使用alt+tab键切换到其它窗口,然后再切换回地图窗口的时候,会多出一条线,INewLineFeedback把切换窗口前的鼠标位置记录了下来。 对于这个问题,我使用ArcMap的测量工具检查了一下,发现不存在上述问题。但是INewLineFeedback为什么会产生这个bug,难道是本人的使用方法不对。在网上搜了一下其他类似代码进行测试,都存在这个bug。ArcEngine的SDK文档上也没有特别强调INewLineFeedback的使用细节。 本来打算用gdi进行解决,最后发现INewLineFeedback有一个Refresh函数,那么应该在什么地方使用它呢?在ArcMapControl的诸多事件中,尝试了OnAfterDraw、OnAfterScreenDraw、OnViewRefresh等,发现OnAfterScreenDraw是OK的。具体是在OnAfterScreenDraw事件中使用Refresh函数刷新INewLineFeedback,完美解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值