#!/bin/sh
#PostGeom -*- TCL -*- Created: ramsan&miguel Jan-1997, Modified: ramsan Jan-1997


#----------------------PostGeom ALONE---------------------------
# the next line restarts using wish \
	exec wish4.2 "$0" "$@"

# set PGRunningAlone 0
# if { ![info exists GIDDEFAULT] } {
#     set PGRunningAlone 1
#     set tcl_interactive 0
#     set tk_strictMotif 1

#     if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
#         set GIDDEFAULTTCL $GIDDEFAULT
#         catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
#         set auto_path "$GIDDEFAULTTCL"
#         source $GIDDEFAULTTCL/init.tcl
#         source $GIDDEFAULTTCL/tk.tcl
#     }
#         wm withdraw .
# }

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------


namespace eval PostGeom {
    variable _w_id
    # pagina_malla_principal
    variable _p_mp
    # pagina__malla_copia 
    variable _p_mc

    variable _MallaPrincipal
    variable _MallaCopia
}

proc FillPGInfo {} {
    global GidPriv

    if { ![info exists GidPriv(PostGeomWindow)] || \
	    ![winfo exists $GidPriv(PostGeomWindow)] } { return }

    PostGeom::ActualizaVariables
}

# Rutinas pagina PRINCIPAL

proc PostGeom::CambioAnalisisPrincipal { nuevo_analisis} {
    variable _MallaPrincipal
    regsub -all {_} $nuevo_analisis { } n_ana
    set _MallaPrincipal(analisis) $n_ana

    # todos los pasos del analisis
    #$_MallaPrincipal(paso,wid) listbox del 0 end
    if { $_MallaPrincipal(analisis) != "" } {
	regsub -all { } $_MallaPrincipal(analisis) {_} analisis
	#set all_st [.central.s info post get all_steps $analisis]
	#foreach j $all_st {
	#    $_MallaPrincipal(paso,wid) add $j
	#}
	set lst_pasos [.central.s info post get all_steps $analisis]
	$_MallaPrincipal(paso,wid) configure -values $lst_pasos
	set _MallaPrincipal(paso) [.central.s info post get cur_step $nuevo_analisis]
    } else {
	set lst_pasos ""
	$_MallaPrincipal(paso,wid) configure -values $lst_pasos
	set _MallaPrincipal(paso) ""
    }
}

proc PostGeom::CambioPasoPrincipal { } {
    variable _MallaPrincipal

    if { "$_MallaPrincipal(paso)" == ""} {
	set _MallaPrincipal(resultado) ""
	PostGeom::CambioResultadoPrincipal
	return
    }

    set old_anal [.central.s info post get cur_analisis]
    set old_step [.central.s info post get cur_step $old_anal]
    regsub -all { } $_MallaPrincipal(analisis) {_} old_analisis
    .central.s process escape escape escape escape Results AnalysisSel $old_analisis $_MallaPrincipal(paso)

    set all [ .central.s info post get main_geom_all_deform]
    $_MallaPrincipal(resultado,wid) del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$_MallaPrincipal(resultado,wid) ins end "$var"
    }

    #if { "$estado" == "Deformed"} {
	set var [ .central.s info post get main_geom_cur_deform]
	if { "$var" == ""} { set var [ lindex $all 0] }
	regsub -all {_} $var { } var
	set _MallaPrincipal(resultado) $var
    #}
    #if { ( "$_MallaPrincipal(analisis)" != "") && \
    #             ( "$_MallaPrincipal(paso)" != "") && \
    #             ( "$_MallaPrincipal(resultado)" != "") } {
    #         regsub -all { } $_MallaPrincipal(resultado) {_} def_vec
    #         regsub -all { } $_MallaPrincipal(analisis) {_} res_anal
    #         set _MallaPrincipal(factor) [.central.s info post \
    #                 get main_geom_factor $res_anal $_MallaPrincipal(paso) $def_vec]
    #} else {
    #         set _MallaPrincipal(factor) 1
    #}

    PostGeom::CambioResultadoPrincipal

    .central.s process escape escape escape escape Results AnalysisSel $old_anal $old_step

}

proc PostGeom::CambioResultadoPrincipal { } {
    variable _MallaPrincipal

    if { "$_MallaPrincipal(resultado)" == ""} {
	set _MallaPrincipal(factor) 1
	return
    }

    if { ( "$_MallaPrincipal(analisis)" != "") && \
	    ( "$_MallaPrincipal(paso)" != "") && \
	    ( "$_MallaPrincipal(resultado)" != "") } {
	regsub -all { } $_MallaPrincipal(resultado) {_} def_vec
	regsub -all { } $_MallaPrincipal(analisis) {_} res_anal
	.central.s process escape escape escape escape \
		results options deformationtype $_MallaPrincipal(TipoDeformacion)
	set _MallaPrincipal(factor) [.central.s info post \
		get main_geom_factor $res_anal $_MallaPrincipal(paso) $def_vec]
	.central.s process escape escape escape escape \
		results options deformationtype relative
    } else {
	set _MallaPrincipal(factor) 1
    }
}

proc PostGeom::GetGiDResultsInfoPrincipal { estado} {
    variable _MallaPrincipal

    # todos los analisis
    set all [.central.s info post get all_analisis]
    $_MallaPrincipal(analisis,wid).menu del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$_MallaPrincipal(analisis,wid).menu add command -label $var -command "PostGeom::CambioAnalisisPrincipal $j"
    }

    # analisis actual
    set cur [.central.s info post get cur_analisis]
    if { $cur != "" } {
	regsub -all {_} $cur { } cur2
    } else { regsub -all {_} [ lindex $all 0] { } cur2 }

    PostGeom::CambioAnalisisPrincipal $cur2

#
#    set _MallaPrincipal(analisis) "$cur"
#
#    # todos los pasos del analisis
#    $_MallaPrincipal(paso,wid) listbox del 0 end
#    if { $_MallaPrincipal(analisis) != "" } {
#         regsub -all { } $_MallaPrincipal(analisis) {_} analisis
#         set all_st [.central.s info post get all_steps $analisis]
#         foreach j $all_st {
#             $_MallaPrincipal(paso,wid) add $j
#         }
#         set _MallaPrincipal(paso) [.central.s info post get cur_step $cur]
#    }
#
#    set all [ .central.s info post get main_geom_all_deform]
#    $_MallaPrincipal(resultado,wid) del 0 end
#    foreach j $all {
#         regsub -all {_} $j { } var
#         $_MallaPrincipal(resultado,wid) ins end "$var"
#    }
#
#    #if { "$estado" == "Deformed"} {
#         set var [ .central.s info post get main_geom_cur_deform]
#         if { "$var" == ""} { set var [ lindex $all 0] }
#         regsub -all {_} $var { } var
#         set _MallaPrincipal(resultado) $var
#    #}
#
#    if { ( "$_MallaPrincipal(analisis)" != "") && \
#             ( "$_MallaPrincipal(paso)" != "") && \
#             ( "$_MallaPrincipal(resultado)" != "") } {
#         regsub -all { } $_MallaPrincipal(resultado) {_} def_vec
#         regsub -all { } $_MallaPrincipal(analisis) {_} res_anal
#         set _MallaPrincipal(factor) [.central.s info post \
#                 get main_geom_factor $res_anal $_MallaPrincipal(paso) $def_vec]
#    } else {
#         set _MallaPrincipal(factor) 1
#    }
#
#    #WarnWin "Current: $_MallaPrincipal(analisis) $_MallaPrincipal(paso) $_MallaPrincipal(resultado) $_MallaPrincipal(factor)"
}


#rutina pagina COPIA

proc PostGeom::CambioAnalisisCopia { nuevo_analisis} {
    variable _MallaCopia
    regsub -all {_} $nuevo_analisis { } n_ana
    set _MallaCopia(analisis) $n_ana

    # todos los pasos del analisis
    #$_MallaCopia(paso,wid) listbox del 0 end
    if { $_MallaCopia(analisis) != "" } {
	regsub -all { } $_MallaCopia(analisis) {_} analisis
	#set all_st [.central.s info post get all_steps $analisis]
	#foreach j $all_st {
	#    $_MallaCopia(paso,wid) add $j
	#}
	set lst_pasos [.central.s info post get all_steps $analisis]
	$_MallaCopia(paso,wid) configure -values $lst_pasos
	set _MallaCopia(paso) [.central.s info post get cur_step $nuevo_analisis]
    } else {
	set lst_pasos ""
	$_MallaCopia(paso,wid) configure -values $lst_pasos
	set _MallaCopia(paso) ""
    }
}

proc PostGeom::CambioPasoCopia { } {
    variable _MallaCopia

    if { "$_MallaCopia(paso)" == ""} {
	set _MallaCopia(resultado) ""
	PostGeom::CambioResultadoCopia
	return
    }

    regsub -all { } $_MallaCopia(analisis) {_} str_analisis
    set all [ .central.s info post get show_geom_all_deform $str_analisis $_MallaCopia(paso)]
    $_MallaCopia(resultado,wid) del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$_MallaCopia(resultado,wid) ins end "$var"
    }

    #if { "$estado" == "Deformed"} {
	set var [ .central.s info post get show_geom_cur_deform]
	if { "$var" == ""} { set var [ lindex $all 0] }
	regsub -all {_} $var { } var
	set _MallaCopia(resultado) $var
    #}

    PostGeom::CambioResultadoCopia
}

proc PostGeom::CambioResultadoCopia { } {
    variable _MallaCopia

    if { "$_MallaCopia(resultado)" == ""} {
	set _MallaCopia(factor) 1
	return
    }

    if { ( "$_MallaCopia(analisis)" != "") && \
	    ( "$_MallaCopia(paso)" != "") && \
	    ( "$_MallaCopia(resultado)" != "") } {
	regsub -all { } $_MallaCopia(resultado) {_} def_vec
	regsub -all { } $_MallaCopia(analisis) {_} res_anal
	.central.s process escape escape escape escape \
		results options deformationtype $_MallaCopia(TipoDeformacion)
	set _MallaCopia(factor) [.central.s info post \
		get main_geom_factor $res_anal $_MallaCopia(paso) $def_vec]
	.central.s process escape escape escape escape \
		results options deformationtype relative
    } else {
	set _MallaCopia(factor) 1
    }
}

proc PostGeom::GetGiDResultsInfoCopia { estado} {
    variable _MallaCopia

    # todos los analisis
    set all [.central.s info post get all_analisis]
    $_MallaCopia(analisis,wid).menu del 0 end
    foreach j $all {
	regsub -all {_} $j { } var
	$_MallaCopia(analisis,wid).menu add command -label $var -command "PostGeom::CambioAnalisisCopia $j"
    }

    # analisis actual
    set cur [.central.s info post get cur_analisis]
    if { $cur != "" } {
	regsub -all {_} $cur { } cur2
    } else { regsub -all {_} [ lindex $all 0] { } cur2 }

    PostGeom::CambioAnalisisCopia $cur2

    #set data(analisis) "$cur"
    #
    ## todos los pasos del analisis
    #$data(paso,wid) listbox del 0 end
    #if { $data(analisis) != "" } {
    #         regsub -all { } $data(analisis) {_} analisis
    #         set all_st [.central.s info post get all_steps $analisis]
    #         foreach j $all_st {
    #             $data(paso,wid) add $j
    #         }
    #         set data(paso) [.central.s info post get cur_step $cur]
    #}
    #
    #regsub -all { } $data(analisis) {_} ana
    #set all [ .central.s info post get show_geom_all_deform $ana $data(paso)]
    #$data(resultado,wid) del 0 end
    #foreach j $all {
    #         regsub -all {_} $j { } var
    #         $data(resultado,wid) ins end "$var"
    #}
    #
    ##if { "$estado" == "Deformed"} {
    #         set var [ .central.s info post get show_geom_cur_deform]
    #         if { "$var" == ""} { set var [ lindex $all 0] }
    #         regsub -all {_} $var { } var
    #         set data(resultado) $var
    ##}
    #
    #if { ( "$data(analisis)" != "") && \
    #             ( "$data(paso)" != "") && \
    #             ( "$data(resultado)" != "") } {
    #         regsub -all { } $data(resultado) {_} def_vec
    #         regsub -all { } $data(analisis) {_} res_anal
    #         set data(factor) [.central.s info post \
    #                 get show_geom_factor $res_anal $data(paso) $def_vec]
    #} else {
    #         set data(factor) 1
    #}
    #
    ##WarnWin "Current: $data(analisis) $data(paso) $data(resultado) $data(factor)"
}

proc PostGeom::ActualizaVariables { } {
    variable _w_id

    variable _MallaPrincipal
    variable _MallaCopia

    # averiguar estado malla principal
    
    set mainst [.central.s info post get main_geom_state]
    set _MallaPrincipal(estado) $mainst
    set maindeftyp [ .central.s info post get main_geom_deformation_type]
    set _MallaPrincipal(TipoDeformacion) $maindeftyp


    trace var _MallaPrincipal(paso) w "PostGeom::CambioPasoPrincipal;#"
    trace var _MallaPrincipal(resultado) w "PostGeom::CambioResultadoPrincipal;#"
    GetGiDResultsInfoPrincipal $mainst

    if { "$mainst" == "Original"} {
	$_MallaPrincipal(wid_pagina).frd.rd1 invoke
	#DisableResultFactorWidget $_MallaPrincipal(wid_pagina).rs
    } else {
	$_MallaPrincipal(wid_pagina).frd.rd2 invoke
	#DisableResultFactorWidget $_MallaPrincipal(wid_pagina).rs
    }
    #WarnWin "$_MallaPrincipal(analisis) $_MallaPrincipal(paso) $_MallaPrincipal(resultado)"

    if { "$maindeftyp" == "relative"} {
	$_MallaPrincipal(wid_pagina).rs.fs.cb1 deselect
    } else { # == "absolute"
	$_MallaPrincipal(wid_pagina).rs.fs.cb1 select
    }

    foreach i "estado analisis paso resultado factor TipoDeformacion" {
	set _MallaPrincipal(antes,$i) $_MallaPrincipal($i)
    }

    # mejor hacerlo antes que buscar los valores de la malla de copia
    # pues igual me deshabilita el radio button y no puedo activarlos
    # el color de la copia
    set res [ .central.s info post get show_geom_color]

    set _MallaCopia(TipoColor) [ lindex $res 0]
    $_MallaCopia(Color,wid) conf -bg [ lindex $res 1]
    set _MallaCopia(Color) [ lindex $res 1]
    if { "$_MallaCopia(TipoColor)" == "UserColor"} {
	#$_MallaCopia(wid_pagina).fcol.rd2 invoke
	$_MallaCopia(wid_pagina).fcol.cb1 select
	$_MallaCopia(Color,wid) conf -state normal
    } else {
	#$_MallaCopia(wid_pagina).fcol.rd1 invoke
	$_MallaCopia(wid_pagina).fcol.cb1 deselect
	$_MallaCopia(Color,wid) conf -state disabled
    }

    foreach i "TipoColor Color" {
	set _MallaCopia(antes,$i) $_MallaCopia($i)
    }



    
    # ahora la copia
    set copiast [.central.s info post get show_geom_state]
    set _MallaCopia(estado) $copiast
    set showdeftyp [ .central.s info post get show_geom_deformation_type]
    set _MallaCopia(TipoDeformacion) $showdeftyp

    trace var _MallaCopia(paso) w "PostGeom::CambioPasoCopia;#"
    trace var _MallaCopia(resultado) w "PostGeom::CambioResultadoCopia;#"
    GetGiDResultsInfoCopia $copiast
    if { "$copiast" == "Off"} {
	$_MallaCopia(wid_pagina).frd.rd1 invoke
    } elseif { "$copiast" == "Original"} {
	$_MallaCopia(wid_pagina).frd.rd2 invoke
    } else {
	$_MallaCopia(wid_pagina).frd.rd3 invoke
    }

    if { "$showdeftyp" == "relative"} {
	$_MallaCopia(wid_pagina).rs.fs.cb1 deselect
    } else { # == "absolute"
	$_MallaCopia(wid_pagina).rs.fs.cb1 select
    }

    foreach i "estado analisis paso resultado factor TipoDeformacion" {
	set _MallaCopia(antes,$i) $_MallaCopia($i)
    }

}

proc DisableResultFactorWidget { w } {
    $w.fa.la conf -foreground grey60
    $w.fa.ma conf -state disabled
    $w.fs.ls conf -foreground grey60
    #$w.fs.cb configure -foreground grey60
    $w.fs.cb configure -state disabled
    $w.fs.cb1 configure -state disabled
    $w.fr.lr conf -foreground grey60
    $w.fr.lf.lb conf -foreground grey60
    #$w.fr.lf.lb conf -state disabled
    $w.ff.l conf -foreground grey60
    $w.ff.e conf -foreground grey60
    $w.ff.e conf -state disabled
}

proc EnableResultFactorWidget { w } {
    $w.fa.la conf -foreground black
    $w.fa.ma conf -state normal
    $w.fs.ls conf -foreground black
    #$w.fs.cb configure -foreground black
    $w.fs.cb configure -state normal
    $w.fs.cb1 configure -state normal
    $w.fr.lr conf -foreground black
    $w.fr.lf.lb conf -foreground black
    #$w.fr.lf.lb conf -state normal
    $w.ff.l conf -foreground black
    $w.ff.e conf -foreground black
    $w.ff.e conf -state normal
}

proc DisableColorCopiaWidgets { w} {
    #$w.l conf -foreground grey60
    #$w.rd1 conf -state disabled
    #$w.rd2 conf -state disabled
    #$w.cb1 conf -foreground grey60
    $w.cb1 conf -state disabled
    $w.b conf -state disabled
}

proc EnableColorCopiaWidgets { w} {
    #$w.l conf -foreground black
    #$w.rd1 conf -state normal
    #$w.rd2 conf -state normal
    #$w.cb1 conf -foreground black
    $w.cb1 conf -state normal
    $w.b conf -state normal
}

proc PostGeom::CambiaMalla { cual que w_id} {
    variable $cual

    set PostGeom::${cual}(estado) $que
    if { "$que" == "Deformed"} {
	EnableResultFactorWidget $w_id.rs
    } else {
	DisableResultFactorWidget $w_id.rs
    }

    if { "$cual" == "_MallaCopia"} {
	if { "$que" == "Off"} {
	    DisableColorCopiaWidgets $w_id.fcol
	} else {
	    EnableColorCopiaWidgets $w_id.fcol
	}
    }
}

proc PostGeom::GetListBoxSelection { w storevar elemento} {
    variable $storevar
    set id_sel [$w curselection]
    if { "$id_sel" == ""} { return}
    set seleccion [$w get $id_sel]
    if { "$seleccion" == ""} { return }
    regsub -all { } $seleccion {_} resultado
    set ${storevar}($elemento) $resultado
}

proc PostGeom::CambiaTipoColorCopia { tipo} {
    variable _MallaCopia

    set _MallaCopia(TipoColor) $tipo
    if { "$_MallaCopia(TipoColor)" == "UserColor"} {
	$_MallaCopia(Color,wid) conf -state normal
    } else {
	$_MallaCopia(Color,wid) conf -state disabled
    }    
}

proc PostGeom::CambiaTipoColorCopia2 { } {
    global PG_MallaColorCopia

    CambiaTipoColorCopia $PG_MallaColorCopia
}



proc PostGeom::CambiaColorCopia { wb} {
    variable _MallaCopia

    set nuevo [ BWChooseColorMenu $wb.mc [ list below $wb] -title [_ "Select color"] -color [ $wb cget -bg]]
    if { [ string length $nuevo]} { 
	$wb conf -bg $nuevo
	#.central.s process escape escape escape escape results geometry ChangeShowCol DefineColor $nuevo
	set _MallaCopia(Color) $nuevo
    }
}

proc PostGeom::TipoDeformacion { cual} {
    variable $cual
    global PG_tipo_defor
    
    set PostGeom::${cual}(TipoDeformacion) $PG_tipo_defor($cual)
    if { "$cual" == "_MallaPrincipal"} {
	PostGeom::CambioResultadoPrincipal
    } else {
	PostGeom::CambioResultadoCopia
    }
}


proc PostGeom::CreateResultFactorSelectionWidget { w storevar} {
    global PG_tipo_defor

    frame $w -bd 0

    frame $w.fa -bd 0
    label $w.fa.la -text [_ "Analysis:"]
    #labels can be translated
    set labels [.central.s info post get all_analisis]
    set values $labels
    set menu [tk_optionMenuRAM $w.fa.ma PostGeom::${storevar}(analisis) \
	    $labels $values]
    $w.fa.ma conf -width 20 -bd 1 -highlightthickness 1 
    $menu conf -bd 1 -activeborderwidth 1
    grid $w.fa.la $w.fa.ma -sticky w
    grid conf $w.fa.ma -sticky we
    grid rowconfigure $w.fa 0 -weight 1
    grid columnconfigure $w.fa 1 -weight 1
    set PostGeom::${storevar}(analisis,wid) $w.fa.ma

    frame $w.fs -bd 0
    label $w.fs.ls -text [_ "Step:"]
    #combobox $w.fs.cb -textvariable PostGeom::${storevar}(paso) -editable 0 -width 6
    ComboBox $w.fs.cb -textvariable PostGeom::${storevar}(paso) -editable 0 -width 6 -bd 1
    checkbutton $w.fs.cb1 -text [_ "Position vector"] -variable PG_tipo_defor($storevar) \
	    -onvalue absolute -offvalue relative \
	    -command "PostGeom::TipoDeformacion $storevar"
    grid $w.fs.ls $w.fs.cb $w.fs.cb1 -sticky w
    grid conf $w.fs.cb1 -sticky e
    grid rowconfigure $w.fs 0 -weight 1
    grid columnconfigure $w.fs 1 -weight 1
    set PostGeom::${storevar}(paso,wid) $w.fs.cb

    frame $w.fr -bd 0
    label $w.fr.lr -text [_ "Result"]:
    frame $w.fr.lf -relief sunken -bd 1
    listbox $w.fr.lf.lb -height 5 -width 0 -borderwidth 0 -relief flat \
	    -selectmode extended -yscrollcommand "$w.fr.lf.ys set" \
	    -highlightthickness 0 -xscrollcommand "$w.fr.lf.xs set"
    bind $w.fr.lf.lb <ButtonRelease-1> "PostGeom::GetListBoxSelection $w.fr.lf.lb $storevar resultado"
    scrollbar $w.fr.lf.ys -orient v -command "$w.fr.lf.lb yview" -bd 1 \
	    -elementborderwidth 1
    scrollbar $w.fr.lf.xs -orient h -command "$w.fr.lf.lb xview" -bd 1 \
	    -elementborderwidth 1
    grid $w.fr.lf.lb -row 0 -column 0 -sticky nesw
    grid $w.fr.lf.ys -row 0 -column 1 -sticky ns
    grid $w.fr.lf.xs -row 1 -column 0 -sticky ew
    grid columnconfigure $w.fr.lf 0 -weight 1
    grid rowconfigure $w.fr.lf 0 -weight 1
    #set menu [ tk_optionMenu $w.fr.mr PostGeom::${storevar}(resultado) {Displacements} {Deformation}]
    #$w.fr.mr conf -width 20 -bd 1 -highlightthickness 1 
    #$menu conf -bd 1 -activeborderwidth 1
    #grid $w.fr.lr $w.fr.mr -sticky w
    #grid conf $w.fr.mr -sticky we
    grid $w.fr.lr $w.fr.lf -sticky nw
    grid conf $w.fr.lf -sticky wens
    grid rowconfigure $w.fr 0 -weight 1
    grid columnconfigure $w.fr 1 -weight 1
    set PostGeom::${storevar}(resultado,wid) $w.fr.lf.lb

    frame $w.ff -bd 0
    label $w.ff.l -text [_ "factor:"]
    entry $w.ff.e -width 12 -textvariable PostGeom::${storevar}(factor) -bd 1 -relief sunken
    grid $w.ff.l $w.ff.e -sticky w
    set PostGeom::${storevar}(factor,wid) $w.ff.l

    grid $w.fa -sticky we -padx 2 -pady 0
    grid $w.fs -sticky nesw -padx 2 -pady 0
    grid $w.fr -sticky we -padx 2 -pady 0
    grid $w.ff -sticky w -padx 2 -pady 0
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
    grid rowconfigure $w 1 -weight 1
    grid rowconfigure $w 2 -weight 1

}

proc PostGeom::Close { } {
    variable _w_id

    destroy $_w_id
}

proc PostGeom::Apply { } {
    variable _w_id
    variable _MallaPrincipal
    variable _MallaCopia

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape escape escape 

    set redibujar 0

    # la malla PRINCIPAL
    set cambio 0
    foreach i "estado analisis paso resultado factor TipoDeformacion" {
	if { "$_MallaPrincipal(antes,$i)" != "$_MallaPrincipal($i)"} {
	    set cambio 1
	    break
	}
    }

    if { $cambio} {
	.central.s process escape escape escape escape results geometry Original
	if { "$_MallaPrincipal(estado)" == "Deformed"} {
	    regsub -all { } $_MallaPrincipal(resultado) {_} var
	    regsub -all { } $_MallaPrincipal(analisis) {_} res_anal
	    if { ( $var != "") && ( $res_anal != "") && ( $_MallaPrincipal(paso) != "")} {
		.central.s process escape escape escape escape \
		        results options deformationtype $_MallaPrincipal(TipoDeformacion)
		.central.s process escape escape escape escape \
		        results AnalysisSel $res_anal $_MallaPrincipal(paso) \
		        geometry deformation $var $_MallaPrincipal(factor) escape \
		        escape escape escape 
		.central.s process escape escape escape escape \
		        results options deformationtype relative
	    }            
	}
	set redibujar 1
	
	# ahora el nuevo estado es este
	foreach i "estado analisis paso resultado factor TipoDeformacion" {
	    set _MallaPrincipal(antes,$i) $_MallaPrincipal($i)
	}
    }

    # el color de la COPIA
    set cambio 0
    foreach i "TipoColor Color" {
	if { "$_MallaCopia(antes,$i)" != "$_MallaCopia($i)"} {
	    set cambio 1
	    break
	}
    }

    if { $cambio} {
	if { "$_MallaCopia(TipoColor)" == "UserColor"} {
	    .central.s process escape escape escape escape results geometry ChangeShowCol DefineColor $_MallaCopia(Color)
	} else {
	    .central.s process escape escape escape escape results geometry ChangeShowCol MeshColor
	}
    }

    # la malla COPIA
    set cambio 0
    foreach i "estado analisis paso resultado factor TipoDeformacion" {
	if { "$_MallaCopia(antes,$i)" != "$_MallaCopia($i)"} {
	    set cambio 1
	    break
	}
    }

    if { $cambio} {
	.central.s process escape escape escape escape results geometry hidetheshow
	if { "$_MallaCopia(estado)" == "Original"} {
	    .central.s process escape escape escape escape results geometry showoriginal
	} elseif { "$_MallaCopia(estado)" == "Deformed"} {
	    regsub -all { } $_MallaCopia(resultado) {_} var
	    regsub -all { } $_MallaCopia(analisis) {_} res_anal
	    if { ( $var != "") && ( $res_anal != "") && ( $_MallaCopia(paso) != "")} {
		.central.s process escape escape escape escape \
		        results options deformationtype $_MallaCopia(TipoDeformacion)
		.central.s process escape escape escape escape \
		        results Geometry ShowDeformed \
		        $res_anal $_MallaCopia(paso) \
		        $var $_MallaCopia(factor) escape \
		        escape escape escape 
		.central.s process escape escape escape escape \
		        results options deformationtype relative
	    }            
	}
	set redibujar 1

	# ahora el nuevo estado es este
	foreach i "estado analisis paso resultado factor TipoDeformacion" {
	    set _MallaCopia(antes,$i) $_MallaCopia($i)
	}
    }


    # lo separamos por si lo de arriba da un error
    .central.s process escape escape escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape escape escape
    
    if { $redibujar} {
	.central.s render
    }
}

proc PostGeom::Create { { w .wPostGeom }} {
    variable _w_id
    variable _p_mp
    variable _p_mc
    variable _MallaPrincipal
    variable _MallaCopia
    global PG_MallaColorCopia

    set _w_id $w
    #toplevel $w
    #puts PostGeom-----------------------------
    InitWindow $w [_ "Mesh Deformation"] PostGeomWindowGeom PostGeom::Create

    set def_back [ $w cget -background]
    set realzado [ CCColorActivo $def_back]

    # marco de las cosas:
    frame $w.f -bd 0

    NoteBook $w.f.nb -internalborderwidth 0 -activebackground $realzado

    # pagina de la geometria principal
    set _p_mp [ $w.f.nb insert end MainMesh -text [_ "Main Mesh"]]

    text $_p_mp.t -bd 4 -relief flat -width 20 -height 2 -wrap word -background $realzado
    $_p_mp.t ins end [_ "Results will be drawn on this model."]
    $_p_mp.t conf -state disabled

    frame $_p_mp.frd -bd 0
    radiobutton $_p_mp.frd.rd1 -text [_ "Original"] -value Original -variable _MallaPrincipal(estado) \
	    -command "PostGeom::CambiaMalla _MallaPrincipal Original $_p_mp"
    radiobutton $_p_mp.frd.rd2 -text [_ "Deformed"] -value Deformed -variable _MallaPrincipal(estado) \
	    -command "PostGeom::CambiaMalla _MallaPrincipal Deformed $_p_mp"
    grid $_p_mp.frd.rd1 $_p_mp.frd.rd2 -sticky wns -padx 4 -pady 0

    set _MallaPrincipal(wid_pagina) $_p_mp
    PostGeom::CreateResultFactorSelectionWidget $_p_mp.rs _MallaPrincipal


    grid $_p_mp.frd -sticky wns -padx 4 -pady 0
    grid $_p_mp.rs -sticky enws -padx 4 -pady 0
    grid $_p_mp.t -sticky nwes -padx 4 -pady 8
    grid columnconfigure $_p_mp 0 -weight 1
    grid rowconfigure $_p_mp 0 -weight 1
    grid rowconfigure $_p_mp 1 -weight 1
    grid rowconfigure $_p_mp 2 -weight 1


    ##############################
    # pagina de la geometria de referencia ( copia del modelo)
    set _p_mc [ $w.f.nb insert end CopyMesh -text [_ "Reference mesh"]]

    text $_p_mc.t -bd 4 -relief flat -width 20 -height 3 -wrap word -background $realzado
    $_p_mc.t ins end [_ "This is a snapshot of the model. Results will NOT be drawn over this representation."]
    $_p_mc.t conf -state disabled

    frame $_p_mc.frd -bd 0
    radiobutton $_p_mc.frd.rd1 -text [_ "Off"] -value Off -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Off $_p_mc"
    radiobutton $_p_mc.frd.rd2 -text [_ "Original"] -value Original -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Original $_p_mc"
    radiobutton $_p_mc.frd.rd3 -text [_ "Deformed"] -value Deformed -variable _MallaCopia(estado) \
	    -command "PostGeom::CambiaMalla _MallaCopia Deformed $_p_mc"
    grid $_p_mc.frd.rd1 $_p_mc.frd.rd2 $_p_mc.frd.rd3 -sticky wns -padx 4 -pady 0

    frame $_p_mc.fcol -bd 0
    checkbutton $_p_mc.fcol.cb1 -text [_ "Use this color: "] -variable PG_MallaColorCopia \
	    -offvalue MeshColor -onvalue UserColor -command "PostGeom::CambiaTipoColorCopia2"
    #label $_p_mc.fcol.l -text "Use color: "
    #radiobutton $_p_mc.fcol.rd1 -text "of mesh " -value MeshColor -variable _MallaCopia(ColorCopia) \
    #             -command "PostGeom::CambiaTipoColorCopia MeshColor"
    #radiobutton $_p_mc.fcol.rd2 -value UserColor -variable _MallaCopia(ColorCopia)  \
    #             -command "PostGeom::CambiaTipoColorCopia UserColor"
    button $_p_mc.fcol.b -width 3 -background $realzado -command "PostGeom::CambiaColorCopia $_p_mc.fcol.b"
    set _MallaCopia(Color,wid) $_p_mc.fcol.b
    #-command "CambiaColorCopia $w.showg.f.opt.bcol"
    grid $_p_mc.fcol.cb1 $_p_mc.fcol.b -sticky wns -padx 0 -pady 0 -ipadx 0
    #grid $_p_mc.fcol.l $_p_mc.fcol.rd1 $_p_mc.fcol.rd2 $_p_mc.fcol.b -sticky wns -padx 0 -pady 0 -ipadx 0

    set _MallaCopia(wid_pagina) $_p_mc
    PostGeom::CreateResultFactorSelectionWidget $_p_mc.rs _MallaCopia


    grid $_p_mc.frd -sticky wns -padx 4 -pady 0
    grid $_p_mc.fcol -sticky wns -padx 4 -pady 0
    grid $_p_mc.rs -sticky enws -padx 4 -pady 0
    grid $_p_mc.t -sticky nwes -padx 4 -pady 8
    grid columnconfigure $_p_mc 0 -weight 1
    grid rowconfigure $_p_mc 0 -weight 1
    grid rowconfigure $_p_mc 1 -weight 1
    grid rowconfigure $_p_mc 2 -weight 1
    grid rowconfigure $_p_mc 3 -weight 1
    


    $w.f.nb compute_size
    $w.f.nb raise [ $w.f.nb page 0]
    grid $w.f.nb -sticky news -padx 4 -pady 2
    grid columnconfigure $w.f 0 -weight 1
    grid rowconfigure $w.f 0 -weight 1



    # marco de los botones
    frame $w.but -bd 0 -background $realzado

    button $w.but.apply -text [_ "Apply"] -command PostGeom::Apply -takefocus 0 -bd 1
    button $w.but.close -text [_ "Close"] -command PostGeom::Close -takefocus 0 -bd 1
    grid $w.but.apply -sticky ews -padx 4 -pady 10
    grid $w.but.close -sticky ews -padx 4 -pady 10 -row 0 -column 1



    #enpaquetamos marcos
    grid $w.f -sticky news -padx 0 -pady 0
    grid $w.but -sticky wes

    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 0 -weight 1
    bind $w <Return> "$w.but.apply conf -relief sunken; \
	    update; \
	    $w.but.apply invoke; \
	    $w.but.apply conf -relief raised"
     bind $w <Escape> "focus $w.but.close; \
             $w.but.close conf -relief sunken; \
             $w.but.close invoke"

    update idletasks
    ActualizaVariables

}


#----------------------SetCoord ALONE---------------------------
# if { $PGRunningAlone == 1 } {
#     PostGeom .wPostPG
# } 
#-----------------------------------------------------------------
