Duplicate existing layout - ArcGIS Pro SDK

681
2
Jump to solution
09-21-2018 05:00 AM
Highlighted
New Contributor

Hi,

Is there any method included in the sdk to duplicate a layout including all its elements? I can't find it. I've also tried to go through all the elements of the source layout to insert them into a new one but I don't see it very effective because you would have to use the LayoutElementFactory.Instance.Create method for each of them, and each method has different parametrers.

Thansk in advance.

Borja

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Contributor

This is how it works:

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;
    }


  }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
Highlighted
Esri Contributor

This is how it works:

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;
    }


  }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

Highlighted
New Contributor

Hi Charles,

Sorry for the delay. It works perfect!

Thanks

Reply
0 Kudos