English

How To: Convert labels to a point shapefile that can be used in another view, project, or application

Summary

The following script converts labels in a view to a new point shapefile. You can convert all labels in the view, or just the labels associated with active themes.

The output point shapefile will contain points representing the lower left corner of each label's bounding box. A text field, populated with the label values, gets added to the shapefile's attribute table.

Procedure

1. Copy and paste the following code into a new script window.

2. Run the script directly from the script editor document, making sure the desired views are active.

You may associate this new script to and run it from a button on your view's GUI.

A. Compile the script.
B. Switch to the Project window.
C. Select Customize from the Project menu.
D. On the Customize dialog box, click the Type dropdown arrow and click View.
E. Select Buttons under Category.
F. Click the New button.
G. Double-click the Click property in the Customize dialog box.
H. Type the name of the script in the Script Manager and click Select.
I. Close the Customize dialog box.

For more information, see "Customize dialog box" in ArcView Help.
Code:
v = av.GetActiveDoc
tl = v.getactivethemes
gl = list.make

' check to see which labels to convert
c = msgbox.listasstring({"Active theme(s) only","Entire view"},
  "Convert labels associated with:","Label to point conversion")
if (c = nil) then return nil end

if (c = "Active theme(s) only") then
  if (tl.count = 0) then
    msgbox.info("At least one theme must be active","Stop")
    return nil
  end  
  for each t in tl
    gl.merge(t.GetGraphics)
  end
else
  gl.merge(v.GetGraphics)
end

' Create output shapefile on disk
fnDefault = FileName.Make("$HOME").MakeTmp("label","shp")
fnOutput  = FileDialog.Put( fnDefault,"*.shp","Output Shape File" )
if (fnOutput = nil) then exit end
fnOutput.SetExtension("shp")
ftbOutput = FTab.MakeNew( fnOutput, POINT )
ftbOutput.AddFields({Field.Make("ID",#FIELD_LONG,8,0),
  Field.Make("Text",#FIELD_CHAR,60,0)})

' loop through each graphic in the view's graphiclist
for each g in gl

  ' if its a graphictext, then add a point to the new shapefile 
  ' and populate fields
  if (g.is(GraphicText)) then 
    newrec = ftbOutput.AddRecord
    p = g.GetBounds.ReturnOrigin
    ftbOutput.SetValue(ftbOutput.FindField("Shape"), newrec, p)
    ftbOutput.SetValue(ftbOutput.FindField("Text"), newrec, 
      g.GetText)  
  end
end

ftbOutput.SetEditable(FALSE)
nt = FTheme.Make(ftbOutput)
v.AddTheme(nt)

Related Information