导入模板有两种思路:
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();
}