Select to view content in your preferred language

Textstring attribute is not set when creating new annotations. A bug in 3.5?

495
4
3 weeks ago
StåleHaug
Occasional Contributor

We have developed functionality in ArcGIS Pro that lets us create annotations in ArcGIS Pro using the official Norwegian place names registry. The place names registry are presented as a point layer in Pro, and the user can select a place name point and 'push the button' to create an annotation. The annotation is assigned the correct place name registry attributes, and symbology (font, color, size etc) is automatically set based on the type of place name, including the Textstring attribute. However, the first time (and sometimes even the second time) the functionality is used in after starting Pro, the Textstring attribute is set to 'Text' and not the correct place name. 'Text' is the default value when creating a new annotation, and no value is given for Textstring. All other attributes are set correctly.

This problem started when we from ArcGIS Pro 3.4 to 3.5.

The code for creating annotation from place name registry-data looks like this:

private async Task<bool> LagAnnotationFraSSR(SSR_skrivemaate ssr_skrivemaate, string skriftkodetall)
{
bool result = await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
string skriftkode = "Sk" + skriftkodetall;

var fc = m_navnAnnoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass;

var label = HentLabelForSkriftkode(fc, skriftkode);

// Finn tekstsymbol-ID for label/annotation class
var symbolName = label.TextSymbol.SymbolName;
var symbolID = HentSymbolIDForLabel(fc, symbolName);

Inspector inspector = m_navnLayer.GetTemplate(skriftkode).Inspector;
var annoProperties = inspector.GetAnnotationProperties();

inspector["AnnotationClassID"] = label.ID;
inspector["SymbolID"] = symbolID;
//THE PROBLEM IS THE FOLLOWING LINE
annoProperties.TextString = ssr_skrivemaate.Valgt_langnavn;
annoProperties.Shape = LagNavnGeometri(ssr_skrivemaate.SSRpunkt);
annoProperties.HorizontalAlignment = ArcGIS.Core.CIM.HorizontalAlignment.Left;
inspector["SKRIVEMAATENUMMER"] = ssr_skrivemaate.Skrivemaatenummer;
inspector["STEDSNUMMER"] = ssr_skrivemaate.Stedsnummer;
inspector["STEDSNAVNNUMMER"] = ssr_skrivemaate.Stedsnavnnummer;
inspector["SKRIVEMATE_ID"] = ssr_skrivemaate.Skrivemaate_ID;
inspector["SPRAK"] = ssr_skrivemaate.Spraak;
inspector["NAVNESTATUS"] = ssr_skrivemaate.Navnestatus;
inspector["NAVNTYPE"] = ssr_skrivemaate.Navneobjekttype_kodeverdi;
inspector["SNAVN"] = ssr_skrivemaate.Valgt_langnavn;
inspector["SKRIFTKODE"] = skriftkodetall;
inspector["OBJTYPE"] = "Tekst";

inspector.SetAnnotationProperties(annoProperties);

var createOperation = new EditOperation();
createOperation.Name = "Lag navn";
createOperation.SelectNewFeatures = true;

createOperation.Create(m_navnLayer.GetTemplate(skriftkode).Layer, inspector);
m_OpprettetFraSSR = true;
return createOperation.Execute();
});
return result;
}



 

0 Kudos
4 Replies
SumitMishra_016
Frequent Contributor

I tried to change the TextString in ArcGIS Pro 3.5 and it worked if I understood the problem correctly

 // Get the first annotation layer in the map
 AnnotationLayer annoLayer = MapView.Active.Map.GetLayersAsFlattenedList()
     .OfType<AnnotationLayer>().FirstOrDefault();
 if (annoLayer == null)
     return;

 await QueuedTask.Run(() =>
 {
     // Open the table for the annotation layer
     using (Table table = annoLayer.GetTable())
     {
         // Get the first row (or define your own query)
         ArcGIS.Core.Data.QueryFilter qf = new ArcGIS.Core.Data.QueryFilter
         {
             WhereClause = "1=1",
             SubFields = "*"
         };

         using (RowCursor cursor = table.Search(qf, false))
         {
             if (!cursor.MoveNext())
                 return;

             using (Row row = cursor.Current)
             {
                 long oid = row.GetObjectID();

                 // Load feature into Inspector
                 var insp = new Inspector();
                 insp.Load(annoLayer, oid);

                 // Get and update annotation properties
                 var annoProps = insp.GetAnnotationProperties();
                 annoProps.TextString = "Updated annotation text";
                 annoProps.FontSize = 24;
                 annoProps.HorizontalAlignment = ArcGIS.Core.CIM.HorizontalAlignment.Center;
                 annoProps.VerticalAlignment = ArcGIS.Core.CIM.VerticalAlignment.Bottom;
                 insp.SetAnnotationProperties(annoProps);

                 // Apply edit operation
                 var op = new EditOperation
                 {
                     Name = "Update annotation text",
                     SelectNewFeatures = false
                 };
                 op.Modify(insp);

                 if (!op.Execute())
                 {
                     System.Diagnostics.Debug.WriteLine("Failed to update annotation.");
                 }
             }
         }
     }
 });

 

0 Kudos
StåleHaug
Occasional Contributor

Thanks for your your feedback!

Changing the textstring works, but not the first time the functionality is used. Then the textstring is only set to 'Text'. I have also tried to hardcode a textstring like you do

annoProperties.TextString = "PleaseDontBeText";

but the same still happens. I have no idea why, this has never happened before.

I have tried running isolated code like you do in your sample, and then this is not a problem. But we have also several other tools, that picks out attributevalues from features and turns them into annotations (for example, taking the altitude attribute of a lake feature and creating an annotation out of it). Then the problem is still there, the first time the code is run the textstring is set to 'Text' and not the altitude value.

We have had this code running for years, through various editions of Pro (both 2.x and 3.x), and this started happening with version 3.5.

0 Kudos
SumitMishra_016
Frequent Contributor

I tried creating a new annotation feature class — it's working, could you please explain"but not the first time the functionality is used"

await QueuedTask.Run(() =>
{
    // Get default GDB path
    string gdbPath = Project.Current.DefaultGeodatabasePath;
    var gdbConnection = new FileGeodatabaseConnectionPath(new Uri(gdbPath));

    using (Geodatabase geodatabase = new Geodatabase(gdbConnection))
    {
        // Feature dataset name
        string featureDatasetName = "Places";

        // Annotation feature class name
        string annotationFeatureClassName = "CitiesAnnotation";

        // Create a SchemaBuilder object
        SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);

        // Open existing annotation feature class name
        using (AnnotationFeatureClass existingAnnotationFeatureClass = geodatabase.OpenDataset<AnnotationFeatureClass>("testanno"))
        {

            // Create Feature dataset description
            FeatureDatasetDescription featureDatasetDescription =
              new FeatureDatasetDescription(featureDatasetName, existingAnnotationFeatureClass.GetDefinition().GetSpatialReference());

            // Add the creation of the Places dataset to DDL task
            FeatureDatasetToken featureDatasetToken = schemaBuilder.Create(featureDatasetDescription);

            // Create an annotation feature class description using existing annotation feature class
            AnnotationFeatureClassDescription annotationFeatureClassDescription = new AnnotationFeatureClassDescription(annotationFeatureClassName, existingAnnotationFeatureClass.GetDefinition())
            {
                IsAutoCreate = true,
                IsSymbolIDRequired = false,
                IsUpdatedOnShapeChange = true
            };

            // Add the creation of the Cities annotation feature class inside Places feature dataset
            schemaBuilder.Create(new FeatureDatasetDescription(featureDatasetToken), annotationFeatureClassDescription);

            // Execute the DDL
            bool success = schemaBuilder.Build();

            // Inspect error messages
            if (!success)
            {
                IReadOnlyList<string> errorMessages = schemaBuilder.ErrorMessages;
                //etc.
            }
            
            var newAnnoFCPath = System.IO.Path.Combine(gdbPath, featureDatasetName, annotationFeatureClassName);
            var annoLayer = LayerFactory.Instance.CreateLayer(new Uri(newAnnoFCPath), MapView.Active.Map) as AnnotationLayer;

            
            var op = new EditOperation { Name = "Insert new annotation" };

            var insp = new Inspector();
            insp.LoadSchema(annoLayer);

            var annoProps = insp.GetAnnotationProperties();
            annoProps.TextString = "New City Annotation";
            annoProps.FontSize = 16;
            insp.SetAnnotationProperties(annoProps);

            // Set shape to center of current map view
            var centerPoint = MapView.Active.Extent.Center;
            insp["SHAPE"] = centerPoint;

            op.Create(annoLayer, insp);
            if (!op.Execute())
                System.Diagnostics.Debug.WriteLine("Annotation creation failed.");
        }
    }
});

SumitMishra_016_0-1747915597012.png

 

0 Kudos
StåleHaug
Occasional Contributor

Once again, thanks for all the help and the effort you make. It is greatly appreciated. 

When AG Pro is started, and editing is started for the first time, the resulting textstring is always ‘Text’. See ‘name_test1.png’. The textstring should have been ‘Skolo’. From there on it seems to be random whether the textstring is ‘Text’ or correct. See ‘name_test2.png’. If I discard all the edits, and starts creating the same annotations once more, everything is correct. See ‘name_test3.png’.

But if I restart Pro, and start editing again by creating the same annoations, the same happens once more. The first annotation is always ‘Text’, and from there it is random if the textstring is ‘Text’ or correct. See ‘name_test4.png’.

There is a lot going on in my code, from calculating annotation geometry with the correct angle to collecting a lot of information from the place name registry layer. It could be this is the problem, creating an annotation by just doing

annoProps.TextString = "New City Annotation";

 

 works correctly for me too.

I hope this explanation was 'understandable'. Like I said, up until AG Pro 3.4 this worked perfectly.

All data are stored locally on my PC in a file geodatabase.



0 Kudos