• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

ArcGIS Pro layout clone

1. Make a new layout

2. Copy the layout to be "duplicated" (this is the Clone method)

3. Replace the layout URI of the copy with the URI of the new layout (in other words, the new layout acts as a placeholder)

4. Set the copy/clone back as the new layout definition

 

Step 3 is the crucial step. I have posted the code below. I'd recommend running it in the debugger so you can grok what's going on

 

internal class DuplicateLayout : Button
  {
    protected async override void OnClick() {
      var layoutView = LayoutView.Active;
      try {
        if (layoutView?.Layout != null) {
          await layoutView.Layout.Duplicate();
        }
      }
      catch (Exception ex) {
        System.Diagnostics.Debug.WriteLine(ex);
      }
      
    }
  }
 

public static class LayoutExtensions
  {

    public static async Task Duplicate(this Layout layout) {
      if (layout == null)
        throw new ArgumentNullException(nameof(layout), "layout cannot be null");
      var layout_clone = await layout.CloneAsync();
      FrameworkApplication.Panes.CreateLayoutPaneAsync(layout_clone);
    }

    public static Task<Layout> CloneAsync(this Layout layout) {
      return QueuedTask.Run(() => {
        return Clone(layout);
      });
    }

    //Must be called on the QueuedTask
    public static Layout Clone(this Layout layout)
    {
      var layout_dup = LayoutFactory.Instance.CreateLayout();
      var metadata_uri = layout_dup.GetDefinition().MetadataURI;

      var layout_def = layout.GetDefinition();
      var layout_def_clone = layout_def.Clone() as CIMLayout;
      layout_def_clone.URI = layout_dup.URI;
      layout_def_clone.MetadataURI = metadata_uri;

      layout_dup.SetDefinition(layout_def_clone);
      return layout_dup;
    }
  }
 

public static class CIMExtensions
  {

    public static CIMObject Clone(this CIMObject cimObject)
    {
      var clone = System.Activator.CreateInstance("ArcGIS.Core", cimObject.GetType().ToString()).Unwrap() as CIMObject;
      var stringReader = new StringReader(cimObject.ToXml());
      var xmlReader = new XmlTextReader(stringReader);
      //xmlReader.MoveToContent();
      clone.ReadXml(xmlReader);
      xmlReader.Dispose();
      return clone;
    }


  }
来自:https://community.esri.com/thread/221610-duplicate-existing-layout-arcgis-pro-sdk

 

posted on 2020-04-02 19:42  gisai  阅读(286)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3