English

How To: Mimic the functionality of the Auto-Label dialog box via Avenue's Labeler class

Summary

This document describes one of two ways to mimic the functionality of the Auto-Label dialog box via Avenue's Labeler class. The other alternative is to use the TextPositioner Class, which is covered by the document in the Related Information section.

Procedure

The Labeler finds the best location for placement of graphic text elements. When a theme is loaded into the labeler, the geometry of each feature that can be labeled, and the graphic text that will be used to label that feature are passed to the labeler. The labeler also keeps track of each graphicText element, once it has found a good location for it, so that other graphicText labels will not be placed in an overlapping position. A label will not be placed if features or other labels are nearby. See the online help for more information on the Labeler class.

  • Example: Using the Labeler class to label a theme on a single field.

    Code:
    aView = av.GetActiveDoc

    ‘-- Theme to label must be the ACTIVE theme

    aTheme = aView.GetActiveThemes.Get(0)
    aExt = aView.GetDisplay.ReturnVisExtent
    aLabeler = Labeler.Make(aExt)

    '-- These weights are the defaults for a line theme.

    aLabeler.SetFeatureWeight(#LABEL_WEIGHT_NO)
    aLabeler.SetLabelWeight(#LABEL_WEIGHT_HIGH)

    aLabeler.RemoveDuplicates(true)

    '-- Set the textsymbol for the theme you are labeling

    aTextSym = TextSymbol.Make
    aTextSym.SetFont(Font.Make("Times","Bold"))
    aTextSym.SetSize(8)
    aTheme.SetLabelTextSym(aTextSym)

    '-- This starts the labeling process and corresponds to
    '-- the first status bar you see on the application window.

    aLabeler.Load(aTheme)

    '-- Get the Labels from the labeler (corresponds to
    '-- the second status bar you see on the application window)
    '-- and draw them on the view.
    '-- The GetAutoLabels request automatically adds
    '-- the new labels to the aView's GraphicList
    '-- and aTheme's GraphicSet.

    aView.GetAutoLabels(aLabeler, false)

  • Example: Using the Labeler class to label a single theme on multiple fields
    (modified from example in help that auto-labels multiple themes).

    Load the theme in the view multiple times (once for each field you are labeling) and give each theme a unique name.

    Code:
    ‘Example code below

    theView = av.GetActiveDoc
    theGraphics=theView.GetGraphics
    States1 = theView.FindTheme("States1")
    States2 = theView.FindTheme("States2")
    States3 = theView.FindTheme("States3")

    States1.SetActive(true)
    States2.SetActive(true)
    States3.SetActive(true)

    ‘-- Set the textSymbol of all the themes to be labeled,
    '-- it needs to be large enough to be useful for all
    '-- themes, since that textSymbol is initially used
    '-- for all themes. The reason you need to do this is that
    '-- there is a bug with setting the textSymbol for any
    ‘-- other theme to be different that causes ArcView to crash.

    aTextSym = TextSymbol.Make

    aTextSym.SetFont(Font.Make("Arial","Bold"))
    aTextSym.SetSize(12)

    States1.SetLabelTextSym(aTextSym)
    States2.SetLabelTextSym(aTextSym)
    States3.SetLabelTextSym(aTextSym)

    ‘-- Make the labeler object and set the extent that is to
    '-- be labeled. This example uses the entire view's extent.
    '-- You might want to use theView.GetDisplay.ReturnVisExtent
    ‘-- to label just the visible extent of the view.

    anExt = theView.ReturnExtent
    aLabeler = Labeler.Make(anExt)

    ‘-- As each of the themes are loaded into the labeler object,
    '-- their feature and label weights may need to be set in
    '-- order to create the most appropriate result.

    aLabeler.SetFeatureWeight(#LABEL_WEIGHT_HIGH)
    aLabeler.SetLabelWeight(#LABEL_WEIGHT_HIGH)

    aLabeler.RemoveDuplicates(true)

    aLabeler.Load(States1)
    aLabeler.Load(States2)
    aLabeler.Load(States3)

    theView.GetAutoLabels(aLabeler, false)

    ‘-- Due to a known issue, the textSymbols for each theme are not
    '-- honored during the label placement process, so here is
    '-- a the workaround to this. Note, that the size of labels
    ‘-- are not accounted for here, so be careful about setting
    '-- a theme's labels to be a larger size as conflicts may rise.

    aTextSym2 = TextSymbol.Make
    aTextSym2.SetFont(Font.Make("Arial","Normal"))
    aTextSym2.SetSize(10)
    theView.GetDisplay.HookUpSymbol(aTextSym2)
    States1.SetLabelTextSym(aTextSym2)

    thedeleteList = {}
    theGraphics.UnSelectAll
    for each sLabel in States1.GetGraphics

    thedeleteList.Add(sLabel.clone)
    sLabel.SetSelected(true)
    end
    theGraphics.ClearSelected

    aStateGTS = {}
    for each oldGL in thedeleteList
    aString = oldGL.GetText
    aPoint = oldGL.GetBounds.ReturnOrigin
    anAngle = oldGL.GetAngle
    newGL = GraphicLabel.MakeWithSym ( aString, aPoint, aTextSym2, aStateGTS )
    newGL.SetAngle(anAngle)
    theGraphics.addbatch(newGL)
    end

    aTextSym3 = TextSymbol.Make
    aTextSym3.SetFont(Font.Make("Arial","Italic"))
    aTextSym3.SetSize(10)

    aTextSym3.SetColor(Color.GetBlue)
    theView.GetDisplay.HookUpSymbol(aTextSym3)

    theDeleteList = {}
    theGraphics.UnSelectAll
    for each s2Label in States3.GetGraphics
    theDeleteList.Add(s2Label.Clone)
    s2Label.SetSelected(true)
    end
    for each s3Label in States2.GetGraphics
    theDeleteList.Add(s3Label.clone)
    s3Label.SetSelected(true)
    end
    theGraphics.ClearSelected

    aState2GTS = {}
    for each oldGL in thedeleteList
    aString = oldGL.GetText
    aPoint = oldGL.GetBounds.ReturnOrigin

    anAngle = oldGL.GetAngle
    newGL = GraphicLabel.MakeWithSym ( aString, aPoint, aTextSym3, aState2GTS )
    newGL.SetAngle(anAngle)
    theGraphics.AddBatch(newGL)
    end

    theGraphics.EndBatch

  • Example: Using Labeler on multiple themes.

    You can use the same Labeler object to label one or more themes as long as they are in the same view. See the following help topic for an example of using the Labeler class to label multiple themes: Help Topics > Index tab > labeling - Avenue Examples > Auto-label multiple themes simultaneously.

Related Information