point\polyline\polygon的转化(转)
首先你要明白Polyline是由path对象构成,Polygon是由ring对象构成,因此实现polyline向polygon的转换,思路如下:
1.提取polyline中的所有path对象
2.将path对象转换成ring对象,由于IRing继承自IPath,因此该转换是合理的
3.利用IGeometryCollection.AddGeometry添加ring对象,构成polygon
示例如下:
- //cast the polyline object to the polygon
- ISegmentCollection pRing;
- IGeometryCollection pPolygon = new PolygonClass();
- object o = Type.Missing;
- for (int i = 0; i < pPolyline.GeometryCount;i++ )
- {
- pRing = new RingClass();
- pRing.AddSegmentCollection(pPolyline.get_Geometry(i) as ISegmentCollection);
- pPolygon.AddGeometry(pRing as IGeometry, ref o, ref o);
- }
point转为polyline,思路如下:
polyline是由path构成,path是由segment构成,因此首先构建segment对象,然后利用ISegmentCollection.AddSegment构造path,最后利用IGeometryCollection.AddGeometry构造polyline
示例如下:
- IPoint point1 = new PointClass();
- point1.PutCoords(100, 200);
- IPoint point2 = new PointClass();
- point2.PutCoords(600, 700);
- IPoint point3 = new PointClass();
- point3.PutCoords(450, 800);
- [color=Blue]//new a line object[/color]
- ILine pLine = new LineClass();
- object o = Type.Missing;
- pLine.PutCoords(point1, point2);
- [color=Blue]//new a path[/color]
- ISegmentCollection pPath = new PathClass();
- pPath.AddSegment((ISegment)pLine, ref o, ref o);
- pLine = new LineClass();
- pLine.PutCoords(point2, point3);
- pPath.AddSegment((ISegment)pLine, ref o, ref o);
- [color=Blue]//new a polyline[/color]
- IGeometryCollection pPolyline = new PolylineClass();
- pPolyline.AddGeometry((IGeometry)pPath, ref o, ref o);