
# You don't have to worry about pulling the variables
# mentioned in the .ttkrf files into scope with either
# a global or variable statement.  The framework takes
# care of that for you.

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

#set ::aie_bctypes_used(impdisp) {ISPC LSDYNA_NSID}
set ::aie_bctypes_used(impdisp) {ISPC}
set ::aie_bctypes_used(mpcdisp) {MPC}
set ::aie_bctypes_used(displacement) {ISPC MPC}

set ::aie_tree_nodes(impdisp) Displacements
set ::aie_tree_nodes(mpcdisp) Displacements
set ::aie_tree_nodes(displacement) Displacements

set ::aie_prop_subtype(impdisp) displacement
set ::aie_prop_subtype(mpcdisp) displacement

proc aie_edit_impdisp {name} {
    aie_edit_attribute $name impdisp Impdisp
}

proc aie_edit_mpcdisp {name} {
    aie_edit_attribute $name mpcdisp Mpcdisp
}

set ::aie_bcclass_title(connector) Connector
set ::aie_bcclass_title(contact) Contact
set ::aie_bcclass_title(impdisp) Displacement
set ::aie_bcclass_title(mpcdisp) Displacement
set ::aie_bcclass_title(displacement) Displacement

set ::aie_bcclass_title(rigidwall) "Rigid wall"
set ::aie_bcclass_title(velocity) Velocity
set ::aie_bcclass_title(singlecontact) "Single Surface Contact"
set ::aie_bcclass_title(constrainednodes) "Constrained nodes"

proc aie_parse_bcs_impdisp {bcs} {
    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    FR {
		# This one is obsolete
		foreach n {spcSet CheckROTX CheckROTY CheckROTZ ROTX ROTY ROTZ } v [concat [lindex $bc 2] [lrange $bc 5 10]] {
		    lappend ret $n $v
		}
		set got_it 1
	    }
	    FD {
		# This one is obsolete
		foreach n {spcSet CheckUX CheckUY CheckUZ UX UY UZ} v [concat [lindex $bc 2] [lrange $bc 5 10]] {
		    lappend ret $n $v
		}
		set got_it 1
	    }
	    ISPC {
		lappend ret spcSet [lindex $bc 2] LCS [lindex $bc 5]
                   set SPCType [lindex $bc 6]
                   if {$SPCType == ""} {set SPCType 1}
                   lappend ret SPCType $SPCType
		set dofs [lindex $bc 3]
		set val [lindex $bc 4]
		foreach i {1 2 3 4 5 6} n {UX UY UZ ROTX ROTY ROTZ} {
		    if [string match *$i* $dofs] {
			lappend ret Check$n 1 $n $val
		    }
		}
		set got_it 1
	    }
	    LSDYNA_NSID {
			lappend ret lsdyna_nsid [lindex $bc 2]
			set got_it 1
	    }
	}
    }
    if $got_it {
	return $ret
    } else {
	return ""
    }
}

proc onCreatePointImpdispDEZLoad {} {
    global current_impdisp_to_load lcs_data
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
    
  foreach {n v} {
    name "" spcSet 1 UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion" lsdyna_nsid ""
  } { set $n $v }

  foreach {n1 n2 v1 v2} {
    PointCheckUXGGVal createPointImpdispCheckUX 0 1
    PointCheckUYGGVal createPointImpdispCheckUY 0 1
    PointCheckUZGGVal createPointImpdispCheckUZ 0 1
    PointCheckROTXGGVal createPointImpdispCheckROTX 0 1
    PointCheckROTYGGVal createPointImpdispCheckROTY 0 1
    PointCheckROTZGGVal createPointImpdispCheckROTZ 0 1
  } {
    set $n1 $v1
    ::AIEnv::GUIManagement::ToggleGrey $n2 $v2
  }

#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>

  set spcSetList [aie_list_sets impdisp]
  if {$spcSet == ""} { set spcSet [lindex $spcSetList 0] }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaPointImpdispNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaPointImpdispNodeSetId 1
  }
}
    

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

    if ![info exists objList] { 
      set objList {} 
    } else {
      if { $objList == "" } {
        mess "No points selected\n"
	return
      } else {
		if {$solve_format == "2"} {
		if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
			user_error "Please enter a valid Node Set ID"
			set objList {}
			return
		}
		}
        set CheckUX $PointCheckUXGGVal
        set CheckUY $PointCheckUYGGVal
        set CheckUZ $PointCheckUZGGVal
        set CheckROTX $PointCheckROTXGGVal
        set CheckROTY $PointCheckROTYGGVal
        set CheckROTZ $PointCheckROTZGGVal

        foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ point $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
        set $n $v 
      }
      set objList {}
    }
  }
}



proc onCreateCurveImpdispDEZLoad {} {
    global current_impdisp_to_load lcs_data
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
	
  foreach {n v} {
    name "" spcSet 1 UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion" lsdyna_nsid ""
  } { set $n $v }

  foreach {n1 n2 v1 v2} {
    CurveCheckUXGGVal createCurveImpdispCheckUX 0 1
    CurveCheckUYGGVal createCurveImpdispCheckUY 0 1
    CurveCheckUZGGVal createCurveImpdispCheckUZ 0 1
    CurveCheckROTXGGVal createCurveImpdispCheckROTX 0 1
    CurveCheckROTYGGVal createCurveImpdispCheckROTY 0 1
    CurveCheckROTZGGVal createCurveImpdispCheckROTZ 0 1
  } {
    set $n1 $v1
    ::AIEnv::GUIManagement::ToggleGrey $n2 $v2
  }
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>
  set spcSetList [aie_list_sets impdisp]
  if {$spcSet == ""} { set spcSet [lindex $spcSetList 0] }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveImpdispNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveImpdispNodeSetId 1
  }
}

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

  if ![info exists objList] { 
    set objList {} 
  } else {
    if { $objList == "" } {
      mess "No curves selected\n"
      return
    } else {
		if {$solve_format == "2"} {
		if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
			user_error "Please enter a valid Node Set ID"
			set objList {}
			return
		}
		}
      set CheckUX $CurveCheckUXGGVal
      set CheckUY $CurveCheckUYGGVal
      set CheckUZ $CurveCheckUZGGVal
      set CheckROTX $CurveCheckROTXGGVal
      set CheckROTY $CurveCheckROTYGGVal
      set CheckROTZ $CurveCheckROTZGGVal

      foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ curve $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
        set $n $v 
      }
      set objList {}
    }
  }
}



proc onCreateSurfImpdispDEZLoad {} {
    global current_impdisp_to_load lcs_data
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
	
  foreach {n v} {
    name "" spcSet 1 UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion" lsdyna_nsid ""
  } { set $n $v }

  foreach {n1 n2 v1 v2} {
    SurfCheckUXGGVal createSurfImpdispCheckUX 0 1
    SurfCheckUYGGVal createSurfImpdispCheckUY 0 1
    SurfCheckUZGGVal createSurfImpdispCheckUZ 0 1
    SurfCheckROTXGGVal createSurfImpdispCheckROTX 0 1
    SurfCheckROTYGGVal createSurfImpdispCheckROTY 0 1
    SurfCheckROTZGGVal createSurfImpdispCheckROTZ 0 1
  } {
    set $n1 $v1
    ::AIEnv::GUIManagement::ToggleGrey $n2 $v2
  }
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>
  set spcSetList [aie_list_sets impdisp]
  if {$spcSet == ""} { set spcSet [lindex $spcSetList 0] }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfImpdispNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfImpdispNodeSetId 1
  }
}

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

  if ![info exists objList] { 
    set objList {} 
  } else {
    if { $objList == "" } {
      mess "No surfaces selected\n"
      return
    } else {
		if {$solve_format == "2"} {
		if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
			user_error "Please enter a valid Node Set ID"
			set objList {}
			return
		}
		}
      set CheckUX $SurfCheckUXGGVal
      set CheckUY $SurfCheckUYGGVal
      set CheckUZ $SurfCheckUZGGVal
      set CheckROTX $SurfCheckROTXGGVal
      set CheckROTY $SurfCheckROTYGGVal
      set CheckROTZ $SurfCheckROTZGGVal

      foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ surf $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
        set $n $v 
      }
      set objList {}
    }
  }
}




proc onCreateSubsetImpdispDEZLoad {} {
    global current_impdisp_to_load lcs_data

    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
	
  foreach {n v} {
    name "" spcSet 1 UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion"
  } { set $n $v }

  foreach {n1 n2 v1 v2} {
    SubsetCheckUXGGVal createSubsetImpdispCheckUX 0 1
    SubsetCheckUYGGVal createSubsetImpdispCheckUY 0 1
    SubsetCheckUZGGVal createSubsetImpdispCheckUZ 0 1
    SubsetCheckROTXGGVal createSubsetImpdispCheckROTX 0 1
    SubsetCheckROTYGGVal createSubsetImpdispCheckROTY 0 1
    SubsetCheckROTZGGVal createSubsetImpdispCheckROTZ 0 1
  } {
    set $n1 $v1
    ::AIEnv::GUIManagement::ToggleGrey $n2 $v2
  }
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>
  set spcSetList [aie_list_sets impdisp]
  if {$spcSet == ""} { set spcSet [lindex $spcSetList 0] }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaSubsetImpdispNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaSubsetImpdispNodeSetId 1
  }
}

proc onCreateSubsetImpdispDEZApply {} {
  global current_impdisp_to_load

  if ![info exists objList] { 
    set objList {} 
  } else {
    if { $objList == "" } {
      mess "No subsets selected\n"
      return
    } else {
      set CheckUX $SubsetCheckUXGGVal
      set CheckUY $SubsetCheckUYGGVal
      set CheckUZ $SubsetCheckUZGGVal
      set CheckROTX $SubsetCheckROTXGGVal
      set CheckROTY $SubsetCheckROTYGGVal
      set CheckROTZ $SubsetCheckROTZGGVal

      foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ subset $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
        set $n $v 
      }
      set objList {}
    }
  }
}

proc onCreateExistingImpdispDEZLoad {} {
  global current_impdisp_to_load lcs_data
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
	
  foreach {n v} {
    name "" spcSet 1 UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion" lsdyna_nsid ""
  } {
    if [info exists current_impdisp_to_load($n)] {
	set $n $current_impdisp_to_load($n)
    } else {
	set $n $v
    }
  }
    set LCS [aie_get_lcs_name $LCS]
    set SPCType [aie_get_SPCType_name $SPCType]

  foreach {n1 n2 n3 v} {
    CheckUX ExistingCheckUXGGVal createExistingImpdispCheckUX 0
    CheckUY ExistingCheckUYGGVal createExistingImpdispCheckUY 0
    CheckUZ ExistingCheckUZGGVal createExistingImpdispCheckUZ 0
    CheckROTX ExistingCheckROTXGGVal createExistingImpdispCheckROTX 0
    CheckROTY ExistingCheckROTYGGVal createExistingImpdispCheckROTY 0
    CheckROTZ ExistingCheckROTZGGVal createExistingImpdispCheckROTZ 0
  } {
    if [info exists current_impdisp_to_load($n1)] {
      set $n2 $current_impdisp_to_load($n1)
      ::AIEnv::GUIManagement::ToggleGrey $n3 0
    } else {
      set $n2 $v
      ::AIEnv::GUIManagement::ToggleGrey $n3 1
    }
  }

    createExistingImpdispLCS setvalue @[lsearch $LCSList $LCS]
 #VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>   
  set spcSetList [aie_list_sets impdisp]
  if {$spcSet == ""} { set spcSet [lindex $spcSetList 0] }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingImpdispNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingImpdispNodeSetId 1
  }

}

proc onCreateExistingImpdispDEZApply {} {
  global current_impdisp_to_load
  global  solver_setup solver_setup_global

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

  if ![info exists objList] { 
    set objList {} 
  }

  if {$solve_format == "2"} {
     if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
        user_error "Please enter a valid Node Set ID"
        set objList {}
        return
     }
  }

  set CheckUX $ExistingCheckUXGGVal
  set CheckUY $ExistingCheckUYGGVal
  set CheckUZ $ExistingCheckUZGGVal
  set CheckROTX $ExistingCheckROTXGGVal
  set CheckROTY $ExistingCheckROTYGGVal
  set CheckROTZ $ExistingCheckROTZGGVal
  foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ existing $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
    set $n $v 
  }
  set objList {}
}

proc applyImpdisp {CheckUX CheckUY CheckUZ UX UY UZ CheckROTX CheckROTY CheckROTZ ROTX ROTY ROTZ type objects name spcset LCS SPCType nsid} {
    #set bcs [list [list 1 FD $spcset 0 0 $CheckUX $CheckUY $CheckUZ $UX $UY $UZ] [list 1 FR $spcset 0 0 $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ]]
    
    foreach d {UX UY UZ ROTX ROTY ROTZ} {
	if [set Check$d] {
	    set v [set $d]
	    lappend with_val($v) $d
	}
    }
    set lcsnum [aie_get_lcs_num $LCS]
    set spc_type [aie_get_SPCType_num $SPCType]
    set bcs ""
    foreach w [array names with_val] {
	set dofs ""
	foreach i {1 2 3 4 5 6} n {UX UY UZ ROTX ROTY ROTZ} {
	    if {[lsearch $with_val($w) $n] >= 0} { append dofs $i }
	}
	#lappend bcs [list 1 ISPC $spcset 0 $dofs $w]
	lappend bcs [list 1 ISPC $spcset $dofs $w $lcsnum $spc_type]
    }
    if {$bcs != ""} {
	if {$nsid != "" && $nsid > 0} {
		lappend bcs [list 1 LSDYNA_NSID $nsid]
	}
	aie_apply_attribute impdisp $bcs IMPDISP $name $type $objects \
	    [select_get_parts_and_subsets]
    }
}

proc aie_apply_icons_impdisp {fam obtype vis lvis} {
    foreach i {CheckUX CheckUY CheckUZ CheckROTX CheckROTY CheckROTZ UX UY UZ ROTX ROTY ROTZ} { set $i 0 }
    set name ""
    set bcs [ic_boco_get $fam]
    if {$bcs == ""} { return 0 }
    set parsed [aie_parse_bcs_impdisp $bcs]
    if {$parsed == ""} { return 0 }
    foreach {n v} $parsed { set $n $v }
    foreach i {UX UY UZ ROTX ROTY ROTZ} {
	if ![set Check$i] { set $i 0 }
    }
    ic_boco_apply_generic_icon_to_group $fam impdisp \
	[list $UX $UY $UZ $ROTX $ROTY $ROTZ] $vis
    return 1
}

proc aie_get_impdisp_set {fam} {
    return [aie_get_bc_argument $fam impdisp spcSet]
}

proc aie_get_SPCType_num {SPCType} {
    if {$SPCType == "Constraint with Enforced Motion"} {return 1}
    if {$SPCType == "Constraint Only"} {return 0}
    if {$SPCType == "Enforced Motion Only"} {return 2}
    return 1
}

proc aie_get_SPCType_name {SPCType} {
    if {$SPCType == "1"} {return "Constraint with Enforced Motion"}
    if {$SPCType == "0"} {return "Constraint Only"}
    if {$SPCType == "2"} {return "Enforced Motion Only"}
    return "Constraint with Enforced Motion"
}

proc onCreateSingledisplacementDEZLoad {} {
    global current_impdisp_to_load lcs_data
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
    set SPCList {"Constraint with Enforced Motion" "Constraint Only" "Enforced Motion Only"}
	
  foreach {n v} {
    name "" spcSet "" UX 0 UY 0 UZ 0 ROTX 0 ROTY 0 ROTZ 0 LCS Global SPCType "Constraint with Enforced Motion" lsdyna_nsid ""
  } { set $n $v }

  foreach {n1 n2 v1 v2} {
    createSPCCheckUXGGVal createSPCCheckUX 0 1
    createSPCCheckUYGGVal createSPCCheckUY 0 1
    createSPCCheckUZGGVal createSPCCheckUZ 0 1
    createSPCCheckROTXGGVal createSPCCheckROTX 0 1
    createSPCCheckROTYGGVal createSPCCheckROTY 0 1
    createSPCCheckROTZGGVal createSPCCheckROTZ 0 1
  } {
    set $n1 $v1
    ::AIEnv::GUIManagement::ToggleGrey $n2 $v2
  }
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}"
	aie_on_change_LCS [namespace current] \
	  {UX UY UZ ROTX ROTY ROTZ} {UX UY UZ ROTX ROTY ROTZ}
#VG>
  set spcSetList [aie_list_sets impdisp]
   set nlength [llength $spcSetList]
   if {$spcSet == "" && $nlength > 0} { 
       set spcSet [expr ([lindex [lsort $spcSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set spcSet 1
   }
    set AllowActiveReload 1

  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 == "2"} {
     ::AIEnv::GUIManagement::ToggleGrey lsdynacreateSPCNodeSetId 0
  } else {
     ::AIEnv::GUIManagement::ToggleGrey lsdynacreateSPCNodeSetId 1
  }
}

proc onCreateSingledisplacementDEZApply {} {
  global current_impdisp_to_load
  global  solver_setup solver_setup_global
  global bc_name_list

  set def_name 0

  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 bc_name_list] && [lsearch $bc_name_list $name] >= 0} {
            user_error "The Name entered is already used.\nPlease enter another Name."
            set objList {}
            return
      }
  } else {
     set def_name 1
  }

  switch $createSPCVal { 
      0 { set entity "point"}
      1 { set entity "curve"}
      2 { set entity "surf"}
      3 { set entity "subset"}
      4 { set entity "part"}
      default {set entity ""}
  }
	    
  if {$solver_setup != "-1"} {
     set solve_format $solver_setup
  } else {
     set solve_format $solver_setup_global
  }

  if ![info exists objList] { 
    set objList {} 
  } else {
    if { $objList == "" } {
      mess "No entity is selected\n"
      return
    } else {
      if {$solve_format == "2"} {
         if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
            user_error "Please enter a valid Node Set ID"
            set objList {}
            return
         }
      }
      set CheckUX $createSPCCheckUXGGVal
      set CheckUY $createSPCCheckUYGGVal
      set CheckUZ $createSPCCheckUZGGVal
      set CheckROTX $createSPCCheckROTXGGVal
      set CheckROTY $createSPCCheckROTYGGVal
      set CheckROTZ $createSPCCheckROTZGGVal

      foreach {n v} [applyImpdisp $CheckUX $CheckUY $CheckUZ $UX $UY $UZ $CheckROTX $CheckROTY $CheckROTZ $ROTX $ROTY $ROTZ $entity $objList $name $spcSet $LCS $SPCType $lsdyna_nsid] { 
        set $n $v 
      }
      set objList {}
      if {$def_name} {set name ""}
    }
  }

}

proc onCreateMultipledisplacementDEZLoad {} {
    global current_mpcdisp_to_load lcs_data
    global  solver_setup solver_setup_global
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
	
    foreach {n v} {
       name "" mpcSet "" LCS Global dep_dof "" dep_coef 1.0 indep_dof "" indep_coef "" lsdyna_nsid ""
    } { set $n $v }

    set mpcSetList [aie_list_sets mpcdisp]
    set nlength [llength $mpcSetList]
    if {$mpcSet == "" && $nlength > 0} { 
       set mpcSet [expr ([lindex [lsort $mpcSetList] [expr ($nlength - 1)]] + 1)]
    } else {
       set mpcSet 1
    }
    set AllowActiveReload 1

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

    if {$solve_format == "2"} {
       ::AIEnv::GUIManagement::ToggleGrey lsdynacreateMPCNodeSetId 0
    } else {
       ::AIEnv::GUIManagement::ToggleGrey lsdynacreateMPCNodeSetId 1
    }
}

proc onCreateMultipledisplacementDEZUnLoad {} {
    if {[info exists objList] && [ic_uns_subset_exists $objList]} {
        ic_uns_subset_delete $objList
        set objList ""
    }
    if {[info exists objList1] && [ic_uns_subset_exists $objList1]} {
        ic_uns_subset_delete $objList1
        set objList1 ""
    }
}

proc oncreateMPCDependentPickPick {} {
    
    if {[info exists objList] && [ic_uns_subset_exists $objList]} {
        ic_uns_subset_delete $objList
    }
}

proc oncreateMPCDependentPickFinish {} {
    
    if {[info exists objList] && [ic_uns_subset_exists $objList]} {
        ic_uns_subset_visible $objList 1
    }
}

proc oncreateMPCIndependentPickPick {} {
    
    if {[info exists objList1] && [ic_uns_subset_exists $objList1]} {
        ic_uns_subset_delete $objList1
    }
}

proc oncreateMPCIndependentPickFinish {} {
    
    if {[info exists objList1] && [ic_uns_subset_exists $objList1]} {
        ic_uns_subset_visible $objList1 1
    }
}

proc onCreateMultipledisplacementDEZApply {} {
    global bc_name_list

    set def_name 0

    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 bc_name_list] && [lsearch $bc_name_list $name] >= 0} {
          user_error "The Name entered is already used.\nPlease enter another Name."
          set objList {}
          set objList1 {}
          return
       }
    } else {
       set def_name 1
    }

    set entity "point"
    set dep 0

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

    if {![info exists objList] || ![info exists objList1]} { 
       if ![info exists objList] {set objList {}}
       if ![info exists objList1] {set objList1 {}}
    } else {
       if { $objList == "" || $objList1 == ""} {
          mess "Some entities are not selected!\nPlease select all necessary entities."
          return
       } else {
          if {$solve_format == "2"} {
             if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
                user_error "Please enter a valid Node Set ID"
                return
             }
          }
          set objList [concat $objList $objList1]
          foreach {n v} [applyMpcdisp $entity $objList $name $mpcSet $LCS $dep_dof $dep_coef $indep_dof $indep_coef $dep $lsdyna_nsid] { 
             set $n $v 
          }
          ic_uns_subset_delete $objList
          ic_uns_subset_delete $objList1
          set objList {}
          set objList1 {}
          if {$def_name} {set name ""}
       }
    }

}

proc onCreateExistingMpcdispDEZLoad {} {
    global current_mpcdisp_to_load lcs_data
    global  solver_setup solver_setup_global
    
    select_reset_parts_and_subsets
    
    set LCSList [lsort [array names lcs_data]]
	
    foreach {n v} {
       name "" mpcSet "" LCS Global dep_dof "" dep_coef 1.0 indep_dof "" indep_coef "" lsdyna_nsid ""
    } {
       if [info exists current_mpcdisp_to_load($n)] {
          set $n $current_mpcdisp_to_load($n)
       } else {
          set $n $v
       }
    }

    createExistingMpcLCS setvalue @[lsearch $LCSList $LCS]

    set mpcSetList [aie_list_sets mpcdisp]

    set AllowActiveReload 1

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

    if {$solve_format == "2"} {
       ::AIEnv::GUIManagement::ToggleGrey lsdynacreateMPCNodeSetId 0
    } else {
       ::AIEnv::GUIManagement::ToggleGrey lsdynacreateMPCNodeSetId 1
    }
}

proc onCreateExistingMpcdispDEZApply {} {
  global current_mpcdisp_to_load
  global  solver_setup solver_setup_global

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

  if {![info exists objList] || ![info exists objList1]} { 
     if ![info exists objList] {set objList {}}
     if ![info exists objList1] {set objList1 {}}
  }

  if {$solve_format == "2"} {
     if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
        user_error "Please enter a valid Node Set ID"
        set objList {}
        return
     }
  }

  set dep 0

  set objList [concat $objList $objList1]
  foreach {n v} [applyMpcdisp existing $objList $name $mpcSet $LCS $dep_dof $dep_coef $indep_dof $indep_coef $dep $lsdyna_nsid] { 
    set $n $v 
  }
  set objList {}
  set objList1 {}

}

proc aie_parse_bcs_mpcdisp {bcs} {
    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    MPC {
		lappend ret mpcSet [lindex $bc 2]                   
                   if {[llength $bc] > 8} {
                      lappend ret LCS [lindex $bc 8]
                   } else {
                      lappend ret LCS 0
                   }
		lappend ret dep_dof [lindex $bc 3]
		lappend ret dep_coef [lindex $bc 4]
		lappend ret indep_dof [lindex $bc 5]
		lappend ret indep_coef [lindex $bc 6]
                   if {[llength $bc] > 7} {
                      lappend ret dep [lindex $bc 7]
                   } else {
                      lappend ret dep 0
                   }
		set got_it 1
	    }
	    LSDYNA_NSID {
		lappend ret lsdyna_nsid [lindex $bc 2]
		set got_it 1
	    }
	}
    }
    if $got_it {
	return $ret
    } else {
	return ""
    }
}

proc applyMpcdisp {type objects name mpcset LCS depdof depcoef indepdof indepcoef dep nsid} {
    
    set lcsnum [aie_get_lcs_num $LCS]
    set mpc [list 1 MPC $mpcset $depdof $depcoef $indepdof $indepcoef $dep $lcsnum]
    if {$nsid != "" && $nsid > 0} {
       set bcs [list $mpc [list 1 LSDYNA_NSID $nsid]]
    } else {
       set bcs [list $mpc]
    }
    aie_apply_attribute mpcdisp $bcs MPCDISP $name $type $objects [select_get_parts_and_subsets]

}

proc aie_apply_icons_mpcdisp {fam obtype vis lvis {nfam -1}} {

    ic_boco_apply_generic_icon_to_group $fam mpcdisp [list $nfam] $vis

 #mess "aie_apply_icons_mpcdisp $fam $obtype $vis $lvis $nfam\n" green

    return 1
}

proc aie_get_mpcdisp_set {fam} {
    return [aie_get_bc_argument $fam mpcdisp mpcSet]
}




























