I've added a shapefile to an MXD and Edited the symbolization of it with ArcObjects. Now I'm trying to edit the labeling with ArcObjects. I've successfully been able to edit the default class in the IAnnotateLayerPropertiesCollection2 object. I've also successfully added two more label classes to that collection object.
The default class that I edited is displaying correctly, but the extra two Label Classes aren't displaying after the script is run. When I manually open the properties of the layer and open the properties for each label class that I added and choose OK or Apply, then they show up. So I know that they are being added properly, but how do I get them to display through the script?
I've tried MxDoc.UpdateContets(), MxDoc.ActiveView.Refresh(), arcpy.RefreshActiveView() and none of them work for this.
I'm using Python to handle the ArcObjects, but if you know how to do it in VB or C#, I can interpolate it.
def ApplyLabels_arcobjects(layersource, pFLayer, pStyleGallery, carto):
# If there are no labels in the custom symbology object
if not layersource.symbology.labels:
return
# Get Annotation Properties Collection
geofeaturelayer = Snippets.CType(pFLayer, carto.IGeoFeatureLayer)
pAnnoPropColl = geofeaturelayer.AnnotationProperties
pAnnoPropColl2 = Snippets.CType(pAnnoPropColl, carto.IAnnotateLayerPropertiesCollection2)
# Loop through each label in the custom symbology object
i = 0
for labelsource in layersource.symbology.labels:
print("This is label number {0}".format(i+1)) # TODO Remove, for debugging
# Get the premade Label Style from the style file
pStyleGallery.LoadStyle(labelsource.style_filename, labelsource.label_classname)
gallerystyle = getLabelSymbol(labelsource, pStyleGallery, carto)
pLabelStyle = Snippets.CType(gallerystyle, carto.ILabelStyle2)
# If I can get a Annotation Property that is already there, great:
if i <= pAnnoPropColl2.Count - 1:
print("Editing the one already there") # TODO Remove, for debugging
pAnnoLayerProp = pAnnoPropColl2.QueryItem(i)[0]
# Class Name
pAnnoLayerProp.Class = labelsource.name
# Sql string for Where expression
pAnnoLayerProp.WhereClause = labelsource.sql_string
# Set Sympol and Placement Properties
pLabelEngineProp = Snippets.CType(pAnnoLayerProp, carto.ILabelEngineLayerProperties2)
pLabelEngineProp.Symbol = pLabelStyle.Symbol
pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties
# If I have to add an Annotation Property to the Collection, then:
else:
print("Adding new Label Engine")# TODO Remove, for debugging
# New Label Engine
pLabelEngineProp = Snippets.NewObj(carto.MaplexLabelEngineLayerProperties, carto.ILabelEngineLayerProperties2)
# Apply Symbol
pLabelEngineProp.Symbol = pLabelStyle.Symbol
# Apply Placement Properties
pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties
# Cast
pAnnoLayerProp = Snippets.CType(pLabelEngineProp, carto.IAnnotateLayerProperties)
# Class Name
pAnnoLayerProp.Class = labelsource.name
# Sql string for Where expression
pAnnoLayerProp.WhereClause = labelsource.sql_string
# Display Annotation
pAnnoLayerProp.DisplayAnnotation = True
# Add it to the collection
pAnnoPropColl2.Add(pAnnoLayerProp)
# Turn on Annotation for the Map Layer
geofeaturelayer.DisplayAnnotation = True
i += 1
3 Answers 3
AnnotationProperties is a get/put property meaning a reference is not handed out. You need to set the collection back into the FeatureLayer after modifying it.
I've not done much with labelling with ArcObjects so this may not be relevant or you have tried it already? Have you tried setting IAnnotateLayerProperties.DisplayAnnotation Property?
-
Yes I have. I'll add my code too.TSJ– TSJ2014年10月20日 16:40:22 +00:00Commented Oct 20, 2014 at 16:40
-
I'm not familiar with Python but what I've done with ArcObject_C# is something like this:Write a class which implements IExtension interface,in this class use a varible for IActiveView and assign it properly.Then cast it to IActiveViewEvents_Event interface.Now you can overload the AfterDraw event or ItemAddes,ItemDeleted.In that overloaded event you can write your code to refresh map view.Reza– Reza2015年12月25日 07:26:16 +00:00Commented Dec 25, 2015 at 7:26
-
You should make this an answer. I can't test it anymore but it sounds like it might work.TSJ– TSJ2016年04月01日 12:20:55 +00:00Commented Apr 1, 2016 at 12:20
Try this ELSE statement:
else:
print("Adding new Label Engine")# TODO Remove, for debugging
# New Label Engine
pLabelEngineProp = Snippets.NewObj(carto.MaplexLabelEngineLayerProperties, carto.ILabelEngineLayerProperties2)
# Apply Symbol
pLabelEngineProp.Symbol = pLabelStyle.Symbol
# Apply Placement Properties
pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties
# Cast
pAnnoLayerProp = Snippets.CType(pLabelEngineProp, carto.IAnnotateLayerProperties)
# Class Name
pAnnoLayerProp.Class = labelsource.name
# Sql string for Where expression
pAnnoLayerProp.WhereClause = labelsource.sql_string
((ILabelEngineLayerProperties2)pAnnoLayerProp).Expression = labelExpression;
((ILabelEngineLayerProperties2)pAnnoLayerProp).Symbol = pLabelStyle.Symbol;
((ILabelEngineLayerProperties2)pAnnoLayerProp).OverposterLayerProperties = pLabelStyle.OverposterLayerProperties;
# Display Annotation
pAnnoLayerProp.DisplayAnnotation = True
# Clear existing labelling classes
pAnnoPropColl2.Clear()
# Add it to the collection
pAnnoPropColl2.Add(pAnnoLayerProp)
# Turn on Annotation for the Map Layer
geofeaturelayer.DisplayAnnotation = True
Explore related questions
See similar questions with these tags.