
# Note that when there is more than one line that applies to a single BC such as OR and
# PBAR the "main" one should come first

set ::aie_bctypes_used(element_prop_0d) {CONM1 CONM2}
set ::aie_bctypes_used(element_prop_1d) { PBAR RBE2 RBE3 RBAR IROD PMASS PDAMP PELAS PVISC PBEAM PGAP \
         PBARL PBEAML LSDYNA_MATERIAL_BEAM LSDYNA_BEAM_FORM OR CMP PINFL}
set ::aie_bctypes_used(element_prop_2d) { ISHE ISHR PCOMP }
set ::aie_bctypes_used(element_prop_3d) { IVOL GASKET }


array set ::aie_bcclass_title {
    CONM1 "Mass point"
    CONM2 "Mass point"
    OR    "Bar"
    PINFL "End Release"
    CMP   "Component Number"
    PBAR  "Bar"
    RBE2  "Rigid beam"
    RBE3  "Rigid beam"
    RBAR  "Rigid beam"
    IROD  "Rod connection"
    PMASS "Mass connection"
    PDAMP "Damper connection"
    PELAS "Spring"
    PVISC "Viscous damper"
    PBEAM "Beam"
    PGAP  "Gap"
    ISHE  "Shell"
    ISHR  "Shear"
    PCOMP "Layered composite"
    IVOL  "Volume"
    PBARL "Beam Cross-Section"
    PBEAML "Beam Cross-Section"
}

set ::aie_bcclass_is_part(element_prop_0d) 1
set ::aie_bcclass_is_part(element_prop_1d) 1
set ::aie_bcclass_is_part(element_prop_2d) 1
set ::aie_bcclass_is_part(element_prop_3d) 1

set ::aie_bcclass_title(element_prop_0d) "Point Properties"
set ::aie_bcclass_title(element_prop_1d) "Line Properties"
set ::aie_bcclass_title(element_prop_2d) "Surface Properties"
set ::aie_bcclass_title(element_prop_3d) "Volume Properties"

# These are some oddball ones.  Fix them later

# uncommented because if this line is commented the connector will not show in the tree
# connectors are not NODE elements to just hold BCs.  They are actual parts.
set ::aie_bcclass_is_part(connector) 1
set ::aie_bctypes_used(connector) { CONNECTOR }

set ::aie_bcclass_is_part(lsdyna_material) 1
#LSDYNA_BEAM_FORM is for LS-Dyna beam formulations.
set ::aie_bctypes_used(lsdyna_material) { LSDYNA_MATERIAL_BEAM LSDYNA_BEAM_FORM }
set ::aie_bcclass_title(lsdyna_material) "LS-Dyna specific material"

proc aie_element_prop_dim__unused {f} {
    global aie_element_prop_dims
    foreach bc [ic_boco_get $f] {
	if [info exists aie_element_prop_dims([lindex $bc 1])] {
	    set got($aie_element_prop_dims([lindex $bc 1])) 1
	}
    }
    return [array names got]
}

proc onCreatePointEleFamModify {dez val} {
    if {$val == "From screen"} {
	create0DElePropDEZ set part_name [geo_select_part_from_screen 1]
    }
}

proc onCreate0DElePropDEZLoad {} {
    global current_element_prop_0d_to_load lcs_data
    
    set PartList [aie_list_parts 1]
    foreach {n v} {
	name "" pid 0 LCS "Global" mass 1 current0DProp 0 part_name ""
	component_c1 "" component_c2 "" mass_type_index 0
         x1 "" x2 "" x3 "" i11 "" i21 "" i22 "" i31 "" i32 "" i33 ""
    } {
	if [info exists current_element_prop_0d_to_load($n)] {
	    if {$current_element_prop_0d_to_load($n) == -999999} {
		set $n ""
	    } else {
		set $n $current_element_prop_0d_to_load($n)
	    }
	} else {
	    set $n $v
	}
    }
    if {$part_name == ""} {
	set part_name [::AIEnv::GUIManagement::Tree::getSelection part]
    }
    
    set AllowActiveReload 1

    set LCSList [array names lcs_data]
    lappend LCSList "-1"
    
    # HACK
    foreach n {mass} {
	if {[set $n] != ""} { set $n [format %f [set $n]] }
    }
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	create0DElePropEXG current0DProp
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	create0DElePropMassEXG mass_type_index
    ::AIEnv::GUIManagement::ToggleGrey createPointEleFam \
	[info exists current_element_prop_0d_to_load(part_name)]
}

proc onCreate0DElePropDEZApply {} {
    if {$part_name == ""} {
       user_error "A part must be selected."
       return
    }

    set etype [lindex [list "Mass point"] $current0DProp]
    
    foreach part $part_name {
	applyEleProp 0 $name $pid point $part $LCS \
	    [list mass $mass etype $etype mass_type_index $mass_type_index component_c1 $component_c1 component_c2 \
              $component_c2 x1 $x1 x2 $x2 x3 $x3 i11 $i11 i21 $i21 i22 $i22 i31 $i31 i32 $i32 i33 $i33]
    }
}

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

proc onCreateCurveEleFamModify {dez val} {
    if {$val == "From screen"} {
	create1DElePropDEZ set part_name [geo_select_part_from_screen 1]
    }
}

proc onCreate1DElePropDEZLoad {} {
    global material_data lcs_data
    global current_element_prop_1d_to_load
    global  solver_setup solver_setup_global

    if {$solver_setup != "-1"} {
       set solve_format $solver_setup
    } else {
       set solve_format $solver_setup_global
    }
    
    set PartList [aie_list_parts 1]
    foreach {n v} {
	name "" pid 0 LCS "Global" mat "" current1DProp 0 part_name ""
	dep_dofs 123456 indep_dofs 123 weight 1.0 area 1.0
	dep_dof_cna "" indep_dof_cna 123
	dep_dof_cnb "" indep_dof_cnb 456
	torsconst 1.0 torscoeff 0.0 nsm 0.0
	force_per_vel 1 stiff 1 mass 1 damp 1
	stress 1 damp_ext 1 damp_rot 1
	beam_type_index 0 etype "" bar_a 1.0 bar_i1 1.0 bar_i2 1.0 bar_j 1.0
	bar_x1 0.0 bar_x2 1.0 bar_x3 0.0 
	bar_wa1 0.0 bar_wa2 0.0 bar_wa3 0.0 bar_wb1 0.0 bar_wb2 0.0 bar_wb3 0.0
	pa "" pb "" 
         bar_i12 0.0
         gap_u0 0.0 gap_f0 0.0 gap_ka 0.0 gap_kb 0.0 gap_kt 0.0 gap_mu1 0.0 gap_mu2 0.0 
         gap_tmax 0.0 gap_mar 100.0 gap_trmin 0.001
         component_c1 "" component_c2 "" lsdyna_mat_type 0 lsdyna_beam_type 0
         beamcs_type 0 group "" beam_type_option "" nsm_length_a "" nsm_length_b "" so_b_option "YES"
    } {
	if [info exists current_element_prop_1d_to_load($n)] {
	    if {$current_element_prop_1d_to_load($n) == -999999} {
		set $n ""
	    } else {
		set $n $current_element_prop_1d_to_load($n)
	    }
	} else {
	    set $n $v
	}
    }
    if {$part_name == ""} {
	set part_name [::AIEnv::GUIManagement::Tree::getSelection part]
    }
    
    # HACK
    foreach n {weight area torsconst torscoeff nsm force_per_vel stiff mass damp \
		   stress damp_ext damp_rot bar_a bar_i1 bar_i2 bar_j \
                      bar_i12 gap_u0 gap_f0 gap_ka gap_kb gap_kt gap_mu1 gap_mu2 \
                      gap_tmax gap_mar gap_trmin \
		   bar_x1 bar_x2 bar_x3 bar_wa1 bar_wa2 bar_wa3 bar_wb1 bar_wb2 bar_wb3} {
	if {[set $n] != ""} { set $n [format %f [set $n]] }
    }

    set YESNO_list [list "YES" "NO"]   
    set MatList [lsort [array names material_data]]
    set LSDynaMatListBeam [list "Type 1: *MAT_ELASTIC" "Type 20: *MAT_RIGID"]
    set LSDynaFormListBeam [list "Type 1H: Hughes-Liu" "Type 1B: Belytschko Resultant" "Type 1T: Truss" \
								 "Type 1I:Belytschko-Schwer Integrated" "Type 1I:Belytschko-Schwer Tubular" Seatbelt]
    set LSDynaMatListSpring [list "Type 66: *MAT_LINEAR_ELASTIC_DISCRETE_BEAM" "Type 74: *MAT_ELASTIC_SPRING_DISCRETE_BEAM"]
    set LCSList [lsort [array names lcs_data]]
    #set BeamTypeList [list RBAR RBE2 RBE3]

    set BEAMTYPE_list [list "I" "T" "L" "H" "BOX" "BAR" "ROD" "TUBE" "HAT" "CHAN"]
    
    create1DElePropMID setvalue @[lsearch $MatList $mat]

    if {$solve_format == "2"} {
 	foreach lsdyna_etype {Spring Damper Viscous} {
		create1DElePropLSDynaMat${lsdyna_etype} setvalue @0
	}
	set lsdyna_etype [lindex [split $etype] 0]
	if {$lsdyna_etype==""} {set lsdyna_etype Bar}
	switch $lsdyna_etype {
		Spring -
		Damper -
		Viscous {
			create1DElePropLSDynaMat${lsdyna_etype} setvalue @$lsdyna_mat_type
		}
	}
	create1DElePropLSDynaFormBeam setvalue @$lsdyna_beam_type
    }
#    create1DElePropLSDynaMatBar setvalue @$lsdyna_mat_type
    #create1DElePropBarCYL setvalue @[lsearch $LCSList $LCS]
    #create1DElePropRBEType setvalue @[lsearch $BeamTypeList $beam_type]
    
    if {$etype != ""} {
	set current1DProp [lsearch [aie_get_1d_subtypes] $etype]
    }
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	create1DElePropEXG current1DProp
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	create1DElePropRBEEXG beam_type_index
    ::AIEnv::GUIManagement::ToggleGrey createCurveEleFam \
	[info exists current_element_prop_1d_to_load(part_name)]
    ic_uns_subset_configure "" -draw_directions 1
    set AllowActiveReload 1
    ::AIEnv::GUIManagement::ToggleGrey createCurveEleFam \
	[info exists current_element_prop_1d_to_load(part_name)]

    if {$solve_format == "2"} {
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBar 0
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatDamper 0
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatSpring 0
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatViscous 0
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBeam 0
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBeamCS 0
    } else {
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBar 1
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatDamper 1
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatSpring 1
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaMatViscous 1
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBeam 1
       ::AIEnv::GUIManagement::ToggleGrey create1DElePropLSDynaFormBeamCS 1
    }
    
}

proc aie_get_1d_subtypes {} {
    return [list "Bar" "Rigid beam" "Rod connection" "Mass connection" "Damper connection" "Spring" "Viscous damper" "Beam" "Gap" \
        "Beam Cross-Section"]
}

proc onCreate1DElePropDEZApply {} {
    if {$part_name == ""} {
       user_error "A part must be selected."
       return
    }
    set etype [lindex [aie_get_1d_subtypes] $current1DProp]
    if {$etype == "Bar" || $etype == "Rod connection" || $etype == "Beam" || $etype == "Beam Cross-Section"} {
	if {$mat == ""} {
		user_error "The material must be given."
		set matnum "-999999"
		return
	}
    }
    switch -regexp $etype {
	Bar -
	Beam -
	"Beam Cross-Section" {
	    set lsdyna_mat_type 0
#	    set lsdyna_mat_type [lsearch $LSDynaMatListBeam $lsdyna_mat_beam]
	    set lsdyna_beam_type [lsearch $LSDynaFormListBeam $lsdyna_form_beam]
	}
	Spring -
	^Damper -
	^Viscous {
	    set lsdyna_mat_type [lsearch $LSDynaMatListSpring $lsdyna_mat_spring]
		set lsdyna_beam_type 0
	}
	default {
	    set lsdyna_mat_type 0
		set lsdyna_beam_type 0
	}
    }
    if {$lsdyna_mat_type==-1} {set lsdyna_mat_type 0}
    foreach part $part_name {    
	applyEleProp 1 $name $pid curve $part $LCS \
	    [list mat $mat etype $etype dep_dofs $dep_dofs indep_dofs $indep_dofs \
		 weight $weight area $area \
		 dep_dof_cna $dep_dof_cna indep_dof_cna $indep_dof_cna \
		 dep_dof_cnb $dep_dof_cnb indep_dof_cnb $indep_dof_cnb \
		 torsconst $torsconst torscoeff $torscoeff nsm $nsm \
		 force_per_vel $force_per_vel stiff $stiff mass $mass damp $damp \
		 stress $stress damp_ext $damp_ext damp_rot $damp_rot \
		 beam_type_index $beam_type_index bar_a $bar_a bar_i1 \
		 $bar_i1 bar_i2 $bar_i2 bar_j $bar_j \
		 bar_i12 $bar_i12 gap_u0 $gap_u0 gap_f0 $gap_f0 gap_ka $gap_ka gap_kb $gap_kb \
		 gap_kt $gap_kt gap_mu1 $gap_mu1 gap_mu2 $gap_mu2 \
		 gap_tmax $gap_tmax gap_mar $gap_mar gap_trmin $gap_trmin \
		 bar_x1 $bar_x1 bar_x2 $bar_x2 bar_x3 $bar_x3  \
                    bar_wa1 $bar_wa1 bar_wa2 $bar_wa2 bar_wa3 $bar_wa3 bar_wb1 $bar_wb1 bar_wb2 $bar_wb2 bar_wb3 $bar_wb3 \
		 pa $pa pb $pb  \
		 component_c1 $component_c1 component_c2 $component_c2 \
		 lsdyna_mat_type $lsdyna_mat_type lsdyna_beam_type $lsdyna_beam_type \
                    beamcs_type $beamcs_type group $group beam_type_option $beam_type_option \
                    nsm_length_a $nsm_length_a nsm_length_b $nsm_length_b so_b_option $so_b_option]
    }
}

proc onCreate1DElePropDEZUnLoad {} {
    ic_uns_subset_configure "" -draw_directions 0
}

proc onCreate1DElePropRBELoad {} {
    if {$beam_type_index < 2} {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
	::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 1
    }
}

proc onCreate1DElePropRBEUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 0
}

proc onCreateBeamRBARUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 0
}

proc onCreateBeamRBE2Unload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 0
}

proc onCreateBeamRBE3Unload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}

proc onCreateBeamRBARLoad {} {
    if {$current1DProp == 1} {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
	::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 1
    }
}

proc onCreateBeamRBE2Load {} {
    if {$current1DProp == 1} {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
	::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 1
    }
}

proc onCreateBeamRBE3Load {} {
    if {$current1DProp == 1} {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
	::AIEnv::GUIManagement::ToggleGrey create1DElePropPID 1
    }
}

proc onCreate1DElePropPMASSLoad {} {
    if {$current1DProp==3 } {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
    }
}

proc onCreate1DElePropPMASSUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}
proc onCreate1DElePropDAMPLoad {} {
    if {$current1DProp==4 } {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
    }
}

proc onCreate1DElePropDAMPUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}

proc onCreate1DElePropELASLoad {} {
    if {$current1DProp==5 } {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
    }
}

proc onCreate1DElePropELASUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}

proc onCreate1DElePropVISCLoad {} {
    if {$current1DProp==6 } {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
    }
}

proc onCreate1DElePropVISCUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}

proc onCreate1DElePropGapLoad {} {
    if {$current1DProp==8 } {
	::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 1
    }
}

proc onCreate1DElePropGapUnload {} {
    ::AIEnv::GUIManagement::ToggleGrey create1DElePropMID 0
}

proc onCreate1DElePropCrossSectionLoad {} {
    global current_cross_section_to_load

    set val [create1DElePropDEZ get pid]

    if {$val == 0} {
       set new_pid 1
       foreach data [array names current_cross_section_to_load] {
            set old_pid [string trimleft $data cross_section]
            if {$old_pid >= $new_pid} {
               set new_pid [expr $old_pid +1]
            }
       }
       set ns [::AIEnv::GUIManagement::GetNamespace DEZ create1DElePropDEZ]
       namespace eval $ns [list set pid $new_pid]
    }

}
# ------------------------------------------------------

proc onCreate2DElePropDEZLoad {} {
    global material_data
    global current_element_prop_2d_to_load
    global current_layers_to_load
    global thickness_data

    set FT_list {"" "HILL" "HOFF" "TSAI" "STRN"}
    set LAM_list {"" "SYM" "MEM" "BEND"}
    
    foreach {n v} {
	name "" pid 0 mat "" current2DProp 0 etype ""
	mat_b "(same as above)" mat_t "(same as above)" nsm 0.0
	mat_c "(same as above)"
	m_thick 0.0 b_stiff 1.0 part_name ""
	t_stiff 0.83333 thick 1.0
         z0 -0.5 nsm_area "" sb "" ft_option "" tref "" ge "" lam_option "" layers_num 1
    } {
	if [info exists current_element_prop_2d_to_load($n)] {
	    if {$current_element_prop_2d_to_load($n) == -999999} {
		set $n ""
	    } else {
		set $n $current_element_prop_2d_to_load($n)
	    }
	} else {
	    set $n $v
	}
    }
    if {$part_name == ""} {
	set part_name [::AIEnv::GUIManagement::Tree::getSelection part]
    }
    
    # HACK
#    foreach n {nsm m_thick b_stiff t_stiff thick} {
#	if {[set $n] != ""} { set $n [format %f [set $n]] }
#    }

    foreach n {nsm b_stiff t_stiff thick} {
	if {[set $n] != ""} { set $n [format %f [set $n]] }
    }
    
    foreach v {mat_b mat_t mat_c} {
	if {[set $v] == ""} { set $v $mat }
    }
    set PartList [aie_list_parts 1]
    set MatList [lsort [array names material_data]]
    set MatList2 [concat [list "(same as above)"] $MatList]
    set LSDynaMatList [list "Type 1: *MAT_ELASTIC" "Type 3: *MAT_PLASTIC_KINEMATIC" \
							"Type 18: *MAT_POWER_LAW_PLASTICITY" "Type 19: *MAT_STRAIN_RATE_DEPENDENT_PLASTICITY" \
							"Type 20: *MAT_RIGID" "Type 24: *MAT_PIECEWISE_LINEAR_PLASTICITY" \
							"Type 28: *MAT_RESULTANT_PLASTICITY" "Type 64: *MAT_RATE_SENSITIVE_POWERLAW_PLASTICITY"]
    
    create2DElePropMIDShell setvalue @[lsearch $MatList $mat]
    create2DElePropMIDShear setvalue @[lsearch $MatList $mat]
    create2DElePropMat_b setvalue @[lsearch $MatList2 $mat_b]
    create2DElePropMat_t setvalue @[lsearch $MatList2 $mat_t]
    create2DElePropMat_c setvalue @[lsearch $MatList $mat_c]
#    create2DElePropLSDynaMat setvalue @$lsdyna_mat_type
#	set lsdyna_mat_shell $lsdyna_mat_type
    
    if {$etype != ""} {
	set current2DProp [lsearch [aie_get_2d_subtypes] $etype]
    }
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	create2DElePropEXG current2DProp
    set AllowActiveReload 1

    ::AIEnv::GUIManagement::ToggleGrey createSurfEleFam \
	[info exists current_element_prop_2d_to_load(part_name)]
}

proc aie_get_2d_subtypes {} {
    return [list "Shell" "Shear" "Layered composite"]
}

proc onCreate2DElePropDEZApply {} {
    global current_layers_to_load
    global thickness_data
    
    if {$part_name == ""} {
       user_error "A part must be selected."
       return
    }

#	set lsdyna_mat_type [lsearch $LSDynaMatList $lsdyna_mat]
#	if {$lsdyna_mat_type==-1} {set lsdyna_mat_type 0}
    set etype [lindex [aie_get_2d_subtypes] $current2DProp]

    if {$etype == "Shell" || $etype == "Shear"} {
	if {$mat == ""} {
		user_error "The material must be given."
		set matnum "-999999"
		return
	}
    }
    if {$etype == "Layered composite"} {
	 set name1 layers$pid
	if {![info exists current_layers_to_load($name1)]} {
                      user_error "Error no Layers were defined.  Please define layers using \"Set Layers\" button.\n"
                      return
	} else {                 
	set layers [lindex $current_layers_to_load($name1) 0]
                    for {set i 1} {$i  <= $layers} {incr i} {
                      set layers_mat [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+1]]
                      if {$layers_mat == ""} {
		       user_error "Materials must be given for Layered Composites."
                         return
                      }
                    }
	}
    }

    if {$mat_b == "(same as above)"} { set mat_b $mat }
    if {$mat_t == "(same as above)"} { set mat_t $mat }
    if {$mat_c == "(same as above)"} { set mat_c $mat }
    if {$ft_option == ""} {set ft_value 1}
    if {$ft_option == "HILL"} {set ft_value 2} 
    if {$ft_option == "HOFF"} {set ft_value 3} 
    if {$ft_option == "TSAI"} {set ft_value 4} 
    if {$ft_option == "STRN"} {set ft_value 5}
    if {$lam_option == ""} {set lam_value 1}
    if {$lam_option == "SYM"} {set lam_value 2} 
    if {$lam_option == "MEM"} {set lam_value 3} 
    if {$lam_option == "BEND"} {set lam_value 4} 

    set length [llength $m_thick] 
    set index 0
    foreach part $part_name {
         if {$index < $length} {
             set thickness_data($part) [lindex $m_thick $index]
         } else {
              set thickness_data($part) [lindex $m_thick [expr $length - 1]]
        }
         set index [expr $index + 1]
    }
    foreach part $part_name {
         if [info exists thickness_data($part)] {
             set m_thick_part $thickness_data($part)
         } else {
             set m_thick_part $m_thick
         }
	applyEleProp 2 $name $pid surface $part 0 \
	    [list etype $etype mat $mat mat_b $mat_b mat_t $mat_t \
		nsm $nsm mat_c $mat_c m_thick $m_thick_part b_stiff $b_stiff \
		t_stiff $t_stiff thick $thick \
                   z0 $z0 nsm_area $nsm_area sb $sb ft_value $ft_value tref $tref ge $ge lam_value $lam_value]
    }

}

proc onCreateSurfEleFamFinish {} {
    global thickness_data

    #set th [format %g [aie_average_thickness $part_name]]
    aie_average_thickness $part_name
    set th ""
    foreach part $part_name {
       lappend th $thickness_data($part)
    }
    set ns [::AIEnv::GUIManagement::GetNamespace DEZ create2DElePropDEZ]
    if {$m_thick == 0.0} {
	namespace eval $ns [list set m_thick $th]
    }

}

proc aie_edit_thickness {what} {
    global thickness_data

    set ok 0

    set val [create2DElePropDEZ get part_name]
        
    if {$val != "" && [info exists thickness_data]} {
       set ok [aie_edit_array thickness_data]
    } else {
       mess "No thickness data to edit\n" red
    }

    if {$ok == 1} {
       set th ""
       foreach part $val {
          lappend th $thickness_data($part)
       }
       set ns [::AIEnv::GUIManagement::GetNamespace DEZ create2DElePropDEZ]
       namespace eval $ns [list set m_thick $th]
    }

}

proc onCreate2DElePropPcompLoad {} {
    global current_layers_to_load

    set val [create2DElePropDEZ get pid]

    if {$val == 0} {
       set new_pid 1
       foreach data [array names current_layers_to_load] {
            set old_pid [string trimleft $data layers]
            if {$old_pid >= $new_pid} {
               set new_pid [expr $old_pid +1]
            }
       }
       set ns [::AIEnv::GUIManagement::GetNamespace DEZ create2DElePropDEZ]
       namespace eval $ns [list set pid $new_pid]
    }

}
# ------------------------------------------------------

proc onCreateVolEleFamModify {dez val} {
    if {$val == "From screen"} {
	create3DElePropDEZ set part_name [geo_select_part_from_screen 1]
    }
}

proc onCreate3DElePropDEZLoad {} {
    global material_data lcs_data
    global current_element_prop_3d_to_load
	global aie_edit_gasket
    
    set PartList [aie_list_parts 1]
    foreach {n v} {
	name "" pid 0 LCS "Global" mat "" part_name "" \
	gasket 0 gasket_gap_data "" gasket_loading_data "" gasket_unloading_data "" gasket_tss_data ""
    } {
	if [info exists current_element_prop_3d_to_load($n)] {
	    if {$current_element_prop_3d_to_load($n) == -999999} {
		set $n ""
	    } else {
		set $n $current_element_prop_3d_to_load($n)
	    }
	} else {
	    set $n $v
	}
    }
    if {$part_name == ""} {
	set part_name [::AIEnv::GUIManagement::Tree::getSelection part]
    }
    
    set MatList [array names material_data]
    set LSDynaMatList [list "Type 1: *MAT_ELASTIC" "Type 3: *MAT_PLASTIC_KINEMATIC" \
							"Type 18: *MAT_POWER_LAW_PLASTICITY" "Type 19: *MAT_STRAIN_RATE_DEPENDENT_PLASTICITY" \
							"Type 20: *MAT_RIGID" "Type 24: *MAT_PIECEWISE_LINEAR_PLASTICITY" \
							"Type 64: *MAT_RATE_SENSITIVE_POWERLAW_PLASTICITY"]
    set LCSList [array names lcs_data]
    set gasketDataList [list "X-Y data" "Temperature dependent data"]
    
    create3DElePropMID setvalue @[lsearch $MatList $mat]
#    create3DElePropLSDynaMat setvalue @$lsdyna_mat_type
#	set lsdyna_mat_solid $lsdyna_mat_type
    create3DElePropCYL setvalue @[lsearch $LCSList $LCS]

	catch {unset aie_edit_gasket}
	if {$gasket_gap_data==""} {
		set gasketInitGapVal 0
		set gasket_gap ""
	} else {
		set gasketInitGapVal [lindex $gasket_gap_data 0]
		set gasket_gap [lindex $gasket_gap_data 1]
	}

	set aie_edit_gasket(loading) ""
	if {$gasket_loading_data==""} {
		set gasket_loading "X-Y data"
	} else {
		set gasket_loading [lindex $gasketDataList [lindex $gasket_loading_data 1]]
		set aie_edit_gasket(loading) [lindex $gasket_loading_data 2]
	}

	set aie_edit_gasket(unloading) ""
	if {$gasket_unloading_data==""} {
		set gasket_unloading_flag 0
		set gasket_unloading "X-Y data"
	} else {
		set gasket_unloading_flag [lindex $gasket_unloading_data 0]
		set gasket_unloading [lindex $gasketDataList [lindex $gasket_unloading_data 1]]
		set aie_edit_gasket(unloading) [lindex $gasket_unloading_data 2]
	}

	set aie_edit_gasket(tss) ""
	set gasket_tss ""
	if {$gasket_tss_data==""} {
		set gasket_tss_flag 0
	} else {
		set gasket_tss_flag [lindex $gasket_tss_data 0]
		if {$gasket_tss_flag==1} {
			set gasket_tss [lindex $gasket_tss_data 1]
		} elseif {$gasket_tss_flag==2} {
			set aie_edit_gasket(tss) [lindex $gasket_tss_data 1]
		}
	}

    set AllowActiveReload 1
    ::AIEnv::GUIManagement::ToggleGrey createVolEleFam \
	[info exists current_element_prop_3d_to_load(part_name)]

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

	if {$solve_format == "1" || $solve_format == "3"} {
		::AIEnv::GUIManagement::ToggleGrey create3DEleGasket 0
	} else {
		::AIEnv::GUIManagement::ToggleGrey create3DEleGasket 1
	}
}

proc onCreate3DElePropDEZApply {} {
	global aie_edit_gasket
#	set lsdyna_mat_type [lsearch $LSDynaMatList $lsdyna_mat]
#	if {$lsdyna_mat_type==-1} {set lsdyna_mat_type 0}
    if {$part_name == ""} {
       user_error "A part must be selected."
       return
    }

	if [info exists aie_edit_gasket(currently_editing)] {
		tk_dialog .duplicate_gasket_table Plot "Please finish editing the plot you have up now before you start editing another one." "" -1 Ok
		if [winfo exists .duplicate_gasket_table] { top_destroy .duplicate_gasket_table }
		return
	}
	set gasket_loading_type [lsearch $gasketDataList $gasket_loading]
	set gasket_unloading_type [lsearch $gasketDataList $gasket_unloading]
	switch $gasket_tss_flag {
		0 { set gasket_tss_data [list $gasket_tss_flag ""] }
		1 { set gasket_tss_data [list $gasket_tss_flag $gasket_tss] }
		2 { set gasket_tss_data [list $gasket_tss_flag $aie_edit_gasket(tss)] }
	}
    foreach part $part_name {
	applyEleProp 3 $name $pid material $part $LCS \
		[list mat $mat gasket $gasket \
			gasket_gap_data [list $gasketInitGapVal $gasket_gap] \
			gasket_loading_data [list 1 $gasket_loading_type $aie_edit_gasket(loading)] \
			gasket_unloading_data [list $gasket_unloading_flag $gasket_unloading_type $aie_edit_gasket(unloading)] \
			gasket_tss_data $gasket_tss_data \
		]
    }
}

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

proc applyEleProp {dimens name pid type part_names lcs props} {
    global material_data aie_bctypes_used or_type
    global current_layers_to_load
    global current_cross_section_to_load

    puts [list applyEleProp $dimens $name $pid $type $part_names $lcs $props]
    
    if {$part_names == ""} {
	user_error "You must select at least one part to which to apply the element property."
	return
    }
    
    set dims [ic_boco_parts_dim $part_names]
    if ![lmember $dims $dimens] {
	mess "Warning: the selected parts do not have any $dimens-D objects\n" red
    }
    if {[llength $dims] > 1} {
	set dd ""
	foreach d $dims { lappend dd $d-D }
	set dd [join $dd ", "]
	mess "Note: the selected parts contain $dd elements.  The requested properties will only be applied to the $dimens-D elements.\n" blue
    }
	
    foreach {p v} $props { set $p $v }
    
    foreach v {b_stiff t_stiff dep_dofs indep_dofs
	dep_dof_cna indep_dof_cna dep_dof_cnb indep_dof_cnb} {
	if {[info exists $v] && ([set $v] == "")} { set $v -999999 }
	#if [info exists $v] { puts "$v [set $v]" }
    }

    foreach v {m_thick thick lsdyna_mat_type lsdyna_beam_type} {
	if {![info exists $v]} { set $v 0 }
    }
    
    set enforce_bar_direction 0
    
    switch $type {
	point {
	    switch $etype {
		"Mass point" {
		    switch $mass_type_index {
			0 {
                                if {$lcs == "-1"} {
                                   set lcsnum $lcs
                                } else {
                                   set lcsnum [aie_get_lcs_num $lcs]
                                }
			    set bcs [list [list 1 CONM2 $lcsnum $mass $x1 $x2 $x3 $i11 $i21 $i22 $i31 $i32 $i33]]
			}
			1 {
			    set bcs [list [list 1 CONM1 0 $mass]]
			}
		    }
		}
		default {
		    error "unhandled etype $etype"
		}
	    }
	}
	curve {
	    switch $etype {
		"Bar" {
                       set or_type "PBAR"
		    if {$mat == ""} {
			user_error "The material must be given for a CBAR."
			set matnum "-999999"
			return
		    } else {
			set matnum [lindex $material_data($mat) 0]
		    }
		    set tmp1 [list 1 PBAR $pid $matnum $bar_a $bar_i1 $bar_i2 $bar_j]
		    set tmp2 [list 1 OR $pid $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3]                                        		    
		    if {$pa != "" || $pb != ""} {
		       set bcs [list $tmp1 $tmp2 [list 1 PINFL $pid $pa $pb]]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		       aie_update_bar_end_release $part_names $pa $pb
		    } else {
		       set bcs [list $tmp1 $tmp2]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		    }
		}
		"Rigid beam" {
		    switch $beam_type_index {
			0 {
			    set bcs [list [list 1 RBAR $indep_dof_cna $indep_dof_cnb $dep_dof_cna $dep_dof_cnb]]
			}
			1 {
			    set bcs [list [list 1 RBE2 1 0 $dep_dofs]]
			    set enforce_bar_direction 1
			}
			2 {
			    #set bcs [list [list 1 RBE3 0 $dep_dofs $weight $indep_dofs]]
			    set bcs [list [list 1 RBE3 $pid $dep_dofs $weight $indep_dofs]]
			}
		    }
		}
		"Rod connection" {
		    if {$mat == ""} {
			user_error "The material must be given for a IROD."
			set matnum "-999999"
			return
		    } else {
			set matnum [lindex $material_data($mat) 0]
		    }
		    set bcs [list [list 1 IROD $pid $matnum $area $torsconst $torscoeff $nsm]]
		}
		"Mass connection" {
                       set or_type "PMASS"
		    set bcs [list [list 1 PMASS $pid $mass]\
		                    [list 1 CMP $pid $component_c1 $component_c2]]
		}
		"Damper connection" {
		    set or_type "PDAMP"
		    set bcs [list  [list 1 PDAMP $pid $force_per_vel]\
                                        [list 1 CMP $pid $component_c1 $component_c2]]
		}
		"Spring" {
		    set or_type "PELAS"
		    set bcs [list [list 1 PELAS $pid $stiff $damp $stress]\
                                        [list 1 CMP $pid $component_c1 $component_c2] ]
		}
		"Viscous damper" {
		    set bcs [list [list 1 PVISC $pid $damp_ext $damp_rot]]
		}
		"Beam" {
                       set or_type "PBEAM"
		    if {$mat == ""} {
			user_error "The material must be given for a CBEAM."
			set matnum "-999999"
			return
		    } else {
			set matnum [lindex $material_data($mat) 0]
		    }
		    set tmp1 [list 1 PBEAM $pid $matnum $bar_a $bar_i1 $bar_i2 $bar_i12 $bar_j]
		    set tmp2 [list 1 OR $pid $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3]
		    if {$pa != "" || $pb != ""} {
		       set bcs [list $tmp1 $tmp2 [list 1 PINFL $pid $pa $pb]]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		       aie_update_bar_end_release $part_names $pa $pb
		    } else {
		       set bcs [list $tmp1 $tmp2]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		    }
		}
		"Gap" {
                       set or_type "PGAP"
		    set bcs [list  [list 1 PGAP $pid $gap_u0 $gap_f0 $gap_ka $gap_kb $gap_kt $gap_mu1 $gap_mu2 $gap_tmax $gap_mar $gap_trmin]\
				  [list 1 OR $pid $bar_x1 $bar_x2 $bar_x3]]
		    aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3
                   }
		"Beam Cross-Section" {
                       set or_type "PBEAM"
		    if {$mat == ""} {
			user_error "The material must be given for a PBARL/PBEAML."
			set matnum "-999999"
			return
		    } else {
			set matnum [lindex $material_data($mat) 0]
		    }
		    switch $beam_type_option {
		       "I" {set beamtype 0}
		       "T" {set beamtype 1}
		       "L" {set beamtype 2}
 		      "H" {set beamtype 3}
 		      "BOX" {set beamtype 4}
 		      "BAR" {set beamtype 5} 
 		      "ROD" {set beamtype 6}
		       "TUBE" {set beamtype 7}
		       "HAT" {set beamtype 8}
		       "CHAN" {set beamtype 9}  
		    }
		    switch $so_b_option {
		       "YES" {set sooption 0}
		       "NO" {set sooption 1}
                       }
		    if {$group == ""} {
			set group "-999999"
                       }
                       set name1 cross_section$pid
                       if {[info exists current_cross_section_to_load($name1)]} {
                          set dims [lindex $current_cross_section_to_load($name1) 0]
                       } else {
		       user_error "No BEAM Cross Section dimensions are given.  Please set the dimensions using the\n\"Set Cross Section Dimensions\" button."
                          return
                       }
		    switch $beamcs_type {
			0 {
                                 set cross_section_list [list 1 PBARL $pid $matnum $group $beamtype $nsm_length_a]
                                 set dims1 $dims
                             }
			1 {
                                 set cross_section_list [list 1 PBEAML $pid $matnum $group $beamtype $nsm_length_a $nsm_length_b $sooption]
                                 set dims1 [expr 2 * $dims]
                             }
                       }
                        lappend cross_section_list $dims1
                        for {set i 1} {$i  <= $dims1} {incr i} {
                            set val [lindex $current_cross_section_to_load($name1) $i]
                            lappend cross_section_list $val
                        }
		    set tmp1 $cross_section_list
		    set tmp2 [list 1 OR $pid $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3]
		    if {$pa != "" || $pb != ""} {
		       set bcs [list $tmp1 $tmp2 [list 1 PINFL $pid $pa $pb]]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		       aie_update_bar_end_release $part_names $pa $pb
		    } else {
		       set bcs [list $tmp1 $tmp2]
		       aie_update_bar_orientations $part_names $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3
		    }
		}
		default {
		    error "unhandled etype $etype"
		}
	    }
		lappend bcs [list 1 LSDYNA_MATERIAL_BEAM $lsdyna_mat_type]
		lappend bcs [list 1 LSDYNA_BEAM_FORM $lsdyna_beam_type]
	}
	surface {
             if {$etype == "Shell" || $etype == "Shear"} {
	       if {$mat == ""} {
		user_error "The material must be given for a SHELL or SHEAR."
		set matnum "-999999"
		return
	      } else {
                  set matnum [lindex $material_data($mat) 0]
               }
               if {$m_thick== ""} { set m_thick 0.0}
               if {$thick== ""} { set thick 0.0}
	    }
              if {$etype == "Layered composite"} {
                 set name1 layers$pid
                 if {![info exists current_layers_to_load($name1)]} {
                      user_error "Error no Layers were defined!\n"
                      return
                 } else {                 
                    set layers [lindex $current_layers_to_load($name1) 0]
                    for {set i 1} {$i  <= $layers} {incr i} {
                      set layers_mat [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+1]]
                      if {$layers_mat == ""} {
		       user_error "Materials must be given for Layered Composites."
                         return
                      }
                    }
                 }
              }
	    switch $etype {
		"Shell" {
		    if {$mat_t == ""} {
			set matnum_t "-999999"
		    } else {
			set matnum_t [lindex $material_data($mat_t) 0]
		    }
		    if {$mat_b == ""} {
			set matnum_b "-999999"
		    } else {
			set matnum_b [lindex $material_data($mat_b) 0]
		    }
		    if {$mat_c == ""} {
			set matnum_c "-999999"
		    } else {
			set matnum_c [lindex $material_data($mat_c) 0]
		    }
		    set bcs [list [list 1 ISHE $pid $matnum $matnum_b $matnum_t $nsm $matnum_c $m_thick $b_stiff $t_stiff]]
		    
		    aie_update_shell_thicknesses $part_names $m_thick		    
		}
		"Shear" {
		    set bcs [list [list 1 ISHR $pid $matnum $thick $nsm]]
		}
                   "Layered composite" {
                       set layers_list [list 1 PCOMP $pid $z0 $nsm_area $sb $ft_value $tref $ge $lam_value]
                       set name1 layers$pid
                       set layers [lindex $current_layers_to_load($name1) 0]
                       lappend layers_list $layers
                       for {set i 1} {$i  <= $layers} {incr i} {
                             set val [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+1]]
                             lappend layers_list [lindex $material_data($val) 0]
                             lappend layers_list [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+2]]
                             lappend layers_list [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+3]]
                             set val [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+4]]
                             if {$val == "NO"} {set val 0} 
                             if {$val == "YES"} {set val 1}
                             lappend layers_list $val
                       }
                       set bcs [list $layers_list]
		}
		default {
		    error "unhandled etype $etype"
		}
	    }
#		lappend bcs [list 1 LSDYNA_MATERIAL_SHELL $lsdyna_mat_type $sigy $fail]
	}
	material {
	    if {$mat == ""} {
		user_error "The material must be given for a volume property"
		return
		set matnum "-999999"
	    } else {
		set matnum [lindex $material_data($mat) 0]
	    }
             set lcsnum [aie_get_lcs_num $lcs]
	    set bcs [list [list 1 IVOL $matnum $lcsnum 0 $pid] \
				[list 1 GASKET $gasket $gasket_gap_data $gasket_loading_data $gasket_unloading_data $gasket_tss_data]]
#		lappend bcs [list 1 LSDYNA_MATERIAL_SOLID $lsdyna_mat_type $sigy $fail]
	}
    }
    set bcs2 ""
    foreach bc $bcs {
	set nbc ""
	foreach b $bc {
	    if {$b == ""} { set b -999999 }
	    lappend nbc $b
	}
	lappend bcs2 $nbc
    }
    set dimtype element_prop_${dimens}d
    set dim_bctypes $aie_bctypes_used($dimtype)
    foreach part_name $part_names {
	set fams [ic_boco_replace_in_part $bcs2 $part_name $dim_bctypes]
	set vstat 1 ;# ???
	foreach f $fams {
	    aie_apply_icons_$dimtype $f $dimtype $vstat $vstat
	}
	::AIEnv::GUIManagement::Tree::AddPartBC $dimtype $part_name $vstat
	
	if $enforce_bar_direction {
	    set parts [ic_geo_list_families_in_part $part_name]
	    set ss [ic_uns_subset_create_family_type $parts {BAR_2 BAR_3} 0 0 0]
	    if {$ss != ""} {
		set n [ic_uns_enforce_bar_direction $ss]
		if $n {
		    mess "Warning: $n RBE2 elements may be misoriented\n" red
		}
		ic_uns_subset_delete $ss
	    }
	}
    }
}

proc aie_average_thickness {parts} {
    global thickness_data

    set area 0.0
    set thickness 0.0
    foreach part $parts {
	set part_th 0
	set part_ar 0
	foreach obj [aie_get_geometry_in_part surface $part] {
	    set th ""
	    foreach {n v} [ic_get_meshing_params surface $obj] {
		if {$n == "ehgt"} { set th $v }
	    }
	    if {$th != ""} {
		if {$th == 0} {
                  set thickness2 [ic_geo_surface_thickness [lindex $obj 0] 2]
                  set C1 [lindex $thickness2 0]
                  set C2 [lindex $thickness2 1]
                  set C3 [lindex $thickness2 2]
                  set C4 [lindex $thickness2 3]
	          set geothickness [expr $C1 + $C2 + $C3 + $C4]
	          set geothickness [expr $geothickness * 0.25]
		  set th $geothickness
		}
		set ar [ic_geo_srf_area $obj]
		set area [expr $area + $ar]
 		set thickness [expr $thickness + $ar * $th]
		
		set part_ar [expr $part_ar + $ar]
		set part_th [expr $part_th + $ar * $th]
	    }
	}
	if $part_ar {
	    set thickness_data($part) [expr $part_th / $part_ar]
	} else {
	    set thickness_data($part) 0.0
	}
    }
    
    if {$area > 0} {
	return [expr $thickness / $area]
    } else {
	return 0
    }
}

proc aie_update_bar_orientations {{part_names ""} {x1 ""} {x2 ""} {x3 ""}
				  {wa1 0} {wa2 0} {wa3 0}
				  {wb1 0} {wb2 0} {wb3 0}} {

    if {$part_names == ""} {
         set parts_tmp ""
	foreach pp [ic_boco_list_with_bctype OR] {
	    lappend parts_tmp [lindex $pp 0]
	}
         if {$parts_tmp != ""} {
            foreach part $parts_tmp {
               set has_bcfield 0
               set fams [ic_geo_list_families_in_part $part]
               set unsmap [ic_uns_subset_create_family_type $fams 1d 0 0]
               if {$unsmap != ""} {
                  foreach p {X1 X2 X3 W1A W2A W3A W1B W2B W3B} {
                     set param_name "Nastran:OR:$p"
                     if ![ic_uns_bcfield_exists $param_name] {ic_uns_bcfield_define $param_name 0}
                     set bcs [ic_uns_bcfield_get $param_name $unsmap 0]
                     if {[lindex $bcs 0] != ""} {set has_bcfield 1}               
                  }
               }
               ic_uns_subset_delete $unsmap
               if {!$has_bcfield} {lappend part_names $part}               
            }
         }
    }
    
    foreach part $part_names {
	foreach i {x1 x2 x3 wa1 wa2 wa3 wb1 wb2 wb3} { set bar_$i [set $i] }
	
	if {$bar_x1 == ""} {
	    foreach {n v} [aie_parse_bcs_element_prop_1d [ic_boco_get $part]] {
		set $n $v
	    }
	}
	if {$bar_x3 == ""} continue
	lappend vals([list $bar_x1 $bar_x2 $bar_x3 $bar_wa1 $bar_wa2 $bar_wa3 $bar_wb1 $bar_wb2 $bar_wb3]) $part
    }

    foreach data [array names vals] {
          set fams [ic_geo_list_families_in_part $vals($data)]
          set unsmap [ic_uns_subset_create_family_type $fams 1d 0 0]
          if {$unsmap != ""} {
             foreach p {X1 X2 X3 W1A W2A W3A W1B W2B W3B} {
                set param_name "Nastran:OR:$p"
                ic_uns_bcfield_define $param_name 0
                set val 0
                if {$p == "X1"} {set val [lindex $data 0]}
                if {$p == "X2"} {set val [lindex $data 1]}
                if {$p == "X3"} {set val [lindex $data 2]}
                if {$p == "W1A"} {set val [lindex $data 3]}
                if {$p == "W2A"} {set val [lindex $data 4]}
                if {$p == "W3A"} {set val [lindex $data 5]}
                if {$p == "W1B"} {set val [lindex $data 6]}
                if {$p == "W2B"} {set val [lindex $data 7]}
                if {$p == "W3B"} {set val [lindex $data 8]}
                if {$val == ""} {set val 0}
                ic_uns_bcfield_set $param_name $unsmap $val
             }
          }
          ic_uns_subset_delete $unsmap
	#eval ic_uns_set_part_bar_orientations \"$vals($data)\" $data
    }

}

proc aie_update_bar_end_release {{part_names ""} {pa ""} {pb ""}} {

    if {$part_names == ""} {
         set parts_tmp ""
	foreach pp [ic_boco_list_with_bctype PINFL] {
	    lappend parts_tmp [lindex $pp 0]
	}
         if {$parts_tmp != ""} {
            foreach part $parts_tmp {
               set has_bcfield 0
               set fams [ic_geo_list_families_in_part $part]
               set unsmap [ic_uns_subset_create_family_type $fams 1d 0 0]
               if {$unsmap != ""} {
                  foreach p {PA PB} {
                     set param_name "Nastran:PINFL:$p"
                     if ![ic_uns_bcfield_exists $param_name] {ic_uns_bcfield_define $param_name 0}
                     set bcs [ic_uns_bcfield_get $param_name $unsmap 0]
                     if {[lindex $bcs 0] != ""} {set has_bcfield 1}               
                  }
               }
               ic_uns_subset_delete $unsmap
               if {!$has_bcfield} {lappend part_names $part}               
            }
         }
    }
    
    foreach part $part_names {	
	if {$pa == "" && $pb == ""} {
	    foreach {n v} [aie_parse_bcs_element_prop_1d [ic_boco_get $part]] {
		set $n $v
	    }
	}
	if {$pa == "" && $pb == ""} continue
	lappend vals([list $pa $pb]) $part
    }

    foreach data [array names vals] {
          set fams [ic_geo_list_families_in_part $vals($data)]
          set unsmap [ic_uns_subset_create_family_type $fams 1d 0 0]
          if {$unsmap != ""} {
             foreach p {PA PB} {
                set param_name "Nastran:PINFL:$p"
                ic_uns_bcfield_define $param_name 0
                set val ""
                if {$p == "PA"} {set val [lindex $data 0]}
                if {$p == "PB"} {set val [lindex $data 1]}
                if {$val == ""} continue
                ic_uns_bcfield_set $param_name $unsmap $val
             }
          }
          ic_uns_subset_delete $unsmap
    }

}

proc aie_update_pcomp_pid {{part_names ""}} {
    global material_data aie_bctypes_used or_type
    global current_layers_to_load

    foreach {n v} {
	name "" pid 0 mat "" current2DProp 0 etype ""
	mat_b "(same as above)" mat_t "(same as above)" nsm 0.0
	mat_c "(same as above)"
	m_thick 0.0 b_stiff 1.0 part_name ""
	t_stiff 0.83333 thick 1.0
	lsdyna_mat_type 0 sigy 0.0 fail 0.0
         z0 -0.5 nsm_area "" sb "" ft_option "" tref "" ge "" lam_option "" layers_num 1
    } {
	set $n $v
    }

    if {$part_names == ""} {
	foreach pp [ic_boco_list_with_bctype PCOMP *:*] {
	    lappend part_names [lindex $pp 0]
	}
    }

    set update_attr 0    
    set pid_new 1
    foreach part $part_names {
	set fixit 0
         set pid_old 0
	foreach {n v} [aie_parse_bcs_element_prop_2d [ic_boco_get $part PCOMP]] {
	   if {$n == "pid"} {
	      if {$v == 0} {
                   set pid_old $v
	         set v $pid_new
	         set fixit 1
	         set update_attr 1
	         set pid_new [expr $pid_new + 1]
	      }
	   }
	   set $n $v 
	}
	if {$fixit} {
		if {$ft_option == ""} {set ft_value 1}
		if {$ft_option == "HILL"} {set ft_value 2} 
    		if {$ft_option == "HOFF"} {set ft_value 3} 
    		if {$ft_option == "TSAI"} {set ft_value 4} 
    		if {$ft_option == "STRN"} {set ft_value 5}
    		if {$lam_option == ""} {set lam_value 1}
    		if {$lam_option == "SYM"} {set lam_value 2} 
    		if {$lam_option == "MEM"} {set lam_value 3} 
    		if {$lam_option == "BEND"} {set lam_value 4} 
                 set name1 layers$pid_old
                 set name2 layers$pid
                 if {![info exists current_layers_to_load($name1)]} {
                      mess "Error no Layers were defined!\n" red
                      return
                 } else {                 
                    set vals ""
                    foreach val $current_layers_to_load($name1) {
	               lappend vals $val
                    }
                    aie_set_Layers_data $name2 $vals
                 }
                 set layers_list [list 1 PCOMP $pid $z0 $nsm_area $sb $ft_value $tref $ge $lam_value]
                 set name1 layers$pid
                 set layers [lindex $current_layers_to_load($name1) 0]
                 lappend layers_list $layers
                 for {set i 1} {$i  <= $layers} {incr i} {
                             set val [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+1]]
                             lappend layers_list [lindex $material_data($val) 0]
                             lappend layers_list [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+2]]
                             lappend layers_list [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+3]]
                             set val [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+4]]
                             if {$val == "NO"} {set val 0} 
                             if {$val == "YES"} {set val 1}
                             lappend layers_list $val
                 }
                 set bcs [list $layers_list]
                 set bcs2 ""
                 foreach bc $bcs {
	          set nbc ""
	          foreach b $bc {
	          if {$b == ""} { set b -999999 }
	             lappend nbc $b
	          }
	          lappend bcs2 $nbc
                 }
                 set dimtype element_prop_2d
                 set dim_bctypes $aie_bctypes_used($dimtype)
	       ic_boco_replace_in_part $bcs2 $part $dim_bctypes]
 	}
    }
    if {$update_attr} {
	mess "updating fbc file to fix Layered Composites...\n" blue
	aie_saveas_attr 0
	mess "done updating fbc file.\n" blue
    }
}

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

proc aie_parse_bcs_element_prop_0d {bcs} {
    global or_type

    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    CONM1 {
		foreach n {pid mass} v [lrange $bc 2 3] {
		    lappend ret $n $v
		}
		lappend ret etype "Mass point"
		lappend ret mass_type_index 1
		set got_it 1
	    }
	    CONM2 {
		foreach n {LCS mass x1 x2 x3 i11 i21 i22 i31 i32 i33} v [lrange $bc 2 12] {
                       if {$n == "LCS" && $v != "-1"} {
		       lappend ret $n [aie_get_lcs_name $v]
                      } else {
		       lappend ret $n $v
                      }
		}
		lappend ret etype "Mass point"
		lappend ret mass_type_index 0
		set got_it 1
	    }
	}
    }
    if !$got_it { return "" }
    return [aie_element_prop_mat_names_from_numbers $ret]
}

proc aie_parse_bcs_element_prop_1d {bcs} {
    global current_cross_section_to_load
    global or_type

    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    PBAR {
		foreach n {pid mat bar_a bar_i1 bar_i2 bar_j} v [lrange $bc 2 7] {
		    lappend ret $n $v
		}
		lappend ret etype "Bar"
		set or_type "PBAR"
		set got_it 1
	    }
	    OR {
		# This goes along with the PBAR
                   if {$or_type == "PELAS" || $or_type == "PDAMP" || $or_type == "PMASS"} {
                             foreach n {component_c1 component_c2} v [lrange $bc 3 4] {
		    	    lappend ret $n $v 
                            }       
                   } else {
			foreach n {bar_x1 bar_x2 bar_x3} v [lrange $bc 3 5] {
		    	   lappend ret $n $v
                            }
			if {[llength $bc] > 6} {
			   foreach n {bar_wa1 bar_wa2 bar_wa3 bar_wb1 bar_wb2 bar_wb3} v [lrange $bc 6 11] {
		    	      lappend ret $n $v
                               }
                            }
		}
	    }
	    PINFL {
		# This goes along with the PBAR
                   foreach n {pa pb} v [lrange $bc 3 4] {
                      lappend ret $n $v
                   }
	    }
	    CMP {
		# This goes along with the PELAS or PDAMP
                   if {$or_type == "PELAS" || $or_type == "PDAMP"|| $or_type == "PMASS"} {
                             foreach n {component_c1 component_c2} v [lrange $bc 3 4] {
		    	    lappend ret $n $v 
                            }       
                   }
	    }
	    RBAR {
		foreach n {indep_dof_cna indep_dof_cnb dep_dof_cna dep_dof_cnb} v [lrange $bc 2 5] {
		    lappend ret $n $v
		}
		lappend ret etype "Rigid beam"
		lappend ret beam_type_index 0
		set got_it 1
	    }
	    RBE2 {
		foreach n {dep_dofs} v [list [lindex $bc 4]] {
		    lappend ret $n $v
		}
		lappend ret etype "Rigid beam"
		lappend ret beam_type_index 1
		set got_it 1
	    }
	    RBE3 {
		foreach n {pid dep_dofs weight indep_dofs} v [list [lindex $bc 2] [lindex $bc 3] [lindex $bc 4] [lindex $bc 5]] {
		    lappend ret $n $v
		}
		lappend ret etype "Rigid beam"
		lappend ret beam_type_index 2
		set got_it 1
	    }
	    IROD {
		foreach n {pid mat area torsconst torscoeff nsm} v [lrange $bc 2 7] {
		    lappend ret $n $v
		}
		lappend ret etype "Rod connection"
		set got_it 1
	    }
	    PMASS {
		foreach n {pid mass} v [lrange $bc 2 3] {
		    lappend ret $n $v
		}
		lappend ret etype "Mass connection"
		set or_type "PMASS"
		set got_it 1
	    }
	    PDAMP {
		foreach n {pid force_per_vel} v [lrange $bc 2 3] {
		    lappend ret $n $v
		}
		lappend ret etype "Damper connection"
                   set or_type "PDAMP"
		set got_it 1
	    }
	    PELAS {
		foreach n {pid stiff damp stress} v [lrange $bc 2 5] {
		    lappend ret $n $v
		}
		lappend ret etype "Spring"
                   set or_type "PELAS"
		set got_it 1
	    }
	    PVISC {
		foreach n {pid damp_ext damp_rot} v [lrange $bc 2 4] {
		    lappend ret $n $v
		}
		lappend ret etype "Viscous damper"
		set got_it 1
	    }
	    PBEAM {
		foreach n {pid mat bar_a bar_i1 bar_i2 bar_i12 bar_j} v [lrange $bc 2 8] {
		    lappend ret $n $v
		}
		lappend ret etype "Beam"
                   set or_type "PBEAM"
		set got_it 1
	    }
	    PGAP {
		foreach n {pid gap_u0 gap_f0 gap_ka gap_kb gap_kt gap_mu1 gap_mu2 gap_tmax gap_mar gap_trmin} v [lrange $bc 2 12] {
		    lappend ret $n $v
		}
		lappend ret etype "Gap"
                   set or_type "PGAP"
		set got_it 1
	    }
	    PBARL {
		foreach n {pid mat group beam_type_option nsm_length_a dim} v [lrange $bc 2 7] {
                       if {$n == "beam_type_option"} {
		      switch $v {
		        0 {set v "I"}
		        1 {set v "T"}
		        2 {set v "L"}
 		        3 {set v "H"}
 		        4 {set v "BOX"}
 		        5 {set v "BAR"} 
 		        6 {set v "ROD"}
		        7 {set v "TUBE"}
		        8 {set v "HAT"}
		        9 {set v "CHAN"}  
		      }
                     }
                     if {$n == "pid"} {
                          set lid $v
                      }
                     if {$n == "dim"} {
                          set dims $v
                      }
  		   lappend ret $n $v
		}
                   set name cross_section$lid
                   set cross_section_list $dims
                   for {set i 1} {$i  <= $dims} {incr i} {
                       set val [lindex $bc [expr 7 + $i]]
                       lappend  cross_section_list  $val
                   }
                   aie_set_cross_section_data $name $cross_section_list
		lappend ret etype "Beam Cross-Section"
		lappend ret beamcs_type 0
                   set or_type "PBEAM"
#for translations to Ansys & LS-Dyna
		lappend ret dims [lrange $bc 8 end]
		set got_it 1
	    }
	    PBEAML {
		foreach n {pid mat group beam_type_option nsm_length_a nsm_length_b so_b_option dim} v [lrange $bc 2 9] {
                       if {$n == "beam_type_option"} {
		      switch $v {
		        0 {set v "I"}
		        1 {set v "T"}
		        2 {set v "L"}
 		        3 {set v "H"}
 		        4 {set v "BOX"}
 		        5 {set v "BAR"} 
 		        6 {set v "ROD"}
		        7 {set v "TUBE"}
		        8 {set v "HAT"}
		        9 {set v "CHAN"}  
		      }
                       }
                       if {$n == "so_b_option"} {
		      switch $v {
		         0 {set v "YES"}
		         1 {set v "NO"}
                         }
                       }
                       if {$n == "pid"} {
                          set lid $v
                       }
                       if {$n == "dim"} {
                          set dims $v
                          set v1 [expr ([llength $bc] - 9)/2]
                          set dims $v1
                       }
		    lappend ret $n $v
		}
                   set name cross_section$lid
                   set cross_section_list $dims
                   for {set i 1} {$i  <= $dims} {incr i} {
                       set val [lindex $bc [expr 9 + $i]]
                       lappend  cross_section_list  $val
                   }
                   for {set i 1} {$i  <= $dims} {incr i} {
                       set val [lindex $bc [expr 9 + $dims + $i]]
                       lappend  cross_section_list  $val
                   }
                   aie_set_cross_section_data $name $cross_section_list
		lappend ret etype "Beam Cross-Section"
		lappend ret beamcs_type 1
                   set or_type "PBEAM"
		lappend ret dims [lrange $bc 10 end]
		set got_it 1
	    }
	    LSDYNA_MATERIAL_BEAM {
		# This goes along with all 1D elements...
		lappend ret lsdyna_mat_type [lindex $bc 2]
		}
	    LSDYNA_BEAM_FORM {
		# This goes along with all 1D elements...
		lappend ret lsdyna_beam_type [lindex $bc 2]
		}
	}
    }
    if !$got_it { return "" }
    return [aie_element_prop_mat_names_from_numbers $ret]
}

proc aie_parse_bcs_element_prop_2d {bcs} {
    global or_type
    global current_layers_to_load

    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    ISHE {
		foreach n {pid mat mat_b mat_t nsm mat_c m_thick b_stiff t_stiff} v [lrange $bc 2 10] {
                       if {$n == "m_thick" && $v == "-999999"} {set v 0}
		    lappend ret $n $v
		}
		lappend ret etype "Shell"
		set got_it 1
	    }
	    ISHR {
		foreach n {pid mat thick nsm} v [lrange $bc 2 5] {
                       if {$n == "thick" && $v == "-999999"} {set v 0}
		    lappend ret $n $v
		}
		lappend ret etype "Shear"
		set got_it 1
	    }
	    PCOMP {
		foreach n {pid z0 nsm_area sb ft_value tref ge lam_value layers_num} v [lrange $bc 2 10] {
                       if {$n == "pid"} {
                           set lid $v
                       }
                       if {$n == "ft_value"} {
                           if {$v == 1} {set v ""} 
                           if {$v == 2} {set v "HILL"} 
                           if {$v == 3} {set v "HOFF"} 
                           if {$v == 4} {set v "TSAI"} 
                           if {$v == 5} {set v "STRN"}
                           set n "ft_option"
                       }
                       if {$n == "lam_value"} {
                           if {$v == 1} {set v ""} 
                           if {$v == 2} {set v "SYM"} 
                           if {$v == 3} {set v "MEM"} 
                           if {$v == 4} {set v "BEND"} 
                            set n "lam_option"
                       }
                       if {$n == "layers_num"} {
                         set layers $v
                       }
		    lappend ret $n $v
		}
                   set name layers$lid
                   set layers_list $layers
                   for {set i 1} {$i  <= $layers} {incr i} {
                       lappend  layers_list [aie_material_name_from_number [lindex $bc [expr 11 + (($i-1)*4)]]]
                       lappend  layers_list [lindex $bc [expr 11 + (($i-1)*4) + 1]]
                       lappend  layers_list [lindex $bc [expr 11 + (($i-1)*4) + 2]]
                       set val [lindex $bc [expr 11 + (($i-1)*4) + 3]]
                       if {$val == 0} {set val "NO"} 
                       if {$val == 1} {set val "YES"}
                       lappend  layers_list  $val
                   }
                   aie_set_Layers_data $name $layers_list
 		lappend ret etype "Layered composite"
		set got_it 1
	    }
	    LSDYNA_MATERIAL_SHELL {
		# This goes along with all 2D elements...
#		foreach n {lsdyna_mat_type sigy fail} v [list [lindex $bc 2] [lindex $bc 3] [lindex $bc 4]] {
#			lappend ret $n $v
#		}
	    }
	}
    }
    if !$got_it { return "" }
    return [aie_element_prop_mat_names_from_numbers $ret]
}

proc aie_parse_bcs_element_prop_3d {bcs} {
    global or_type
    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    IVOL {
		foreach n {mat LCS pid} v [list [lindex $bc 2] [lindex $bc 3] [lindex $bc 5]] {
                       if {$n == "LCS"} {
		       lappend ret $n [aie_get_lcs_name $v]
                      } else {
		       lappend ret $n $v
                      }
		}
		lappend ret etype "Volume"
		set got_it 1
	    }
	    GASKET {
		lappend ret gasket [lindex $bc 2]
		lappend ret gasket_gap_data [lindex $bc 3]
		lappend ret gasket_loading_data [lindex $bc 4]
		lappend ret gasket_unloading_data [lindex $bc 5]
		lappend ret gasket_tss_data [lindex $bc 6]
		set got_it 1
	    }
	    LSDYNA_MATERIAL_SOLID {
		# This goes along with all 3D elements...
#		foreach n {lsdyna_mat_type sigy fail} v [list [lindex $bc 2] [lindex $bc 3] [lindex $bc 4]] {
#			lappend ret $n $v
#		}
	    }
	}
    }
    if !$got_it { return "" }
    return [aie_element_prop_mat_names_from_numbers $ret]
}

proc aie_element_prop_mat_names_from_numbers {bcs} {
    set bcs2 ""
    # This is not so elegant
    foreach {n v} $bcs {
	if [string match mat* $n] {
	    lappend bcs2 ${n}_number $v
	    set v [aie_material_name_from_number $v]
	}
	lappend bcs2 $n $v
    }
    return $bcs2
}

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

proc aie_apply_icons_element_prop_all {dim fam obtype vis lvis} {
    global aie_bctypes_used
    
    set bcs [ic_boco_get $fam]
    if {$bcs == ""} { return 0 }
    set parsed [aie_parse_bcs_element_prop_${dim}d $bcs]
    if {$parsed == ""} { return 0 }
    foreach {n v} $parsed { set $n $v }
    
    if [catch {
	switch $dim {
	    0 {
	    }
	    1 {
		switch $etype {
		    "Bar" - "Rigid beam" - "Rod connection" - "Beam" - "Gap" - "Beam Cross-Section" {
			ic_boco_apply_generic_icon_to_group $fam rigid_bar {} $vis 1
		    }
		    "Spring" {
			ic_boco_apply_generic_icon_to_group $fam spring {} $vis 1
		    }
		    "Mass connection" - "Damper connection" - "Viscous damper" {
			ic_boco_apply_generic_icon_to_group $fam damper {} $vis 1
		    }
		}
		if [info exists bar_x1] {
		    ic_boco_apply_generic_icon_to_group $fam orientation \
			[list $bar_x1 $bar_x2 $bar_x3 0 0 0 0 0 0] $vis 1
		}
	    }
	    2 {
	    }
	    3 {
	    }
	}
    } res] {
	mess $res\n red
    }
    
    return 1
}

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

proc aie_apply_icons_element_prop_0d {fam obtype vis lvis} {
    aie_apply_icons_element_prop_all 0 $fam $obtype $vis $lvis
}

proc aie_apply_icons_element_prop_1d {fam obtype vis lvis} {
    aie_apply_icons_element_prop_all 1 $fam $obtype $vis $lvis
}

proc aie_apply_icons_element_prop_2d {fam obtype vis lvis} {
    aie_apply_icons_element_prop_all 2 $fam $obtype $vis $lvis
}

proc aie_apply_icons_element_prop_3d {fam obtype vis lvis} {
    aie_apply_icons_element_prop_all 3 $fam $obtype $vis $lvis
}

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

proc aie_edit_element_prop_0d {name} {
    aie_edit_attribute $name element_prop_0d 0DEleProp
}

proc aie_edit_element_prop_1d {name} {
    aie_edit_attribute $name element_prop_1d 1DEleProp
}

proc aie_edit_element_prop_2d {name} {
    aie_edit_attribute $name element_prop_2d 2DEleProp
}

proc aie_edit_element_prop_3d {name} {
    aie_edit_attribute $name element_prop_3d 3DEleProp
}

proc aie_element_prop_2d_pcomp_layers {} {
    global current_layers_to_load
    global material_data
    global layers_data
    
    set layers [create2DElePropDEZ get layers_num]
    set lid [create2DElePropDEZ get pid]
    
    set MatList [lsort [array names material_data]]
    
    catch {form_kill .pcomp_layers}
    
    set buts [list [list OK "aie_pcomp_layers_doit $lid $layers"]  cancel]
    set d [form_init .pcomp_layers "Set Layers" "" $buts]
    if {$d != ""} {
	form_label $d.layer "Layer" {0 1}
	form_label $d.mid "Material" {0 2}
	form_label $d.t "Thickness" {0 4}
	form_label $d.theta "Orientation angle" {0 5}
	form_label $d.sout "Stress/Strain output request" {0 6}
         set name1 layers$lid
	if {[info exists current_layers_to_load($name1)]} {
              set layers1 [lindex $current_layers_to_load($name1) 0]
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name layer$i
             set layers_data($name) $i
	    form_entry $d.layer$i layers_data($name) int "$i 1 -sticky ewn -padx 0"
	    set name mid$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name) [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+1]]
             } else {
                 set layers_data($name) ""
                 if {$i > 1} {set layers_data($name) "(same as above)"}
             }
	    futil_menuentry $d.mid$i layers_data($name) [concat list $MatList] "$i 2 -sticky ewn -padx 0"
	    set name t$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name) [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+2]]
             } else {
                 set layers_data($name) 0.0
                 if {$i > 1} {set layers_data($name) "(same as above)"}
             }
	    form_entry $d.t$i layers_data($name) float "$i 4 -sticky ewn -padx 0"
	    set name theta$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name) [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+3]]
             } else {
                 set layers_data($name) 0.0
             }
	    form_entry $d.theta$i layers_data($name) float "$i 5 -sticky ewn -padx 0"
             set name sout$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name) [lindex $current_layers_to_load($name1) [expr (($i-1)*4)+4]]
             } else {
                 set layers_data($name) "NO"
                 if {$i > 1} {set layers_data($name) "(same as above)"}
             }
	    futil_menuentry $d.sout$i layers_data($name) {list YES NO} "$i 6 -sticky ewn -padx 0"
	}

	form_finish  middle 1 1
    }
    
}

proc aie_pcomp_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data
   global material_data

   set mid_last ""
   set t_last ""
   set sout_last ""

   set name1 layers$lid
   set layers_list $layers
   for {set i 1} {$i  <= $layers} {incr i} {
         set name mid$i
         if {$layers_data($name) == "(same as above)"} {
             lappend  layers_list $mid_last
         } else { 
             lappend  layers_list $layers_data($name)
             set mid_last $layers_data($name)
         }
         set name t$i
         if {$layers_data($name) == "(same as above)"} {
             lappend  layers_list $t_last
         } else { 
             lappend  layers_list $layers_data($name)
             set t_last $layers_data($name)
         }
         set name theta$i
         lappend  layers_list $layers_data($name)
         set name sout$i
         if {$layers_data($name) == "(same as above)"} {
             lappend  layers_list $sout_last
         } else { 
             lappend  layers_list $layers_data($name)
             set sout_last $layers_data($name)
         }
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .pcomp_layers

}

proc aie_set_Layers_data {name vals} {
    global current_layers_to_load
    set current_layers_to_load($name) $vals
}

proc aie_element_prop_3d_gasket {{type loading}} {

	global aie_edit_gasket tdv_default gasket_data

	set use 1
    if {$type=="unloading" || $type=="tss" } {
		set use [create3DElePropDEZ get gasket_${type}_flag]
	}
	if !$use {
		mess "error: $type not turned on for gasket data\n" red
		return
	}

	set temp 0
#	set x_label "Ordinate"
#	set y_label "Abscissa"
#	set graph_title "$type plot"
   
	switch -exact $type {
		loading - unloading {
			set x_label "Pressure"
			set y_label "Closure"
			set temp [lsearch [create3DElePropDEZ get gasketDataList] [create3DElePropDEZ get gasket_$type]]
			if $temp {
				set graph_title "Temperature dependent $type plots"
			} else {
				set graph_title "X-Y $type plot"
			}
		}
		tss {
			if {$use==1} {
				mess "error: Please enter constant value for $type gasket data\n" red
				return
			}
			set x_label "Temperature"
			set y_label "TSS"
			set graph_title "Temperature dependent $type plot"
		}
		default {
			mess "error: $type curve not supported for gasket data\n" red
			return
		}
	}

    set data $aie_edit_gasket($type)

	if [info exists aie_edit_gasket(currently_editing)] {
		tk_dialog .duplicate_gasket_table Plot "Please finish editing the plot you have up now before you start editing another one." "" -1 Ok
		return
	}

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

}

proc aie_element_prop_1d_cross_section {beam_type} {
    global current_cross_section_data_to_load
    global cross_section_data

    set di0 0 
    set di1 0
    set di2 0
    set di3 0 
    set di4 0
    set di5 0

    set cross_section_type [create1DElePropDEZ get beam_type_option]
    set lid [create1DElePropDEZ get pid]

    if {$cross_section_type == ""} {
        user_error "No Beam Cross Section Type is selected.\n"
        return
    }

    switch $cross_section_type {
       "I" {
            set dims 6
            set di0 3.2 
            set di1 1.5
            set di2 2.0
            set di3 0.5 
            set di4 0.6
            set di5 0.8
       }
       "T" {
            set dims 4
            set di0 2 
            set di1 3
            set di2 0.6
            set di3 0.5 
       }
       "L" {
            set dims 4
            set di0 1.8 
            set di1 2.8
            set di2 0.6
            set di3 0.25 
       }
       "H" {
            set dims 4
            set di0 1.0 
            set di1 0.6
            set di2 3
            set di3 0.5 
       }
       "BOX" {
            set dims 4
            set di0 3 
            set di1 3
            set di2 0.6
            set di3 0.5 
       }
       "BAR" {
            set dims 2
            set di0 3 
            set di1 2
       } 
       "ROD" {
            set dims 1
            set di0 3
       }
       "TUBE" {
            set dims 2
            set di0 3
            set di1 2
      }
       "HAT" {
            set dims 4
             set di0 2.2 
            set di1 0.5
            set di2 3
            set di3 0.8 
       }
       "CHAN" {
            set dims 5
            set di0 1.8 
            set di1 2.8
            set di2 0.25
            set di3 0.6
       }  
    }

    catch {form_kill .dcross_section}
    
    set buts [list [list OK "aie_dcross_section_doit $beam_type $lid $dims"]  [list cancel "aie_dcross_section_cancel"]]
    set d [form_init .dcross_section "Set Cross Section" "" $buts]
    if {$d != ""} {
	form_label $d.dim "Dimension" {0 1}
	if {$beam_type == "PBARL"} {form_label $d.val "Value" {0 2}}
	if {$beam_type == "PBEAML"} {form_label $d.val "Value (A)" {0 2}}
         if {$beam_type == "PBEAML"} {form_label $d.valb "Value (B)" {0 3}}
         set name1 cross_section$lid
	if {[info exists current_cross_section_to_load($name1)]} {
              set dims1 [lindex $current_cross_section_to_load($name1) 0]
         }
         for {set i 1} {$i  <= $dims} {incr i} {
             set name dim$i
             set cross_section_data($name) D$i
 	    form_entry $d.dim$i cross_section_data($name) string "$i 1 -sticky ewn -padx 0"
             set name val$i
             set j [expr ($i-1)]
	    if {[info exists current_cross_section_to_load($name1)] && ($i <= $dims1)} {
                 set cross_section_data($name) [lindex $current_cross_section_to_load($name1) $i]
             } else {
                set k [set di$j]
                set cross_section_data($name) $k
             }
             set di$j $cross_section_data($name)
             form_entry $d.val$i cross_section_data($name) float "$i 2 -sticky ewn -padx 0"
             if {$beam_type == "PBEAML"} {
                 set name valb$i
                 set j [expr ($i-1)]
	        if {[info exists current_cross_section_to_load($name1)] && ($i <= $dims1)} {
                    set cross_section_data($name) [lindex $current_cross_section_to_load($name1) [expr $dims1 +$i]]
                 } else {
                    set k [set di$j]
                    set cross_section_data($name) $k
                 }
                 form_entry $d.valb$i cross_section_data($name) float "$i 3 -sticky ewn -padx 0"
            }
        }
        set j [expr ($i+1)]
        form_button $d.update Update "aie_dcross_section_update $dims $cross_section_type val" "$j 2" 
        if {$beam_type == "PBEAML"} {form_button $d.updateb Update "aie_dcross_section_update $dims $cross_section_type valb" "$j 3" }
        form_finish  middle 1 1
    }

    beamcross_cmd in_plot_frame
    beamcross_cmd reset $cross_section_type $di0 $di1 $di2 $di3 $di4 $di5

}

proc aie_dcross_section_doit {beam_type lid dims} {
   global current_cross_section_to_load
   global cross_section_data
 
   set name1 cross_section$lid
   set cross_section_list $dims
   for {set i 1} {$i  <= $dims} {incr i} {
         set name val$i
         lappend  cross_section_list $cross_section_data($name)
   }

   if {$beam_type == "PBEAML"} {
      for {set i 1} {$i  <= $dims} {incr i} {
           set name valb$i
           lappend  cross_section_list $cross_section_data($name)
      }
   }

   aie_set_cross_section_data $name1 $cross_section_list

   beamcross_cmd off
   form_kill .dcross_section

}

proc aie_set_cross_section_data {name vals} {
    global current_cross_section_to_load
    set current_cross_section_to_load($name) $vals
}

proc aie_dcross_section_update {dims cross val} {
    global cross_section_data

    set di0 0 
    set di1 0
    set di2 0
    set di3 0 
    set di4 0
    set di5 0

    for {set i 1} {$i  <= $dims} {incr i} {
         set name $val$i
         set j [expr ($i-1)]
         set di$j $cross_section_data($name)
    }
    beamcross_cmd reset $cross $di0 $di1 $di2 $di3 $di4 $di5 
}

proc aie_dcross_section_cancel {} {
   beamcross_cmd off
   form_kill .dcross_section
}

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

proc aie_element_summary {} {
    global aie_bctypes_used aie_bcclass_title aie_bcclass_is_part
    global material_tbl_data material_data lcs_data
    
    array set bc_usage [ic_boco_bctypes_used_by_families]
    
    foreach ff [ic_uns_list_families] {
	set c [ic_uns_count_in_families [list $ff]]
	foreach g [split $ff :] {
	    if [info exists groupcount($g)] {
		incr groupcount($g) $c
	    } else {
		set groupcount($g) [lindex $c 0]
	    }
	}
    }
    
    foreach type [lsort [array names aie_bctypes_used]] {
	set title $aie_bcclass_title($type)
	if {[info proc aie_apply_icons_$type] == ""} continue
	foreach b $aie_bctypes_used($type) {
	    if [info exists bc_usage($b)] {
		foreach fam [lsort $bc_usage($b)] {
		    if [string match *:* $fam] continue
		    if [info exists seen($fam)] continue
		    set seen($fam) 1
		    
		    set title1 $title
		    if {[info exists aie_bcclass_is_part($type)] && $aie_bcclass_is_part($type)} {
			set b2 $b
			if [info exists aie_bcclass_title($b)] { set b2 $aie_bcclass_title($b) }
			append title1 " ($b2)"
		    }
		    if [info exists typecount($title1)] {
			incr typecount($title1)
			incr elemcount($title1) $groupcount($fam)
		    } else {
			set typecount($title1) 1
                            if [info exists groupcount($fam)] {
			   set elemcount($title1) $groupcount($fam)
                            } else {
			   set elemcount($title1) 0
                            }
		    }
		}
	    }
	}
    }
    mess "\nElement/property summary:\n"
    foreach type [lsort [array names typecount]] {
	mess "$type: $typecount($type) definitions, $elemcount($type) elements\n"
    }
    mess "Tables: [array size material_tbl_data]\n"
    mess "Materials: [array size material_data]\n"
    mess "LCSs: [expr [array size lcs_data] - 1]\n"
}













