Small script to select objects by their attribute.

Udo Bürgel (buergel@goofy.zdv.Uni-Mainz.de)
Wed, 11 Oct 1995 13:41:17 +0100

Hi,

I wrote a small script to select objects by their attribute. It does the following:

- a list of all the attributes appearing in the current map is generated. (In
proc GetAttList)

- a submenu to "Select by attribute" contains this list, so the user can choose
the name of the attribute by which the object should be selected.

- after choosing the name of the attribute a request window similar to
"Select by name" submenu pops up and all the objects with the
corresponding value of the selected attribute are selected.

I think it would be good if a similar "Select by attribute" submenu would
appear in the tkined-"Select"-menu. It would be quite easy to adapt the
script to the tkined sources.

Any comments???

Udo

###########################################################
Here is the script:

#! /usr/local/bin/scotty -inf
#########################################################################

#This proc returns a list containing the ID's of all the objects of any type
#mentioned in typelist
proc getids { {typelist {REFERENCE NETWORK NODE TEXT GROUP IMAGE MENU \
INTERPRETER LOG STRIPCHART BARCHART GRAPH}} } {
foreach object [ined retrieve] {
if {[lsearch $typelist [lindex $object 0]] != -1} {
lappend idlist [lindex $object 1]
}
}
return $idlist
}

#This is the main proc to select the objects with attribute attname set to
# a value entered in a request window.
proc SelbyAtt { attname } {

static regex type
UpdateMenu
if {![info exists regex]} { set regex "" }
if {![info exists type]} { set type "glob" }

set result [ined request "Select objects by attribute $attname" \
[list [list Expression: $regex entry 20] \
[list Type: $type radio regexp glob] ] \
[list select cancel] ]

set regex [lindex $result 1]
set type [lindex $result 2]

if {[lindex $result 0] == "cancel"} return

# test if the regex is valid
if {[catch {lsearch -$type foobar $regex} res]} {
ined acknowledge \
"$regex is not a valid $type expression!"
return
}

foreach id [getids] {
set att [ined attribute $id $attname]
if {[lsearch -$type $att $regex] >= 0} {
ined select $id
}
}
}

proc "Delete uSelect" { list } {
global menus
catch {
foreach id $menus { ined delete $id }
}
exit
}

proc "Delete_menus" {menus} {
catch {
foreach id $menus { ined delete $id }
}
}

#This proc returns a list with the name of all the attributes appearing
#in the current tki-map
proc GetAttList {} {
set attlist ""
foreach id [getids] {
foreach att [ined attribute $id] {
if {[lsearch $attlist $att] == -1} {
lappend attlist $att
}
}
}
return $attlist
}

#this proc creates the menus
proc UpdateMenu {} {
global menus
if {[info exists menus]} {
Delete_menus $menus
}
foreach att [GetAttList] {
set procname "+$att"
proc $procname { args } [list SelbyAtt $att]
lappend cmds "Select by Attribute:$procname"
}
set cmds [concat $cmds [list "" "UpdateMenu" "Delete uSelect"]]
set menus [eval ined create MENU uSelect $cmds]
}

UpdateMenu

#######################################################