Create stylx file from sketch

205
1
01-14-2022 08:01 AM
DylanBuiLe
New Contributor

Hello all, I know that creating symbol in Arcgis Pro is documented well but I would like to do it programmatically to adapt external symbol style definitition. Like in the thread description, I would like to create a stylx file and fill it with symbols.

I searched through the arcgis pro sdk API references and read the documentation / samples but I am not able to find a way to create stylx file (without using the UI in arcgis pro). The goal is to make a custom stylx file and fill it with my custom generated symbol. I think there must be a method to generate those stylx file since you can do it in the UI of Arcgis Pro or ?

One thing came in my mind is to  prepare an empty stylx file by using an existing style file, open it via db browser for sqlite and manually remove all the existing symbol in it. Then I can apply the symbol to the prepared stylx file like described here: https://community.esri.com/t5/arcgis-pro-documents/how-to-programatically-add-symbols-to-a-stylx/tac....

Also in case I know how to generate a stylx file, which type of Symbol is kompatible in the stylx file ? What class hold the information of those symbol ? Is it those class coming from this base class "ArcGIS.Core.CIM Namespace.CIMSymbol "which need to be created like  CIMPolygonSymbol, CIMLineSymbol? And what about custom symbol ?

I really wish there are some documentation / examples for my case. 

Many Thanks

0 Kudos
1 Reply
CharlesMacleod
Esri Regular Contributor

Hi Dylan, See here for how to create a style: https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Map-Authoring#styles 

"Style Items" are what is stored in style files. The base class is StyleItem: https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/#topic12305.html . There is also more information here: https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Map-Authoring#style-item 

The concrete style items are listed in the Inheritance Hierarchy on the API reference page (the link to the topic page above) and any, or all, of them can be stored in, or retrieved from, a .stylx. Specifically, in your case, where u want to store symbols in a style file, you want a SymbolStyleItem. https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/index.html#topic12405.html . SymbolStyleItem is used to wrap a CIMSymbol - whether custom or otherwise.

In this snippet I am using SymbolFactory to create 4 custom symbols - a point, line, poly, and text CIMSymbol - just using the defaults. Note how I wrap them in a SymbolStyleItem to add each of them to my style file. The only caveat is that the style file to which I am adding them must be in the project.

style_1.png

 

 

 

QueuedTask.Run(() => {
   //make some (custom) symbols
   var symbols = new List<CIMSymbol>();
      symbols.Add(SymbolFactory.Instance.DefaultPointSymbol);
      symbols.Add(SymbolFactory.Instance.DefaultLineSymbol);
      symbols.Add(SymbolFactory.Instance.DefaultPolygonSymbol);
      symbols.Add(SymbolFactory.Instance.DefaultTextSymbol);
//access the style file to be updated
var stylx_file = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(
      item => item.Name == "My_Defaults");//must already be in the project
//make the style items that wrap the symbols
  int p = 1;
  int l = 1;
  int pl = 1;
  int t = 1;
  foreach (var symbol in symbols) {
        var key = "";
        var category = "";
        var itemType = StyleItemType.Unknown;

        if (symbol is CIMPointSymbol ptSym) {
          key = $"Point Symbol {p++}";
          category = "Point";
          itemType = StyleItemType.PointSymbol;
        }
        else if (symbol is CIMLineSymbol lnSym){
          key = $"Line Symbol {l++}";
          category = "Line";
          itemType = StyleItemType.LineSymbol;
        }
        else if (symbol is CIMPolygonSymbol polySym) {
          key = $"Polygon Symbol {pl++}";
          category = "Polygon";
          itemType = StyleItemType.PolygonSymbol;
        }
        else if (symbol is CIMTextSymbol textSym) {
          key = $"Text Symbol {t++}";
          category = "Text";
          itemType = StyleItemType.TextSymbol;
        }
        else {
          continue;//mesh
         }
   //make the symbol style item
   var ssi = new SymbolStyleItem() {
     Name = key,//Can be any string - localized is fine
     Key = key,//must be unique
     Tags = itemType.ToString(),//tag1;tag2;tag3;etc
     Category = category,//arbitrary string for organizing your items
     ItemType = itemType,//style item type
     Symbol = symbol //your symbol
  };
   //add the style item
   stylx_file.AddItem(ssi);
  }

  //copy an existing symbol into the style file
  var arcgis_2d = Project.Current.GetItems<StyleProjectItem().FirstOrDefault(
               item => item.Name == "ArcGIS 2D");
  //get an arbitrary symbol, or symbols
  var poly_ssi1 = arcgis_2d.SearchSymbols(StyleItemType.PolygonSymbol, "Glacier")[0];
  var poly_ssi2 = arcgis_2d.SearchSymbols(StyleItemType.PolygonSymbol, "Buffered Gradient")[0];
  //add them
  stylx_file.AddItem(poly_ssi1);
  stylx_file.AddItem(poly_ssi2);
  //... etc ...
});
  

 

 

 

 

For more code snippets on how to access the various style items from a style file, please see the snippets here: https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-MapAuthoring#symbol-search 

Result:

style_2.png

eg, the polygon symbols:

style_3.png

 

 

 

0 Kudos