Get all "StyleProjectItems"
IEnumerable<StyleProjectItem> newStyleItemsContainer = null;
newStyleItemsContainer = Project.Current.GetItems<StyleProjectItem>();
foreach (var styleItem in newStyleItemsContainer)
{
//Do Something with the style.
}
Get a specific "StyleProjectItem"
var container = Project.Current.GetItems<StyleProjectItem>();
StyleProjectItem testStyle = container.FirstOrDefault(style => (style.Name == "ArcGIS 3D"));
StyleItem cone = null;
if (testStyle != null)
cone = testStyle.LookupItem(StyleItemType.PointSymbol, "Cone_Volume_3");
Get the "Favorite" StyleProjectItem
var fav_style_item = await QueuedTask.Run(() =>
{
var containerStyle = Project.Current.GetProjectItemContainer("Style");
return containerStyle.GetItems().OfType<StyleProjectItem>().First(item => item.TypeID == "personal_style");
});
ProjectItem: Get an Item or Find an Item
//GetItems searches project content
var map = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(m => m.Name == "Map1");
var layout = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(m => m.Name == "Layout1");
var folders = Project.Current.GetItems<FolderConnectionProjectItem>();
var style = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(s => s.Name == "ArcGIS 3D");
//Find item uses a catalog path. The path can be to a file or dataset
var fcPath = @"C:\Pro\CommunitySampleData\Interacting with Maps\Interacting with Maps.gdb\Crimes";
var pdfPath = @"C:\Temp\Layout1.pdf";
var imgPath = @"C:\Temp\AddinDesktop16.png";
var fc = Project.Current.FindItem(fcPath);
var pdf = Project.Current.FindItem(pdfPath);
var img = Project.Current.FindItem(imgPath);
Create_ScaleBar
//Create a scale bar for a specific map frame and assign a scale bar style item.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Reference a North Arrow in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars("Double Alternating Scale Bar 1")[0];
//Build geometry
Coordinate2D center = new Coordinate2D(7, 8);
//Reference MF, create north arrow and add to layout
MapFrame mf = layout.FindElement("New Map Frame") as MapFrame;
if (mf == null)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
return;
}
//At 2.x -
//ScaleBar sbElm = LayoutElementFactory.Instance.CreateScaleBar(layout, center, mf, sbStyleItm);
//sbElm.SetName("New Scale Bar");
//sbElm.SetWidth(2);
//sbElm.SetX(6);
//sbElm.SetY(7.5);
var sbInfo = new ScaleBarInfo()
{
MapFrameName = mf.Name,
ScaleBarStyleItem = sbStyleItm
};
var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
layout, center.ToMapPoint(), sbInfo, "New Scale Bar") as ScaleBar;
sbElm.SetWidth(2);
sbElm.SetX(6);
sbElm.SetY(7.5);
});
Create_NorthArrow
//Create a north arrow for a specific map frame and assign a north arrow style item.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Reference a North Arrow in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
NorthArrowStyleItem naStyleItm = stylePrjItm.SearchNorthArrows("ArcGIS North 10")[0];
//Build geometry
Coordinate2D center = new Coordinate2D(7, 5.5);
//Reference MF, create north arrow and add to layout
MapFrame mf = layout.FindElement("New Map Frame") as MapFrame;
if (mf == null)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
return;
}
//At 2.x -
//NorthArrow arrowElm = LayoutElementFactory.Instance.CreateNorthArrow(layout, center, mf, naStyleItm);
//arrowElm.SetName("New North Arrow");
//arrowElm.SetHeight(1.75);
//arrowElm.SetX(7);
//arrowElm.SetY(6);
var naInfo = new NorthArrowInfo()
{
MapFrameName = mf.Name,
NorthArrowStyleItem = naStyleItm
};
var arrowElm = ElementFactory.Instance.CreateMapSurroundElement(
layout, center.ToMapPoint(), naInfo, "New North Arrow") as NorthArrow;
arrowElm.SetHeight(1.75);
arrowElm.SetX(7);
arrowElm.SetY(6);
});
Create Line Element
//Must be on QueuedTask.Run(() => { ...
//Build geometry
List<Coordinate2D> plCoords = new List<Coordinate2D>();
plCoords.Add(new Coordinate2D(1, 8.5));
plCoords.Add(new Coordinate2D(1.66, 9));
plCoords.Add(new Coordinate2D(2.33, 8.1));
plCoords.Add(new Coordinate2D(3, 8.5));
Polyline linePl = PolylineBuilderEx.CreatePolyline(plCoords);
//Reference a line symbol in a style
var ProjectStyles = Project.Current.GetItems<StyleProjectItem>();
StyleProjectItem style = ProjectStyles.First(x => x.Name == "ArcGIS 2D");
var symStyle = style.SearchSymbols(StyleItemType.LineSymbol, "Line with 2 Markers")[0];
CIMLineSymbol lineSym = symStyle.Symbol as CIMLineSymbol;
lineSym.SetSize(20);
//Set symbolology, create and add element to layout
//CIMLineSymbol lineSym = SymbolFactory.Instance.ConstructLineSymbol(ColorFactory.Instance.BlueRGB, 4.0, SimpleLineStyle.Solid);
ElementFactory.Instance.CreateGraphicElement(
container, linePl, lineSym, "New Line");
Create Point Element
//Must be on QueuedTask.Run(() => { ...
//Build geometry
Coordinate2D coord2D = new Coordinate2D(2.0, 10.0);
//Reference a point symbol in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
.FirstOrDefault(item => item.Name == "ArcGIS 2D");
SymbolStyleItem symStyleItm = stylePrjItm.SearchSymbols(
StyleItemType.PointSymbol, "City Hall")[0];
CIMPointSymbol pointSym = symStyleItm.Symbol as CIMPointSymbol;
pointSym.SetSize(50);
var elemInfo = new ElementInfo()
{
CustomProperties = new List<CIMStringMap>() {
new CIMStringMap() { Key = "Key1", Value = "Value1"},
new CIMStringMap() { Key = "Key2", Value = "Value2"}
},
Anchor = Anchor.TopRightCorner,
Rotation = 45.0
};
var graphic = GraphicFactory.Instance.CreateSimpleGraphic(
coord2D.ToMapPoint(), pointSym);
ElementFactory.Instance.CreateGraphicElement(
container, graphic, "New Point", true, elemInfo);
Create point graphic with symbology
//Create a simple 2D point graphic and apply an existing point style item as the symbology.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Build 2D point geometry
Coordinate2D coord2D = new Coordinate2D(2.0, 10.0);
//(optionally) Reference a point symbol in a style
StyleProjectItem ptStylePrjItm = Project.Current.GetItems<StyleProjectItem>()
.FirstOrDefault(item => item.Name == "ArcGIS 2D");
SymbolStyleItem ptSymStyleItm = ptStylePrjItm.SearchSymbols(
StyleItemType.PointSymbol, "City Hall")[0];
CIMPointSymbol pointSym = ptSymStyleItm.Symbol as CIMPointSymbol;
pointSym.SetSize(50);
//Set symbolology, create and add element to layout
//An alternative simple symbol is also commented out below.
//This would elminate the four optional lines of code above that
//reference a style.
//CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(
// ColorFactory.Instance.RedRGB, 25.0, SimpleMarkerStyle.Star);
//At 2.x - GraphicElement ptElm =
// LayoutElementFactory.Instance.CreatePointGraphicElement(
// layout, coord2D, pointSym);
GraphicElement ptElm = ElementFactory.Instance.CreateGraphicElement(
container, coord2D.ToMapPoint(), pointSym);
ptElm.SetName("New Point");
});
Create line graphic with symbology
//Create a simple 2D line graphic and apply an existing line
//style item as the symbology.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Build 2d line geometry
List<Coordinate2D> plCoords = new List<Coordinate2D>();
plCoords.Add(new Coordinate2D(1, 8.5));
plCoords.Add(new Coordinate2D(1.66, 9));
plCoords.Add(new Coordinate2D(2.33, 8.1));
plCoords.Add(new Coordinate2D(3, 8.5));
//At 2.x - Polyline linePl = PolylineBuilder.CreatePolyline(plCoords);
Polyline linePl = PolylineBuilderEx.CreatePolyline(plCoords);
//(optionally) Reference a line symbol in a style
StyleProjectItem lnStylePrjItm = Project.Current.GetItems<StyleProjectItem>()
.FirstOrDefault(item => item.Name == "ArcGIS 2D");
SymbolStyleItem lnSymStyleItm = lnStylePrjItm.SearchSymbols(
StyleItemType.LineSymbol, "Line with 2 Markers")[0];
CIMLineSymbol lineSym = lnSymStyleItm.Symbol as CIMLineSymbol;
lineSym.SetSize(20);
//Set symbolology, create and add element to layout
//An alternative simple symbol is also commented out below.
//This would elminate the four optional lines of code above that
//reference a style.
//
//CIMLineSymbol lineSym = SymbolFactory.Instance.ConstructLineSymbol(
// ColorFactory.Instance.BlueRGB, 4.0, SimpleLineStyle.Solid);
//At 2.x - GraphicElement lineElm =
// LayoutElementFactory.Instance.CreateLineGraphicElement(
// layout, linePl, lineSym);
GraphicElement lineElm = ElementFactory.Instance.CreateGraphicElement(
container, linePl, lineSym);
lineElm.SetName("New Line");
});
Create rectangle graphic with simple symbology
//Create a simple 2D rectangle graphic and apply simple fill and
//outline symbols.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Build 2D envelope geometry
Coordinate2D rec_ll = new Coordinate2D(1.0, 4.75);
Coordinate2D rec_ur = new Coordinate2D(3.0, 5.75);
//At 2.x - Envelope rec_env = EnvelopeBuilder.CreateEnvelope(rec_ll, rec_ur);
Envelope rec_env = EnvelopeBuilderEx.CreateEnvelope(rec_ll, rec_ur);
//Set symbolology, create and add element to layout
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlackRGB, 5.0, SimpleLineStyle.Solid);
CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.GreenRGB, SimpleFillStyle.DiagonalCross, outline);
//At 2.x - GraphicElement recElm =
// LayoutElementFactory.Instance.CreateRectangleGraphicElement(
// layout, rec_env, polySym);
// recElm.SetName("New Rectangle");
//
GraphicElement recElm = ElementFactory.Instance.CreateGraphicElement(
container, rec_env, polySym, "New Rectangle");
//Or use Predefined shape
GraphicElement recElm2 = ElementFactory.Instance.CreatePredefinedShapeGraphicElement(
container, PredefinedShape.Rectangle, rec_env, polySym,
"New Rectangle2");
});
Create Scale Bar From StyleItem
//Create a scale bar using a style.
//Search for a style project item by name
StyleProjectItem arcgis_2dStyle = Project.Current.GetItems<StyleProjectItem>()
.First(si => si.Name == "ArcGIS 2D");
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Reference the specific scale bar by name
ScaleBarStyleItem scaleBarItem = arcgis_2dStyle.SearchScaleBars(
"Double Alternating Scale Bar").FirstOrDefault();
//Reference the map frame and define the location
MapFrame myMapFrame = layout.FindElement("Map Frame") as MapFrame;
Coordinate2D coord2D = new Coordinate2D(10.0, 7.0);
//Construct the scale bar
//At 2.x - LayoutElementFactory.Instance.CreateScaleBar(
// layout, coord2D, myMapFrame, scaleBarItem);
var sbarInfo = new ScaleBarInfo()
{
MapFrameName = myMapFrame.Name,
ScaleBarStyleItem = scaleBarItem
};
ElementFactory.Instance.CreateMapSurroundElement(
layout, coord2D.ToMapPoint(), sbarInfo);
});
Create North Arrow From StyleItem 1
//Create a north arrow using a style.
//Search for a style project item by name
StyleProjectItem arcgis2dStyles = Project.Current.GetItems<StyleProjectItem>()
.First(si => si.Name == "ArcGIS 2D");
//Construct on the worker thread
await QueuedTask.Run(() =>
{
NorthArrowStyleItem naStyleItem = arcgis2dStyles.SearchNorthArrows(
"ArcGIS North 13").FirstOrDefault();
//Reference the map frame and define the location
MapFrame newFrame = layout.FindElement("New Map Frame") as MapFrame;
Coordinate2D nArrow = new Coordinate2D(6, 2.5);
//Construct the north arrow
//At 2.x - var newNorthArrow = LayoutElementFactory.Instance.CreateNorthArrow(
// layout, nArrow, newFrame, naStyleItem);
var naInfo = new NorthArrowInfo()
{
MapFrameName = newFrame.Name,
NorthArrowStyleItem = naStyleItem
};
var newNorthArrow = ElementFactory.Instance.CreateMapSurroundElement(
layout, nArrow.ToMapPoint(), naInfo);
});
Create Scale Bar
//Must be on QueuedTask.Run(() => { ...
//Build geometry
Coordinate2D ll = new Coordinate2D(5.0, 6);
Coordinate2D ur = new Coordinate2D(6.0, 7);
Envelope sbEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);
//Reference a Scale Bar in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
.FirstOrDefault(item => item.Name == "ArcGIS 2D");
ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
"Alternating Scale Bar 1")[0];
//ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
// "Double Alternating Scale Bar 1")[0];
//ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
// "Hollow Scale Bar 1")[0];
//Create Scale Bar
ScaleBarInfo sbInfo = new ScaleBarInfo()
{
MapFrameName = mapFrame.Name
};
var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
layout, sbEnv, sbInfo) as ScaleBar;
Create Scale Line
//Must be on QueuedTask.Run(() => { ...
//Build geometry
Coordinate2D ll = new Coordinate2D(5.0, 8);
Coordinate2D ur = new Coordinate2D(6.0, 9);
Envelope sbEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);
//Reference a Scale Bar in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
.FirstOrDefault(item => item.Name == "ArcGIS 2D");
ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
"Scale Line 1")[0];
//ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
// "Stepped Scale Line")[0];
//ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars(
// "Scale Line 2")[0];
//Create Scale Bar
ScaleBarInfo sbInfo = new ScaleBarInfo()
{
MapFrameName = mapFrame.Name,
ScaleBarStyleItem = sbStyleItm
};
var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
layout, sbEnv, sbInfo, "ScaleBar Line") as ScaleBar;
How to get a style in project by name
//Get all styles in the project
var ProjectStyles = Project.Current.GetItems<StyleProjectItem>();
//Get a specific style in the project by name
StyleProjectItem style = ProjectStyles.First(x => x.Name == "NameOfTheStyle");
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3.0 or higher.