获取Polyline两点间的线段
先看一下效果图
看看主要的函数
/// <summary>
/// 创建区间线段
/// </summary>
/// <param name="pLine">输入的线图形</param>
/// <param name="p1">插入的其中一个点</param>
/// <param name="p2">插入的一种一个点</param>
/// <returns>这两点间的线段</returns>
/// 创建人 : 懒羊羊
private IPolyline BuildLine(IPolyline pLine, IPoint p1, IPoint p2)
{
bool isSplit;
int splitIndex, segIndex;
//插入第一点,segIndex记录插入点的相对线的节点位置
pLine.SplitAtPoint(p1, true, false, out isSplit, out splitIndex, out segIndex);
int fIndex = segIndex;
//插入第二点
pLine.SplitAtPoint(p2, true, false, out isSplit, out splitIndex, out segIndex);
int sIndex = segIndex;
//比较一下插入第一点和第二点的节点次序
if (fIndex > sIndex)
{
int temp = fIndex;
fIndex = sIndex;
sIndex = temp;
}
IPointCollection pPointCol = new PolylineClass();
object o = Type.Missing;
//利用两点区间,获取线上区间所在的点,并将其转换为线
IPointCollection LineCol = pLine as IPointCollection;
for (int i = fIndex; i <= sIndex; i++)
{
pPointCol.AddPoint(LineCol.get_Point(i), ref o, ref o);
}
return pPointCol as IPolyline;
}
顺便发一个工程文件。喜欢的就下来看看吧。都是很简单的代码,看明白上面的代码就不要去下载
完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
namespace BreakLine
{
public partial class Form1 : Form
{
IFeatureClass oLineClass;
IFeature pfeat;
IPoint firstPoint;
IPoint secondPoint;
bool first;
public Form1()
{
InitializeComponent();
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
IPolyline pLine = pfeat.Shape as IPolyline;
IPoint clickPoint = new PointClass();
clickPoint.PutCoords(e.mapX, e.mapY);
if (first == false)
{
firstPoint = queryLinePoint(pLine, clickPoint);
drawPoint(firstPoint);
first = true;
}
else
{
secondPoint = queryLinePoint(pLine, clickPoint);
drawPoint(secondPoint);
IPolyline myLine = BuildLine(pLine, firstPoint, secondPoint);
drawLine(myLine);
first = false;
}
}
private IFeatureClass FindClassByIndex(int layerIndex)
{
ILayer pLayer = axMapControl1.get_Layer(layerIndex);
IFeatureLayer pFtr = pLayer as IFeatureLayer;
return pFtr.FeatureClass;
}
private void Form1_Load(object sender, EventArgs e)
{
oLineClass = FindClassByIndex(0);
pfeat = oLineClass.GetFeature(1);
first = false;
}
/// <summary>
/// 查询线上的点
/// </summary>
/// <param name="pPolyline"></param>
/// <param name="pPoint"></param>
/// <returns></returns>
private IPoint queryLinePoint(IPolyline pPolyline, IPoint pPoint)
{
IPoint outPoint = new PointClass();
double n=1;
double t=1;
bool isR=false;
pPolyline.QueryPointAndDistance(esriSegmentExtension.esriNoExtension, pPoint, false, outPoint, ref n, ref t, ref isR);
return outPoint;
}
private void drawPoint(IPoint cPoint)
{
ISimpleMarkerSymbol pMark = new SimpleMarkerSymbolClass();
IRgbColor myColor = new RgbColorClass();
myColor.Blue = 0;
myColor.Red = 255;
myColor.Green = 0;
pMark.Color = myColor as IColor;
pMark.Size = 2;
pMark.Style = esriSimpleMarkerStyle.esriSMSSquare;
object pSym = pMark as object;
axMapControl1.DrawShape(cPoint, ref pSym);
}
/// <summary>
/// 创建线
/// </summary>
/// <param name="pLine">输入的线图形</param>
/// <param name="p1">插入的其中一个点</param>
/// <param name="p2">插入的一种一个点</param>
/// <returns>这两点间的线段</returns>
/// 创建人 : 懒羊羊
private IPolyline BuildLine(IPolyline pLine, IPoint p1, IPoint p2)
{
bool isSplit;
int splitIndex, segIndex;
//插入第一点,segIndex记录插入点的相对线的节点位置
pLine.SplitAtPoint(p1, true, false, out isSplit, out splitIndex, out segIndex);
int fIndex = segIndex;
//插入第二点
pLine.SplitAtPoint(p2, true, false, out isSplit, out splitIndex, out segIndex);
int sIndex = segIndex;
//比较一下插入第一点和第二点的节点次序
if (fIndex > sIndex)
{
int temp = fIndex;
fIndex = sIndex;
sIndex = temp;
}
IPointCollection pPointCol = new PolylineClass();
object o = Type.Missing;
//利用两点区间,获取线上区间所在的点,并将其转换为线
IPointCollection LineCol = pLine as IPointCollection;
for (int i = fIndex; i <= sIndex; i++)
{
pPointCol.AddPoint(LineCol.get_Point(i), ref o, ref o);
}
return pPointCol as IPolyline;
}
/// <summary>
/// 绘制线
/// </summary>
/// <param name="myLine"></param>
private void drawLine(IPolyline myLine)
{
ISimpleLineSymbol lineSym = new SimpleLineSymbolClass();
IRgbColor myColor = new RgbColorClass();
myColor.Blue = 0;
myColor.Red = 0;
myColor.Green = 255;
lineSym.Color= myColor as IColor;
lineSym.Width = 2;
lineSym.Style = esriSimpleLineStyle.esriSLSSolid;
object pSym = lineSym as object;
axMapControl1.DrawShape(myLine, ref pSym);
}
}
}