用ArcEngine的工具条添加图层要素
发现AE比起其它的组件GIS来要好用的多
但也有一些bt的地方
比如说AE没有提供图层编辑的工具条
但最近因为程序里要添加图元
必须得开发图层编辑的功能
于是去找了找资料
发现自带的帮助里有一个MapEditing的示例
但是从AO的示例改造而来
需要从按钮写起
想想如果这样搞的话
倒不如直接用AO算了
一直想省点事
在AE自带的工具条上做点文章
其实AE中也有一组相似的编辑按钮,是用于编辑Graphics的
自然就有了一个想法:能不能先生成一个Graphic,然后将其加载进来
琢磨了几日,终于实现了
现在把代码拿出来和大家分享
private void Form1_Load(object sender, System.EventArgs e)
{
//清空图层
this.axMapControl1.Map.ClearLayers();
//加载服务器SDE地图信息
ESRI.ArcGIS.esriSystem.IPropertySet Proset = new ESRI.ArcGIS.esriSystem.PropertySetClass();//属性集
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory Fact;//工作空间仓库
ESRI.ArcGIS.Geodatabase.IWorkspace Workspace;//工作空间
Proset.SetProperty("Server","服务器名");//服务器名
Proset.SetProperty("Instance","端口");//实例化端口
Proset.SetProperty("user","用户名");//SDE用户名
Proset.SetProperty("password","密码");//密码
Proset.SetProperty("version","sde.DEFAULT");//连接版本
Fact = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();//实例化为SDE工作空间仓库
Workspace = Fact.Open(Proset,Int32.Parse(this.Handle.ToString()));//填入属性集
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace FeatureWorkspace;
FeatureWorkspace = Workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;//传递给Feature工作空间
ESRI.ArcGIS.Geodatabase.IFeatureClass FeatureClass;
FeatureClass = FeatureWorkspace.OpenFeatureClass("SDE.BBBB");//填充所需Feature集合
ESRI.ArcGIS.Carto.FeatureLayer FLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
FLayer.FeatureClass = FeatureClass;//将Feature加载到图层
axMapControl1.Map.AddLayer(FLayer);//加载图层
axMapControl1.Update();//更新空白区域
axMapControl1.Refresh();//刷新地图视窗
}
private void Form1_Closed(object sender, System.EventArgs e)
{
ESRI.ArcGIS.esriSystem.IAoInitialize aoi = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
aoi.Shutdown();
}
private void button1_Click(object sender, System.EventArgs e)
{
IGraphicsContainerSelect GraphicsContainerSelect = (IGraphicsContainerSelect)axMapControl1.Map;
if(GraphicsContainerSelect.ElementSelectionCount ==0)//判断是否选中图斑
{
MessageBox.Show("请选中欲导入图斑!");
return;
}
IWorkspaceEdit WorkspaceEdit;
IFeatureLayer FeatureLayer;
IFeatureClass FeatureClass;
IFeature Feature;
IDataset Dataset;
FeatureLayer = (IFeatureLayer)this.axMapControl1.get_Layer(0);//选中导入图层
FeatureClass = FeatureLayer.FeatureClass;//该图层的feature集
Dataset = (IDataset)FeatureClass;//该图层的数据集
WorkspaceEdit = (IWorkspaceEdit)Dataset.Workspace;//关联到编辑空间
WorkspaceEdit.StartEditing(true);//使之可编辑
WorkspaceEdit.StartEditOperation();//开始编辑
Feature = FeatureClass.CreateFeature();//创建空feature
Feature.Shape = GraphicsContainerSelect.SelectedElement(0).Geometry;//仅导入最新选中图斑,即多选时只有一个可以导入
Feature.Store();
WorkspaceEdit.StopEditOperation();
WorkspaceEdit.StopEditing(true);//结束编辑并保存
IGraphicsContainer GraphicsContainer = (IGraphicsContainer)GraphicsContainerSelect;
GraphicsContainer.DeleteAllElements();//删除graphic图层
axMapControl1.ActiveView.Refresh();//刷新当前视图
MessageBox.Show("ok");
}
但也有一些bt的地方
比如说AE没有提供图层编辑的工具条
但最近因为程序里要添加图元
必须得开发图层编辑的功能
于是去找了找资料
发现自带的帮助里有一个MapEditing的示例
但是从AO的示例改造而来
需要从按钮写起
想想如果这样搞的话
倒不如直接用AO算了
一直想省点事
在AE自带的工具条上做点文章
其实AE中也有一组相似的编辑按钮,是用于编辑Graphics的
自然就有了一个想法:能不能先生成一个Graphic,然后将其加载进来
琢磨了几日,终于实现了
现在把代码拿出来和大家分享
private void Form1_Load(object sender, System.EventArgs e)
{
//清空图层
this.axMapControl1.Map.ClearLayers();
//加载服务器SDE地图信息
ESRI.ArcGIS.esriSystem.IPropertySet Proset = new ESRI.ArcGIS.esriSystem.PropertySetClass();//属性集
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory Fact;//工作空间仓库
ESRI.ArcGIS.Geodatabase.IWorkspace Workspace;//工作空间
Proset.SetProperty("Server","服务器名");//服务器名
Proset.SetProperty("Instance","端口");//实例化端口
Proset.SetProperty("user","用户名");//SDE用户名
Proset.SetProperty("password","密码");//密码
Proset.SetProperty("version","sde.DEFAULT");//连接版本
Fact = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();//实例化为SDE工作空间仓库
Workspace = Fact.Open(Proset,Int32.Parse(this.Handle.ToString()));//填入属性集
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace FeatureWorkspace;
FeatureWorkspace = Workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;//传递给Feature工作空间
ESRI.ArcGIS.Geodatabase.IFeatureClass FeatureClass;
FeatureClass = FeatureWorkspace.OpenFeatureClass("SDE.BBBB");//填充所需Feature集合
ESRI.ArcGIS.Carto.FeatureLayer FLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
FLayer.FeatureClass = FeatureClass;//将Feature加载到图层
axMapControl1.Map.AddLayer(FLayer);//加载图层
axMapControl1.Update();//更新空白区域
axMapControl1.Refresh();//刷新地图视窗
}
private void Form1_Closed(object sender, System.EventArgs e)
{
ESRI.ArcGIS.esriSystem.IAoInitialize aoi = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
aoi.Shutdown();
}
private void button1_Click(object sender, System.EventArgs e)
{
IGraphicsContainerSelect GraphicsContainerSelect = (IGraphicsContainerSelect)axMapControl1.Map;
if(GraphicsContainerSelect.ElementSelectionCount ==0)//判断是否选中图斑
{
MessageBox.Show("请选中欲导入图斑!");
return;
}
IWorkspaceEdit WorkspaceEdit;
IFeatureLayer FeatureLayer;
IFeatureClass FeatureClass;
IFeature Feature;
IDataset Dataset;
FeatureLayer = (IFeatureLayer)this.axMapControl1.get_Layer(0);//选中导入图层
FeatureClass = FeatureLayer.FeatureClass;//该图层的feature集
Dataset = (IDataset)FeatureClass;//该图层的数据集
WorkspaceEdit = (IWorkspaceEdit)Dataset.Workspace;//关联到编辑空间
WorkspaceEdit.StartEditing(true);//使之可编辑
WorkspaceEdit.StartEditOperation();//开始编辑
Feature = FeatureClass.CreateFeature();//创建空feature
Feature.Shape = GraphicsContainerSelect.SelectedElement(0).Geometry;//仅导入最新选中图斑,即多选时只有一个可以导入
Feature.Store();
WorkspaceEdit.StopEditOperation();
WorkspaceEdit.StopEditing(true);//结束编辑并保存
IGraphicsContainer GraphicsContainer = (IGraphicsContainer)GraphicsContainerSelect;
GraphicsContainer.DeleteAllElements();//删除graphic图层
axMapControl1.ActiveView.Refresh();//刷新当前视图
MessageBox.Show("ok");
}
e-mail:shisong.zhu@gmail.com
GISer in China, for engineering