Public Sub ConvertLabels2Anno()
''Interface Pointers necessary for accessing basic information about the map
Dim pMxDoc As IMxDocument
Dim pApp As IApplication
Dim pMap As IMap
Dim pAView As IActiveView
''Interface Pointers necessary for getting information about the layer being labeled
Dim pLayer As ILayer
Dim pDataset As IDataset
Dim pAnnotationLayer As IAnnotationLayer
Dim pGeoFeatureLayer As IGeoFeatureLayer
Dim pFClass As IFeatureClass
''Interface Pointers necessary for setting up the labeling properties for conversion
Dim pAnnotateLayerPropertiesCollection As IAnnotateLayerPropertiesCollection
Dim pMapAnnoPropsColl As IAnnotateLayerPropertiesCollection
Dim pAnnotateLayerProperties As IAnnotateLayerProperties
Dim pLabelEngineLayerProperties As ILabelEngineLayerProperties2
Dim pOverposterLayerProperties As IOverposterLayerProperties2
Dim pClone As IClone
Dim propsIndex As Long
''Interface Pointers necessary for creating the Map Annotation Group
Dim pCompositeGraphicsLayer As ICompositeGraphicsLayer
Dim pRefScale As IGraphicsLayerScale
Dim pGraphicsLayer As IGraphicsLayer
''Interface Pointers necessary for performing labeling
Dim pScreenDisplay As IScreenDisplay
Dim pAnnotateMapProps As IAnnotateMapProperties
Dim pAnnotateMap2 As IAnnotateMap2
Dim pTrackCancel As ITrackCancel
Dim pMapOverposter As IMapOverposter
Dim pOverposterProperties As IOverposterProperties
Dim pOverflowGraphicsContainer As IOverflowGraphicsContainer
Dim pElement As IElement
Dim pUnplacedElements As IElementCollection
Dim lngUnplacedIndex As Long
Dim lngUnplacedCount As Long
''setup the document, map, and get the first layer
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
Set pLayer = pMap.Layer(0)
''check to make sure map is valid
If pMap Is Nothing Then
MsgBox There is not an active map, vbCritical
Exit Sub
End If
''see if the first layer is the proper type
If TypeOf pLayer Is IGeoFeatureLayer Then
Set pGeoFeatureLayer = pLayer
Else
''throw an error if the first layer is not a GeoFeatureLayer because only layers implementing this interface can be labeled
MsgBox First layer in map must be feature layer, vbCritical
End If
Set pMapAnnoPropsColl = New AnnotateLayerPropertiesCollection ''a new properties collection which will be populated
''loop through the properties collection of the layer
''for each item, copy it to the new properties collection,
''pull the symbol out for the SymbolCollection, and setup the ID
Set pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties
For propsIndex = 0 To (pAnnotateLayerPropertiesCollection.Count - 1)
pAnnotateLayerPropertiesCollection.QueryItem propsIndex, pAnnotateLayerProperties
If Not pAnnotateLayerProperties Is Nothing Then
''Clone the properties and add them to the new collection
Set pClone = pAnnotateLayerProperties
pMapAnnoPropsColl.Add pClone.Clone
End If
Next propsIndex
''clear the pointer for later use in the sub
Set pAnnotateLayerProperties = Nothing
''get the overposter (label engine) properties from the map
Set pMapOverposter = pMap
Set pOverposterProperties = pMapOverposter.OverposterProperties
''Get the maps basic graphics layer and add a new layer to it
''We''ll put the labels into this new Annotation Group
Set pCompositeGraphicsLayer = pMap.BasicGraphicsLayer
Set pGraphicsLayer = pCompositeGraphicsLayer.AddLayer(ANNO_GROUP_NAME, pGeoFeatureLayer)
''setup the scale on the annotation group
Set pRefScale = pGraphicsLayer
If pMap.ReferenceScale = 0 Then
pRefScale.ReferenceScale = pMap.MapScale
Else
pRefScale.ReferenceScale = pMap.ReferenceScale
End If
''activate the graphics container for adding elements.
Set pAView = pMap
Set pScreenDisplay = pAView.ScreenDisplay
pGraphicsLayer.Activate pScreenDisplay
''Prepare the annotation properties for label placement
For propsIndex = 0 To (pMapAnnoPropsColl.Count - 1)
pMapAnnoPropsColl.QueryItem propsIndex, pAnnotateLayerProperties ''get the properties from the collection
If Not pAnnotateLayerProperties Is Nothing Then
Set pAnnotateLayerProperties.FeatureLayer = pGeoFeatureLayer ''point the properties to the feature layer
Set pAnnotateLayerProperties.GraphicsContainer = pGraphicsLayer ''set the AnnoLayer as the destination for the labels
pAnnotateLayerProperties.AddUnplacedToGraphicsContainer = False ''we do not want to add unplaced elements to the annotation group
pAnnotateLayerProperties.CreateUnplacedElements = True ''ALWAYS create unplaced elements
pAnnotateLayerProperties.DisplayAnnotation = True ''turn on the label class if it isn''t already
pAnnotateLayerProperties.FeatureLinked = False ''This sample creates map annotation, so set this to false
pAnnotateLayerProperties.LabelWhichFeatures = esriAllFeatures ''this creates labels/anno for the full extent. This can be changed to produce labels for the current extent, selection etc
pAnnotateLayerProperties.UseOutput = True '' yes, we want to produce elements
Set pLabelEngineLayerProperties = pAnnotateLayerProperties ''QI to LabelEngineLayerProperties
Set pOverposterLayerProperties = pLabelEngineLayerProperties.OverposterLayerProperties ''Get the overposter layer properties from the LabelEngineLayerProps
pOverposterLayerProperties.TagUnplaced = True ''add unplaced labels as unplaced (true) or placed (false)
End If
Next propsIndex
''sort the collection so labels are placed in the proper order
pMapAnnoPropsColl.Sort
''populate AnnotateMapProperties with the prepared collection
Set pAnnotateMapProps = New AnnotateMapProperties
Set pAnnotateMapProps.AnnotateLayerPropertiesCollection = pMapAnnoPropsColl
Set pTrackCancel = New CancelTracker ''cocreat a cancel tracker
''get the current AnnotateMap object from the map
''this ensures we are using the proper label engine
Set pAnnotateMap2 = pMap.AnnotationEngine
''Now, call Label which will populate the annotation group with labels based on the properties we setup
''The Label method know to put the labels in the annotation group because we specify it as the destination
''as the destination GraphicsContainer in the above preparation loop
pAnnotateMap2.Label pOverposterProperties, pAnnotateMapProps, pMap, pTrackCancel
''Handle unplaced elements and finish off the process
For propsIndex = 0 To (pMapAnnoPropsColl.Count - 1)
pMapAnnoPropsColl.QueryItem propsIndex, pAnnotateLayerProperties, Nothing, pUnplacedElements
If Not pAnnotateLayerProperties Is Nothing Then
''Get a reference to the graphics container and add the unplaced elements as unplaced
lngUnplacedCount = lngUnplacedCount + pUnplacedElements.Count
If pUnplacedElements.Count > 0 Then
Set pOverflowGraphicsContainer = pGraphicsLayer
For lngUnplacedIndex = 0 To (pUnplacedElements.Count - 1)
pUnplacedElements.QueryItem lngUnplacedIndex, pElement
''add the unplaced elements as unplaced using the IOverflowGraphicsContainer interface
pOverflowGraphicsContainer.AddOverflowElement pElement
Next lngUnplacedIndex
End If
''release the feature layer reference in the properties collection to be safe
''in some cases, not doing this would lead to a circular reference
Set pAnnotateLayerProperties.FeatureLayer = Nothing
End If
Next propsIndex
''turn off labeling of feature layer
pGeoFeatureLayer.DisplayAnnotation = False
''refresh the map
Set pAView = pMap
''fire contents changed to fill the Overflow Annotation window
pAView.ContentsChanged
''Refresh the map
pAView.Refresh
''if we have unplaced labels, show the dialog
Set pApp = Application ''get a refrence to the application to show the overflow dialog
If lngUnplacedCount > 0 Then
pApp.ShowDialog esriMxDlgOverflowLabels, True
End If
1 个回复
刘峥 - ArcGIS多面手
赞同来自:
获取unplace label必须通过把label转成annotation才可以,以下是测试代码:
要回复问题请先登录或注册