Skip to content

ProSnippets Layouts

arcgisprosdk edited this page Mar 27, 2017 · 21 revisions
Language:              C#  
Subject:               Layouts  
Contributor:           ArcGIS Pro SDK Team <arcgisprosdk@esri.com>  
Organization:          Esri, http://www.esri.com  
Date:                  1/5/2017  
ArcGIS Pro:            1.4  
Visual Studio:         2013, 2015  
.NET Target Framework: 4.6.1  

Retrieve layout project items

// get all the layouts
IEnumerable<LayoutProjectItem> layouts = Project.Current.GetItems<LayoutProjectItem>();


// or get a specific layout (by name)
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  // Delete the layout from the project
  Project.Current.RemoveAsync(layoutItem);
}

Change layout properties

// get the project item
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));

// Check for layoutItem
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
    {
      // get the layout
      Layout layout = layoutItem.GetLayout();
      if (layout != null)
      {
        // change properties
        ArcGIS.Core.CIM.CIMPage page = layout.GetPage();
        page.Width = 8.5;
        page.Height = 11;
        layout.SetPage(page);

        layout.SetName("New Name");             
      }
    });

}

Find an element

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
  {
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();

    if (layout != null)
    {
      // find a single specific element
      Element rect = layout.FindElement("Rectangle") as Element;

      // or use the Elements collection
      Element rect2 = layout.Elements.FirstOrDefault(item => item.Name.Equals("Rectangle"));
    }
  });
}

Update element properties

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
  // update the name
  element.SetName("New Name");

  // update visibility
  element.SetVisible(true);
});

Delete an element

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
  // delete a specific element
  myLayout.DeleteElement(element);

  // or delete a group of elements
  myLayout.DeleteElements(item => item.Name.Contains("Clone"));
});

Group elements

if (element is GroupElement)
{
  var grpElement = element as GroupElement;
  var grpList = grpElement.Elements;

  StringBuilder sb = new StringBuilder();
  foreach (Element grpItem in grpList)
      sb.AppendLine(grpItem.Name);
}

Update Text Element properties

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
  {
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout != null)
    {
      // Reference a text element by name
      TextElement txtElm = layout.FindElement("MyTextElement") as TextElement;
      if (txtElm != null)
      {
        // Change placement properties
        PlacementProperties txtPlacement = txtElm.GetPlacement();
        txtPlacement.Anchor = ArcGIS.Core.CIM.Anchor.CenterPoint;
        txtPlacement.X = x;
        txtPlacement.Y = y;
        txtElm.SetPlacement(txtPlacement);

        // Change TextProperties
        TextProperties txtProperties = new TextProperties("Hello world", "Times New Roman", 48);
        txtElm.SetTextProperties(txtProperties);
      }
    }
  });
}

Update a picture element

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout != null)
    {
      // Reference a picture element by name
      PictureElement picElm = layout.FindElement("MyPicture") as PictureElement;
      // Change the path to a new source
      if (picElm != null)
        picElm.SetSourcePath(@"D:\MyData\Pics\LakeHuron.jpg");
    }
});

Update a map surround

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout != null)
    {
      // Reference a scale bar element by name
      MapSurround scaleBar = layout.FindElement("MyScaleBar") as MapSurround;

      // Reference a map frame element by name
      MapFrame mf = layout.FindElement("MyMapFrame") as MapFrame;

      if ((scaleBar != null) && (mf != null))
        //Set the scale bar to the newly referenced map frame
        scaleBar.SetMapFrame(mf);
    }
});

Lock an element

// The Locked property is displayed in the TOC as a lock symbol next to each element.  
// If locked the element can't be selected in the layout using the graphic 
// selection tools.

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout != null)
    {
      //Reference an element by name
      Element element = layout.FindElement("MyElement");
      if (element != null)
      {
        // Modify the Locked property via the CIM
        ArcGIS.Core.CIM.CIMElement CIMElement = element.Definition as ArcGIS.Core.CIM.CIMElement;
        CIMElement.Locked = true;
        element.SetDefinition(CIMElement);
      }
    }
});

Update an elements transparency

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
  // Reference and load the layout associated with the layout item
  Layout layout = layoutItem.GetLayout();
  if (layout != null)
  {
    // Reference a element by name
    GraphicElement graphicElement = layout.FindElement("MyElement") as GraphicElement;
    if (graphicElement != null)
    {
      // Modify the Transparency property that exists only in the CIMGraphic class.
      ArcGIS.Core.CIM.CIMGraphic CIMGraphic = graphicElement.Graphic as ArcGIS.Core.CIM.CIMGraphic;
      CIMGraphic.Transparency = 50;             // mark it 50% transparent
      graphicElement.SetGraphic(CIMGraphic);
    }
  }
});

Clone an element

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
  // Reference and load the layout associated with the layout item
  Layout layout = layoutItem.GetLayout();
  if (layout != null)
  {
    // Reference a element by name
    GraphicElement graphicElement = layout.FindElement("MyElement") as GraphicElement;
    if (graphicElement != null)
    {
      // get the placement properties
      PlacementProperties graPlace = graphicElement.GetPlacement();

      // clone and set the new x,y
      var cloneElement = graphicElement.Clone("Clone");
      graPlace.X = graPlace.X + xOffset;
      graPlace.Y = graPlace.Y - yOffset;
      cloneElement.SetPlacement(graPlace);
    }
  }
});

Access map frame and map bookmarks from Layout

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
  {
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout == null)
      return;

    // Reference a mapframe by name
    MapFrame mf = layout.Elements.FirstOrDefault(item => item.Name.Equals("MapFrame")) as MapFrame;
    if (mf == null)
      return;

    // get the mapframe's MapView
    ArcGIS.Desktop.Mapping.MapView mv = mf.MapView;
    if (mv == null)
      return;

    // get the map and the bookmarks
    ArcGIS.Desktop.Mapping.Map map = mf.Map;
    var bookmarks = map.GetBookmarks();
    if (bookmarks == null)
      return;

    // Loop through each bookmark
    foreach (var bookmark in bookmarks)
    {
      // Set up a PDF format and set its properties
      ArcGIS.Desktop.Mapping.PDFFormat PDF = new ArcGIS.Desktop.Mapping.PDFFormat();
      String path = String.Format(@"C:\Temp\{0}.pdf", bookmark.Name);
      PDF.OutputFileName = path;

      // Zoom to the bookmark - use 0 just in case the app backstage Navigation Transition Time is > 0 
      mv.ZoomTo(bookmark, TimeSpan.FromSeconds(0));

      // Export to PDF
      if (PDF.ValidateOutputFilePath())
        mf.Export(PDF);
    }
  });
}

Export a layout

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
  {
    Layout layout = layoutItem.GetLayout();
    if (layout == null)
      return;

    // Create BMP format with appropriate settings
    ArcGIS.Desktop.Mapping.BMPFormat BMP = new ArcGIS.Desktop.Mapping.BMPFormat();
    BMP.Resolution = 300;
    BMP.OutputFileName = @"C:\temp\Layout.bmp";
    if (BMP.ValidateOutputFilePath())
    {
      layout.Export(BMP);
    }

    // Create EMF format with appropriate settings
    ArcGIS.Desktop.Mapping.EMFFormat EMF = new ArcGIS.Desktop.Mapping.EMFFormat();
    EMF.Resolution = 300;
    EMF.OutputFileName = @"C:\temp\Layout.emf";
    if (EMF.ValidateOutputFilePath())
    {
      layout.Export(EMF);
    }

    // create eps format with appropriate settings
    ArcGIS.Desktop.Mapping.EPSFormat EPS = new ArcGIS.Desktop.Mapping.EPSFormat();
    EPS.Resolution = 300;
    EPS.OutputFileName = @"C:\temp\Layout.eps";
    if (EPS.ValidateOutputFilePath())
    {
      layout.Export(EPS);
    }

    // Create GIF format with appropriate settings
    ArcGIS.Desktop.Mapping.GIFFormat GIF = new ArcGIS.Desktop.Mapping.GIFFormat();
    GIF.Resolution = 300;
    GIF.OutputFileName = @"C:\temp\Layout.gif";
    if (GIF.ValidateOutputFilePath())
    {
      layout.Export(GIF);
    }

    // Create JPEG format with appropriate settings
    ArcGIS.Desktop.Mapping.JPEGFormat JPEG = new ArcGIS.Desktop.Mapping.JPEGFormat();
    JPEG.Resolution = 300;
    JPEG.OutputFileName = @"C:\temp\Layout.jpg";
    if (JPEG.ValidateOutputFilePath())
    {
      layout.Export(JPEG);
    }

    // Create PDF format with appropriate settings
    ArcGIS.Desktop.Mapping.PDFFormat PDF = new ArcGIS.Desktop.Mapping.PDFFormat();
    PDF.Resolution = 300;
    PDF.OutputFileName = @"C:\temp\Layout.pdf";
    if (PDF.ValidateOutputFilePath())
    {
      layout.Export(PDF);
    }

    // Create PNG format with appropriate settings
    ArcGIS.Desktop.Mapping.PNGFormat PNG = new ArcGIS.Desktop.Mapping.PNGFormat();
    PNG.Resolution = 300;
    PNG.OutputFileName = @"C:\temp\Layout.png";
    if (PNG.ValidateOutputFilePath())
    {
      layout.Export(PNG);
    }

    // Create SVG format with appropriate settings
    ArcGIS.Desktop.Mapping.SVGFormat SVG = new ArcGIS.Desktop.Mapping.SVGFormat();
    SVG.Resolution = 300;
    SVG.OutputFileName = @"C:\temp\Layout.svg";
    if (SVG.ValidateOutputFilePath())
    {
      layout.Export(SVG);
    }

    // Create TGA format with appropriate settings
    ArcGIS.Desktop.Mapping.TGAFormat TGA = new ArcGIS.Desktop.Mapping.TGAFormat();
    TGA.Resolution = 300;
    TGA.OutputFileName = @"C:\temp\Layout.tga";
    if (TGA.ValidateOutputFilePath())
    {
      layout.Export(TGA);
    }

    // Create TIFF format with appropriate settings
    ArcGIS.Desktop.Mapping.TIFFFormat TIFF = new ArcGIS.Desktop.Mapping.TIFFFormat();
    TIFF.Resolution = 300;
    TIFF.OutputFileName = @"C:\temp\Layout.tif";
    if (TIFF.ValidateOutputFilePath())
    {
      layout.Export(TIFF);
    }
  });
}

Export a map frame

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{
  ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
  {
    // get the layout 
    Layout layout = layoutItem.GetLayout(); 
    if (layout == null)
      return;

    // get the map frame
    MapFrame mf = layout.FindElement("MyMapFrame") as MapFrame;

    if (mf != null)
    {
      // Create BMP format with appropriate settings
      ArcGIS.Desktop.Mapping.BMPFormat BMP = new ArcGIS.Desktop.Mapping.BMPFormat();
      BMP.HasWorldFile = true;
      BMP.Resolution = 300;
      BMP.OutputFileName = @"C:\temp\MapFrame.bmp";
      if (BMP.ValidateOutputFilePath())
      {
        mf.Export(BMP);
      }

      // emf, eps, gif, jpeg, pdf, png, svg, tga, tiff formats are also available for export
    }
  });
}

Export the active Mapview

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
  if (MapView.Active != null)
  {
    //Create BMP format with appropriate settings
    ArcGIS.Desktop.Mapping.BMPFormat bmp = new ArcGIS.Desktop.Mapping.BMPFormat();
    bmp.Resolution = 300;
    bmp.Height = 500;
    bmp.Width = 800;
    bmp.HasWorldFile = true;
    bmp.OutputFileName = @"C:\temp\MapView.bmp";

    //Export active map view
    if (bmp.ValidateOutputFilePath())
    {
      MapView.Active.Export(bmp);
    }

    // emf, eps, gif, jpeg, pdf, png, svg, tga, tiff formats also available for export
  }
});

Home

ProSnippets: Layouts

Clone this wiki locally