1.封闭线与打断操作,这部分新手未必知道。
///< summary>
/// 利用面生成闭合的曲线
///< /summary>
///< param name="pPolygon">传入的面图形</param>
///< returns></returns>
/// 创建人: 懒羊羊
private IPolyline PolygonToLine(IPolygon pPolygon)
{
IGeometryCollection pGeometryCollectionPolygon;
IClone pClone;
ISegmentCollection pSegmentCollectionPath;
object o = Type.Missing;
IGeometryCollection pGeoCol = new PolylineClass();
pClone = (IClone)pPolygon;
pGeometryCollectionPolygon = pClone.Clone() as IGeometryCollection;
for (int i = 0; i < pGeometryCollectionPolygon.GeometryCount; i++)
{
pSegmentCollectionPath = new PathClass();
pSegmentCollectionPath.AddSegmentCollection(pGeometryCollectionPolygon.get_Geometry(i) as ISegmentCollection);
pGeoCol.AddGeometry(pSegmentCollectionPath as IGeometry, ref o, ref o);
}
return pGeoCol as IPolyline;
}
///< summary>
/// 线上距离FromPoint的DisOnLine距离上的一点,在这点处把线打断为两段
///< /summary>
///< param name="myPolyline">传入的限图形</param>
///< param name="DisOnLine">离FromPoint的距离</param>
///< returns></returns>
/// 创建人:懒羊羊
private IPolyline[] BreakLineToTwoPart(IPolyline myPolyline,double DisOnLine)
{
if (DisOnLine < myPolyline.Length) //如果传入的长度大于线的长度,不与操作
{
return null;
}
IPolyline[] Lines = new IPolyline[2];
bool isSplit;
int splitIndex, segIndex;
object o = Type.Missing;
myPolyline.SplitAtDistance(DisOnLine, false, false, out isSplit, out splitIndex, out segIndex);
IPolyline newLine = new PolylineClass();
ISegmentCollection lineSegCol = (ISegmentCollection)myPolyline;
ISegmentCollection newSegCol = (ISegmentCollection)newLine;
for (int j = segIndex; j < lineSegCol.SegmentCount; j++)
{
newSegCol.AddSegment(lineSegCol.get_Segment(j), ref o, ref o);
}
//重新构建两条线
lineSegCol.RemoveSegments(segIndex, lineSegCol.SegmentCount - segIndex, true);
lineSegCol.SegmentsChanged();
newSegCol.SegmentsChanged();
IPolyline oldLine = lineSegCol as IPolyline;
newLine = newSegCol as IPolyline;
Lines[0] = oldLine;
Lines[1] = newLine;
return Lines;
}
///< summary>
/// 在SplitePoint处打断线要素的图形,并去除新的线
///< /summary>
///< param name="LineCurve">传入的线</param>
///< param name="SplitePoint">线上的一点</param>
///< returns></returns>
/// 创建人:懒羊羊
private IPolyline[] SpliteLineAtPoint(IPolyline LineCurve, IPoint SplitePoint)
{
IPolyline[] Lines = new IPolyline[2];
bool isSplit;
int splitIndex, segIndex;
LineCurve.SplitAtPoint(SplitePoint, true, false, out isSplit, out splitIndex, out segIndex);
if (isSplit)
{
IPolyline newLine = new PolylineClass();
ISegmentCollection lineSegCol = (ISegmentCollection)LineCurve;
ISegmentCollection newSegCol = (ISegmentCollection)newLine;
object o = Type.Missing;
for (int j = segIndex; j < lineSegCol.SegmentCount; j++)
{
newSegCol.AddSegment(lineSegCol.get_Segment(j), ref o, ref o);
}
lineSegCol.RemoveSegments(segIndex, lineSegCol.SegmentCount - segIndex, true);
lineSegCol.SegmentsChanged();
newSegCol.SegmentsChanged();
IPolyline oldLine = lineSegCol as IPolyline;
newLine = newSegCol as IPolyline;
Lines[0] = newLine;
Lines[1] = oldLine;
}
return Lines;
}
///< summary>
/// 用一个面图形把一个面要素切割为两部分
///< /summary>
///< param name="polyClass">面要素集</param>
///< param name="polyFeat">面要素</param>
///< param name="CutPolygon">用于切割的图形</param>
/// 创建人:懒羊羊
private void BreakPolygonToTwoPart(IFeatureClass polyClass, IFeature polyFeat, IPolygon CutPolygon)
{
IGeometry featGeo = polyFeat.Shape;
ITopologicalOperator topo = featGeo as ITopologicalOperator;
IGeometry pOutGeometry = topo.Difference(CutPolygon); //去除相交的部分
if (pOutGeometry is IGeometryCollection)
{
IGeometryCollection pGeometryCollection = pOutGeometry as IGeometryCollection;
for (int i = 0; i < pGeometryCollection.GeometryCount; i++)
{
IGeometry pGeometry = pGeometryCollection.get_Geometry(i);
if (pGeometry != null && !pGeometry.IsEmpty&& pGeometry.GeometryType == esriGeometryType.esriGeometryRing)
{
IClone pClone = pGeometry as IClone;
IPolygon pPolygon = ConvertGeometryToPolygon(pClone.Clone() as IGeometry);
if (pPolygon != null && !pPolygon.IsEmpty)
{
IFeature pNewFeature = polyClass.CreateFeature();
pNewFeature.Shape = pPolygon;
pNewFeature.Store();
}
}
}
polyFeat.Delete();
}
}
private IPolygon ConvertGeometryToPolygon(IGeometry pGeometry)
{
IPointCollection pPointCollection = pGeometry as IPointCollection;
IPointCollection pNewPointCollection = new PolygonClass();
pNewPointCollection.AddPointCollection(pPointCollection);
IPolygon pPolygon = pNewPointCollection as IPolygon;
return pPolygon;
}
2.补上一些常用的图形操作函数
/// <summary>
/// 返回点到图形之间的垂直距离
///< /summary>
///< param name="pPoint"></param>
///< param name="pGeo"></param>
///< returns></returns>
/// 创建人:懒羊羊
public static double getPointToGeoDis(IPoint pPoint, IGeometry pGeo)
{
IProximityOperator Pro = pPoint as IProximityOperator;
double dis = Pro.ReturnDistance(pGeo);
return dis;
}
///< summary>
/// 求两点间距离
///< /summary>
///< param name="p1"></param>
///< param name="p2"></param>
///< returns></returns>
/// 创建人:懒羊羊
private double getDisByTwoPoint(IPoint p1, IPoint p2)
{
double step1 = (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y);
double step2 = Math.Sqrt(step1);
return step2;
}
///< summary>
/// 利用两点生成一条PolyLine
///< /summary>
///< param name="p1"></param>
///< param name="p2"></param>
///< returns></returns>
/// 创建人:懒羊羊
private IPolyline CreatePolyLineByTwoPoint(IPoint p1, IPoint p2)
{
INewLineFeedback m_LineFeed = new NewLineFeedback();
m_LineFeed.Start(p1);
m_LineFeed.AddPoint(p2);
IPolyline m_PolyLine = m_LineFeed.Stop();
return m_PolyLine;
}
///< summary>
/// 粗略判断一个已知点是否在线上
///< /summary>
///< param name="pPoint">已知点</param>
///< param name="myLine"></param>
///< returns></returns>
/// 创建人:懒羊羊
private bool isPointOnLine(IPoint pPoint, IPolyline myLine)
{
ITopologicalOperator topo = pPoint as ITopologicalOperator;
IGeometry buffer = topo.Buffer(0.00001); //缓冲一个极小的距离
topo = buffer as ITopologicalOperator;
IGeometryCollection pgeo = topo.Intersect(myLine, esriGeometryDimension.esriGeometry0Dimension) as IGeometryCollection;
bool result = false;
if (pgeo.GeometryCount > 0)
result = true;
return result;
}
/// <summary>
/// 利用三点求角度(余弦定理)
///< /summary>
///< param name="p1"></param>
///< param name="p2"></param>
///< param name="p3"></param>
///< returns>返回弧度值</returns>
/// 创建人:懒羊羊
private double getAngleByThreePoint(IPoint p1, IPoint p2, IPoint p3)
{
//求出三条边
double a = getDisByTwoPoint(p1, p2);
double b = getDisByTwoPoint(p1, p3);
double c = getDisByTwoPoint(p2, p3);
double cosB = (a * a + c * c - b * b) / (2 * a * c);
double angle = Math.Acos(cosB);
return angle;
}