Select to view content in your preferred language

Project Template

237
1
04-24-2023 01:11 AM
LongNguyễnVăn
New Contributor

I am developing a Pro AddIn using Pro version 3.1. How do I import a .aptx template file into an existing map?

0 Kudos
1 Reply
RichardDaniels
Regular Contributor
The aptx file is usually loaded into the Pro as a projec, use the layout template for a map layout. You can use code to use the 'current active map' as the primary may shown in the newly created layout template. Recall templates can have 'tags' for text items, this means you can search for them an replace the text as well (e.g., in this example we have map layout template with Title1, subtitle1, Title2, subtitle2, etc. tags that get populated form a form) prior to executing the LoadLayoutTemplate sub.
internal static void LoadLayoutTemplate(string myMapTemplatePath, string lytTitle1, string lytTitle2, string lytSubTitle1, string lytSubTitle2, string lytDateInfo, string lytDataSource, Map lytMap, Envelope lytEnvelope)
{
// Load WSDOT Layout templates, added as a Tool at ArcGIS Pro 2.9.
try
{
IProjectItem myItem = ItemFactory.Instance.Create(@myMapTemplatePath, ItemFactory.ItemType.PathItem) as IProjectItem;
IProjectMultiItem myMultiItem = myItem as IProjectMultiItem;
LoadNewLayoutItem(myMultiItem, lytTitle1, lytTitle2, lytSubTitle1, lytSubTitle2, lytDateInfo, lytDataSource, lytMap, lytEnvelope);
}
catch (Exception eX)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Unable to Load requested Layout template ( internal static void LoadLayoutTemplate(string myMapTemplatePath, string lytTitle1, string lytTitle2, string lytSubTitle1, string lytSubTitle2, string lytDateInfo, string lytDataSource, Map lytMap, Envelope lytEnvelope)
{
// Load WSDOT Layout templates, added as a Tool at ArcGIS Pro 2.6.
try
{
IProjectItem myItem = ItemFactory.Instance.Create(@myMapTemplatePath, ItemFactory.ItemType.PathItem) as IProjectItem;
IProjectMultiItem myMultiItem = myItem as IProjectMultiItem;
LoadNewLayoutItem(myMultiItem, lytTitle1, lytTitle2, lytSubTitle1, lytSubTitle2, lytDateInfo, lytDataSource, lytMap, lytEnvelope);
}
catch (Exception eX)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Unable to Load requested Layout template (pagx). " + eX.ToString(),"Information",0);
}
}

public async static void LoadNewLayoutItem(IProjectMultiItem theItem, string lytTitle1, string lytTitle2, string lytSubTitle1, string lytSubTitle2, string lytDateInfo, string lytDataSource, Map lytMap, Envelope lytEnvelope)
{
//add the newly project item to the current project; must be public to avoid threading errors!
try
{
//add the layout to the project and wait for import to complete
await QueuedTask.Run(() => Project.Current.ImportItem(theItem, true));
//wait 1 econdto ensure we are Ready
await Task.Delay(1000);
// Reference the new active layout in the project
LayoutView lytView = LayoutView.Active;
if (lytView != null)
{
await QueuedTask.Run(() =>
{
Layout myLayout = lytView.Layout;
// set the text elements based on values passed from frmLayouts
// help at https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/index.html#topic11176.html
// https://github.com/Esri/arcgis-pro-sdk/blob/master/Examples/Layouts/MethodSnippets.cs
TextElement txtTitles = myLayout.FindElement("Title1") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytTitle1, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("Title2") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytTitle2, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("SubTitle1") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytSubTitle1, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("SubTitle2") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytSubTitle2, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("DateInfo") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytDateInfo, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("DataSource") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytDataSource, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
MapFrame frameMap = myLayout.FindElement("Layers Map Frame") as MapFrame;
if (frameMap != null)
{
//Update the MapFrame to use the Map that was active when the tool was run; set layout map element envelope based on the active map (added 1/28/2022)
if (lytMap != null)
{
frameMap.SetMap(lytMap);
frameMap.SetCamera(lytEnvelope);
}
}
});
}

}
catch (Exception eX)
{
//do nothing
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("System Error: " + eX.ToString(), "Error",0);
}
}
). " + eX.ToString(),"Information",0);
}
}

public async static void LoadNewLayoutItem(IProjectMultiItem theItem, string lytTitle1, string lytTitle2, string lytSubTitle1, string lytSubTitle2, string lytDateInfo, string lytDataSource, Map lytMap, Envelope lytEnvelope)
{
//add the newly project item to the current project; must be public to avoid threading errors!
try
{
//add the layout to the project and wait for import to complete
await QueuedTask.Run(() => Project.Current.ImportItem(theItem, true));
//wait 1 econdto ensure we are Ready
await Task.Delay(1000);
// Reference the new active layout in the project
LayoutView lytView = LayoutView.Active;
if (lytView != null)
{
await QueuedTask.Run(() =>
{
Layout myLayout = lytView.Layout;
// set the text elements based on values passed from frmLayouts
// help at https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/index.html#topic11176.html
// https://github.com/Esri/arcgis-pro-sdk/blob/master/Examples/Layouts/MethodSnippets.cs
TextElement txtTitles = myLayout.FindElement("Title1") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytTitle1, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("Title2") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytTitle2, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("SubTitle1") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytSubTitle1, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("SubTitle2") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytSubTitle2, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("DateInfo") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytDateInfo, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
txtTitles = myLayout.FindElement("DataSource") as TextElement;
if (txtTitles != null)
{
TextProperties txtProp = new TextProperties(lytDataSource, txtTitles.TextProperties.Font, txtTitles.TextProperties.FontSize, txtTitles.TextProperties.FontStyle);
txtTitles.SetTextProperties(txtProp);
}
MapFrame frameMap = myLayout.FindElement("Layers Map Frame") as MapFrame;
if (frameMap != null)
{
//Update the MapFrame to use the Map that was active when the tool was run; set layout map element envelope based on the active map (added 1/28/2022)
if (lytMap != null)
{
frameMap.SetMap(lytMap);
frameMap.SetCamera(lytEnvelope);
}
}
});
}

}
catch (Exception eX)
{
//do nothing
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("System Error: " + eX.ToString(), "Error",0);
}
}

0 Kudos