通过点击事件,把光标所在点增加到点集,用点集构建面要素。为了避免数据冗余,从第四个点开始,要删除前面几个点组成的面要素。
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
PointsAdd.AddPoint(pPoint);
if(PointsAdd.PointCount>2)
{
Ring ring = new RingClass();
for (int i2 = 0; i2 < PointsAdd.PointCount; i2++)
{
ring.AddPoint(PointsAdd.Point[i2], Type.Missing, Type.Missing);
}
ring.AddPoint(PointsAdd.Point[0], Type.Missing, Type.Missing);//必须补充起点,作为终点
IGeometryCollection pointPolygon = new PolygonClass();
pointPolygon.AddGeometry(ring as IGeometry, Type.Missing, Type.Missing);
polygonGeo = pointPolygon as IPolygon;
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
ILayer ptempLayer = axMapControl1.get_Layer(i);
if (ptempLayer.Name == "样本矢量")
pFeatureLayer = ptempLayer as IFeatureLayer;
}
if (PointsAdd.PointCount == 3)
{
pFeature1 = pFeatureLayer.FeatureClass.CreateFeature();
pFeature1.Shape = polygonGeo;
pFeature1.Store();
}
else
{
pFeature1.Delete();
pFeature1 = pFeatureLayer.FeatureClass.CreateFeature();
pFeature1.Shape = polygonGeo;
pFeature1.Store();
}
axMapControl1.Refresh();