private void buttonAddOneArea_Click(object sender, EventArgs e)
{
// 定义4个点
IPoint pPt1 = new PointClass();
pPt1.SpatialReference = axMapControl1.SpatialReference;// 设置点的参考系
pPt1.PutCoords(0, 0);//设置点的坐标
IPoint pPt2 = new PointClass();
pPt2.SpatialReference = axMapControl1.SpatialReference;
pPt2.PutCoords(50, 0);
IPoint pPt3 = new PointClass();
pPt3.PutCoords(50, 50);
pPt3.SpatialReference = axMapControl1.SpatialReference;
IPoint pPt4 = new PointClass();
pPt4.SpatialReference = axMapControl1.SpatialReference;
pPt4.PutCoords(0, 50);
// 由点 构造 连续坐标集
ISegmentCollection pSegColl = new RingClass();
object Missing1 = Type.Missing;
object Missing2 = Type.Missing;
ILine pLine;
pLine = new LineClass();
pLine.PutCoords(pPt1, pPt2); //点pPt1、pPt2……pPt4等的位置不一样,出来的图形效果是不一样的
pSegColl.AddSegment(pLine as ISegment, ref Missing1, ref Missing2);
pLine = new LineClass();
pLine.PutCoords(pPt2, pPt3);
pSegColl.AddSegment(pLine as ISegment, ref Missing1, ref Missing2);
pLine = new LineClass();
pLine.PutCoords(pPt3, pPt4);
pSegColl.AddSegment(pLine as ISegment, ref Missing1, ref Missing2);
// 转化为 闭合线
IRing pRing;
pRing = pSegColl as IRing;
pRing.Close();
// 由闭合线 构造 多边形
IGeometryCollection pPolygon = new PolygonClass();
pPolygon.AddGeometry(pRing, ref Missing1, ref Missing2);
ISimpleFillSymbol pSimpleFillSym = new SimpleFillSymbolClass(); //填充样式以及颜色
pSimpleFillSym.Style = esriSimpleFillStyle.esriSFSSolid;
pSimpleFillSym.Color = GetRGB(250, 50, 100);
IFillShapeElement pPolygonEle = new PolygonElementClass();
pPolygonEle.Symbol = pSimpleFillSym;
IElement pEle = pPolygonEle as IElement;
pEle.Geometry = (IGeometry)pPolygon;
//获取图层
ILayer pLayer = GetLayerByName("MapsSheet");
// 多边形添加进图层
IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer; //将ILayer转换为IFeaturelayer,为了对图层上的要素进行编辑
IFeatureClass pFeatCls = pFeatureLyr.FeatureClass; //定义一个要素集合,并获取图层的要素集合
IFeatureClassWrite fr = (IFeatureClassWrite)pFeatCls; //定义一个实现新增要素的接口实例,并该实例作用于当前图层的要素集
IFeature f = pFeatCls.CreateFeature(); //实例化IFeature对象, 这样IFeature对象就具有当前图层上要素的字段信息
f.Shape = (IGeometry)pPolygon; //设置IFeature对象的形状属性
//f.set_Value(2, "house1"); //设置IFeature对象的索引是3的字段值
f.Store(); //保存IFeature对象
IWorkspaceEdit w =
(pFeatCls as IDataset).Workspace as IWorkspaceEdit; //定义一个工作编辑工作空间,用于开启前图层的编辑状态
//定义一个IFeature实例,用于添加到当前图层上
w.StartEditing(true); //开启编辑状态
w.StartEditOperation(); //开启编辑操作
fr.WriteFeature(f);//将IFeature对象,添加到当前图层上
w.StopEditOperation();//停止编辑操作
w.StopEditing(true);//关闭编辑状态,并保存修改
axMapControl1.ActiveView.Refresh();
}