1、获取指定类型的图层
在创建要素之前,要获取相应的图层。
以下函数根据指定要素类型获取图层:
private IFeatureLayer getLayerByShape(esriGeometryType type)
{
int lyerNum = axMapControl1.LayerCount;
IFeatureLayer resLayer = null;
for (int i = 0; i < lyerNum; i++)
{
ILayer pLayer = axMapControl1.get_Layer(i);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer == null)
{
continue;
}
else
{
esriGeometryType thistype = pFeatureLayer.FeatureClass.GetFeature(1).Shape.GeometryType;
if (thistype == type)
{
resLayer = pFeatureLayer;
}
}
}
return resLayer;
}
2、创建点要素
添加点的函数
private void addPoint(IPoint pPoint, IFeatureLayer pFeaLyr)
{
IFeatureClass pFeaClass = pFeaLyr.FeatureClass;
IFeature pFeature = pFeaClass.CreateFeature();
pFeature.Shape = pPoint;
pFeature.Store();
}
添加axMapControl的点击事件
IPoint pPoint = new PointClass();
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
IFeatureLayer pFealyr = getLayerByShape(esriGeometryType.esriGeometryPoint);
if (pFealyr != null)
{
addPoint(pPoint, pFealyr);
}
axMapControl1.ActiveView.Refresh();
3、创建线要素
添加线的函数
private void addLine(IPolyline pLine, IFeatureLayer pFeatureLayer)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pLine;
pFeature.Store();
}
添加axMapControl的点击事件
IPolyline polyline = axMapControl1.TrackLine() as IPolyline;
IFeatureLayer pFeatureLayer = getLayerByShape(esriGeometryType.esriGeometryPolyline);
if (pFeatureLayer != null)
{
addLine(polyline, pFeatureLayer);
}
axMapControl1.ActiveView.Refresh();
4、创建面要素
添加面的函数
private void addPolygon(IPolygon pPolygon, IFeatureLayer pFeatureLayer)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pPolygon;
pFeature.Store();
}
添加axMapControl的点击事件
IPolygon pPolygon = axMapControl1.TrackPolygon() as IPolygon;
IFeatureLayer pFeatureLayer2 = getLayerByShape(esriGeometryType.esriGeometryPolygon);
if (pFeatureLayer2 != null) {
addPolygon(pPolygon, pFeatureLayer2);
}
axMapControl1.ActiveView.Refresh();
PS:网络对于创建线要素的影响
似乎由于效应网络的影响,有些线要素图层上,不能使用要素类的 Create方法直接创建,笔者直接使用 Create方法时,报错说:
不能在编辑会话之外更新此类中的对象
自己创建一个线要素图层,之后添加线要素成功。