
# ------------------------------------------------------
# This DEZ is defined in the file matDEZ.ttkrf

proc onCreateMatDEZLoad {} {
    global current_material_to_load material_data material_tbl_data material_tbl_type
    global current_material_dpnd_to_load material_dpnd_data
    global material_type
    global  solver_setup solver_setup_global
    global modify_material

    if {$solver_setup != "-1"} {
       set solve_format $solver_setup
    } else {
       set solve_format $solver_setup_global
    }

    #set [MatYngsType get -key variable] 0

    #set TblList [lsort [array names material_tbl_data]]
    set TblList {}
    foreach name [array names material_tbl_data] {
	if {$material_tbl_type($name) == 0  || $material_tbl_type($name) == 1 || $material_tbl_type($name) == 2} { set TblList [concat $TblList $name]}
    }
    set TblList [lsort $TblList]

    foreach t2 {Yngs Shear Pois Dens CTE Dampc Slt Slc Sls \
                      G12 G13 G14 G15 G16 G22 G23 G24 G25 G26 \
                      G33 G34 G35 G36 G44 G45 G46 G55 G56 G66 \
                      A2 A3 A4 A5 A6 E2 Yt Yc F12 Strn} {
       set val Mat${t2}Option
       if {[set $val] == ""} {set $val 0}
    }

    #Note: (mat2/mat9) shear:g11, cte:a1, (mat8) yngs:e1, nu12:poiss, g13:g1z, g23:g2z, slt:xt, slc:xc, sls:s
    
#Added LS-Dyna material type
    foreach {n v} {
	name "" number 0 yngs "" shear "" poiss "" dens ""
	cte "" ref "" dampc "" slt "" slc "" sls "" mcsid "" g12  "" g13 "" g14 "" g15 "" g16 "" g22 "" g23 "" g24 "" g25 "" g26 ""
          g33 "" g34 "" g35 "" g36 "" g44 "" g45 "" g46 "" g55 "" g56 "" g66 "" a2 "" a3 "" a4 "" a5 "" a6 "" 
          e2 "" yt "" yc "" f12 "" strn "" lsdyna_matType 0 lsdyna_mat24_sigy 0.0 lsdyna_mat24_fail 0.0
          thermk "" thermcp "" thermh "" visc "" hgen "" tch "" tdelta "" qlat ""
          thermkxx "" thermkxy "" thermkxz "" thermkyy "" thermkyz "" thermkzz ""
    } {
	if [info exists current_material_to_load($n)] {
	    set $n $current_material_to_load($n)
	} else {
	    set $n $v
	}
    }
    if {$name == ""} {
	foreach i [array names material_data] {
	    set used($i) 1
	    set used([lindex $material_data($i) 0]) 1
	}
	for {set i 1} {[info exists used(MAT$i)] || [info exists used($i)]} {incr i} { }
	set name MAT$i
	set number $i
    }

    if [info exists material_type($name)] {
        set material_format [lindex $material_type($name) 0]
        set mat_dpnd [lindex $material_type($name) 1]
    } else {
        set material_format 0
        set mat_dpnd 0
    }

    foreach {n v} {
	MatYngsTbl "" MatShearTbl "" MatPoisTbl "" MatDensTbl "" MatCTETbl "" MatDampcTbl "" MatSltTbl "" MatSlcTbl "" MatSlsTbl ""
         MatG12Tbl  "" MatG13Tbl "" MatG14Tbl "" MatG15Tbl "" MatG16Tbl "" MatG22Tbl "" MatG23Tbl "" MatG24Tbl "" MatG25Tbl "" 
         MatG26Tbl "" MatG33Tbl "" MatG34Tbl "" MatG35Tbl "" MatG36Tbl "" MatG44Tbl "" MatG45Tbl "" MatG46Tbl "" MatG55Tbl "" 
         MatG56Tbl "" MatG66Tbl "" MatA2Tbl "" MatA3Tbl "" MatA4Tbl "" MatA5Tbl "" MatA6Tbl "" MatE2Tbl "" MatYtTbl "" MatYcTbl "" 
         MatF12Tbl "" MatStrnTbl ""
         MatThermKTbl "" MatThermCPTbl "" MatThermHTbl "" MatVISCTbl "" MatHGENTbl ""
         MatThermKxxTbl "" MatThermKxyTbl "" MatThermKxzTbl "" MatThermKyyTbl "" MatThermKyzTbl "" MatThermKzzTbl ""
    } {
	if [info exists current_material_dpnd_to_load($n)] {
	    set $n $current_material_dpnd_to_load($n)
	} else {
	    set $n $v
	}
    }

    foreach t2 {Yngs Shear Pois Dens CTE Dampc Slt Slc Sls \
                      G12 G13 G14 G15 G16 G22 G23 G24 G25 G26 \
                      G33 G34 G35 G36 G44 G45 G46 G55 G56 G66 \
                      A2 A3 A4 A5 A6 E2 Yt Yc F12 Strn ThermK ThermCP ThermH VISC HGEN \
                      ThermKxx ThermKxy ThermKxz ThermKyy ThermKyz ThermKzz} {
        set val1 Mat${t2}Tbl
        set val2 Mat${t2}Option
        if {[set $val1] != ""} {
             set $val2 1
             set mat_dpnd 1
        } else {
             set $val2 0
        }
    }
  
    if {$name == ""} {
	foreach i [array names material_dpnd_data] {
	    set used($i) 1
	    set used([lindex $material_dpnd_data($i) 0]) 1
	}
    }

    ::AIEnv::GUIManagement::ToggleGrey createMatName [info exists current_material_to_load(name)]
    set modify_material [info exists current_material_to_load(name)]

    if {$solve_format == "2"} {
       ::AIEnv::GUIManagement::ToggleGrey LSDynaMaterialType 0
    } else {
       ::AIEnv::GUIManagement::ToggleGrey LSDynaMaterialType 1
    }
    
    set AllowActiveReload 1
    puts load
}

proc onCreateMatDEZApply {} {
    global mat_name_list modify_material

    if {[info exists modify_material] && $modify_material != 1} {
       if {$name != ""} {
          set name2 [ic_geo_valid_name $name 1]
          if {$name2 != $name} {
             mess "Name \"$name\" is not valid - changed to \"$name2\"\n" red
             set name $name2
          }
          if {[info exists mat_name_list] && [lsearch $mat_name_list $name] >= 0} {
             user_error "The Name entered is already used.\nPlease enter another Name."
             return
          }
       }
    }

    if ![info exists mat_name_list] {set mat_name_list ""}

    foreach val {Valpoiss1 Valpoiss2 Ortho2DValpoiss1 Ortho2DValpoiss2} {
       set w [createMatDEZ pathIgnoreErrors $val]
       foreach wc $w {
           if [string match *-text $wc] {
              if {$poiss != "" && ($poiss < 0 || $poiss >0.5)}  {      
                 $wc configure -background red
              } else {
                  $wc configure -background white
             }
           }
      }
   }

   if {$poiss != "" && ($poiss < 0 || $poiss >0.5)} {
       user_error "Invalid Poisson's ratio.\nPlease enter a number between 0 and 0.5."
       return
   }

    foreach t2 {Yngs Shear Pois Dens CTE Dampc Slt Slc Sls \
                      G12 G13 G14 G15 G16 G22 G23 G24 G25 G26 \
                      G33 G34 G35 G36 G44 G45 G46 G55 G56 G66 \
                      A2 A3 A4 A5 A6 E2 Yt Yc F12 Strn ThermK ThermCP ThermH VISC HGEN \
                      ThermKxx ThermKxy ThermKxz ThermKyy ThermKyz ThermKzz} {
        set val1 Mat${t2}Tbl
        set val2 Mat${t2}Option
        if {[set $val2] == 0} {
             set $val1 ""
             set mat_dpnd 0
        }
    }
 
    if {$MatYngsTbl !="" || $MatShearTbl !="" || $MatPoisTbl !="" || $MatDensTbl !="" || $MatCTETbl !="" || $MatDampcTbl !="" || \
        $MatSltTbl !="" || $MatSlcTbl !="" || $MatSlsTbl !="" || $MatG12Tbl !="" || $MatG13Tbl !="" || $MatG14Tbl !="" || \
        $MatG15Tbl !="" || $MatG16Tbl !="" || $MatG22Tbl !="" || $MatG23Tbl !="" || $MatG24Tbl !="" || $MatG25Tbl !="" || \
        $MatG26Tbl !="" || $MatG33Tbl !="" || $MatG34Tbl !="" || $MatG35Tbl !="" || $MatG36Tbl !="" || $MatG44Tbl !="" || \
        $MatG45Tbl !="" || $MatG46Tbl !="" || $MatG55Tbl !="" || $MatG56Tbl !="" || $MatG66Tbl !="" || $MatA2Tbl !="" || \
        $MatA3Tbl !="" || $MatA4Tbl !="" || $MatA5Tbl !="" || $MatA6Tbl !="" || $MatE2Tbl !="" || $MatYtTbl !="" || \
        $MatYcTbl !="" || $MatF12Tbl !="" || $MatStrnTbl !="" || $MatThermKTbl != "" || $MatThermCPTbl != "" || \
        $MatThermHTbl !="" || $MatVISCTbl !="" || $MatHGENTbl  !="" || $MatThermKxxTbl !="" || $MatThermKxyTbl !="" || \
        $MatThermKxzTbl !="" || $MatThermKyyTbl !="" || $MatThermKyzTbl !="" || $MatThermKzzTbl !=""} {
       set mat_dpnd 1
    }

    aie_set_material_type $name [list $material_format $mat_dpnd]

    if {$material_format == 0} {
       aie_set_material_data $name [list $number $yngs $shear $poiss $dens $cte $ref $dampc $slt $slc $sls $mcsid $lsdyna_matType \
	   								$lsdyna_mat24_sigy $lsdyna_mat24_fail]
       aie_set_material_dpnd_data $name [list $number $MatYngsTbl $MatShearTbl $MatPoisTbl $MatDensTbl $MatCTETbl \
                                                                    $MatDampcTbl $MatSltTbl $MatSlcTbl $MatSlsTbl]
    }
    if {$material_format == 1} {
       aie_set_material_data $name [list $number $shear $g12 $g13 $g22 $g23 $g33 $dens $cte $a2 $a3 $ref $dampc \
                                                           $slt $slc $sls $mcsid]
       aie_set_material_dpnd_data $name [list $number $MatShearTbl $MatG12Tbl $MatG13Tbl $MatG22Tbl $MatG23Tbl $MatG33Tbl \
                                                                    $MatDensTbl $MatCTETbl $MatA2Tbl $MatA3Tbl $MatDampcTbl  \
                                                                    $MatSltTbl $MatSlcTbl $MatSlsTbl]
    }
    if {$material_format == 2} {
       aie_set_material_data $name [list $number $shear $g12 $g13 $g14 $g15 $g16 $g22 $g23 $g24 $g25 $g26 $g33 $g34 $g35 $g36 \
                                                           $g44 $g45 $g46 $g55 $g56 $g66 $dens $cte $a2 $a3 $a4 $a5 $a6 $ref $dampc] 
       aie_set_material_dpnd_data $name [list $number $MatShearTbl $MatG12Tbl $MatG13Tbl $MatG14Tbl $MatG15Tbl $MatG16Tbl \
                                                                    $MatG22Tbl $MatG23Tbl $MatG24Tbl $MatG25Tbl $MatG26Tbl $MatG33Tbl $MatG34Tbl \
                                                                    $MatG35Tbl $MatG36Tbl $MatG44Tbl $MatG45Tbl $MatG46Tbl $MatG55Tbl $MatG56Tbl \
                                                                    $MatG66Tbl $MatDensTbl $MatCTETbl $MatA2Tbl $MatA3Tbl $MatA4Tbl $MatA5Tbl $MatA6Tbl \
                                                                    $MatDampcTbl]
    }
    if {$material_format == 3} {
       aie_set_material_data $name [list $number $yngs $e2 $poiss $g12 $g13 $g23 $dens $cte $a2 $ref $slt $slc $yt $yc $sls $dampc \
                                                           $f12 $strn]
       aie_set_material_dpnd_data $name [list $number $MatYngsTbl $MatE2Tbl $MatPoisTbl $MatG12Tbl $MatG13Tbl $MatG23Tbl \
                                                                    $MatDensTbl $MatCTETbl $MatA2Tbl $MatSltTbl $MatSlcTbl $MatYtTbl $MatYcTbl $MatSlsTbl \
                                                                    $MatDampcTbl $MatF12Tbl $MatStrnTbl]
    }
    if {$material_format == 4} {
       aie_set_material_data $name [list $number $thermk $thermcp $dens $thermh $visc $hgen $ref $tch $tdelta $qlat]
       aie_set_material_dpnd_data $name [list $number $MatThermKTbl $MatThermCPTbl $MatThermHTbl $MatVISCTbl $MatHGENTbl]
    }
    if {$material_format == 5} {
       aie_set_material_data $name [list $number $thermkxx $thermkxy $thermkxz $thermkyy $thermkyz $thermkzz $thermcp $dens $hgen]
       aie_set_material_dpnd_data $name [list $number $MatThermKxxTbl $MatThermKxyTbl $MatThermKxzTbl $MatThermKyyTbl $MatThermKyzTbl \
                                         $MatThermKzzTbl $MatThermCPTbl $MatHGENTbl]
    }

    if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
       lappend mat_name_list $name
    }

    createMatDEZ setclean

}

proc aie_set_material_type {name vals} {
    global material_data material_type
    set material_type($name) $vals
}

proc aie_set_material_data {name vals} {
    global material_data material_type
    global use_workbench
    set material_data($name) $vals
    ::AIEnv::GUIManagement::Tree::PutMaterials [list $name] 0
    ic_param_set_modified
    if {[info exists use_workbench] && $use_workbench} {
        aie_wb_update_material [list $name]
    }
}

proc aie_set_material_dpnd_data {name vals} {
    global material_dpnd_data
    set material_dpnd_data($name) $vals 
}

proc aie_material_name_from_number {n} {
    global material_data
    foreach name [array names material_data] {
	if {$n == [lindex $material_data($name) 0]} { return $name }
    }
    return ""
}

proc aie_edit_material {name {edit_type 0}} {
    global current_material_to_load
    global material_data material_type
    global current_material_dpnd_to_load
    global material_dpnd_data
    global use_workbench wb_material
    
    set current_material_to_load(name) $name
    set current_material_dpnd_to_load(name) $name

    set material_format [lindex $material_type($name) 0]
    set mat_dpnd [lindex $material_type($name) 1]

    if {$material_format == 0} {
       foreach {p n} {
	    number 0 yngs 1 shear 2 poiss 3 dens 4 cte 5 ref 6 dampc 7 slt 8 slc 9 sls 10 mcsid 11 lsdyna_matType 12 \
			lsdyna_mat24_sigy 13 lsdyna_mat24_fail 14
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatYngsTbl 1 MatShearTbl 2 MatPoisTbl 3 MatDensTbl 4 MatCTETbl 5 MatDampcTbl 6 MatSltTbl 7 MatSlc 8 MatSls 9
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {$material_format == 1} {
       foreach {p n} {
	    number 0 shear 1 g12 2 g13 3 g22 4 g23 5 g33 6 dens 7 cte 8 a2 9 a3 10 ref 11 dampc 12 slt 13 slc 14 sls 15 mcsid 16
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatShearTbl 1 MatG12Tbl 2 MatG13Tbl 3 MatG22Tbl 4 MatG23Tbl 5 MatG33Tbl 6 MatDensTbl 7 MatCTETbl 8
            MatA2Tbl 9 MatA3Tbl 10 MatDampcTbl  11 MatSltTbl 12 MatSlcTbl 13 MatSlsTbl 14
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {$material_format == 2} {
       foreach {p n} {
	    number 0 shear 1 g12 2 g13 3 g14 4 g15 5 g16 6 g22 7 g23 8 g24 9 g25 10 g26 11 g33 12 g34 13 g35 14 g36 15
              g44 16 g45 17 g46 18 g55 19 g56 20 g66 21 dens 22 cte 23 a2 24 a3 25 a4 26 a5 27 a6 28 ref 29 dampc 30
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatShearTbl 1 MatG12Tbl 2 MatG13Tbl 3 MatG14Tbl 4 MatG15Tbl 5 MatG16Tbl 6 MatG22Tbl 7 MatG23Tbl 8
            MatG24Tbl 9 MatG25Tbl 10 MatG26Tbl 11 MatG33Tbl 12 MatG34Tbl 13 MatG35Tbl 14 MatG36Tbl 15 MatG44Tbl 16 
            MatG45Tbl 17 MatG46Tbl 18 MatG55Tbl 19 MatG56Tbl 20 MatG66Tbl 21 MatDensTbl 22 MatCTETbl 23 MatA2Tbl 24
            MatA3Tbl 25 MatA4Tbl 26 MatA5Tbl 27 MatA6Tbl 28 MatDampcTbl 29
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {$material_format == 3} {
       foreach {p n} {
	    number 0 yngs 1 e2 2 poiss 3 g12 4 g13 5 g23 6 dens 7 cte 8 a2 9 ref 10 slt 11 slc 12 yt 13 yc 14 sls 15 dampc 16 f12 17 strn 18
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatYngsTbl 1 MatE2Tbl 2 MatPoisTbl 3 MatG12Tbl 4 MatG13Tbl 5 MatG23Tbl 6 MatDensTbl 7 MatCTETbl 8 
            MatA2Tbl 9 MatSltTbl 10 MatSlcTbl 11 MatYtTbl 12 MatYcTbl 13 MatSlsTbl 14 MatDampcTbl 15 MatF12Tbl 16 MatStrnTbl 17
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {$material_format == 4} {
       foreach {p n} {
	    number 0 thermk 1 thermcp 2 dens 3 thermh 4 visc 5 hgen 6 ref 7 tch 8 tdelta 9 qlat 10
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatThermKTbl 1 MatThermCPTbl 2 MatThermHTbl 3 MatVISCTbl 4 MatHGENTbl 5 
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {$material_format == 5} {
       foreach {p n} {
	    number 0 thermkxx 1 thermkxy 2 thermkxz 3 thermkyy 4 thermkyz 5 thermkzz 6 thermcp 7 dens 8 hgen 9
       } {
	    set current_material_to_load($p) [lindex $material_data($name) $n]
       }
       foreach {p n} {
	   number 0 MatThermKxxTbl 1 MatThermKxyTbl 2 MatThermKxzTbl 3 MatThermKyyTbl 4 MatThermKyzTbl 5 MatThermKzzTbl 6 MatThermCPTbl 7 MatHGENTbl 8 
       } {
             if [info exists material_dpnd_data($name)] {
	       set current_material_dpnd_to_load($p) [lindex $material_dpnd_data($name) $n]
            }
       }
    }

    if {[info exists use_workbench] && $use_workbench  && $edit_type == 0} {
        set wb_material $name
        AIEWBSendToWBScript "AIEEditEnggDataObject"
    } else {
       createMatDEZ show
    }
    
    if [info exists current_material_to_load] {unset current_material_to_load}
    if [info exists current_material_dpnd_to_load] {unset current_material_dpnd_to_load}
}

proc aie_delete_material {name} {
    global material_data material_type
    global material_dpnd_data
    global mat_name_list
    
    catch {unset material_data($name)}
    catch {unset material_type($name)}
    catch {unset material_dpnd_data($name)}
    if [info exists mat_name_list] {
        set pos [lsearch $mat_name_list $name]
        if { $pos >= 0} {
            set mat_name_list [lreplace $mat_name_list $pos $pos]
        }
    }
    ::AIEnv::GUIManagement::Tree::DeleteMaterials [list $name]
    ic_param_set_modified
}

proc aie_rename_material {name} {
    global material_data material_type
    global material_dpnd_data
    global mat_name_list
    
    set newname [question_box .newname "New name" "New name for material $name" $name]
    if {$newname == "" || $newname == $name || [string toupper $newname] == [string toupper $name]} return

    #if ![aie_valid_name $newname 1] {return}
    #set newname [string toupper $newname]
    set newname [aie_get_valid_name $newname]

    if {[info exists mat_name_list] && [lsearch $mat_name_list $newname] >= 0} {
       user_error "The Name entered is already used.\nPlease enter another Name."
       return
    }
       
    if [info exists material_data($name)] {
        set material_data($newname) $material_data($name)
        unset material_data($name)
    }
    if [info exists material_type($name)] {
        set material_type($newname) $material_type($name)
        unset material_type($name)
    }
    if [info exists material_dpnd_data($name)] {
        set material_dpnd_data($newname) $material_dpnd_data($name)
        unset material_dpnd_data($name)
    }
    set pos [lsearch $mat_name_list $name]
    if { $pos >= 0} {
       set mat_name_list [lreplace $mat_name_list $pos $pos]
    }
    if {[info exists mat_name_list] && [lsearch $mat_name_list $newname] < 0} {
       lappend mat_name_list $newname
    }
    ::AIEnv::GUIManagement::Tree::RenameMaterial $name $newname
    ic_param_set_modified
}

proc aie_tree_load_materials {clear} {
    global material_data material_dpnd_data env
    global material_type
    global use_workbench
    global mat_name_list
    
    if [info exists env(AI_ENV_CFX)] return  ;# for now
    
    if $clear { catch {unset material_data} }

    if ![info exists mat_name_list] {set mat_name_list ""}
    
    set mats {}
    if 0 {
	foreach f [ic_boco_list_families] {
	    foreach b [ic_boco_get $f] {
		if {[lindex $b 1] == "MAT"} {
		    set name MAT[lindex $b 2]
		    set material_data($name) [lrange $b 2 end]
		    lappend mats [concat $name $material_data($name)]
		}
	    }
	}
    }

    foreach mat [ic_param_list_children :Isotropic_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Isotropic_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Isotropic_Material_Temperature_Dependence $name]
          aie_material_nonlinear_from_param Stress_Dependent_Material $name
          set material_format 0
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }
    foreach mat [ic_param_list_children :Anisotropic_Shell_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Anisotropic_Shell_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Anisotropic_Shell_Temperature_Dependence $name]
          set material_format 1
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }
    foreach mat [ic_param_list_children :Anisotropic_Solid_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Anisotropic_Solid_Material $name]
         set material_dpnd_data($name) [aie_material_dpnd_from_param Anisotropic_Solid_Temperature_Dependence $name]
         set material_format 2
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }
    foreach mat [ic_param_list_children :Orthotropic_Shell_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Orthotropic_Shell_Material $name]
         set material_format 3
         set mat_dpnd 0
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }
    foreach mat [ic_param_list_children :Isotropic_Thermal_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Isotropic_Thermal_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Isotropic_Thermal_Material_Temperature_Dependence $name]
          set material_format 4
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }
    foreach mat [ic_param_list_children :Anisotropic_Thermal_Material] {
	set name [lindex [split $mat /] 1]
	set material_data($name) [aie_material_from_param Anisotropic_Thermal_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Anisotropic_Thermal_Material_Temperature_Dependence $name]
          set material_format 5
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
	if {[info exists mat_name_list] && [lsearch $mat_name_list $name] < 0} {
	   lappend mat_name_list $name
         }
    }

    ::AIEnv::GUIManagement::Tree::PutMaterials $mats $clear
    if {[info exists use_workbench] && $use_workbench} {
        aie_wb_update_material $mats
    }
}

proc aie_material_property_order {type} {

    if  {$type == "Isotropic_Material"} {
       return {
	Material_ID Youngs_Modulus Shear_Modulus Poissons_Ratio
	Mass_Density Thermal_Expansion_Coefficient Reference_Temperature 
	Structural_Damping_Coefficient Stress_Limit_Tension 
         Stress_Limit_Compression Stress_Limit_Shear Material_Coordinate_System LSDyna_Material_Type \
		 LSDyna_Material24_Sigy LSDyna_Material24_Fail
      }
    }
    if {$type == "Anisotropic_Shell_Material"} {
       return {
	Material_ID Material_Matrix_Element_G11 Material_Matrix_Element_G12 Material_Matrix_Element_G13     
         Material_Matrix_Element_G22 Material_Matrix_Element_G23 Material_Matrix_Element_G33 Mass_Density                    
         Thermal_Expansion_Coefficient_Component_1 Thermal_Expansion_Coefficient_Component_2  Thermal_Expansion_Coefficient_Component_3 
         Reference_Temperature Structural_Damping_Coefficient Stress_Limit_Tension Stress_Limit_Compression        
         Stress_Limit_Shear Material_Coordinate_System
      } 
    }
     if {$type == "Anisotropic_Solid_Material"} {
       return {
        Material_ID Material_Matrix_Element_G11 Material_Matrix_Element_G12 Material_Matrix_Element_G13     
        Material_Matrix_Element_G14 Material_Matrix_Element_G15 Material_Matrix_Element_G16 Material_Matrix_Element_G22     
        Material_Matrix_Element_G23 Material_Matrix_Element_G24 Material_Matrix_Element_G25 Material_Matrix_Element_G26     
        Material_Matrix_Element_G33 Material_Matrix_Element_G34 Material_Matrix_Element_G35 Material_Matrix_Element_G36     
        Material_Matrix_Element_G44 Material_Matrix_Element_G45 Material_Matrix_Element_G46 Material_Matrix_Element_G55     
        Material_Matrix_Element_G56 Material_Matrix_Element_G66 Mass_Density Thermal_Expansion_Coefficient_A1
        Thermal_Expansion_Coefficient_A2 Thermal_Expansion_Coefficient_A3 Thermal_Expansion_Coefficient_A4
        Thermal_Expansion_Coefficient_A5 Thermal_Expansion_Coefficient_A6 Reference_Temperature Structural_Damping_Coefficient  
      }
    }
     if {$type == "Orthotropic_Shell_Material"} {
       return {
       Material_ID Youngs_Modulus_Longitudinal Youngs_Modulus_Lateral Poissons_Ratio                  
       InPlane_Shear_Modulus_G12 Transverse_Shear_Modulus_G1Z Transverse_Shear_Modulus_G2Z    
       Mass_Density Thermal_Expansion_Coefficient_A1 Thermal_Expansion_Coefficient_A2
       Reference_Temperature Allowable_Longitudinal_Tension Allowable_Longitudinal_Compression
       Allowable_Transverse_Tension  Allowable_Transverse_Compression Allowable_InPlane_Shear         
       Structural_Damping_Coefficient Tsai-Wu_Interaction_Term Stress_or_strain
      }
    }
    if  {$type == "Isotropic_Thermal_Material"} {
       return {
	Material_ID Thermal_Conductivity Heat_Capacity Density Free_Convection_Heat_Transfer_Coefficient
	Dynamic_Viscosity Heat_Generation_Capability Reference_Enthalpy Phase_Change_Region_Lower_Temperature
	Total_Temperature_Range_for_Phase_Change Latent_Heat_of_Fusion
       }
    }
    if  {$type == "Anisotropic_Thermal_Material"} {
       return {
	Material_ID Thermal_Conductivity_xx Thermal_Conductivity_xy Thermal_Conductivity_xz Thermal_Conductivity_yy
	Thermal_Conductivity_yz Thermal_Conductivity_zz Heat_Capacity Density Heat_Generation_Capability
       }
    }
}

proc aie_material_dpnd_property_order {type} {

    if  {$type == "Isotropic_Material_Temperature_Dependence"} {
      return {
	Isotropic_Material_ID Temperature_Dependent_Youngs_Modulus Temperature_Dependent_Shear_Modulus 
         Temperature_Dependent_Poissons_Ratio Temperature_Dependent_Mass_Density 
         Temperature_Dependent_Thermal_Expansion_Coefficient 
	Temperature_Dependent_Structural_Damping_Coefficient
          Temperature_Dependent_Tension_Limit Temperature_Dependent_Compression_Limit Temperature_Dependent_Shear_Stress_Limit
      }
    }
     if {$type == "Anisotropic_Shell_Temperature_Dependence"} {
      return {
	Anisotropic_Shell_ID Temperature_Dependent_Matrix_Element_G11 Temperature_Dependent_Matrix_Element_G12
	Temperature_Dependent_Matrix_Element_G13 Temperature_Dependent_Matrix_Element_G22
	Temperature_Dependent_Matrix_Element_G23 Temperature_Dependent_Matrix_Element_G33
	Temperature_Dependent_Mass_Density Temperature_Dependent_Expansion_1
	Temperature_Dependent_Expansion_2 Temperature_Dependent_Expansion_3
	Temperature_Dependent_Damping_Coefficient Temperature_Dependent_Tension_Limit
	Temperature_Dependent_Compression_Limit Temperature_Dependent_Shear_Limit      
      }
    }
     if {$type == "Anisotropic_Solid_Temperature_Dependence"} {
      return {
        Anisotropic_Solid_ID Temperature_Dependent_Matrix_Element_G11 Temperature_Dependent_Matrix_Element_G12
        Temperature_Dependent_Matrix_Element_G13 Temperature_Dependent_Matrix_Element_G14 Temperature_Dependent_Matrix_Element_G15
        Temperature_Dependent_Matrix_Element_G16 Temperature_Dependent_Matrix_Element_G22 Temperature_Dependent_Matrix_Element_G23
        Temperature_Dependent_Matrix_Element_G24 Temperature_Dependent_Matrix_Element_G25 Temperature_Dependent_Matrix_Element_G26
        Temperature_Dependent_Matrix_Element_G33 Temperature_Dependent_Matrix_Element_G34 Temperature_Dependent_Matrix_Element_G35
        Temperature_Dependent_Matrix_Element_G36 Temperature_Dependent_Matrix_Element_G44 Temperature_Dependent_Matrix_Element_G45
        Temperature_Dependent_Matrix_Element_G46 Temperature_Dependent_Matrix_Element_G55 Temperature_Dependent_Matrix_Element_G56
        Temperature_Dependent_Matrix_Element_G66 Temperature_Dependent_Mass_Density Temperature_Dependent_Expansion_1 
        Temperature_Dependent_Expansion_2 Temperature_Dependent_Expansion_3 Temperature_Dependent_Expansion_4 
        Temperature_Dependent_Expansion_5 Temperature_Dependent_Expansion_6 Temperature_Dependent_Damping_Coefficient    
      }
    }
    if  {$type == "Stress_Dependent_Material"} {
      return {
	Material_ID Stress_Dependent_Table_ID Nonlinearity_Type Work_Hardening_Slope
	Yield_Function_Criterion Hardening_Rule Initial_Yield_Point Internal_Friction_Angle_Degrees
      }
    }
    if  {$type == "Isotropic_Thermal_Material_Temperature_Dependence"} {
      return {
	Isotropic_Thermal_Material_ID Temperature_Dependent_Thermal_Conductivity Temperature_Dependent_Heat_Capacity
	Temperature_Dependent_Free_Convection_Heat_Transfer_Coefficient Temperature_Dependent_Dynamic_Viscosity
	Temperature_Dependent_Heat_Generation_Capability
      }
    }
    if  {$type == "Anisotropic_Thermal_Material_Temperature_Dependence"} {
      return {
	Anisotropic_Thermal_Material_ID Temperature_Dependent_Thermal_Conductivity_xx Temperature_Dependent_Thermal_Conductivity_xy			
	Temperature_Dependent_Thermal_Conductivity_xz Temperature_Dependent_Thermal_Conductivity_yy Temperature_Dependent_Thermal_Conductivity_yz		
	Temperature_Dependent_Thermal_Conductivity_zz Temperature_Dependent_Heat_Capacity Temperature_Dependent_Heat_Generation_Capability		
      }
    }
}

#proc aie_material_dpnd_property_order {} {
#    return {
#	Isotropic_Material_ID Temperature_Dependent_Youngs_Modulus Temperature_Dependent_Shear_Modulus 
#         Temperature_Dependent_Poissons_Ratio Temperature_Dependent_Mass_Density 
#         Temperature_Dependent_Thermal_Expansion_Coefficient 
#	Temperature_Dependent_Structural_Damping_Coefficient
#    }
#}

proc aie_material_from_param {type name} {
    set vals [ic_param_get_values :$type/$name]
    if {$vals == ""} {
	error "no values for $type $name"
    }
    array set val $vals
#for backward compatibility...
	if ![info exists val(LSDyna_Material_Type)] {set val(LSDyna_Material_Type) 0}
	if ![info exists val(LSDyna_Material24_Sigy)] {set val(LSDyna_Material24_Sigy) 0.0}
	if ![info exists val(LSDyna_Material24_Fail)] {set val(LSDyna_Material24_Fail) 0.0}
    set ret ""
    foreach n [aie_material_property_order $type] {
	if [info exists val($n)] {
	    lappend ret $val($n)
	} else {
	    lappend ret ""
	}
    }
    return $ret
}

proc aie_material_tbl_name_from_number {n} {
    global material_tbl_data material_tbl_type
    foreach name [array names material_tbl_data] {
	if {$n == [lindex $material_tbl_data($name) 0]} { return $name }
    }
    return ""
}

proc aie_material_dpnd_from_param {type name} {
    global material_tbl_data material_tbl_type
    set vals [ic_param_get_values :$type/$name]
    set ret ""
    if {$vals != ""} {
       array set val $vals
       foreach n [aie_material_dpnd_property_order $type] {
	   if [info exists val($n)] {
                 if {$n == "Isotropic_Material_ID" || $n == "Anisotropic_Shell_ID" || $n == "Anisotropic_Solid_ID" || \
                     $n == "Isotropic_Thermal_Material_ID" || $n == "Anisotropic_Thermal_Material_ID"} {
                        lappend ret $val($n)
                 } else {
	              lappend ret [aie_material_tbl_name_from_number $val($n)]
                 }
	   } else {
	       lappend ret ""
	   }
       }
    }
    return $ret
}

proc aie_material_nonlinear_from_param {type name} {
    global material_data material_dpnd_data material_type
    global material_tbl_data material_tbl_type

    set vals [ic_param_get_values :$type/$name]
    
    if {$vals != ""} {
       array set val $vals
       foreach n [aie_material_dpnd_property_order $type] {
	   if [info exists val($n)] {
                 if {$n == "Stress_Dependent_Table_ID"} {
                        set new [aie_material_tbl_name_from_number $val($n)]
                        set material_dpnd_data($name)  [lreplace $material_dpnd_data($name) 1 1 $new]
                  }
            }
       }
    }

}

proc aie_update_materials_to_params {} {
    global material_data material_dpnd_data material_tbl_data material_tbl_type
    global material_type
    
    foreach m { :Isotropic_Material_Temperature_Dependence :Anisotropic_Shell_Temperature_Dependence :Anisotropic_Solid_Temperature_Dependence \
                :Stress_Dependent_Material :Isotropic_Thermal_Material_Temperature_Dependence :Anisotropic_Thermal_Material_Temperature_Dependence \
                :Isotropic_Material :Anisotropic_Shell_Material :Anisotropic_Solid_Material :Orthotropic_Shell_Material :Isotropic_Thermal_Material \
                :Anisotropic_Thermal_Material } {
        foreach mat [ic_param_list_children $m] {
            ic_param_delete_node $mat
        }
    }
     
    foreach mat [array names material_dpnd_data] {
	    set material_format [lindex $material_type($mat) 0]
	    set mat_dpnd [lindex $material_type($mat) 1]
	    if {$mat_dpnd == "1"} {
	        set values ""
	        set id 0
	        if {$material_format == 0} {set type "Isotropic_Material_Temperature_Dependence"}
	        if {$material_format == 1} {set type "Anisotropic_Shell_Temperature_Dependence"}
	        if {$material_format == 2} {set type "Anisotropic_Solid_Temperature_Dependence"}
	        if {$material_format == 4} {set type "Isotropic_Thermal_Material_Temperature_Dependence"}
	        if {$material_format == 5} {set type "Anisotropic_Thermal_Material_Temperature_Dependence"}
	        foreach n [aie_material_dpnd_property_order $type] v $material_dpnd_data($mat) {
		        set id [expr int($id) + 1]
		        if {$id > 1 && $v != ""} {
		            if ![info exists material_tbl_data($v)] {
			            user_error "Material $mat uses a table $v that no longer exists.  Saving without the table reference.  Please fix the material definition or recreate the table."
			            lappend values $n ""
		            } else {
			            set matid [lindex $material_tbl_data($v) 0]
			            if {[lindex $material_tbl_type($v) 0] == 0} {
			                lappend values $n $matid
			            } else {
			                lappend values $n ""
			            }
		            }
		        } else {
		            lappend values $n $v
		        }
	       }
	       ic_param_add_node :$type $mat $values
	    }
    }
    
    foreach mat [array names material_dpnd_data] {
	    set material_format [lindex $material_type($mat) 0]
	    set mat_dpnd [lindex $material_type($mat) 1]
	    if {$material_format == 0 && $mat_dpnd == 1} {
	        set values ""
	        set id 0
	        set type "Stress_Dependent_Material"
	        set nonlinear 0
	        foreach n [aie_material_dpnd_property_order $type] v $material_dpnd_data($mat) {
		        set id [expr int($id) + 1]
		        if {$id == 1 && $v != ""} {
		            lappend values $n $v
		        } elseif {$id == 2 && $v != ""} {
		            if ![info exists material_tbl_data($v)] {
			            lappend values $n ""
		            } else {
			            set matid [lindex $material_tbl_data($v) 0]
			            if {[lindex $material_tbl_type($v) 0] != 0} {
			                set nonlinear 1
			                lappend values $n $matid
			            } else {
			                lappend values $n ""
			            }
		            }
		        } elseif {$n != ""} {
		            if {$n == "Nonlinearity_type"} {
			            lappend values $n "NLELAST"
		            } else {
			            lappend values $n ""
		            }
		        }
	        }
	        if {$nonlinear} {ic_param_add_node :$type $mat $values}
	    }
    }
    
    foreach mat [array names material_data] {
	    set material_format [lindex $material_type($mat) 0]
	    set mat_dpnd [lindex $material_type($mat) 1]
	    set values ""
	    if {$material_format == 0} {set type "Isotropic_Material"}	
	    if {$material_format == 1} {set type "Anisotropic_Shell_Material"}
	    if {$material_format == 2} {set type "Anisotropic_Solid_Material"}
	    if {$material_format == 3} {set type "Orthotropic_Shell_Material"}
	    if {$material_format == 4} {set type "Isotropic_Thermal_Material"}
	    if {$material_format == 5} {set type "Anisotropic_Thermal_Material"}
	    foreach n [aie_material_property_order $type] v $material_data($mat) {
	        lappend values $n $v
	    }
	    ic_param_add_node :$type $mat $values
    }
    
}

proc aie_read_materials {} {
    set rootn [aie_import_nastran "" 0 1]
    if {$rootn == ""} return
    set par $rootn.par
    
    #famboco_edit_load_param_values $rootn.par
    ic_param_load $rootn.par 0
    aie_load_tbls 0
    aie_tree_load_materials 0
}

proc aie_write_materials {} {
    global file_name
    set nas [tk_getSaveFile -initialdir $file_name(project_dir) \
		 -filetypes {{{Nastran material files} *.mat} {{All files} *}}]
    if {$nas == ""} return

    if {[file extension $nas] != ".mat"} {
       set nas [file root $nas].mat
    }

    set nas_par [file root $nas].par
    
    aie_update_materials_to_params
    famboco_edit_save_param_values $nas.par [list :Isotropic_Material/* :Anisotropic_Shell_Material/* :Anisotropic_Solid_Material/* :Orthotropic_Shell_Material/* :Isotropic_Thermal_Material/* :Anisotropic_Thermal_Material/*]
    set mpar [file rootname $nas].mat.par
    ic_param_save $mpar {*Material* *Dependence*}
    aie_export_nastran_materials $nas $mpar
    mess "The nastran format file is $nas\nYou can read it using Properties->Read Material File\n\n" blue
    mess "The AI*Environment parameter format file is $mpar\nYou can read it using File->Parameters->Open Parameters\n" blue
}

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

proc oncreateMatTblDEZLoad {} {
    global current_material_tbl_to_load material_tbl_data material_tbl_type
    global modify_material_tbl
    
    foreach {n v} {
	tblname "" tblid 0 x_axis_type "LINEAR" y_axis_type "LINEAR" table_param_1 "0.0" table_param_2 "0.0"
         table_param_3 "0.0" table_param_4 "0.0" length 0 mattbl ""
    } {
	if [info exists current_material_tbl_to_load($n)] {
	    set $n $current_material_tbl_to_load($n)
	} else {
	    set $n $v
	}
    }
    if {$tblname == ""} {
	foreach i [array names material_tbl_data] {
	    set used($i) 1
	    set used([lindex $material_tbl_data($i) 0]) 1
	}
	for {set i 1} {[info exists used(TABLE$i)] || [info exists used($i)]} {incr i} { }
	set tblname TABLE$i
	set tblid $i
    }

    ::AIEnv::GUIManagement::ToggleGrey createMatTblName [info exists current_material_tbl_to_load(tblname)]
    set modify_material_tbl [info exists current_material_tbl_to_load(tblname)]
    
    set AllowActiveReload 1
    puts load
     
}

proc onCreateMatTblDEZApply {} {
    global tbl_name_list modify_material_tbl

    if {[info exists modify_material_tbl] && $modify_material_tbl != 1} {
       if {$tblname != ""} {
          set name2 [ic_geo_valid_name $tblname 1]
          if {$name2 != $tblname} {
             mess "Name \"$tblname\" is not valid - changed to \"$name2\"\n" red
             set tblname $name2
          }
          if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] >= 0} {
             user_error "The Name entered is already used.\nPlease enter another Name."
             return
          }
       }
    }

    if ![info exists tbl_name_list] {set tbl_name_list ""}

    if [info exists tablvals] {unset tablvals}
    array set tablvals $mattbl

    foreach n [array names tablvals] {
        set tblesize [llength $tablvals($n)] 
        if {$n == "temp"} {set table_type 0}
        if {$n == "nonlinear"} {set table_type 1}
        if {$n == "temp_nonlinear"} {set table_type 2}                                 
    }

    set length [expr int($tblesize)/2] 

    aie_set_material_tbl_type $tblname [list $table_type]

    if {$table_type == 0} {
        aie_set_material_tbl_data $tblname [list $tblid $x_axis_type $y_axis_type $table_param_1 \
                                                                 $table_param_2 $table_param_3 $table_param_4 $length $mattbl]
    } else {
        aie_set_material_tbl_data $tblname [list $tblid $length $mattbl]
    }
    if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
       lappend tbl_name_list $tblname
    }

}

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

proc oncreateTblDEZLoad {} {
    global current_material_tbl_to_load material_tbl_data material_tbl_type
    global aie_edit_table table_data
    global modify_material_tbl
    
    array set aie_edit_table { temp 0 nonlinear 0 load 0 damping 0 data "" }

    set TblTypList {"Temperature Dependent" "Nonlinear" "Nonlinear Temperature Dependent" "Dynamic Load" "Modal Damping"}

    set TblTyp "Temperature Dependent"
    set tbl_found 0

    foreach {n v} {
	tblname "" tblid 0 x_axis_type "LINEAR" y_axis_type "LINEAR" table_param_1 "0.0" table_param_2 "0.0"
         table_param_3 "0.0" table_param_4 "0.0" table_damp_type "" length 0 mattbl "" 
    } {
	if [info exists current_material_tbl_to_load($n)] {
              if {$n == "mattbl"} {
                  set tbl_found 1
                  if {[lindex $current_material_tbl_to_load($n) 0] == "temp"} {set TblTyp "Temperature Dependent"}
                  if {[lindex $current_material_tbl_to_load($n) 0] == "nonlinear"} {set TblTyp "Nonlinear"}
                  if {[lindex $current_material_tbl_to_load($n) 0] == "temp_nonlinear"} {set TblTyp "Nonlinear Temperature Dependent"}
                 if {[lindex $current_material_tbl_to_load($n) 0] == "load"} {set TblTyp "Dynamic Load"}
                 if {[lindex $current_material_tbl_to_load($n) 0] == "damping"} {set TblTyp "Modal Damping"}
              }
	    set $n $current_material_tbl_to_load($n)
	} else {
	    set $n $v
	}
    }

    if {!$tbl_found} {set table_data ""}

    if {$tblname == ""} {
	foreach i [array names material_tbl_data] {
	    set used($i) 1
	    set used([lindex $material_tbl_data($i) 0]) 1
	}
	for {set i 1} {[info exists used(TABLE$i)] || [info exists used($i)]} {incr i} { }
	set tblname TABLE$i
	set tblid $i
    }
    
    ::AIEnv::GUIManagement::ToggleGrey createTblName [info exists current_material_tbl_to_load(tblname)]
    set modify_material_tbl [info exists current_material_tbl_to_load(tblname)]

    set AllowActiveReload 1
    puts load
     
}

proc onCreateTblDEZApply {} {
    global table_data
    global tbl_name_list modify_material_tbl

    if [info exists aie_edit_table(currently_editing)] {
	    tk_dialog .confirm Table "Please finish editing the table you have up now before you start editing another one." "" -1 Ok
	    if [winfo exists .confirm] { top_destroy .confirm }
        createTblDEZ setdirty
	    return
    }

    if {[info exists modify_material_tbl] && $modify_material_tbl != 1} {
       if {$tblname != ""} {
          set name2 [ic_geo_valid_name $tblname 1]
          if {$name2 != $tblname} {
             mess "Name \"$tblname\" is not valid - changed to \"$name2\"\n" red
             set tblname $name2
          }
          if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] >= 0} {
             user_error "The Name entered is already used.\nPlease enter another Name."
             return
          }
       }
    }

    if ![info exists tbl_name_list] {set tbl_name_list ""}

    set tblesize ""

    set mattbl $table_data

    if [info exists tablvals] {unset tablvals}
    
    if {[llength $mattbl] > 1} {
       array set tablvals $mattbl

       foreach n [array names tablvals] {
           set tblesize [llength $tablvals($n)] 
           if {$n == "temp"} {set table_type 0}
           if {$n == "nonlinear"} {set table_type 1}
           if {$n == "temp_nonlinear"} {set table_type 2}                                 
           if {$n == "load"} {set table_type 3}                                 
           if {$n == "damping"} {set table_type 4}                                 
       }
    }

    if {$tblesize == ""} {
        user_error "NO table was created!\nPease make sure to hit \"Accept\" when done editing the table.\n\n"
        return
    }

    set length [expr int($tblesize)/2] 

    aie_set_material_tbl_type $tblname [list $table_type]

    if {$table_type == 0 || $table_type == 3} {
        aie_set_material_tbl_data $tblname [list $tblid $x_axis_type $y_axis_type $table_param_1 \
                                                                 $table_param_2 $table_param_3 $table_param_4 $length $mattbl]
    } elseif {$table_type == 4} {
        aie_set_material_tbl_data $tblname [list $tblid $table_damp_type $length $mattbl]
    } else {
        aie_set_material_tbl_data $tblname [list $tblid $length $mattbl]
    }

    if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
       lappend tbl_name_list $tblname
    }

}

proc aie_create_table {} {

    global aie_edit_table tdv_default table_data

    set tbl_type [createTblDEZ get TblTyp]
   
    set lvar ""
    set aie_edit_table(temp) 0
    set aie_edit_table(nonlinear) 0
    set aie_edit_table(load) 0
    set aie_edit_table(damping) 0
    set x_label ""
    set y_label ""
    set graph_title ""
   
    if {$tbl_type == "Temperature Dependent"} {set lvar "temp"}
    if {$tbl_type == "Nonlinear"} {set lvar "nonlinear"}
    if {$tbl_type == "Nonlinear Temperature Dependent"} {set lvar "temp_nonlinear"}
    if {$tbl_type == "Dynamic Load"} {set lvar "load"}
    if {$tbl_type == "Modal Damping"} {set lvar "damping"}

    switch -- $lvar {
	temp { 
              set aie_edit_table(temp) 1 
              set x_label "Temp." 
              set y_label "Value" 
              set graph_title "Temperature Dependent Curve"
         }
	nonlinear { 
              set aie_edit_table(nonlinear) 1 
              set x_label "Strain" 
              set y_label "Stress" 
              set graph_title "Stress-Strain Curve"
          }
	temp_nonlinear {
	    set aie_edit_table(temp) 1
	    set aie_edit_table(nonlinear) 1
              set x_label "Temp." 
              set y_label "Table ID" 
              set graph_title "Temperature Dependent Curve"
	}
	load { 
             set aie_edit_table(load) 1 
              set x_label "Time or Freq." 
              set y_label "Load" 
              set graph_title "Dynamic Load Curve"
         }
 	damping { 
             set aie_edit_table(damping) 1 
              set x_label "Natural Freq." 
              set y_label "Damping Val." 
              set graph_title "Modal Damping Curve"
         }
   }

    if {[llength $table_data] > 1} {
       set aie_edit_table(data) [list [lindex $table_data 1]]
    } else {
       set aie_edit_table(data) ""
    }

    if [info exists aie_edit_table(currently_editing)] {
	    tk_dialog .confirm Table "Please finish editing the table you have up now before you start editing another one." "" -1 Ok
	    if [winfo exists .confirm] { top_destroy .confirm }
	    return
    }

    set aie_edit_table(currently_editing) 1
    set par [winfo parent $tdv_default]
    set inf [pack info $tdv_default]
    pack forget $tdv_default
    set top $par.table
    frame $top -relief sunken -borderwidth 3
    eval pack $top $inf
    
    if {$aie_edit_table(temp) && $aie_edit_table(nonlinear)} {
         #set val [st_edit_curves -curves $aie_edit_table(data) -toplevel $top]
	set val [st_edit_curves -curves $aie_edit_table(data) -single -inty -xlabel $x_label -ylabel $y_label -title $graph_title -toplevel $top]
    } else {
	#set val [st_edit_curves -curves $aie_edit_table(data) -single -toplevel $top]
          set val [st_edit_curves -curves $aie_edit_table(data) -single -xlabel $x_label -ylabel $y_label -title $graph_title -toplevel $top]
    }
    if {$val != ""} { set aie_edit_table(data) $val }
    
    eval pack $tdv_default $inf
    unset aie_edit_table(currently_editing)

    set table_data ""
    if {$aie_edit_table(temp) && $aie_edit_table(nonlinear)} {
	eval lappend table_data temp_nonlinear $aie_edit_table(data)
    } elseif $aie_edit_table(temp) {
	eval lappend table_data temp $aie_edit_table(data)
    } elseif $aie_edit_table(nonlinear) {
	eval lappend table_data nonlinear $aie_edit_table(data)
    } elseif $aie_edit_table(load) {
	eval lappend table_data load $aie_edit_table(data)
    } elseif $aie_edit_table(damping) {
	eval lappend table_data damping $aie_edit_table(data)
    }

    createTblDEZ setdirty

}

proc aie_set_material_tbl_type {tblname vals} {
    global material_tbl_type
    set material_tbl_type($tblname) $vals
}

proc aie_set_material_tbl_data {tblname vals} {
    global material_tbl_data material_tbl_type
    set material_tbl_data($tblname) $vals
    ::AIEnv::GUIManagement::Tree::PutMaterialTbls [list $tblname] 0
    ic_param_set_modified
}

proc aie_edit_material_tbl_old {tblname} {
    global current_material_tbl_to_load
    global material_tbl_data material_tbl_type
    
    set current_material_tbl_to_load(tblname) $tblname

    set table_type [lindex $material_tbl_type($tblname) 0] 

    if {$table_type == 0} {
       foreach {p n} {
            tblid 0 x_axis_type 1 y_axis_type 2 table_param_1 3
            table_param_2 4 table_param_3 5 table_param_4 6 length 7 mattbl 8
       } {
	   set current_material_tbl_to_load($p) [lindex $material_tbl_data($tblname) $n]
       }
    } else {
       foreach {p n} {
            tblid 0  length 1 mattbl 2
       } {
	   set current_material_tbl_to_load($p) [lindex $material_tbl_data($tblname) $n]
       }

    }

    createMatTblDEZ show
    
    if [info exists current_material_tbl_to_load] {unset current_material_tbl_to_load}
}

proc aie_edit_material_tbl {tblname} {
    global current_material_tbl_to_load
    global material_tbl_data material_tbl_type
    global table_data
    
    set current_material_tbl_to_load(tblname) $tblname

    set table_type [lindex $material_tbl_type($tblname) 0]

    if {$table_type == 0 || $table_type == 3} {
       set table_data [lindex $material_tbl_data($tblname) 8]
       foreach {p n} {
            tblid 0 x_axis_type 1 y_axis_type 2 table_param_1 3
            table_param_2 4 table_param_3 5 table_param_4 6 length 7 mattbl 8
       } {
	   set current_material_tbl_to_load($p) [lindex $material_tbl_data($tblname) $n]
       }
    } elseif {$table_type == 4} {
       set table_data [lindex $material_tbl_data($tblname) 3]
       foreach {p n} {
            tblid 0  table_damp_type 1 length 2 mattbl 3
       } {
	   set current_material_tbl_to_load($p) [lindex $material_tbl_data($tblname) $n]
       }
    } else {
       set table_data [lindex $material_tbl_data($tblname) 2]
       foreach {p n} {
            tblid 0  length 1 mattbl 2
       } {
	   set current_material_tbl_to_load($p) [lindex $material_tbl_data($tblname) $n]
       }
    }

    createTblDEZ show
    
    if [info exists current_material_tbl_to_load] {unset current_material_tbl_to_load}
}

proc aie_delete_material_tbl {tblname} {
    global material_tbl_data material_tbl_type
    global tbl_name_list
    
    catch {unset material_tbl_data($tblname)}
    catch {unset material_tbl_type($tblname)}
    set pos [lsearch $tbl_name_list $tblname]
    if { $pos >= 0} {
       set tbl_name_list [lreplace $tbl_name_list $pos $pos]
    }
    ::AIEnv::GUIManagement::Tree::DeleteMaterialTbl [list $tblname]
    ic_param_set_modified
}

proc aie_rename_material_tbl {tblname} {
    global material_tbl_data material_tbl_type
    global tbl_name_list
    
    set newname [question_box .newname "New name" "New name for material table $tblname" $tblname]
    if {$newname == ""} return

    #if ![aie_valid_name $newname 1] {return}
    #set newname [string toupper $newname]
    set newname [aie_get_valid_name $newname]

    if {[info exists tbl_name_list] && [lsearch $tbl_name_list $newname] >= 0} {
       user_error "The Name entered is already used.\nPlease enter another Name."
       return
    }

    if [info exists material_tbl_data($tblname)] {
        set material_tbl_data($newname) $material_tbl_data($tblname)
        unset material_tbl_data($tblname)
    }
    if [info exists material_tbl_type($tblname)] {
        set material_tbl_type($newname) $material_tbl_type($tblname)
        unset material_tbl_type($tblname)
    }
    set pos [lsearch $tbl_name_list $tblname]
    if { $pos >= 0} {
       set tbl_name_list [lreplace $tbl_name_list $pos $pos]
    }
    if {[info exists tbl_name_list] && [lsearch $tbl_name_list $newname] < 0} {
       lappend tbl_name_list $newname
    }
    ::AIEnv::GUIManagement::Tree::RenameMaterialTbl $tblname $newname
    ic_param_set_modified
}

proc aie_material_table_order {type} {

    if { $type == "Material_Property_Table" || $type == "Dynamic_Load_Table"} {
       return {
         Table_ID X_axis_type Y_axis_type Table_Parameter_1
         Table_Parameter_2 Table_Parameter_3 Table_Parameter_4 Length Table
       }
     } elseif { $type == "Modal_Damping_Table"} {
       return {
         Table_ID Table_type Length Table
       }
     } else {
       return {
         Table_ID Length Table
       }
     }
}

proc aie_save_tbls {} {
    global material_tbl_data material_tbl_type

    foreach m { :Material_Property_Table :Stress_Dependent_Material_Property_Table :Nonlinear_Material_Temp_Dependence_Tables \
                :Dynamic_Load_Table :Modal_Damping_Table } {
        foreach mat [ic_param_list_children $m] {
            ic_param_delete_node $mat
        }
    }

    foreach mat_tbl [array names material_tbl_data] {
	    set values ""
        set table_type [lindex $material_tbl_type($mat_tbl) 0] 
        if {$table_type == 0} {set type "Material_Property_Table"}
        if {$table_type == 1} {set type "Stress_Dependent_Material_Property_Table"}
        if {$table_type == 2} {set type "Nonlinear_Material_Temp_Dependence_Tables"}
        if {$table_type == 3} {set type "Dynamic_Load_Table"}
        if {$table_type == 4} {set type "Modal_Damping_Table"}
	    foreach n [aie_material_table_order $type] v $material_tbl_data($mat_tbl) {
	        lappend values $n $v
	    }
	    ic_param_add_node :$type $mat_tbl $values
    }
}

proc aie_load_tbls {clear} {
    global material_tbl_data material_tbl_type env
    global tbl_name_list

    if [info exists env(AI_ENV_CFX)] return  ;# for now
    
    if $clear { catch {unset material_tbl_data} }

    if ![info exists tbl_name_list] {set tbl_name_list ""}
    
    set mat_tbls {}
    
    foreach mat_tbl [ic_param_list_children :Material_Property_Table] {
	set tblname [lindex [split $mat_tbl /] 1]
	set material_tbl_data($tblname) [aie_material_tbl_from_param Material_Property_Table $tblname]
          set table_format 0
          aie_set_material_tbl_type $tblname [list $table_format]
	lappend mat_tbls [concat $tblname $material_tbl_data($tblname)]
	if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
	   lappend tbl_name_list $tblname
         }
    }

    foreach mat_tbl [ic_param_list_children :Stress_Dependent_Material_Property_Table] {
	set tblname [lindex [split $mat_tbl /] 1]
	set material_tbl_data($tblname) [aie_material_tbl_from_param Stress_Dependent_Material_Property_Table $tblname]
          set table_format 1
          aie_set_material_tbl_type $tblname [list $table_format]
	lappend mat_tbls [concat $tblname $material_tbl_data($tblname)]
	if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
	   lappend tbl_name_list $tblname
         }
    }

    foreach mat_tbl [ic_param_list_children :Nonlinear_Material_Temp_Dependence_Tables] {
	set tblname [lindex [split $mat_tbl /] 1]
	set material_tbl_data($tblname) [aie_material_tbl_from_param Nonlinear_Material_Temp_Dependence_Tables $tblname]
          set table_format 2
          aie_set_material_tbl_type $tblname [list $table_format]
	lappend mat_tbls [concat $tblname $material_tbl_data($tblname)]
	if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
	   lappend tbl_name_list $tblname
         }
    }

    foreach mat_tbl [ic_param_list_children :Dynamic_Load_Table] {
	set tblname [lindex [split $mat_tbl /] 1]
	set material_tbl_data($tblname) [aie_material_tbl_from_param Dynamic_Load_Table $tblname]
          set table_format 3
          aie_set_material_tbl_type $tblname [list $table_format]
	lappend mat_tbls [concat $tblname $material_tbl_data($tblname)]
	if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
	   lappend tbl_name_list $tblname
         }
    }

    foreach mat_tbl [ic_param_list_children :Modal_Damping_Table] {
	set tblname [lindex [split $mat_tbl /] 1]
	set material_tbl_data($tblname) [aie_material_tbl_from_param Modal_Damping_Table $tblname]
          set table_format 4
          aie_set_material_tbl_type $tblname [list $table_format]
	lappend mat_tbls [concat $tblname $material_tbl_data($tblname)]
	if {[info exists tbl_name_list] && [lsearch $tbl_name_list $tblname] < 0} {
	   lappend tbl_name_list $tblname
         }
    }

    ::AIEnv::GUIManagement::Tree::PutMaterialTbls $mat_tbls $clear
}

proc aie_material_tbl_from_param {type tblname} {
    set vals [ic_param_get_values :$type/$tblname]
    if {$vals == ""} {
	error "no values for $type $tblname"
    }
    array set val $vals
    set ret ""
    foreach n [aie_material_table_order $type] {
	if [info exists val($n)] {
	    lappend ret $val($n)
	} else {
	    lappend ret ""
	}
    }
    return $ret
}


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

proc aie_select_dependent_nodes {} {
}

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

# The format of a table is 
# baseval temp { { 0 x11 y11 x12 y12 ... x1p y1p } }
# baseval nonlinear { { 0 x11 y11 x12 y12 ... x1p y1p } }
# baseval temp_nonlinear { { temp0 x11 y11 x12 y12 ... x1p y1p } ... }

proc aie_edit_table_pre {type varname} {
    global aie_edit_table
    
    array set aie_edit_table { temp 0 nonlinear 0 base 0 data "" }
    
    upvar \#0 $varname lvar
    set aie_edit_table(base) [lindex $lvar 0]
    switch -- [lindex $lvar 1] {
	temp { set aie_edit_table(temp) 1 }
	nonlinear { set aie_edit_table(nonlinear) 1 }
	temp_nonlinear {
	    set aie_edit_table(temp) 1
	    set aie_edit_table(nonlinear) 1
	}
    }
    set aie_edit_table(data) [lrange $lvar 2 end]
}

proc aie_edit_table_post  {type varname} {
    global aie_edit_table
    
    upvar \#0 $varname lvar
    set lvar $aie_edit_table(base)
    if {$aie_edit_table(temp) && $aie_edit_table(nonlinear)} {
	eval lappend lvar temp_nonlinear $aie_edit_table(data)
    } elseif $aie_edit_table(temp) {
	eval lappend lvar temp $aie_edit_table(data)
    } elseif $aie_edit_table(nonlinear) {
	eval lappend lvar nonlinear $aie_edit_table(data)
    }
}

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

# The format of a table is 
# temp { { 0 x11 y11 x12 y12 ... x1p y1p } }
# nonlinear { { 0 x11 y11 x12 y12 ... x1p y1p } }
# temp_nonlinear { { temp0 x11 y11 x12 y12 ... x1p y1p } ... }

proc aie_edit_table_pre_new {type varname} {
    global aie_edit_table
    
    array set aie_edit_table { temp 0 nonlinear 0 data "" }
    
    upvar \#0 $varname lvar
    switch -- [lindex $lvar 0] {
	temp { set aie_edit_table(temp) 1 }
	nonlinear { set aie_edit_table(nonlinear) 1 }
	temp_nonlinear {
	    set aie_edit_table(temp) 1
	    set aie_edit_table(nonlinear) 1
	}
    }
    set aie_edit_table(data) [lrange $lvar 1 end]
}

proc aie_edit_table_post_new  {type varname} {
    global aie_edit_table
    
    upvar \#0 $varname lvar
    set lvar ""
    if {$aie_edit_table(temp) && $aie_edit_table(nonlinear)} {
	eval lappend lvar temp_nonlinear $aie_edit_table(data)
    } elseif $aie_edit_table(temp) {
	eval lappend lvar temp $aie_edit_table(data)
    } elseif $aie_edit_table(nonlinear) {
	eval lappend lvar nonlinear $aie_edit_table(data)
    }
}

proc aie_edit_table_doit {} {
    global aie_edit_table tdv_default

    if [info exists aie_edit_table(currently_editing)] {
	    tk_dialog .confirm Table "Please finish editing the table you have up now before you start editing another one." "" -1 Ok
	    if [winfo exists .confirm] { top_destroy .confirm }
	    return
    }
    set aie_edit_table(currently_editing) 1
    set par [winfo parent $tdv_default]
    set inf [pack info $tdv_default]
    pack forget $tdv_default
    set top $par.table
    frame $top -relief sunken -borderwidth 3
    eval pack $top $inf
    
    if {$aie_edit_table(temp) && $aie_edit_table(nonlinear)} {
	set val [st_edit_curves -curves $aie_edit_table(data) -toplevel $top]
    } else {
	set val [st_edit_curves -curves $aie_edit_table(data) -single -toplevel $top]
    }
    if {$val != ""} { set aie_edit_table(data) $val }
    
    eval pack $tdv_default $inf
    unset aie_edit_table(currently_editing)
}

proc aie_copy_material {name} {
    global material_data material_type
    global material_dpnd_data
    
    set newname [question_box .newname "New name" "Give name for the the new material" $name]
    if {$newname == ""} return

    #if ![aie_valid_name $newname 1] {return}
    #set newname [string toupper $newname]
    set newname [aie_get_valid_name $newname]

    if {$newname == $name} {return}
    
    if [info exists material_data($name)] {
        set material_data($newname) $material_data($name)
    }
    if [info exists material_type($name)] {
        set material_type($newname) $material_type($name)
    }
    if [info exists material_dpnd_data($name)] {
        set material_dpnd_data($newname) $material_dpnd_data($name)
    }

    ::AIEnv::GUIManagement::Tree::CopyMaterial $name $newname
    ic_param_set_modified

    aie_edit_material $newname

}

proc aie_load_materialsLib {} {
    global material_data material_dpnd_data env
    global material_type
    global mat_selected
    global use_workbench

    set MatList1 {}
    set MatList2 {}
    set MatList3 {}

    foreach mat [array names material_data] {
          lappend MatList1 $mat
    }

    set MatLibFile [file join $env(ICEM_ACN) icemcfd output-interfaces materials_lib.par]

    ic_param_load $MatLibFile 0

    foreach mat [ic_param_list_children :Isotropic_Material] {
	set name [lindex [split $mat /] 1]
          lappend MatList2 $name
    }
    foreach mat [ic_param_list_children :Anisotropic_Shell_Material] {
	set name [lindex [split $mat /] 1]
          lappend MatList2 $name
    }
    foreach mat [ic_param_list_children :Anisotropic_Solid_Material] {
	set name [lindex [split $mat /] 1]
          lappend MatList2 $name
   }
    foreach mat [ic_param_list_children :Orthotropic_Shell_Material] {
	set name [lindex [split $mat /] 1]
          lappend MatList2 $name
    }

    foreach mat $MatList2 {
       if {[lsearch $MatList1 $mat] < 0} {
          lappend MatList3 $mat
       }
    }

   set mat_list_selected {}
   set mat_selected "inactive"
   set buttons {
      {Accept "set mat_selected active; form_done .select_filter"}
      {Cancel "set mat_selected inactive; form_done .select_filter"}
   }
   set d [form_init .select_filter "Material Library" "" $buttons]
   if {$d != ""} {
      form_listbox $d.list "Available Materials" $MatList3 "" "" 30x10 {3 0} ""  0 0 1
      form_finish +300+300
   }
   form_wait .select_filter
   set nitems 0
   if {$mat_selected == "active"} {
      set idx [form_listbox_get_select_index $d.list]
      foreach item $idx {
         lappend mat_list_selected [form_listbox_get $d.list $item]
         set nitems 1
      }
   }
   if {$nitems == 0} {
      set mat_list_selected {}
      set mat_selected "inactive"
   }
   form_kill .select_filter

   if {$nitems == 1} {
    set mats {}
    foreach mat [ic_param_list_children :Isotropic_Material] {
       set name [lindex [split $mat /] 1]
      if {[lsearch $mat_list_selected $name] >= 0} {
	      set material_data($name) [aie_material_from_param Isotropic_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Isotropic_Material_Temperature_Dependence $name]
          aie_material_nonlinear_from_param Stress_Dependent_Material $name
          set material_format 0
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
       }
    }
    foreach mat [ic_param_list_children :Anisotropic_Shell_Material] {
       set name [lindex [split $mat /] 1]
      if {[lsearch $mat_list_selected $name] >= 0} {
	set material_data($name) [aie_material_from_param Anisotropic_Shell_Material $name]
          set material_dpnd_data($name) [aie_material_dpnd_from_param Anisotropic_Shell_Temperature_Dependence $name]
          set material_format 1
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
       }
    }
    foreach mat [ic_param_list_children :Anisotropic_Solid_Material] {
       set name [lindex [split $mat /] 1]
      if {[lsearch $mat_list_selected $name] >= 0} {
	set material_data($name) [aie_material_from_param Anisotropic_Solid_Material $name]
         set material_dpnd_data($name) [aie_material_dpnd_from_param Anisotropic_Solid_Temperature_Dependence $name]
         set material_format 2
          if {$material_dpnd_data($name) != ""} {
              set mat_dpnd 1
          } else {
              set mat_dpnd 0
          }
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
       }
    }
    foreach mat [ic_param_list_children :Orthotropic_Shell_Material] {
       set name [lindex [split $mat /] 1]
      if {[lsearch $mat_list_selected $name] >= 0} {
	set material_data($name) [aie_material_from_param Orthotropic_Shell_Material $name]
         set material_format 3
         set mat_dpnd 0
          aie_set_material_type $name [list $material_format $mat_dpnd]
	lappend mats [concat $name $material_data($name)]
       }
    }
    ::AIEnv::GUIManagement::Tree::PutMaterials $mats 0
    ic_param_set_modified
    if {[info exists use_workbench] && $use_workbench} {
        aie_wb_update_material $mats
    }
   }

}

proc aie_wb_update_material {mats} {
    global material_data material_dpnd_data
    global material_type
    global wb_material

    foreach m $mats {
	set name [lindex $m 0]
	set wb_material $name
	AIEWBSendToWBScript "AIEUpdateMaterial"
    }

}


















































