# $Revision$ on $Date$
# The above revision line needs to be the first line for all techtk files
# Copyright (c) 2006 Ansys Inc

namespace eval ::AIEnv::GUIManagement {}

namespace eval ::AIEnv::GUIManagement::Picker {
	variable Commands
	variable Options
	variable PickerData
	variable SelectFunctionMap
	variable TypeNameToObjectClassMap

	set Commands {
		set
		get
		pick
		showItems
		cancelpick
	}
	set Options {
		variable
		type
		requireExactNumber
		maxNumber
		parent
	    selectOptions
	    selectMessage
	    editorFinish
		autoApply
	}

	set SelectionTypes {
		pointorxyz
		vid_param
	    geometry
		point
		curve
	    curve_or_compcurve
		surf
		body
        density
		point_0d_el
		curve_1d_el
		surf_2d_el
		body_3d_el
		1d_el
		2d_el
		3d_el
		node
		elem
		endpoint
		edge
	    any_entity
	    subset
	    part
	    part_0d
	    part_1d
	    part_0d_1d
	    part_0d_1d_2d
	    part_2d
	    part_3d
	    part_prism
	    part_volume
	    subpart
	    hex_compcurve
	    hex_vertex
	    hex_edge
	    hex_face
	    hex_block
	    str_domain
	    str_subface
	    str_edge
	    str_vertex
	    str_node
	}
	set SelectFunctionMap(pointorxyz) ::AIEnv::GUIManagement::Picker::SelectPointOrXYZ
	set SelectFunctionMap(vid_param) ::AIEnv::GUIManagement::Picker::SelectVIDParam
	set SelectFunctionMap(geometry) ::AIEnv::GUIManagement::Picker::SelectAnyGeometry
	set SelectFunctionMap(point) ::AIEnv::GUIManagement::Picker::SelectPoint
	set SelectFunctionMap(curve) ::AIEnv::GUIManagement::Picker::SelectCurve
	set SelectFunctionMap(curve_or_compcurve) ::AIEnv::GUIManagement::Picker::SelectCurveOrCompcurve
	set SelectFunctionMap(surf) ::AIEnv::GUIManagement::Picker::SelectSurf
	set SelectFunctionMap(body) ::AIEnv::GUIManagement::Picker::SelectBody
	set SelectFunctionMap(density) ::AIEnv::GUIManagement::Picker::SelectDensity
	set SelectFunctionMap(point_0d_el) ::AIEnv::GUIManagement::Picker::SelectPoint0dEl
	set SelectFunctionMap(curve_1d_el) ::AIEnv::GUIManagement::Picker::SelectCurve1dEl
	set SelectFunctionMap(surf_2d_el) ::AIEnv::GUIManagement::Picker::SelectSurf2dEl
	set SelectFunctionMap(body_3d_el) ::AIEnv::GUIManagement::Picker::SelectBody3dEl
	set SelectFunctionMap(1d_el) ::AIEnv::GUIManagement::Picker::Select1dEl
	set SelectFunctionMap(2d_el) ::AIEnv::GUIManagement::Picker::Select2dEl
	set SelectFunctionMap(3d_el) ::AIEnv::GUIManagement::Picker::Select3dEl
	set SelectFunctionMap(node) ::AIEnv::GUIManagement::Picker::SelectNode
	set SelectFunctionMap(elem) ::AIEnv::GUIManagement::Picker::SelectElem
	set SelectFunctionMap(endpoint) ::AIEnv::GUIManagement::Picker::SelectEndpoint
	set SelectFunctionMap(edge) ::AIEnv::GUIManagement::Picker::SelectEdge
    set SelectFunctionMap(any_entity) ::AIEnv::GUIManagement::Picker::SelectAnyEntity
    set SelectFunctionMap(subset) ::AIEnv::GUIManagement::Picker::SelectSubset
    set SelectFunctionMap(part) ::AIEnv::GUIManagement::Picker::SelectPart
    set SelectFunctionMap(part_0d) ::AIEnv::GUIManagement::Picker::SelectPart0d
    set SelectFunctionMap(part_1d) ::AIEnv::GUIManagement::Picker::SelectPart1d
    set SelectFunctionMap(part_0d_1d) ::AIEnv::GUIManagement::Picker::SelectPart0d1d
    set SelectFunctionMap(part_0d_1d_2d) ::AIEnv::GUIManagement::Picker::SelectPart0d1d2d
    set SelectFunctionMap(part_2d) ::AIEnv::GUIManagement::Picker::SelectPart2d
    set SelectFunctionMap(part_3d) ::AIEnv::GUIManagement::Picker::SelectPart3d
    set SelectFunctionMap(part_prism) ::AIEnv::GUIManagement::Picker::SelectPartPrism
    set SelectFunctionMap(part_volume) ::AIEnv::GUIManagement::Picker::SelectPartVolume
    set SelectFunctionMap(hex_compcurve) ::AIEnv::GUIManagement::Picker::SelectHexCompcurve
    set SelectFunctionMap(hex_vertex) ::AIEnv::GUIManagement::Picker::SelectHexVertex
    set SelectFunctionMap(hex_edge) ::AIEnv::GUIManagement::Picker::SelectHexEdge
    set SelectFunctionMap(hex_face) ::AIEnv::GUIManagement::Picker::SelectHexFace
    set SelectFunctionMap(hex_block) ::AIEnv::GUIManagement::Picker::SelectHexBlock
    set SelectFunctionMap(str_domain) ::AIEnv::GUIManagement::Picker::SelectStrDomain
    set SelectFunctionMap(str_subface) ::AIEnv::GUIManagement::Picker::SelectStrSubFace
    set SelectFunctionMap(str_edge) ::AIEnv::GUIManagement::Picker::SelectStrEdge
    set SelectFunctionMap(str_vertex) ::AIEnv::GUIManagement::Picker::SelectStrVertex
    set SelectFunctionMap(str_node) ::AIEnv::GUIManagement::Picker::SelectStrNode
    set SelectFunctionMap(subpart) ::AIEnv::GUIManagement::Picker::SelectSubpart

	set TypeNameToObjectClassMap(pointorxyz) EZCadObject
	set TypeNameToObjectClassMap(geometry) MedObject
	set TypeNameToObjectClassMap(point) MedObject
	set TypeNameToObjectClassMap(curve) MedObject
	set TypeNameToObjectClassMap(curve_or_compcurve) MedObject
	set TypeNameToObjectClassMap(surf) MedObject
	set TypeNameToObjectClassMap(body) MedObject
	set TypeNameToObjectClassMap(density) MedObject
	set TypeNameToObjectClassMap(point_0d_el) MedObject
	set TypeNameToObjectClassMap(curve_1d_el) MedObject
	set TypeNameToObjectClassMap(surf_2d_el) MedObject
	set TypeNameToObjectClassMap(body_3d_el) MedObject
	set TypeNameToObjectClassMap(1d_el) MedObject
	set TypeNameToObjectClassMap(2d_el) MedObject
	set TypeNameToObjectClassMap(3d_el) MedObject
	set TypeNameToObjectClassMap(node) MedObject
	set TypeNameToObjectClassMap(elem) MedObject
	set TypeNameToObjectClassMap(endpoint) MedObject
	set TypeNameToObjectClassMap(edge) MedObject
	set TypeNameToObjectClassMap(any_entity) MedObject
	set TypeNameToObjectClassMap(subset) MedObject
	set TypeNameToObjectClassMap(part) MedObject
	set TypeNameToObjectClassMap(hex_compcurve) MedObject
	set TypeNameToObjectClassMap(hex_vertex) MedObject
	set TypeNameToObjectClassMap(hex_edge) MedObject
	set TypeNameToObjectClassMap(hex_face) MedObject
	set TypeNameToObjectClassMap(hex_block) MedObject
    set TypeNameToObjectClassMap(str_domain) MedObject
    set TypeNameToObjectClassMap(str_subface) MedObject
    set TypeNameToObjectClassMap(str_edge) MedObject
    set TypeNameToObjectClassMap(str_vertex) MedObject
    set TypeNameToObjectClassMap(str_node) MedObject
	set TypeNameToObjectClassMap(subpart) MedObject
}

proc ::AIEnv::GUIManagement::Picker::RouterProc {Name Cmd args} {

	eval [list RouterProcImpl $Name $Cmd] $args
}

proc ::AIEnv::GUIManagement::Picker::RouterProcImpl {Name Cmd args} {
	# Check the command to see if it is valid
	variable Commands
	if { [lsearch $Commands $Cmd] != -1 } {
		return [eval [list ::AIEnv::GUIManagement::Picker::_$Cmd $Name] $args]
	} else {
		set Optlist [join $Commands ", "]
		set Optlist [linsert $Optlist "end-1" "or"]
		error "Unknown command: \"Cmd\" given to picker named: \"$Name\".  Valid commands are: \"$Optlist\"."
	}
}

proc ::AIEnv::GUIManagement::Picker::_set {Name args} {
	eval [list SetImpl $Name] $args
}

proc ::AIEnv::GUIManagement::Picker::SetImpl {Name args} {
	variable Options
	variable PickerData
	variable SelectionTypes
	if { [llength $args] == 3 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		set Value [lindex $args 2]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				if { $Key == "type" } {
					set Value [string tolower $Value]
					if { [lsearch $SelectionTypes $Value] == -1 } {
						set Optlist [join $SelectionTypes ", "]
						set Optlist [linsert $Optlist "end-1" "or"]
						error "Invalid selection type: \"$Value\" given for picker: \"$Name\"\n. Valid selection types are: $Optlist."
					}
				}
				set PickerData($Name,$Key) $Value
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for picker: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for picker: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name set -key key value\""
	}

}


proc ::AIEnv::GUIManagement::Picker::_get {Name args} {

	if {[catch {eval [list GetImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::GetImpl {Name args} {
	variable Options
	variable PickerData
	if { [llength $args] == 2 } {
		set Flag [lindex $args 0]
		set Key [lindex $args 1]
		if { $Flag == "-key" } {
			if { [lsearch $Options $Key] != -1 } {
				return $PickerData($Name,$Key)
			} else {
				set Optlist [join $Options ", "]
				set Optlist [linsert $Optlist "end-1" "or"]
				error "Invalid option: \"$Key\" for picker: \"$Name\". Valid options are $Optlist."
			}
		} else {
			error "Invalid flag: \"$Flag\" for picker: \"$Name\". Must be -key"
		}
	} else {
		error "Wrong number of arguments: Should be \"$Name get -key key\""
	}
}
proc ::AIEnv::GUIManagement::Picker::Highlight {Name color} {
     variable ItemNameMap 
     set node ::AIEnv::GUIManagement::ItemNameMap($Name)
     if [info exists $node] {set node [set $node]}
     set ww [[TreeStack peek] get $node -key WindowPath] 
     foreach w $ww {
        set ew [lindex [split $w -] end]
        if {$ew != "label"} continue
        $w configure -fg $color
        return
      }
 }

proc ::AIEnv::GUIManagement::Picker::_pick {Name args} {
    # 35859: Highlight picker text during pick
    # 103220: Grey out during selection to avoid multiple calls of the picker
    Highlight $Name red
    ::AIEnv::GUIManagement::ToggleGrey $Name 1
    set current [TreeStack peek_one]
    set res [eval [list PickImpl $Name] $args]
    if {$current != [TreeStack peek_one]} {
        aie_reset_picker $current $Name ResetToggleGrey 1
    } else {
        Highlight $Name black
        ::AIEnv::GUIManagement::ToggleGrey $Name 0
    }
    return $res
}

proc ::AIEnv::GUIManagement::Picker::_cancelpick {Name args} {

	if {[catch {eval [list CancelPickImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::CancelPickImpl {Name args} {
	global select_action
	global va_set
	variable PickerData
	variable TypeNameToObjectClassMap
		
	if {[array names PickerData $Name,type] != ""} {
		set Type [string tolower $PickerData($Name,type)]
	} else {
		error "The type was not set for the picker: \"$Name\"."
	}

	set ObjectClass $TypeNameToObjectClassMap($Type)

	switch $ObjectClass {
		MedObject {
			set select_action cancel
		}

		EZCadObject {
			set select_action cancel
		}
	}
	set PickerData($Name-PickCanceled) 1
}

proc ::AIEnv::GUIManagement::Picker::PickImpl {Name args} {
    variable PickerData
    variable SelectFunctionMap
    global select_mess select_opts
    
    # If we are picking a subset or otherwise not using the screen then
    # we want this to be defined as something sensible
    global select_action
    set select_action done

    if {[array names PickerData $Name,variable] != ""} {
        set ListVariable $PickerData($Name,variable)
    } else {
        error "The list variable was not set for the picker: \"$Name\"."
    }

    if {[array names PickerData $Name,type] != ""} {
        set Type [string tolower $PickerData($Name,type)]
    } else {
        error "The type was not set for the picker: \"$Name\"."
    }

    if {[array names PickerData $Name,maxNumber] != ""} {
        set MaxNumber $PickerData($Name,maxNumber)
    } else {
        error "The maxnumber was not set for the picker: \"$Name\"."
    }

    if {[array names PickerData $Name,requireExactNumber] != ""} {
        set RequireExact $PickerData($Name,requireExactNumber)
    } else {
        error "The require exact number flag was not set for the picker: \"$Name\"."
    }

    if {[array names PickerData $Name,parent] != ""} {
        set ParentObject $PickerData($Name,parent)
    } else {
        error "The parent object was not set for the picker: \"$Name\"."
    }

    set select_opts $PickerData($Name,selectOptions)
    set select_mess $PickerData($Name,selectMessage)

    if {[array names PickerData $Name,autoApply] != ""} {
        set AutoApply $PickerData($Name,autoApply)
    } else {
        set AutoApply 0
    }

    # Check to see if the user decided to cancel the operation
    global select_action

    set Action [$ParentObject proc on${Name}Pick]
    if {[info exists Action]} {
        if { $Action == "cancelpick" } {
	        return 0
        }
    }

    set Add 0
    foreach a $args {
        switch -- $a {
	        -add {
		        set Add 1
	        }
	        -remove {
		        set Add -1
	        }
	    }
    }
    eval "$SelectFunctionMap($Type) $ParentObject $ListVariable $MaxNumber $RequireExact $Add"
    # Check to see if the user decided to cancel the operation
    # This should really be done as "cancel" but cancel and done are both used to finish selection
    if {$select_action == "leave"} { return 0 }
    if { $AutoApply == 1 } {
        $ParentObject apply
        eval PickImpl $Name $args
    } else {
        # If select_action = cancel, then don't call finish - Ganesan
        global select_action

        if {$select_action == "cancel"} {
	        return 0
        }

        if {$ListVariable == ""} {
            return 0
        } else {
            after idle "$ParentObject proc on${Name}Finish"
        }
    }
    return 1
}


proc ::AIEnv::GUIManagement::Picker::CheckUserCancel {Name} {
	global take_first_press

	variable TypeNameToObjectClassMap
	variable PickerData

	if {[array names PickerData $Name,type] != ""} {
		set Type [string tolower $PickerData($Name,type)]
	} else {
		error "The type was not set for the picker: \"$Name\"."
	}
	set ObjectClass $TypeNameToObjectClassMap($Type)

	switch $ObjectClass {
		MedObject {
		    global select_action

		    if {$select_action == "done" || $select_action == "all" || $select_action == "visible" || \
			    (([lindex $select_action 0] == "release") && ($take_first_press == 0)) || \
			    (([lindex $select_action 0] == "press") && ($take_first_press == 1))} {
			# User finished picking so return false
			return 0
		    } else {
			return 1
		    }
		}
		EZCadObject {
			global va_set select_action
			return 0
		}
	}
}

proc ::AIEnv::GUIManagement::Picker::_showItems {Name args} {

	if {[catch {eval [list ShowItemsImpl $Name] $args} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}

}

proc ::AIEnv::GUIManagement::Picker::ShowItemsImpl {Name args} {
	variable PickerData
	if { [array names PickerData $Name,variable] != ""} {
	    # Create the multiline editor
	    set Editor [::AIEnv::GUIManagement::Widgets::TextEditor::TextEditor]
	    $Editor create "editable" "List Editor"
	    $Editor setlistvariable $PickerData($Name,variable)
	    $Editor loadlist $PickerData($Name,variable)
	    if [info exists PickerData($Name,editorFinish)] {
                if {[info procs ::[lindex $PickerData($Name,editorFinish) 0]] != ""} {
	            $Editor runaftersave "$PickerData($Name,editorFinish)"
	        }
	    }
	} else {
		error "The list variable was not set for the picker: \"$Name\"."
	}
}


proc ::AIEnv::GUIManagement::Picker::SelectPointOrXYZ {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectScreen $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               pointorxyz $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectVIDParam {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectVIDParamScreen $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               image $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectPoint {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometry $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               point $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectCurve {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometry $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               curve $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectCurveOrCompcurve {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometry $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               curve_or_compcurve $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectSurf {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometry $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               surface $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectBody {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
 	SelectGeometry $DEZName \
                       $ListVariableName \
                       $NumberOfEntities \
                       $UseExactNumber \
                       body $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectDensity {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
 	SelectGeometry $DEZName \
                       $ListVariableName \
                       $NumberOfEntities \
                       $UseExactNumber \
                       density $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectPoint0dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometryOrMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               point_0d_el $Add
}


proc ::AIEnv::GUIManagement::Picker::SelectCurve1dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometryOrMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               curve_1d_el $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectSurf2dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometryOrMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               surface_2d_el $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectBody3dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
 	SelectGeometryOrMesh $DEZName \
                       $ListVariableName \
                       $NumberOfEntities \
                       $UseExactNumber \
                       body_3d_el $Add
}

proc ::AIEnv::GUIManagement::Picker::Select1dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               1d $Add
}

proc ::AIEnv::GUIManagement::Picker::Select2dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               2d $Add
}

proc ::AIEnv::GUIManagement::Picker::Select3dEl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
 	SelectMesh $DEZName \
                       $ListVariableName \
                       $NumberOfEntities \
                       $UseExactNumber \
                       3d $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectAnyEntity {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectGeometryOrMesh $DEZName \
	               $ListVariableName \
	               $NumberOfEntities \
	               $UseExactNumber \
	               any_entity $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectNode {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectMesh $DEZName \
	            $ListVariableName \
	            $NumberOfEntities \
	            $UseExactNumber \
	            node $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectElem {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectMesh $DEZName \
	            $ListVariableName \
	            $NumberOfEntities \
	            $UseExactNumber \
	            element $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectEdge {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectMesh $DEZName \
	            $ListVariableName \
	            $NumberOfEntities \
	            $UseExactNumber \
	            edge $Add
}
proc ::AIEnv::GUIManagement::Picker::SelectEndpoint {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {

}

proc ::AIEnv::GUIManagement::Picker::SelectGeometry {DEZName ListVariableName NumberOfEntities UseExactNumber GeometryType Add} {
    global geo_selected select_action

    set GeometryType1 $GeometryType
    if {$GeometryType == "curve_or_compcurve"} { set GeometryType1 curve }
    if {!$Add && [info exists geo_selected($GeometryType1)]} {
	    set names ""
	    foreach name $geo_selected($GeometryType1) {
	        if {[ic_geo_check_objects_exist $GeometryType1 $name] != ""} {
		        lappend names $name
	        }
	    }
	    ic_show_geo_selected $GeometryType1 $names 0
	    unset geo_selected($GeometryType1)
    }
    
    # What's the point of doing a catch - it just makes it harder to
    # get a full stack trace.  -Wayne
    SelectGeometryImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber $GeometryType $Add

    # This should really be done as "cancel" but cancel and done are both used to finish selection
    if {$select_action == "leave"} return

    set names ""
    foreach name [set $ListVariableName] {
	    if {[ic_geo_check_objects_exist $GeometryType1 $name] != ""} {
	        lappend names $name
	    }
    }
    #ic_show_geo_selected $GeometryType1 $names 1
    if {$select_action == "cancel"} {
	    aie_reset_geo_selected
    } else {
	    set geo_selected($GeometryType1) [set $ListVariableName]
    }
}

proc ::AIEnv::GUIManagement::Picker::SelectGeometryImpl {DEZName ListVariableName NumberOfEntities UseExactNumber GeometryType Add} {
    global tdv_default select_action aie_in_select_geom select_opts auto_pick_mode

    aie_reset_geo_selected
    if [info exists aie_in_select_geom] {
        mess "Already in select geometry mode!\n" red
        after 1000 { catch "unset aie_in_select_geom" }
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_geom 1
    
    #++++++++++++++++++++++++++++++++++++++	
	# In order to reduce traffic & Replay  
	# ic_functions must recognize "all" "visible" "blanked" as a return from geo_select
	# 
		global med_selection_mode
    set med_selection_mode 1
    if {[info exists select_opts] && ([lsearch $select_opts "allow_keyword"] >= 0)} {
		set med_selection_mode "allow_all_visible_blanked"
	}
	#++++++++++++++++++++++++++++++++++++++++
    focus $tdv_default
    set pre_select ""
    if $Add { set pre_select [$DEZName get $ListVariableName] }
	if {$NumberOfEntities == "ANY"} {
	    if {$DEZName == "restoreEntityDEZ"} {
	        set Names [geo_select $GeometryType 0 "" 0 0 "" 0 $pre_select 0 1]
	    } else {
	        set Names [geo_select $GeometryType 0 "" 0 0 "" 0 $pre_select]
	    }
	} elseif {$NumberOfEntities > 0} {
	    set Names [geo_select $GeometryType $NumberOfEntities "" 0 0 "" 0 $pre_select]
	} else {
	    set Names ""
	    unset aie_in_select_geom
	    error "Bad entity number given to select geometry: NumberOfEntities must be either \"ANY\" or a positive integer"
	}
	
    # This happens when the user clicks the pick button while already in selection mode.
    # The second selection stilll continues.  We want the first selection to cancel.
    # This should really be done as "cancel" but cancel and done are both used to finish selection
    if ![info exists aie_in_select_geom] {set select_action leave; return}

    # side effect: "set" set select_action "cancel"  Reset old:
    set was_select_action $select_action
    $DEZName set $ListVariableName $Names
    set select_action $was_select_action

    unset aie_in_select_geom
}


proc ::AIEnv::GUIManagement::Picker::SelectAnyGeometry {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
        global geo_selected select_action aie_in_select_geom auto_pick_mode 

    aie_reset_geo_selected
    if [info exists aie_in_select_geom] {
        mess "Already in select geometry mode!\n" red
        after 1000 { catch "unset aie_in_select_geom" }
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_geom 1

    if [info exists geo_selected(any)] {
        set names ""
        foreach {type name} $geo_selected(any) {
            if {[ic_geo_check_objects_exist $type $name] != ""} {
                lappend names $type $name
            }
        }
        ic_show_geo_selected mixed $names 0
        unset geo_selected(any)
    }
    if {[catch {eval SelectAnyGeometryImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber $Add} result] != 0} {
        ::AIEnv::GUIManagement::RuntimeError $result
    }
    set names ""
    foreach {type name} [set $ListVariableName] {
        if {[ic_geo_check_objects_exist $type $name] != ""} {
            lappend names $type $name
        }
    }
    #ic_show_geo_selected $GeometryType $names 1
    if {$select_action == "cancel"} {
        aie_reset_geo_selected
    } else {
        set geo_selected(any) [set $ListVariableName]
    }
    catch "unset aie_in_select_geom"
}

proc ::AIEnv::GUIManagement::Picker::SelectAnyGeometryImpl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    global tdv_default select_action med_selection_mode
		# med_selection_mode 0 or 1 (if exists) is a common selection mode in geo_select. 
		set med_selection_mode 1
    focus $tdv_default

    set pre_select ""
    if $Add { set pre_select [$DEZName get $ListVariableName] }

    if {$NumberOfEntities == "ANY"} {
	set Names [geo_select entity 0 "" 0 0 "" 0 $pre_select]
    } elseif {$NumberOfEntities > 0} {
	set Names [geo_select entity $NumberOfEntities "" 0 0 "" 0 $pre_select]
    } else {
	set Names ""
	error "Bad entity number given to select geometry: NumberOfEntities must be either \"ANY\" or a positive integer"
    }
    set Names2 ""
    foreach ll $Names {
	set t [lindex $ll 0]
	foreach n [lrange $ll 1 end] { lappend Names2 $t $n }
    }
# side effect: "set" set select_action "cancel"  Reset old:
	set was_select_action $select_action
    $DEZName set $ListVariableName $Names2
	set select_action $was_select_action	    


}

proc ::AIEnv::GUIManagement::Picker::SelectGeometryOrMesh {DEZName ListVariableName NumberOfEntities UseExactNumber GeometryType Add} {
    global select_action
    
    aie_reset_geo_selected
    set result [eval SelectGeometryOrMeshImpl $DEZName $ListVariableName \
		    $NumberOfEntities $UseExactNumber $GeometryType $Add]
    if {$select_action == "cancel"} {
	aie_reset_geo_selected
    }
}

proc ::AIEnv::GUIManagement::Picker::SelectSubset {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    set result [eval SelectSubsetImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add -1]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart0d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add 0]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart1d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add 1]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart0d1d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add {{0 1}}]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart0d1d2d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add {{0 1 2}}]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart2d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add 2]
}

proc ::AIEnv::GUIManagement::Picker::SelectPart3d {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add 3]
}

proc ::AIEnv::GUIManagement::Picker::SelectPartPrism {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add prism]
}

proc ::AIEnv::GUIManagement::Picker::SelectPartVolume {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    
    set result [eval SelectPartImpl $DEZName $ListVariableName $NumberOfEntities \
		    $UseExactNumber $Add volume]
}

proc ::AIEnv::GUIManagement::Picker::SelectHexCompcurve {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	if {[catch {eval SelectHexEntityImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber compcurve} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::SelectHexVertex {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectHexEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber vertex $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectHexEdge {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectHexEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber edge $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectHexFace {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectHexEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber face $Add
}


proc ::AIEnv::GUIManagement::Picker::SelectHexBlock {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectHexEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber block $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectStrDomain {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectStrEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber domain $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectStrSubFace {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectStrEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber subface $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectStrEdge {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectStrEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber edge $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectStrVertex {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectStrEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber vertex $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectStrNode {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
	SelectStrEntity $DEZName $ListVariableName $NumberOfEntities $UseExactNumber node $Add
}

proc ::AIEnv::GUIManagement::Picker::SelectSubpart {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {

	if {[catch {eval SelectSubpartImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber $Add} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::SelectGeometryOrMeshImpl {DEZName ListVariableName NumberOfEntities UseExactNumber GeometryType Add} {
    global tdv_default aie_in_select_geom_or_mesh auto_pick_mode select_action

    focus $tdv_default 
    if [info exists aie_in_select_geom_or_mesh] {
        mess "Already in select geometry or mesh mode!\n" red
        after 1000 { catch "unset aie_in_select_geom_or_mesh" }
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_geom_or_mesh 1

    set gt [lindex [split $GeometryType _] 0]
    set pre_select ""
    if $Add { set pre_select [$DEZName get $ListVariableName] }
    if {$NumberOfEntities == "ANY"} {
        set Names [geo_select_either $gt 0 "" 0 0 "" $pre_select]
    } elseif {$NumberOfEntities > 0} {
        set Names [geo_select_either $gt $NumberOfEntities "" 0 0 "" $pre_select]
    } else {
        catch "unset aie_in_select_geom_or_mesh"
        error "Bad entity number given to select geometry: NumberOfEntities must be either \"ANY\" or a positive integer"
    }
    if {$gt == "any"} {
        set Names2 ""
        foreach pair $Names {
            eval lappend Names2 $pair
        }
        set Names $Names2
    }
    # side effect: "set" set select_action "cancel"  Reset old:
    set was_select_action $select_action
    $DEZName set $ListVariableName $Names
    set select_action $was_select_action
    catch "unset aie_in_select_geom_or_mesh"
}

proc ::AIEnv::GUIManagement::Picker::SelectSubsetImpl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    set subsets [aie_subset_names]
    if {$subsets == ""} {
	user_error "There are no subsets available"
	return
    }
# side effect: "set" set select_action "cancel"  Reset old:
	global select_action
	set was_select_action $select_action
    $DEZName set $ListVariableName [choose_multiple "Select subsets." $subsets]
	set select_action $was_select_action	    
}

proc ::AIEnv::GUIManagement::Picker::SelectPartImpl {DEZName ListVariableName NumberOfEntities UseExactNumber Add Dimens} {
    global select_action select_opts aie_in_select_part auto_pick_mode
#
# select_opts may be a namespace variable with list of parts to select from
#

    if [info exists aie_in_select_part] {
        mess "Already in select part mode!\n" red
        after 1000 { catch "unset aie_in_select_part" }
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_part 1

    set select_action done
    set selexact 0
    if {$NumberOfEntities == "ANY"} {
        set multiple 1
    } elseif {$NumberOfEntities == 1} {
        set multiple 0
        set selexact 1
    } elseif {$NumberOfEntities > 1} {
        set multiple 1
        set selexact 1
    } else {
        set multiple 0
    }
    set allow_new 0
    set exp [$DEZName get $ListVariableName]

    set select_from ""
    if [info exists select_opts] {
        if {[lindex $select_opts 0] == "$ListVariableName"} {
            set select_from [lrange $select_opts 1 end]
        } else {
            set namespace [namespace qualifiers $ListVariableName]
            set v "${namespace}::$select_opts"
            if [info exists $v] {
                set select_from [set $v]
            }
        }
    }
    set pn [select_existing_parts $Dimens $multiple $exp $select_from]
    if $selexact {
        while {([llength $pn]) && ([llength $pn] != $NumberOfEntities)} {
            if {$Dimens == "prism"} {
                msg ERROR "Please select $NumberOfEntities prism parts (only)."
            } elseif {$Dimens == "volume"} {
                msg ERROR "Please select $NumberOfEntities volume parts (only)."
            } else {
                msg ERROR "Please select $NumberOfEntities parts (only)."
            }
            set pn [select_existing_parts $Dimens $multiple $exp]
        }
    }
    # side effect: "set" set select_action "cancel"  Reset old:
    set was_select_action $select_action
    $DEZName set $ListVariableName $pn
    set select_action $was_select_action

    if [llength $pn] {
        set select_action done
    }
    catch "unset aie_in_select_part" 
}

proc ::AIEnv::GUIManagement::Picker::SelectSubpartImpl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    global select_action

    set select_action done
    set selexact 0
    if {$NumberOfEntities == "ANY"} {
	set multiple 1
    } elseif {$NumberOfEntities > 0} {
	set multiple 1
	set selexact 1
    } else {
	set multiple 0
    }
    set allow_new 0
    set pn [select_families subpart $multiple $allow_new "Select Parts"]
    if $selexact {
	while {([llength $pn]) && ([llength $pn] != $NumberOfEntities)} {
	    msg ERROR "Please select $NumberOfEntities sub-parts (only)."
	    set pn [select_families subpart $multiple $allow_new "Select Sub-Parts"]
	}
    }
# side effect: "set" set select_action "cancel"  Reset old:
	set was_select_action $select_action
    $DEZName set $ListVariableName $pn
	set select_action $was_select_action	    

    if [llength $pn] {
	set select_action done
    }
}

proc ::AIEnv::GUIManagement::Picker::SelectMesh {DEZName ListVariableName NumberOfEntities UseExactNumber MeshType Add} {

	if {[catch {eval SelectMeshImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber $MeshType $Add} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::SelectMeshImpl {DEZName ListVariableName NumberOfEntities UseExactNumber MeshType Add} {
    global select_opts tdv_default aie_subset_mesh_map aie_in_select_mesh
    global uns_select_for take_first_press auto_pick_mode select_action

    if [info exists aie_in_select_mesh] {
        mess "Already in select mesh mode!\n" red
        after 1000 {catch "unset aie_in_select_mesh"}
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_mesh 1

    focus $tdv_default
    set from_names visible
    if {[info exists aie_subset_mesh_map] && $aie_subset_mesh_map != ""} {
        lappend from_names $aie_subset_mesh_map
    }

    set pre_select ""
    if $Add { set pre_select [$DEZName get $ListVariableName] }

    set warn_for_all 0
    if {$select_opts == "warn_for_all"} {
        set warn_for_all 1
    }
    set replace_previous 0
    if {$select_opts == "replace_previous"} {
        set replace_previous 1
    }
    set fast_parts 0
    if {$select_opts == "fast_parts"} {
        set fast_parts 1
    }

    if {$NumberOfEntities == "ANY"} {
        set SelectedItems [select_unstruct $MeshType 0 $from_names "" "" $uns_select_for $take_first_press \
            0 0 $pre_select $warn_for_all $replace_previous $fast_parts]
    } elseif {$NumberOfEntities > 0} {
        set SelectedItems [select_unstruct $MeshType $NumberOfEntities $from_names "" "" $uns_select_for $take_first_press \
            0 0 $pre_select $warn_for_all $replace_previous $fast_parts]
    } else {
        unset aie_in_select_mesh
        error "Bad entity number given to select mesh: NumberOfEntities must be either \"ANY\" or a positive integer"
    }
    set was_select_action $select_action
    $DEZName set $ListVariableName $SelectedItems
    set select_action $was_select_action
    unset aie_in_select_mesh
}

proc ::AIEnv::GUIManagement::Picker::SelectVIDParamScreen {DEZName ListVariableName NumberOfEntities UseExactNumber PickType Add} {
global select_opts select_action
# Replacement for seek SelectScreenImpl
# Now use the same va_create_xyz with args:
# $select_how $NumberOfEntities $allow_name $show_how $var $param $param_var $selected_item
	# Settings priority: a) default b) from ttkrf c) from DEZ
	#a)
	foreach {n v} {DynType curve onDynMotion DefaultMotionProc onDynButton DefaultButtonProc} {
		set $n $v
	}
	#b)
	foreach {n v} $select_opts {set $n $v}
	#c)
	set namespace [namespace qualifiers $ListVariableName]
	foreach maybe "DynType onDynMotion onDynButton" {
		set v "${namespace}::$maybe"
		if [info exists $v] {
			set $maybe [set $v]
		}
	}
	set Ret [vid_picker $DynType $onDynMotion $onDynButton]
# side effect: "set" set select_action "cancel"  Reset old:
	set was_select_action $select_action
	$DEZName set $ListVariableName $Ret
	set select_action $was_select_action	    
# 
} 
#
proc ::AIEnv::GUIManagement::Picker::SelectScreen {DEZName ListVariableName NumberOfEntities UseExactNumber PickType Add} {

	if {[catch {eval SelectScreenImpl $DEZName $ListVariableName $NumberOfEntities $UseExactNumber $PickType $Add} result] != 0} {
		::AIEnv::GUIManagement::RuntimeError $result
	}
}

proc ::AIEnv::GUIManagement::Picker::SelectScreenImpl {DEZName ListVariableName NumberOfEntities UseExactNumber PickType Add} {
	global va_set tdv_default select_action select_mess select_opts
#   set varname [lindex [split $ListVariableName :] end] 
# This procedure currently used only by POINTORXYZ Picker
# Numerous options are available for/during selection
# Instead of create separate "picker" for each option
#  we use optional parameters in "EntityChooser" line
#  ================================================  
# Auto selection "from" is a default
# Other options are: (no difference for lower or uppercase)
#  XYZ -> output is triplet (names not allowed)
#  DO  -> set va_set(do_when_create_xyz) 1 (enabled command during selection)
#  faceted ... -> use faceted options 

	if ![info exists select_opts] {set select_opts none}

	set param 0
	set param_var ""
	set fi [lsearch -regexp $select_opts "***:(?i)^param$"]
	if {$fi >= 0} {set param 1} \
	else {
		set fi [lsearch -regexp $select_opts "***:(?i)^param "]
		if {$fi >= 0} {
		    set param 1
		    set param_var [lindex [lindex $select_opts $fi] 1]
		    if {$param_var != ""} {
			set namespace [namespace qualifiers $ListVariableName]
			set param_var "${namespace}::$param_var"
		    }
		}
	}
# Use select_opts variable for qualified names.
	set sel [string tolower $select_opts]
    if {$sel == "nearest" && $NumberOfEntities == 1} {
	set point [select_location "a point" 1]
	if {$point != "" } {
	    set im [tdv_point_im -point $point -color yellow -size 5]
	    $tdv_default add_image $im
	    $tdv_default redraw
	    $im delete
	    set point [list $point]
	}

# side effect: "set" set select_action "cancel"  Reset old:
	set was_select_action $select_action
	$DEZName set $ListVariableName $point
	set select_action $was_select_action	    
	return
    }

	set allow_name 1
	if {[lsearch $sel xyz] >= 0} {set allow_name 0}

	if {[lsearch $sel do] >= 0} {
		set va_set(do_when_create_xyz) 1
	} else {
		if [info exists va_set(do_when_create_xyz)] {unset va_set(do_when_create_xyz)}
	}

	set select_how auto
	
	if {$NumberOfEntities != "" && [string is integer $NumberOfEntities]} {
	    if {$NumberOfEntities > 1} {
	        set mess_what locations
	    } else {
	        set mess_what location
	    }
	} else {
	    set mess_what location
	}
	

	if {[lsearch $sel curve] >= 0} {
		set select_how curve
		set mess_what curve
	}
	if {[lsearch $sel vis_plane] >= 0} {
		set select_how vis_plane
		set mess_what "location(s) on XYZ plane"
	}		
# may be surface_selected!
	set i [string first surface $sel]
	if {$i >= 0} {
		set select_how [lindex [string range $sel $i end] 0]
		set mess_what "location(s) on surface"
	}			
	set n [lsearch $sel faceted]
	if {$n >= 0} {
		incr n
		set select_how faceted.[lindex $sel $n]
		set mess_what node
	}

	if {$select_how != "curve" && $select_how != "surface"} {
		set param 0
		set param_var ""
	}

	set show_how ""
# for curves may use direction or direction to end 
	if {[lsearch $sel show_direction] >= 0} {lappend show_how direction}
	if {[lsearch $sel show_end] >= 0} {lappend show_how end}		 
	if {[lsearch $sel show_plane] >= 0} {lappend show_how plane}

	set var ""
	if {[lsearch $sel update_var] >= 0} {
	    set var $ListVariableName
	}

	set selected_item ""
	set i [string first "selected" $sel ]
	if {$i >= 0} {
	# check may be next word is a valid variable (from namespace)
		set maybe [lindex [string range $select_opts $i end] 1]
		set namespace [namespace qualifiers $ListVariableName]
		set v "${namespace}::$maybe"
		if [info exists $v] {
			set selected_item [set $v]
		} elseif [info exists va_set(selected)] {
			set selected_item $va_set(selected)
		}
		if {$select_how == "auto"} {
			set select_how selected
		}
	}

	if {[lsearch $sel "no_confirm"] >= 0} {
	    set va_set(confirm) 0
	}

	foreach word $sel {
		if {[string first . $word] >= 0} {
			set s [split $word .]
			set s0 [lindex $s 0]
# only tag and image for now:
			if {$s0 == "tag"} {
			set mess_what [lrange $s 1 end]
			set select_how tag
			set selected_item [lindex $s 1].[lindex $s 2] 
			}
			if {$s0 == "image"} {
				set select_how [lindex $s 1]_im
			}
		}
	}
# ttkrf file message has top priority; in 11.0 set all irregular mess_what in ttkrf
	if [llength $select_mess] {
		set mess_what $select_mess
	}
#===========================================
	focus $tdv_default
	set select_action done
# banner--->
	set Message Select
    if {$NumberOfEntities == "ANY" && ([string index $mess_what end] != "s")} { 
		append Message " " $mess_what s " "
	} elseif {$NumberOfEntities == 1} {
		append Message " 1 " $mess_what
	} else {
		append Message " $NumberOfEntities $mess_what "		
	}
	lappend Message with left button, 
	if {[info exists va_set(confirm)] && ($va_set(confirm) == 0)} {
		append Message " right=cancel"
	} else {
		append Message " middle=done, right=cancel"
	}
	banner $Message
	
# <---banner
# 3-rd parameter va_create_xyz controls point usage (if selected):
#	1 (default) - return point name if point was selected
#	0 - return xyz triplet always
# 4-th parameter controls display visualization of location
# 5-th parameter is fully qualified name of variable to interactively store result in
# 6-th parameter 1/0 will return parameter in place of xyz if select_how is cuve or surface
# 7-th parameter is fully qualified name of variable to interactively store parameter in
# 8-th parameter is empty or name of entity to select location from
#
# 
	$DEZName set $ListVariableName ""
	set Ret [va_create_xyz $select_how $NumberOfEntities $allow_name $show_how $var $param $param_var $selected_item]
	# Clear out the banner
	banner ""
 	if {$select_action == "cancel"} {
	    va_end_of_screen_input
	    if [info exists va_set(img)] {
            catch {$va_set(img) delete}
		    unset va_set(img)
        }
	} else {
        # New 05/18/04: if xyz && 1 location, then return {x y z}, not {{x y z}}
	    if {$NumberOfEntities == 1} {
		    set Ret [lindex $Ret 0]
	    }
        # side effect: "set" set select_action "cancel"  Reset old:
	    set was_select_action $select_action
	    if {$param && $param_var != ""} {
		    $DEZName set $ListVariableName $va_set(names_for_coords)
		    $DEZName set $param_var $Ret		    
	    } else {
		    $DEZName set $ListVariableName $Ret
	    }
	    set select_action $was_select_action	    
	}
}

proc ::AIEnv::GUIManagement::Picker::SelectHexCompcurveImpl {DEZName ListVariableName NumberOfEntities UseExactNumber Add} {
    global select_action
    global button_instructions
    global select_mess select_opts
    global last_line

    set select_action done
    if ![ic_hex_blocking_loaded] {
        return 0
    }

    SelectGeometry $DEZName \
	$ListVariableName \
	$NumberOfEntities \
	$UseExactNumber \
	curve $Add

    set selectedCurves [$DEZName get $ListVariableName]

    # Get the composite curve
    set scomp_curves ""
    foreach curve $selectedCurves {
        hex_command "find_comp_curve $curve"
        lappend scomp_curves [lindex $last_line 0]
    }

    # uniquify the list
    set ucomp_curves ""
    foreach curve $scomp_curves {
        if ![info exists selected($curve)] {
            lappend ucomp_curves $curve
            set  selected($curve) 1
        }
    }

    if 0 {
	set opts ""
	if [info exists select_opts] {
	    set opts $select_opts
	}
	set hxcompcrv [select_curve "$msg $button_instructions" $nmax $opts]
    }

# side effect: "set" set select_action "cancel"  Reset old:
	set was_select_action $select_action
    $DEZName set $ListVariableName $ucomp_curves
	set select_action $was_select_action	    
}

proc ::AIEnv::GUIManagement::Picker::SelectHexEntity {DEZName ListVariableName NumberOfEntities UseExactNumber type {Add 0}} {
    global hexa_sel

    if {$Add == 1} {
        set hexa_sel add 
    } elseif {$Add == -1} {
        set hexa_sel remove 
    } else {
        set hexa_sel select
    }

    if {[catch {eval SelectHexEntityImpl $DEZName $ListVariableName $NumberOfEntities \
        $UseExactNumber $type} result] != 0} {
        ::AIEnv::GUIManagement::RuntimeError $result
    }
}

proc ::AIEnv::GUIManagement::Picker::SelectHexEntityImpl {DEZName ListVariableName NumberOfEntities UseExactNumber type} {
    global select_action button_instructions select_mess select_opts
    global tdv_default select_in_dynamics hexa_select select_premesh_families
    global aie_in_select_hexa auto_pick_mode cancel_picker_now

    if [info exists ::cancel_picker_now] { unset ::cancel_picker_now; return }

    set select_action done
    
    if {[info exists select_opts] && ($select_opts == "use_local_select")} {
        if [info exists hexa_select] {
            mess "Already in $type select mode\n" red
            set select_action "local_cancel"
            # for selection and apply/cancel in C++ code only
            if {$DEZName == "editEdgeMenuDEZ"} {
                set reset_picker [lindex [split $ListVariableName "::"] end]
                if {$reset_picker == "edgeUnSplits"} {
                    catch { aie_blocking_menu_unload editEdgeUnSplit }
                }
            }
            return 0
        }
        select_unset_bindings 1
        set ::select_in_dynamics 0
        tdv_set_key_command F9 select_hexa_toggle_dynamics
        busy reset
        return 0
    }
    if ![ic_hex_blocking_loaded] { return 0 }
    
    if [info exists aie_in_select_hexa] {
        mess "Already in select $type mode!\n" red
        after 1000 {catch "unset aie_in_select_hexa"}
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_hexa 1

    set take_first_press 0
    set select_duplicate 0
    set opts ""
    if [info exists select_opts] {
        foreach opt $select_opts {
            if {$opt == "take_first_press"} {
                set take_first_press 1
            } elseif {$opt == "select_duplicate"} {
                set select_duplicate 1		
            } else {
                lappend opts $opt
            }
        }
    }

    if {$NumberOfEntities == "ANY"} {
        set nmax 0
    } elseif {$NumberOfEntities > 0} {
        set nmax $NumberOfEntities
    } else {
        set nmax 1
    }

    if {([info exists select_mess]) && $select_mess != ""} {
        set msg $select_mess
    } else {
        set msg "Select Hexa $type"
    }

    set ents [select_hexa $type $nmax "$msg" $opts $take_first_press $select_duplicate]
    # special case if variable select_premesh_families exist
    if [info exists select_premesh_families] {
        set was_select_action done
    } else {
        # side effect: "set" set select_action "cancel"  Reset old:
        set was_select_action $select_action
    }
    $DEZName set $ListVariableName [string trimright $ents \n]
    set select_action $was_select_action
    catch "unset aie_in_select_hexa"
}

proc ::AIEnv::GUIManagement::Picker::SelectStrEntity {DEZName ListVariableName NumberOfEntities UseExactNumber type {Add 0}} {
    global struct_sel

    if {$Add == 1} {
        set struct_sel add 
    } elseif {$Add == -1} {
        set struct_sel remove 
    } else {
        set struct_sel select
    }
    if {[catch {eval SelectStrEntityImpl $DEZName $ListVariableName $NumberOfEntities \
        $UseExactNumber $type} result] != 0} {
        ::AIEnv::GUIManagement::RuntimeError $result
    }
}

proc ::AIEnv::GUIManagement::Picker::SelectStrEntityImpl {DEZName ListVariableName NumberOfEntities UseExactNumber type} {
    global select_action select_opts select_struct_families aie_in_select_str auto_pick_mode

    if {[info exists select_opts] && ($select_opts == "use_local_select")} {
        return 0
    }
    if ![ic_str_is_loaded] {
        return 0
    }
    if [info exists aie_in_select_str] {
        mess "Already in select mode!\n" red
        after 1000 { catch "unset aie_in_select_str" }
        if $auto_pick_mode {
            set auto_pick_mode 0
            after 1000 "set auto_pick_mode 1"
        }
        return 0
    }
    set aie_in_select_str 1


    if {$NumberOfEntities == "ANY"} {
        set nmax 0
    } elseif {$NumberOfEntities > 0} {
        set nmax $NumberOfEntities
    } else {
        set nmax 1
    }

    if {$type == "node"} {	
        set take_first_press 0
        if [info exists select_opts] {
            foreach opt $select_opts {
                if {$opt == "take_first_press"} {
                    set take_first_press 1
                    break	
                }
            }
        }
        set ents [select_struct_node $nmax "" 0 "" 1 $take_first_press]
    } else {
        set ents [select_struct_surf $type $nmax]
    }
    # side effect: "set" set select_action "cancel"  Reset old:
    # special case if variable select_struct_families exist
    if [info exists select_struct_families] {
        set was_select_action done
    } else {
        set was_select_action $select_action
    }
    $DEZName set $ListVariableName $ents
    set select_action $was_select_action
    catch "unset aie_in_select_str"
}


