二三维联动之MapControl与SceneControl的联动

针对ArcEngine项目需要,开始由JAVA转向C#开发,最近狂乱的google加baidu,再加上esrichina做升级,搜索出来的内容全部无法进入他的论坛查看,还好有baidu快照,帮了我很大的忙,再说esrichina论坛,提问比回答的人多。就一个二三维联动,不知道有多少贴子,VB的,C++的,C#的很少。没有一个好的贴子能直接解决我的问题,郁闷之极,最主要是那个论坛分权限,刚注册的用户想看牛人的博客还进不去,我是ArcEngine+C#的新手,我的项目很急,但是面对这样的论坛我很悲剧。决定将我花按天去google和baidu所找出的答案在这里记下来,同时也供和我一样的新手学习学习。

 

多的话说了没用,看代码如下:

 

1.导入shp文件到MapControl,按个button 同步到SceneControl中

 

C#代码 复制代码 收藏代码
  1. //加载shp文件到MapControl中 
  2. privatevoid btnItem9_1_SHP_Click(object sender, EventArgs e) 
  3.     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
  4.     openFileDialog1.CheckFileExists = true
  5.     openFileDialog1.CheckFileExists = true
  6.     openFileDialog1.Multiselect = true
  7.     openFileDialog1.Filter = "shape files(*.shp)|*.shp"
  8.     openFileDialog1.Title = "请选择shp格式文件"
  9.  
  10.     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
  11.     { 
  12.         foreach (string fname in openFileDialog1.FileNames) 
  13.         { 
  14.             if (fname != null
  15.             { 
  16.                 string pathToWorkspace = System.IO.Path.GetDirectoryName(fname); 
  17.                 string shapefileName = System.IO.Path.GetFileNameWithoutExtension(fname); 
  18.  
  19.                 axMapControl1.AddShapeFile(pathToWorkspace, shapefileName); 
  20.             } 
  21.         } 
  22.     } 
  23.  
  24.     IActiveView pActiveView = (IActiveView)axMapControl1.Map; 
  25.     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,接下来是同步的方法:

 

C#代码 复制代码 收藏代码
  1. //重新同步MapControl中的所有图层到SceneControl 
  2. privatevoid synLayersToSceneControl() 
  3.      if (axMapControl1.ActiveView != null
  4.      { 
  5.          if (axMapControl1.Map.LayerCount > 0) 
  6.          { 
  7.              axSceneControl1.Scene.ClearLayers(); 
  8.              UID uid = new UIDClass(); 
  9.              uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}";   //所有图层 
  10.              axSceneControl1.Scene.AddLayers(axMapControl1.Map.get_Layers(uid, true), true); 
  11.          } 
  12.      } 
  13.      axSceneControl1.SceneGraph.RefreshViewers(); 
  14.  
       //重新同步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,让二维和三维一起放大,即同步放大

 

 

C#代码 复制代码 收藏代码
  1. //同步放大 
  2. privatevoid btnItem9_5_ZoomInFixed_Click(object sender, EventArgs e) 
  3.     //得到接口 
  4.     IActiveView pActiveView = this.axMapControl1.Map as IActiveView; 
  5.  
  6.     //获得显示范围 
  7.     IEnvelope pEnvelope = (IEnvelope)pActiveView.Extent; 
  8.  
  9.     //刷新 
  10.     pEnvelope.Expand(0.9, 0.9, true); 
  11.     pActiveView.Extent = pEnvelope; 
  12.     pActiveView.Refresh(); 
  13.  
  14.     //三维 
  15.     //获得现场的相机 
  16.     ICamera pCamera = this.axSceneControl1.Camera as ICamera; 
  17.  
  18.     //拓宽视野 
  19.     double dAngle; 
  20.     dAngle = pCamera.ViewFieldAngle; 
  21.     pCamera.ViewFieldAngle = dAngle * 0.9; 
  22.  
  23.     //重绘现场 
  24.     ISceneViewer pSceneViewer = this.axSceneControl1.SceneGraph.ActiveViewer as ISceneViewer; 
  25.     pSceneViewer.Redraw(false); 
posted @ 2013-04-06 22:01  GIS_Darren  阅读(561)  评论(0编辑  收藏  举报