ArcGIS Pro SDK method for changing layout text color

692
4
Jump to solution
05-15-2023 08:46 AM
RichardFairhurst
MVP Honored Contributor

I am trying to convert an ArcObjects tool written in VB to an ArcGIS Pro SDK tool written in C#.  One of the subroutines of my ArcObjects passes a layout graphics container and a text element name on the layout to make the text color black and another subroutine is passed the same kinds of parameters to change the text color to null (invisible).  Here is the ArcObjects method for making the color of the text black:

 

    Private Sub setTextElementBlackColor(ByVal pGraphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer, ByVal strElementName As String)
        Dim pActiveView As ESRI.ArcGIS.Carto.IActiveView
        Dim bFound As Boolean
        Dim pElement As ESRI.ArcGIS.Carto.IElement
        Dim pElementProperties As ESRI.ArcGIS.Carto.IElementProperties2
        Dim pTextElement As ESRI.ArcGIS.Carto.ITextElement
        Dim pTextSymbol As ESRI.ArcGIS.Display.ITextSymbol
        Dim pColor As ESRI.ArcGIS.Display.IColor

        pActiveView = pGraphicsContainer
        pColor = New ESRI.ArcGIS.Display.RgbColorClass
        pColor.NullColor = False
        pColor.RGB = RGB(0, 0, 0)
        pTextSymbol = New ESRI.ArcGIS.Display.TextSymbolClass
        'Element Search escape variable
        bFound = False

        pGraphicsContainer.Reset()
        pElement = pGraphicsContainer.Next()
        Do While Not pElement Is Nothing
            pElementProperties = pElement
            If pElementProperties.Name = strElementName Then
                If TypeOf pElement Is ESRI.ArcGIS.Carto.ITextElement Then
                    pTextElement = pElement
                    pTextSymbol = pTextElement.Symbol
                    pTextSymbol.Color = pColor
                    pTextElement.Symbol = pTextSymbol
                    pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
                    bFound = True
                    If Not pTextSymbol Is Nothing Then
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pTextSymbol)
                    End If
                    If Not pTextElement Is Nothing Then
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(pTextElement)
                    End If
                End If
            End If
            If bFound = False Then
                pElement = pGraphicsContainer.Next()
            Else
                pElement = Nothing
            End If
            If Not pElementProperties Is Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pElementProperties)
            End If
        Loop
        If Not pColor Is Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pColor)
        End If
        If Not pActiveView Is Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pActiveView)
        End If
    End Sub

 

What is the equivalent code in the ArcGIS Pro SDK  for changing a layout text element to black or invisible?

 

I have a method for changing the text of a TextElement, but the TextProperties of the element do not include any way to change the text color.  Presumably the code would be similar to that code for changing the text element color, but I do not see any sample code in the git hub examples that tells me how to access the color properties of the text element.  Here is the code I use to change the text value of the TextElement:

        public async static void SetTextElement(string ElementName, string TextValue)
        {
            await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
            {
                //Reference and load the layout associated with the layout item
                Layout lyt = LayoutView.Active.Layout;
                if (lyt != null)
                {
                    //Reference a text element by name
                    TextElement txtElm = lyt.FindElement(ElementName) as TextElement;

                    //Change TextProperties
                    TextProperties txt_prop = txtElm.TextProperties;
                    txt_prop.Text = TextValue;
                    txtElm.SetTextProperties(txt_prop);
                }
            });
        }
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

In order to change transparency, you have to access the CIM (Cartographic Information Model) definition of the text graphic (via GetDefinition), change the CIM definition, and finally set the updated definition (via SetDefinition).

This will turn the text graphic symbol's transparency (name of graphic is TestText) to transparent (value of 100) and non-transparent (value of 0):

protected override async void OnClick()
{
  var myLayoutView = LayoutView.Active;
  if (myLayoutView == null) return;
  await QueuedTask.Run(() =>
  {
    Layout layout = myLayoutView.Layout;
    if (layout == null) return;
    TextElement txtElm = layout.FindElement("TestText") as TextElement;
    if (txtElm == null) return;
    // Change Transparency
    var cimSym = txtElm.GetDefinition();
    cimSym.Visible = true;
    var cimTextGraphicBase = (cimSym as CIMGraphicElement).Graphic;
    if (cimTextGraphicBase != null)
    {
      cimTextGraphicBase.Transparency = 100;
      txtElm.SetDefinition(cimSym);
    }
  });
}

 

View solution in original post

4 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

In order to change transparency, you have to access the CIM (Cartographic Information Model) definition of the text graphic (via GetDefinition), change the CIM definition, and finally set the updated definition (via SetDefinition).

This will turn the text graphic symbol's transparency (name of graphic is TestText) to transparent (value of 100) and non-transparent (value of 0):

protected override async void OnClick()
{
  var myLayoutView = LayoutView.Active;
  if (myLayoutView == null) return;
  await QueuedTask.Run(() =>
  {
    Layout layout = myLayoutView.Layout;
    if (layout == null) return;
    TextElement txtElm = layout.FindElement("TestText") as TextElement;
    if (txtElm == null) return;
    // Change Transparency
    var cimSym = txtElm.GetDefinition();
    cimSym.Visible = true;
    var cimTextGraphicBase = (cimSym as CIMGraphicElement).Graphic;
    if (cimTextGraphicBase != null)
    {
      cimTextGraphicBase.Transparency = 100;
      txtElm.SetDefinition(cimSym);
    }
  });
}

 

RichardFairhurst
MVP Honored Contributor

Based on the code you provided I have created the following method that finds the text element by the name that is passed to the method and sets the transparency to the value that is passed:

 

        public async static void SetTextTransparency(string ElementName, int TextTransparency)
        {
            // Ensure TextTransparency is within transparency bounds
            if (TextTransparency < 0) TextTransparency = 0;
            if (TextTransparency > 100) TextTransparency = 100;
            var myLayoutView = LayoutView.Active;
            if (myLayoutView == null) return; 
            await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
            {
                //Reference and load the layout associated with the layout item
                Layout lyt = myLayoutView.Layout;
                if (lyt == null) return;
                //Reference a text element by name
                TextElement txtElm = lyt.FindElement(ElementName) as TextElement;
                if (txtElm == null) return;
                //Change TextProperties
                var cimSym = txtElm.GetDefinition();
                cimSym.Visible = true;
                var cimTextGraphicBase = (cimSym as CIMGraphicElement).Graphic;
                if (cimTextGraphicBase == null) return;
                cimTextGraphicBase.Transparency = TextTransparency;
                txtElm.SetDefinition(cimSym);
            });
        }

 

 I have also rewritten my method for setting the text value to follow a similar coding pattern to make sure  that the process of checking for null values is more thorough.

RichardFairhurst
MVP Honored Contributor

I have a question about using transparency on the text element.  Can you tell me where the text element transparency is exposed through the ArcGIS Pro interface?  I am unable to find where the transparency property of the text element is set.  It does not appear on any ribbon when I select the element that is set to 100% transparent, nor does a transparency property appear within the Text Symbol properties of the text element on the Element tab when I select a text element.  I am asking, because I need to manually change the set up the template layout to make all items visible with black text color, but I wanted to also be able to manually set the transparency of the items that I wanted to be opaque or transparent for a specific layout.

 

I prefer to not set the Visible property of the elements to false, since then they cannot be selected on the layout itself.  They can only be accessed through the Contents pane for the layout if the Visible property is set to false.  The Transparency property allows me to select the elements on the layout, but I don't see where to set it manually.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Are you looking for this:

Wolf_0-1684262602512.png

in essence you are changing the transparency of the text color 

0 Kudos