二三维联动之MapControl与SceneControl的联动
针对ArcEngine项目需要,开始由JAVA转向C#开发,最近狂乱的google加baidu,再加上esrichina做升级,搜索出来的内容全部无法进入他的论坛查看,还好有baidu快照,帮了我很大的忙,再说esrichina论坛,提问比回答的人多。就一个二三维联动,不知道有多少贴子,VB的,C++的,C#的很少。没有一个好的贴子能直接解决我的问题,郁闷之极,最主要是那个论坛分权限,刚注册的用户想看牛人的博客还进不去,我是ArcEngine+C#的新手,我的项目很急,但是面对这样的论坛我很悲剧。决定将我花按天去google和baidu所找出的答案在这里记下来,同时也供和我一样的新手学习学习。
多的话说了没用,看代码如下:
1.导入shp文件到MapControl,按个button 同步到SceneControl中
- //加载shp文件到MapControl中
- privatevoid btnItem9_1_SHP_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- openFileDialog1.CheckFileExists = true;
- openFileDialog1.CheckFileExists = true;
- openFileDialog1.Multiselect = true;
- openFileDialog1.Filter = "shape files(*.shp)|*.shp";
- openFileDialog1.Title = "请选择shp格式文件";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- foreach (string fname in openFileDialog1.FileNames)
- {
- if (fname != null)
- {
- string pathToWorkspace = System.IO.Path.GetDirectoryName(fname);
- string shapefileName = System.IO.Path.GetFileNameWithoutExtension(fname);
- axMapControl1.AddShapeFile(pathToWorkspace, shapefileName);
- }
- }
- }
- IActiveView pActiveView = (IActiveView)axMapControl1.Map;
- pActiveView.Refresh();
- }
//加载shp文件到MapControl中 private void btnItem9_1_SHP_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.CheckFileExists = true; openFileDialog1.CheckFileExists = true; openFileDialog1.Multiselect = true; openFileDialog1.Filter = "shape files(*.shp)|*.shp"; openFileDialog1.Title = "请选择shp格式文件"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { foreach (string fname in openFileDialog1.FileNames) { if (fname != null) { string pathToWorkspace = System.IO.Path.GetDirectoryName(fname); string shapefileName = System.IO.Path.GetFileNameWithoutExtension(fname); axMapControl1.AddShapeFile(pathToWorkspace, shapefileName); } } } IActiveView pActiveView = (IActiveView)axMapControl1.Map; pActiveView.Refresh(); }
OK,接下来是同步的方法:
- //重新同步MapControl中的所有图层到SceneControl
- privatevoid synLayersToSceneControl()
- {
- if (axMapControl1.ActiveView != null)
- {
- if (axMapControl1.Map.LayerCount > 0)
- {
- axSceneControl1.Scene.ClearLayers();
- UID uid = new UIDClass();
- uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}"; //所有图层
- axSceneControl1.Scene.AddLayers(axMapControl1.Map.get_Layers(uid, true), true);
- }
- }
- axSceneControl1.SceneGraph.RefreshViewers();
- }
//重新同步MapControl中的所有图层到SceneControl private void synLayersToSceneControl() { if (axMapControl1.ActiveView != null) { if (axMapControl1.Map.LayerCount > 0) { axSceneControl1.Scene.ClearLayers(); UID uid = new UIDClass(); uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}"; //所有图层 axSceneControl1.Scene.AddLayers(axMapControl1.Map.get_Layers(uid, true), true); } } axSceneControl1.SceneGraph.RefreshViewers(); }
2.点击一个button,让二维和三维一起放大,即同步放大
- //同步放大
- privatevoid btnItem9_5_ZoomInFixed_Click(object sender, EventArgs e)
- {
- //得到接口
- IActiveView pActiveView = this.axMapControl1.Map as IActiveView;
- //获得显示范围
- IEnvelope pEnvelope = (IEnvelope)pActiveView.Extent;
- //刷新
- pEnvelope.Expand(0.9, 0.9, true);
- pActiveView.Extent = pEnvelope;
- pActiveView.Refresh();
- //三维
- //获得现场的相机
- ICamera pCamera = this.axSceneControl1.Camera as ICamera;
- //拓宽视野
- double dAngle;
- dAngle = pCamera.ViewFieldAngle;
- pCamera.ViewFieldAngle = dAngle * 0.9;
- //重绘现场
- ISceneViewer pSceneViewer = this.axSceneControl1.SceneGraph.ActiveViewer as ISceneViewer;
- pSceneViewer.Redraw(false);
- }