ArcGIS Engine - 导入mxd模板

导入模板有两种思路:
1.直接将当前PageLayout替换为模板的PageLayout,再同步一下Map。
2.获取模板PageLayout的所有要素,更新到当前PageLayout。

		/// <summary>
        /// 直接替换模板
        /// </summary>
		public void ImportTemplate(AxMapControl MapControl, AxPageLayoutControl PageLayoutControl)
        {
            //打开工程对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开模板";
            openFileDialog.Filter = "模板工程|*.mxd";
            openFileDialog.Multiselect = false;
            openFileDialog.InitialDirectory = Configuration.Environment.WorkPath;
            string filePath = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
            }

            IMapDocument mapDocument = new MapDocument();
            if (!mapDocument.get_IsPresent(filePath) || !mapDocument.get_IsMapDocument(filePath)) return new Response(Constant.Failure, "工程文件无效!");
            if (mapDocument.get_IsPasswordProtected(filePath)) return new Response(Constant.Failure, "工程文件已加密无法打开!");

            //打开工程文件
            mapDocument.Open(filePath, string.Empty);

            //获取模板要素
            IGraphicsContainer GraphicsContainer = mapDocument.PageLayout as IGraphicsContainer;
            IMapFrame mMapFrame = GraphicsContainer.FindFrame(mapDocument.ActiveView.FocusMap) as IMapFrame;
            mMapFrame.Map.ClearLayers();
            
            //替换地图
            try
            {
                mMapFrame.Map = MapControl.Map;
                PageLayoutControl.PageLayout = mapDocument.PageLayout;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("加载模板失败!" + ex.Message);
            }

            PageLayoutControl.ActiveView.Refresh();
            mapDocument.Close();
        }
        /// <summary>
        /// 导入模板
        /// </summary>
        public void ImportTemplate(AxPageLayoutControl PageLayoutControl)
        {
            //打开工程对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开模板";
            openFileDialog.Filter = "模板工程|*.mxd";
            openFileDialog.Multiselect = false;
            string filePath = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
            }

            IMapDocument mapDocument = new MapDocument();
            if (!mapDocument.get_IsPresent(filePath) || !mapDocument.get_IsMapDocument(filePath)) return new Response(Constant.Failure, "工程文件无效!");
            if (mapDocument.get_IsPasswordProtected(filePath)) return new Response(Constant.Failure, "工程文件已加密无法打开!");

            //打开工程文件
            mapDocument.Open(filePath, string.Empty);

            //获取模板要素
            IGraphicsContainer GraphicsContainer = mapDocument.PageLayout as IGraphicsContainer;
            IMapFrame mMapFrame = GraphicsContainer.FindFrame(mapDocument.ActiveView.FocusMap) as IMapFrame;
            mMapFrame.Map.ClearLayers();
            
            //获取当前布局要素
            IGraphicsContainer pGraphicsContainer = PageLayoutControl.PageLayout as IGraphicsContainer;
            IMapFrame pMapFrame = pGraphicsContainer.FindFrame(PageLayoutControl.ActiveView.FocusMap) as IMapFrame;
            IPageLayout pPageLayout = PageLayoutControl.PageLayout;

            //替换单位
            IPage mPage = mapDocument.PageLayout.Page;
            pPageLayout.Page.Units = mPage.Units;
            pPageLayout.Page.Orientation = mPage.Orientation; 

            //替换页面尺寸
            double dWith , dHeight;
            mPage.QuerySize(out dWith, out dHeight);
            pPageLayout.Page.PutCustomSize(dWith, dHeight);

            //替换数据框尺寸
            IEnvelope env = new EnvelopeClass();
            IElement mIElement = mMapFrame as IElement;
            mIElement.QueryBounds(PageLayoutControl.ActiveView.ScreenDisplay as IDisplay, env);
            IElement pIElement = pMapFrame as IElement;
            pIElement.Geometry = env;
            PageLayoutControl.ActiveView.GraphicsContainer.AddElement(pIElement, 0);

            //读取布局所有要素
            GraphicsContainer.Reset();
            IElement pElement = GraphicsContainer.Next();
            while (pElement != null)
            {
                PageLayoutControl.ActiveView.GraphicsContainer.AddElement(pElement, 0);
                pElement = GraphicsContainer.Next();
            }
			PageLayoutControl.ActiveView.Refresh();
            mapDocument.Close();
            
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王八八。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值