English

How To: Convert CAD annotation to a point shapefile

Summary

This article provides a script for converting CAD annotation features to a point shapefile. The new shapefile will contain a field called CADTEXT which contains the text string of the original annotation features.

Procedure

  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the new script window.

    Code:
    'This script converts the selected text from the active
    'theme into point shapes with the text as attributes.
    'If no text is found an error message is returned.

    'First make sure the active theme is an annotation theme
    theView = av.GetActiveDoc
    theTextTheme=av.getactivedoc.getactivethemes.get(0)
    if (theTextTheme.getsrcname.getsubname <> "annotation") then
    msgbox.warning("The active theme is not an annotation theme!","")
    exit
    end

    'Get the number of selected text guys
    theTextTable=theTextTheme.GetFtab
    theOrigBitmap=theTextTheme.GetFtab.GetLastSelection

    'Check if there is a selection set
    if (theTextTable.GetSelection.Count = 0) then
    doall=true else doall=false
    end

    'If there is, grab all of the text in the theme
    if (doall) then
    theTextTable.query("([Entity]=""Text"")",
    theTextTable.GetSelection,#VTAB_SELTYPE_NEW)
    if (theTextTable.GetSelection.Count = 0) then
    msgbox.info ("Sorry, no text features in the theme!","")
    exit
    end
    'If there is a selected set find all the text in it
    else
    theTextTable.query("([Entity]=""Text"")",
    theTextTable.GetSelection,#VTAB_SELTYPE_AND)
    if (theTextTable.GetSelection.Count = 0) then
    msgbox.info ("Sorry, no text features in selected set!","")
    exit
    end
    end

    'Find and report the total number of text entities to be converted
    theNumRec=theTextTable.GetSelection.Count
    theanswer=msgbox.yesno(theNumRec.asstring++"elements to convert."
    +nl+"(Some may have been block attributes!)"
    +nl+"Label new point theme and turn of annotation?", "",true)

    'Make a new shape theme
    type ="Point"
    class = Point
    'Make sure to get a new name for it

    'This is where we write all the extra needed files:
    themepath=thetexttheme.getsrcname.getfilename.returndir

    'Get a new file name, but make a suggestions of thetexttheme.shp
    newfilename=themepath.maketmp(theTextTheme.asstring,"shp")
    shapethemename=FileDialog.Put (newfilename, "shp", "Save")

    if (shapethemename = nil ) then
    exit
    end

    def=shapethemename
    if (def <> nil) then
    tbl = FTab.MakeNew(def, class)
    'fld = Field.Make("ID", #FIELD_DECIMAL, 8, 0)
    'fld.SetVisible( FALSE )
    'tbl.AddFields({fld})
    theTheme = FTheme.Make(tbl)
    theView.AddTheme(theTheme)
    theTheme.SetActive(TRUE)
    theTheme.SetVisible(TRUE)
    theView.SetEditableTheme(theTheme)
    av.GetProject.SetModified(true)
    end

    'Add the TEXT field to the new theme
    theme1table=av.getactivedoc.findtheme(def.getbasename).getftab
    thenewfield=field.make("CADText", #FIELD_CHAR, 25, 0)
    theme1table.addfields({thenewfield})

    'Walk through each record and pull out the text value and
    'the shape. Stuff each into the new theme.
    for each n in theTextTable.GetSelection
    'msgbox.info(n.asstring,"N")
    theTextValue=theTextTable.returnvalue(
    theTextTable.findfield("Text"),n)
    theShapeValue=theTextTable.returnvalue(
    theTextTable.findfield("Shape"),n)
    theShapeCoors=theShapeValue.AsLine.ReturnStart
    thePointTable=av.getactivedoc.findtheme(DEF.getbasename).getftab
    theNewPoint=point.make(theShapeCoors.getx,theshapecoors.gety)
    theRecordNum=thePointTable.addrecord
    thePointTable.setvalue(thepointTable.findfield("CADTEXT"),
    theRecordNum,theTextvalue)
    thePointTable.setvalue(thepointTable.findfield("Shape"),
    theRecordNum, theNewPoint)
    end

    if (theanswer) then
    'thetheme
    theview=av.getactivedoc
    thetexttheme.setactive(false)
    thetheme.setlabelfield(thetheme.getftab.findfield("CADtext"))
    theview.labelthemes(false)
    thetexttheme.setvisible(false)
    'thetexttheme.getgraphics.delete

    end

    'Turn the pointtable editing off and update the
    'bitmap to the one we started with
    thepointtable.seteditable(false)
    theTheme.setactive(false)
    thetexttheme.setactive(true)
    theTextTable.Setselection(TheOrigBitmap.clone)
    theTextTable.UpdateSelection

  3. Click the compile button.
    [O-Image] Script compile button
  4. Add a CAD annotation theme to the view.
  5. Make the Script window active.
  6. Click the run button.
    [O-Image] Run compiled script button