
# This code creates the user interface for global.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc select_node {mess type {num 1} {vis_only 0}} {
    global last_line
    
    banner $mess
    
    if $vis_only {
	set vis vis
    } else {
	set vis ""
    }
    
    set last_line ""
    set cmd "select_nodes $num -type $type $vis [range_string] \
[surface_string] [volume_string]"
    command $cmd 0
    
    banner ""
    
    return $last_line
}

proc select_edge {mess {max_sel 1} {opts ""}} {
    global last_line display_flag
    
    banner $mess
    
    set cmd "select_edge [range_string] max $max_sel m [volume_string] $opts"
    command $cmd 1 1
    
    banner ""
    if {$last_line == ""} { return "" }
    
    if {$max_sel == 1} {
	set edge [lindex $last_line 0]
	
	set vert [lindex $edge 0]
	set dim [lindex $edge 1]
	
	if {$dim < 3} {
	    set ind [lindex $vert $dim]
	} else {
	    set ind 0
	    foreach i $vert {
		if {[scan $i "%d:%d" ndim nind] == 2} {
		    if {$ndim == $dim} {
			set ind $nind
			break
		    }
		}
	    }
	}
	return [list $vert $dim $ind]
    } else {
	return $last_line
    }
}

proc select_surface {mess} {
    global last_line display_flag
    
    if !$display_flag(surf) { set display_flag(surf) 1; update_display surf }
    
    banner $mess
    
    command "select_surface" 1 1
    
    banner ""
    
    return $last_line
}

proc select_line {mess} {
    global last_line display_flag
    
    if !$display_flag(curve) { set display_flag(curve) 1; update_display curve }
    banner $mess
    command "select_curve" 1 1
    banner ""
    return $last_line
}

proc select_point {mess} {
    global last_line display_flag
    
    if !$display_flag(point) { set display_flag(point) 1; update_display point }
    banner $mess
    command "select_point" 1 1
    banner ""
    return $last_line
}

# -------------------

proc block_mark_families {var {one 0}} {
    global $var
    
    if $one {
	set ff [set $var]
    } else {
	set ff ""
	foreach f [array names $var] {
	    if [set ${var}($f)] { lappend ff $f }
	}
    }
    if {$ff != ""} {
	command "mark_blocks $ff" 0
    } else {
	command "mark_blocks unmark" 0
    }
    command "del solid marked" 0
}

proc surface_mark_families {var {one 0}} {
    global $var
    
    if $one {
	set ff [set $var]
    } else {
	set ff ""
	foreach f [array names $var] {
	    if [set ${var}($f)] { lappend ff $f }
	}
    }
    if {$ff != ""} {
	command "display_surfaces solid $ff" 0
    } else {
	command "erase_surfaces" 0
    }
}

# -----------------------

proc block_unmark {} {
    command "mark_blocks unmark" 0
}

proc block_mark {on face} {
    set first [select_node "Please select the first node bound with\
	    the left button; right = cancel." node]
    if {$first == ""} return
    set second [select_node "Please select the second node bound with\
	    the left button; right = cancel." node]
    if {$second == ""} return
    
    set cmd "mark_blocks $first $second"
    if !$on  { append cmd " unmark" }
    if $face { append cmd " face_neighbors" }
    command $cmd 0
    command "del marked solid" 0
}

# --------------------------

# Not really selection-oriented things....

proc get_families {} {
    global all_lines families family_dims family_active family_name
    
    command "list_families dims" 0
    catch {unset family_dims}
    set families "0"
    set family_name(0) ORFN
    set family_dims(0) 3
    set family_active(0) 1
    set i 1
    foreach line $all_lines {
	if {$line == ""} continue
	set f [lindex $line 0]
	set d [lindex $line 1]
	lappend families $i
	set family_dims($i) $d
	set family_name($i) $f
	if ![info exists family_active($i)] {
	    set family_active($i) 1
	}
	incr i
    }
}

proc get_indices {reset} {
    global last_line index_min index_max index_min_limit index_max_limit
    
    if [info exists index_max_limit] {
	foreach i [array names index_max_limit] {
	    set old_limits($i) $index_max_limit($i)
	}
    }
    catch {unset index_min_limit}
    catch {unset index_max_limit}
    
    set should_update 0
    
    #command {print 0 "%s %s %s\n" $NI $NJ $NK}
    command print_nijk
    if {[lindex $last_line 0] == "variable"} {
	mess "Error: can't set up mesh\n" red
	foreach i {0 1 2} {
	    foreach t {min min_limit max max_limit} {
		set index_${t}($i) 0
	    }
	}
	return
    }
    set i 0
    foreach m $last_line {
	if {[scan $m %d:%d dim val] != 2} {
	    set dim $i
	    set val $m
	    incr val -1
	}
	if {$reset || ![info exists index_min($dim)] || \
		($index_min($dim) == "")} {
	    set index_min($dim) 0
	}
	set index_min_limit($dim) 0
	if {$reset || ![info exists index_max($dim)] || \
		($index_max($dim) == "")} {
	    set index_max($dim) $val
	}
	set index_max_limit($dim) $val
	set index_min_limit($dim) 0
	incr i
    }
    
    # Maybe not always needed but can't hurt too much.
    setup_index_box
    if $should_update {
	update_display block mesh vert
    }
}
