当前位置:Gxlcms > 数据库问题 > 关于使用INewLineFeedback创建线段

关于使用INewLineFeedback创建线段

时间:2021-07-01 10:21:17 帮助过:56人阅读


我知道之前大家都使用过这个接口来生成空间要素PolyLine,我之前也这样使用过这个接口,大概的流程是
1-生成一全局的IDisplayFeedback接口变量(当然生成线直接使用INewlineFeedback也行);
2-在Mousedown时间中根据点击的点位置第一点调用Start()方法或者非第一点调用IFeedback.AddPoint()方法将点击点添加如Feedback中去;
3-在MouseMove中调用MoveTo()方法;
4-在你要停止的相应事件中进行Stop()方法,该方法会返回你需要的Polyline;

当然我这里写的很简便,具体的过程可以参看帮助文档,写的还是比较详细的,稍后我会贴上关键的代码。


一般来说这样就会生成一个Polyline了我们只需要创建一个Feature接受这个折线的Geometry就可以保存该线了;但是问题来了,参看帮助文档的时候也会发现这个问题,当我们需要使用这个接口创建多于2个点要素的折线的时候,使用上面的流程是完全没有问题的,而且也可以创建只有2点的线段

但是当我只想创建一条只有2点的线段该如何是好呢?!

经测定,如果直接按照上面的流程是万万不行的,会发生The number of points is less than required for feature! 这个异常,意思很简单,就是你的添加点不够构建一个线要素!

所以需要将你想要创建的线段点全部都添加到Feedback中去,而不是从第二点开始添加,所以你需要在第二步的时候做出改变:
2-在Mousedown事件的start(point)方法之后(之前也会发生异常)立刻调用AddPoint()方法将point点添加到Feedback中去;
4-在相应的结束事件调用stop方法获取Polyline之前,将结束点endpoint也添加到Feedback中去
这样,就可以顺利的利用INewlineFeedback只添加只有2点的线段了!


废话结束上代码!!!
INewLineFeedback iLineFeed;
//计算画线的点个数
int ptCount = 0;
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
//iOriginalGeo = base.m_mapControl.TrackLine();
if (Button == 1 && ptCount == 0 && iLineFeed == null)
{
StartDrawLine(X, Y);
ptCount++;
}
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
// TODO: Add WxCrossSectionTool.OnMouseMove implementation
MoveToLine(X, Y);
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (ptCount == 1)
{
//得到所画线
IPolyline iPolyTrack = EndLine(X,Y);
}
}
//开始画线
private void StartDrawLine(int X,int Y)
{
iLineFeed = new NewLineFeedbackClass();
iLineFeed.Display = base.m_mapControl.ActiveView.ScreenDisplay;
IPoint iStartPt = base.m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
iLineFeed.Start(iStartPt);
iLineFeed.AddPoint(iStartPt);//在Start方法之后添加点,否则异常
}
//移动鼠标
private void MoveToLine(int X, int Y)
{
if (iLineFeed == null)
return;
IPoint iMoveToPt = base.m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
iLineFeed.MoveTo(iMoveToPt);
}
//结束画线
private IPolyline EndLine(int X,int Y)
{
IPolyline iLine;
try
{
if (iLineFeed == null)
return null;
else
{
iLineFeed.AddPoint(base.m_mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y));
iLine = iLineFeed.Stop();
return iLine;
}
}
finally
{
//重置变量
ptCount = 0;
//重置全局变量
iLineFeed = null;
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

关于使用INewLineFeedback创建线段

标签:接口   arcengine   

人气教程排行