#
# Stuff for handling meshing directives.
# Author: Jason Aftosmis, 2001
#


# ---> These are the procs to be used outside of this file---jka
#
proc unstruct_meshdirect_boltspiders {} {
    unstruct_meshdirect_editrun_type "boltspider"
}
proc unstruct_meshdirect_spotwelds {} {
    unstruct_meshdirect_editrun_type "spotweld"
}
proc unstruct_meshdirect_seamwelds {} {
    unstruct_meshdirect_editrun_type "seamweld"
}
proc unstruct_meshdirect_connectors {} {
    unstruct_meshdirect_editrun_type "connector"
}

# ---> These procs help the above procs---jka

proc unstruct_meshdirect_editrun_type {type} {
    
    if ![ic_uns_is_loaded] {
	mess "must have unstruct mesh loaded to execute $type!\n" red
	return 0
    }
    if ![ic_geo_is_loaded] {
	mess "must have geometry loaded to execute $type!\n" red
	return 0
    }
    
    # edit the parameters for this type
    set accepted [unstruct_meshdirect_listedit $type]    
    if !$accepted { return 0 }
    
    # run the mesh direct library function tcl_method in UnsMesh obj
    set status [catch {ic_run_meshdirect_obj $type} res]
    if $status {
	mess "there was an error running meshing directive $type:\n $res\n" red
    }
    
    display_update unstruct    
    
    return $status
}

# procs for selecting and stuff...
proc unstruct_meshdirect_select_family {title} {
    return [select_families 0 0 0 "Select the [string tolower $title]."]
}
proc unstruct_meshdirect_select_family_new {title} {
    return [select_families 0 0 1 "Select the [string tolower $title]."]
}
proc unstruct_meshdirect_weld_method {w num name nval} {
    #mess "DEBUG: $w $num $name $nval\n"
    set tval [ic_meshdirect_spotweld_types_param_get $nval]
    #mess "DEBUG: $tval\n"
    return [list $name $tval]
}

## creates a flexlist to select_new/edit_old parameters of a given directive

proc unstruct_meshdirect_listedit {directive} {
    
    if {$directive == ""} {
	mess "cannot edit the list of meshing directives without specifying a directive type\n" red
	return 0
    }
    
    set data [ic_meshdirect_get_current_for_flexlist $directive ""]
    #mess "data: $data\n" red
    #if ![llength $data] { return 0 }  i'm not sure why was there...xxx --jka
    
    set title "Edit $directive meshing directives"    
    set format ""
    
    switch $directive {
	connector {
	    set format {{one "Family of start entities" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family"} \
		    {two "Start entity type" choice point \
		    "point curve surface"} \
		    {three "Family of end entities" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family_new"} \
		    {four "End entity type" choice point \
		    "point curve surface"} \
		    {five "Family for connecting elements" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family_new"} \
		    {six  "Active" checkbutton 1} }
	}
	seamweld {
	    set format {{one "Family of weld curves" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family"} \
		    {two "Target surface family" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family"} \
		    {three "Family for seamweld bars" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family_new"} \
		    {four  "Active" checkbutton 1} }
	}
	spotweld {
	    # should like to use [ic_meshdirect_spotweld_types_verbose_list] here xx --jka
	    set format {{one "Family of weld points" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family"} \
		    {two "Target surface family" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family"} \
		    {three "Family for spotweld bars" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family_new"} \
		    {four "Weld method" choice "Point to point"\
		    {"Point to point" "Mesh indpendenent line" "Mesh indpendenent hexa" "Area weld"} \
		    unstruct_meshdirect_weld_method } \
		    {five "Family for rbe3 bars" entry_func \
		    "call_function __this__" \
		    "unstruct_meshdirect_select_family_new"} \
		    {six "Weld radius" entry 1.} \
		    {seven  "Active" checkbutton 1} }
	}
	boltspider {
	    set format {
		{one Family entry_func "call_function __this__" \
		     "unstruct_meshdirect_select_family"} \
		{two "Family for new spider bars" entry_func \
		     "call_function __this__" \
		     "unstruct_meshdirect_select_family_new"} \
		{three "No. quad layers" entry 3} \
		{four  "Active" checkbutton 1} }
	}
	default {
	    mess "unexpected type in unstruct_meshdirect_listedit\n"
	    return 0
	}
    }
    if { $format == "" } { return 0 }
    
    set fdata [lindex $data 0] ;# format data; this is kind of ugly
    #set fdata $data

    # --> make the flexlist
    
    set focus_func ""
    set meshdirect_data [flexlist .flexedit $title $format \
			     $fdata "" $focus_func 0]
    #mess "DEBUG: meshdirect_data: $meshdirect_data\n"
    if { $meshdirect_data == "*cancel*" } {
	return 0 
    }
    
    # --> new data accepted; delete all such data from the proj file, then set new in proj
    
    ic_meshdirect_reset $directive
    
    if {"" != $meshdirect_data} {
	set stat [ic_meshdirect_set $directive $meshdirect_data]
    } else {
	# the form was cleared--nothing to do.
	set stat 0
    }
    
    return $stat
}
