Select to view content in your preferred language

Place labels at points centers

614
2
06-10-2019 05:29 AM
MaxMax2
Frequent Contributor

I'm trying to add label class for points layer and place labels at points centers. My code:

var maplexLabelPlacementProperties = labelClass.GetMaplexLabelPlacementProperties() ?? new CIMMaplexLabelPlacementProperties();
maplexLabelPlacementProperties.PointPlacementMethod = MaplexPointPlacementMethod.CenteredOnPoint;
maplexLabelPlacementProperties.CanRemoveOverlappingLabel = false;
maplexLabelPlacementProperties.CanTruncateLabel = false;
maplexLabelPlacementProperties.EnablePointPlacementPriorities = false;
labelClass.SetMaplexLabelPlacementProperties(maplexLabelPlacementProperties);

So I explicitly set MaplexPointPlacementMethod.CenteredOnPoint. But placement is not applied. If I go to Label Classes dock pane, I will see Best position in placement combobox.

Is it possible or am I doing something wrong?

Tags (2)
0 Kudos
2 Replies
NarelleChedzey
Esri Contributor

Max, 

I am unable to duplicate this problem, in either ArcGIS Pro 2.3 Final or a pre-ArcGIS Pro 2.4 build.   Here is my code (very similar to yours)

protected override void OnClick()
{
   var layer = MapView.Active.Map.Layers.FirstOrDefault() as FeatureLayer;
   if (layer == null) return;

   var labelClass = layer.LabelClasses.FirstOrDefault(l => l.Name == "My label");
   if (labelClass == null) return;

   QueuedTask.Run(() =>
   {
      var maplexLabelPlacementProperties = labelClass.GetMaplexLabelPlacementProperties() ?? new                         CIMMaplexLabelPlacementProperties();
      maplexLabelPlacementProperties.PointPlacementMethod = MaplexPointPlacementMethod.CenteredOnPoint;
      maplexLabelPlacementProperties.CanRemoveOverlappingLabel = false;
      maplexLabelPlacementProperties.CanTruncateLabel = false;
      maplexLabelPlacementProperties.EnablePointPlacementPriorities = false;
      labelClass.SetMaplexLabelPlacementProperties(maplexLabelPlacementProperties);
   });

}

My label class successfully changes from Best Position to Centered on Point. 

Do you perhaps have multiple label classes on this layer and might be showing one of those in the label properties dialog?

Narelle

0 Kudos
MaxMax2
Frequent Contributor

Hi Narelle,

It seems I found the root cause of the bug. My full workflow is: create layer, change name of label class, set its properties. In your example you get already added layer (after some time after adding, I suppose) and change label class.

If I insert thread sleep for small amount of time before changing placement, placement will be changed as expected. I believe the problem is related to https://community.esri.com/thread/234941-featurelayeraddlabelclass-ignores-passed-class-name.

My workaround at now is:

layer.LabelClasses.ToList().ForEach(lc => layer.RemoveLabelClass(lc.Name));
layer.AddLabelClass(LabelClassName);
SpinWait.SpinUntil(() => layer.LabelClasses.Any(lc => lc.Name == LabelClassName));

After that I am able to change placement properties.

Thanks

0 Kudos