#!/bin/sh
#DebugMesher  -*- TCL -*- Created: ramsan Oct-1997, Modified: ramsan Oct-1997


#----------------------DebugMesher ALONE---------------------------
# the next line restarts using wish \
	exec wish8.0 "$0" "$@"

set DebugMesherRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set DebugMesherRunningAlone 1
	set tcl_interactive 0
	set tk_strictMotif 1
	wm withdraw .

	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
	}
    }
}

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


proc DebugMesher { NumNodes NumElems { close 0 } } {
    global DebugMPriv

    set w .wdebugmesher

    if { ![winfo exists $w] } {

	if { $close == 1 } { return -1 }

	catch { destroy .gid.wprogress ; tkwait window .gid.wprogress}

	toplevel $w

	wm title $w [_ "Debug mesher"]
	set pare .gid
#         set x [expr [winfo x $pare]+[winfo width $pare ]/2- \
#                    [winfo reqwidth $w]/2]
	set x 0
	set y [expr [winfo y $pare]+[winfo height $pare ]/2- \
		   [winfo reqheight $w]]
	wm geom $w +$x+$y

	label $w.label#1 \
		-text NumElems:

	label $w.label#2 \
		-text 0

	label $w.label#3 \
		-text NumNodes:

	label $w.label#4 \
		-text 0

	label $w.label#5 \
		-text {Stop after:}

	entry $w.entry#1 \
		-textvariable DebugMPriv(endval) \
		-width 6

	bind $w.entry#1 <Return> "$w.button#1 invoke"
	focus $w.entry#1

	bind .gid.comm.e <Tab> " \
		if  !\[winfo exists $w.entry#1] {
	    bind .gid.comm.e <Tab> {}
	    break
	} ; \
	focus $w.entry#1 ; break"


	set DebugMPriv(inival) 1
	set DebugMPriv(endval) 1
	$w.entry#1 sel from 0
	$w.entry#1 sel to end

	button $w.button#1 \
		-text Go \
		-command {
	    set DebugMPriv(inival) 1
	    if { [catch { 
		set DebugMPriv(endval) [expr int($DebugMPriv(endval))] 
	    } ] } {
		set DebugMPriv(endval) 0
	    }
	}

	button $w.button#2 \
		-text Finish \
		-command {
	    set DebugMPriv(inival) 0
	    set DebugMPriv(endval) -1
	}

	button $w.button#3 \
		-text Stop \
		-command {
	    set DebugMPriv(inival) 1
	    set DebugMPriv(endval) 1
	}

	checkbutton $w.button#4 \
		-text "Draw labels" -variable DebugMPriv(drawlabels)

	set DebugMPriv(drawlabels) 0


	frame $w.dump

	checkbutton $w.dump.button#5 \
		-text "Draw full" -variable DebugMPriv(drawfull)

	set DebugMPriv(drawfull) 0

	label $w.dump.l -text "Dump to: Front-NUM.mesh"
	button $w.dump.b -text Dump -command {
	    set DebugMPriv(inival) 1
	    set DebugMPriv(endval) -2
	}
	pack $w.dump.l $w.dump.b $w.dump.button#5 -side left


	# Geometry management

	grid $w.label#1 -in $w        -row 1 -column 1 
	grid $w.label#2 -in $w        -row 1 -column 2 
	grid $w.label#3 -in $w        -row 2 -column 1 
	grid $w.label#4 -in $w        -row 2 -column 2 
	grid $w.label#5 -in $w        -row 3 -column 1 
	grid $w.entry#1 -in $w        -row 3 -column 2 
	grid $w.button#1 -in $w        -row 3 -column 3 
	grid $w.button#2 -in $w        -row 4 -column 1 
	grid $w.button#3 -in $w        -row 4 -column 2 
	grid $w.button#4 -in $w        -row 4 -column 3 
	grid $w.dump -in $w        -row 5 -column 1 -columnspan 3

	# Resize behavior management

	grid rowconfigure $w 1 -weight 0 -minsize 30
	grid rowconfigure $w 2 -weight 0 -minsize 30
	grid rowconfigure $w 3 -weight 0 -minsize 30
	grid rowconfigure $w 4 -weight 0 -minsize 30
	grid columnconfigure $w 1 -weight 0 -minsize 30
	grid columnconfigure $w 2 -weight 0 -minsize 30
	grid columnconfigure $w 3 -weight 0 -minsize 30

	label $w.l01 -text VeryHardTolerance
	entry $w.e01 -relief sunken -textvariable DebugMPriv(VeryHardTolerance)

	label $w.l1 -text HardTolerance
	entry $w.e1 -relief sunken -textvariable DebugMPriv(HardTolerance)

	label $w.l2 -text SoftTolerance
	entry $w.e2 -relief sunken -textvariable DebugMPriv(SoftTolerance)

	label $w.l3 -text NumUsedClosePoints
	entry $w.e3 -relief sunken -textvariable DebugMPriv(NumUsedClosePoints)

	label $w.l4 -text ClosePointFactor2
	entry $w.e4 -relief sunken -textvariable DebugMPriv(ClosePointFactor2)

	label $w.l5 -text VeryHardDebugging
	entry $w.e5 -relief sunken -textvariable DebugMPriv(VeryHardDebugging)

	checkbutton $w.donotstop -text "Not stop until end" \
		-var DebugMPriv(nostop)

	checkbutton $w.l6 -text "Add animation frame" \
		-var DebugMPriv(AddFrame)
	button $w.br -text "debuggidfile" -command {
	    set file [file join [lindex [.central.s info Project ] 9] debuggidfile]
	    exec gnuclient $file &
	}
	grid $w.l01  -in $w        -row 6 -column 1 
	grid $w.e01  -in $w        -row 6 -column 2 -columnspan 2

	grid $w.l1  -in $w        -row 7 -column 1 
	grid $w.e1  -in $w        -row 7 -column 2 -columnspan 2

	grid $w.l2  -in $w        -row 8 -column 1 
	grid $w.e2  -in $w        -row 8 -column 2 -columnspan 2


	grid $w.l3  -in $w        -row 9 -column 1 
	grid $w.e3  -in $w        -row 9 -column 2 -columnspan 2

	grid $w.l4  -in $w        -row 10 -column 1 
	grid $w.e4  -in $w        -row 10 -column 2 -columnspan 2

	grid $w.l5  -in $w        -row 11 -column 1 
	grid $w.e5  -in $w        -row 11 -column 2 -columnspan 2

	grid $w.donotstop -in $w        -row 12 -column 1 
	grid $w.l6 -in $w        -row 13 -column 1  -columnspan 2
	grid $w.br -in $w -row 14 -column 1 -columnspan 2
    }

    $w.label#2 conf -text $NumElems
    $w.label#4 conf -text $NumNodes


    update

    if { $DebugMPriv(AddFrame) } {
	.central.s process redraw
	.central.s process AnimationFile AddStep
    }

    if { $close == 1 || $DebugMPriv(endval) == -1 } {
	destroy $w
	return -1
    } elseif { $close == -1 } {
	set DebugMPriv(inival) 1
	set DebugMPriv(endval) 1
	$w.entry#1 sel from 0
	$w.entry#1 sel to end
	$w.entry#1 icursor end
    }
    
    if { $DebugMPriv(endval) > 0 && $DebugMPriv(inival) >= $DebugMPriv(endval) \
	&& !$DebugMPriv(nostop) } {
	vwait DebugMPriv(inival)
    } else {
	incr DebugMPriv(inival)
    }

    if { $DebugMPriv(endval) == -2 } {
	set DebugMPriv(inival) 1
	set DebugMPriv(endval) 1
	return 2
    } elseif { $DebugMPriv(endval) == -1 } {
	destroy $w
	return -1
    }

    if { $DebugMPriv(drawlabels) } {
	if { $DebugMPriv(drawfull) } { return -6 } else { return -4 }
    } else {
	if { $DebugMPriv(drawfull) } { return -5 } else { return -3 }
    }
}

proc debugmesher2_ui {root} {
    global DebugMPriv


    # this treats "." as a special case

    if {$root == "."} {
	set base ""
    } else {
	set base $root
    }
    
    frame $base.frame#1
    
    scale $base.scale#1 \
	    -label {Element number:} \
	    -orient h \
	    -variable DebugMPriv(elmnumber) \
	    -bigincrement 100
    
    focus $base.scale#1
    
    bind .gid.comm.e <Tab> " \
	if  !\[winfo exists $base.scale#1] {
	  bind .gid.comm.e <Tab> {}
	  break
	} ; \
	focus $base.scale#1 ; break"
	
    label $base.label#1 \
	    -text {Element number:}
	
	
    entry $base.entry#1 
    set DebugMPriv(elmnumberentry) $base.entry#1 
    
    bind $base.entry#1 <Return> "set DebugMPriv(elmnumber) \[$base.entry#1 get]"
    bind $base.entry#1 <Up> "set DebugMPriv(elmnumber) \
	    \[expr \[$base.entry#1 get]+1]"
    bind $base.entry#1 <Down> "set DebugMPriv(elmnumber) \
	    \[expr \[$base.entry#1 get]-1]"
    bind $base.entry#1 <Alt-Up> "set DebugMPriv(elmnumber) \
	    \[expr \[$base.entry#1 get]+10] ; break"
    bind $base.entry#1 <Alt-Down> "set DebugMPriv(elmnumber) \
	    \[expr \[$base.entry#1 get]-10] ; break"
    
    button $base.button#2 \
	    -text Update -command { .central.s process escape escape escape escape view redraw }
    #-text Update -command DebugMesherPostUpdate2
    
    checkbutton $base.checkbutton#1 \
	    -text {Update dinamically} \
	    -variable DebugMPriv(Updatedinamically)
    
    listbox $base.text#1 \
	    -height 1 \
	    -width 1 \
	    -xscrollcommand "$base.scrollbar#2 set" \
	    -yscrollcommand "$base.scrollbar#1 set" \
	    -selectmode single \
	    -exportselection 0
    
    scrollbar $base.scrollbar#1 \
	    -command "$base.text#1 yview" \
	    -orient v 
    
    
    scrollbar $base.scrollbar#2 \
	    -command "$base.text#1 xview" \
	    -orient h 
    
    label $base.label#2 \
	    -text Search:
    
    
    entry $base.entry#2 \
	    -textvariable DebugMPriv(search)
    
    button $base.button#1 \
	    -text Close -command "destroy $base"
    
    
    frame $base.frame#3
    
    listbox $base.listbox#1 \
	    -height 1 \
	    -width 1 \
	    -xscrollcommand "$base.scrollbar#4 set" \
	    -yscrollcommand "$base.scrollbar#3 set" \
	    -selectmode single
    
    scrollbar $base.scrollbar#3 \
	    -command "$base.listbox#1 yview" \
	    -orient v 
    
    
    scrollbar $base.scrollbar#4 \
	    -command "$base.listbox#1 xview" \
	    -orient h 
    
    
    # Geometry management
    
    grid $base.frame#1 -in $root        -row 4 -column 1  \
	    -columnspan 2 -sticky nesw
    grid $base.scale#1 -in $root        -row 1 -column 1  \
	    -columnspan 2 \
	    -sticky ew
    grid $base.label#1 -in $root        -row 2 -column 1  \
	    -sticky e
    grid $base.entry#1 -in $root        -row 2 -column 2  \
	    -sticky nesw
    grid $base.button#2 -in $root        -row 3 -column 1 
    grid $base.checkbutton#1 -in $root        -row 3 -column 2  \
	    -sticky e
    grid $base.text#1 -in $base.frame#1        -row 1 -column 1  \
	    -sticky nesw
    grid $base.scrollbar#1 -in $base.frame#1        -row 1 -column 2  \
	    -sticky ns
    grid $base.scrollbar#2 -in $base.frame#1        -row 2 -column 1  \
	    -sticky ew
    grid $base.label#2 -in $root        -row 5 -column 1  \
	    -sticky e
    grid $base.entry#2 -in $root        -row 5 -column 2  \
	    -sticky nesw
    grid $base.button#1 -in $root        -row 7 -column 1  \
	    -columnspan 2
    

    grid $base.frame#3 -in $root        -row 6 -column 1  \
	    -columnspan 2 -sticky nesw
    grid $base.listbox#1 -in $base.frame#3        -row 1 -column 1  \
	    -sticky nesw
    grid $base.scrollbar#3 -in $base.frame#3        -row 1 -column 2  \
	    -sticky ns
    grid $base.scrollbar#4 -in $base.frame#3        -row 2 -column 1  \
	    -sticky ew
    
    
    # Resize behavior management
    
    grid rowconfigure $root 1 -weight 0 -minsize 30
    grid rowconfigure $root 2 -weight 0 -minsize 30
    grid rowconfigure $root 3 -weight 0 -minsize 30
    grid rowconfigure $root 6 -weight 1 -minsize 122
    grid rowconfigure $root 5 -weight 0 -minsize 30
    grid rowconfigure $root 4 -weight 1 -minsize 122
    grid columnconfigure $root 1 -weight 0 -minsize 30
    grid columnconfigure $root 2 -weight 1 -minsize 2
    
    grid rowconfigure $base.frame#1 1 -weight 1 
    grid rowconfigure $base.frame#1 2 -weight 0 
    grid columnconfigure $base.frame#1 1 -weight 1 
    grid columnconfigure $base.frame#1 2 -weight 0 
    
    grid rowconfigure $base.frame#3 1 -weight 1 
    grid rowconfigure $base.frame#3 2 -weight 0 
    grid columnconfigure $base.frame#3 1 -weight 1 
    grid columnconfigure $base.frame#3 2 -weight 0 
}


proc DebugMesherPostUpdateT { name1 name2 op } {
    global DebugMPriv
    if { ! $DebugMPriv(Updatedinamically) } return 
	
    #DebugMesherPostUpdate2 
    .central.s process escape escape view redraw escape

    $DebugMPriv(elmnumberentry) del 0 end
    $DebugMPriv(elmnumberentry) ins end $DebugMPriv(elmnumber)

}

proc DebugMesherPostUpdate {} {
    global DebugMPriv
   set DebugMPriv(lastelmnumber) $DebugMPriv(elmnumber)
   set fastselection [.central.s info variables FastSelection]
   .central.s process escape escape utilities variables FastSelection 1
   .central.s process layer entities ElementsHide element \
	1: escape material ElementsShow trick:$DebugMPriv(elmnumber) escape
   .central.s process layer material ElementsShow trick:$DebugMPriv(elmnumber)
   .central.s process layer entities ElementsShow LowerEntities \
	   Elements layer:ElementsShow escape
   .central.s process escape escape utilities variables FastSelection \
       $fastselection escape
}

proc DebugMesherPostUpdate2 {} {
    global DebugMPriv 
    ::GidUtils::DisableGraphics
    set fastselection [.central.s info variables FastSelection]
    .central.s process escape escape utilities variables FastSelection 1
    #.central.s process escape escape view layers SendToBack LowerEntities Elements Layer:Layer0
    #no uso back y front porque no puedo enviar al front una seleccion de entidades
    .central.s process escape escape view layers entities ElementsHide Elements Layer:ElementsShow escape
    #si no pongo un maximo colapso el buffer de process
    #set maxbuf 500
    set elemsfront ""
    for { set i 1 } { $i <= $DebugMPriv(NumElems) } { incr i } {
	if { ![info exists DebugMPriv($ElemStart,$i)] } {
	    if { $i > $DebugMPriv(elmnumber) } break
	} else {
	    if { $DebugMPriv(ElemStart,$i) > $DebugMPriv(elmnumber) } continue
	    if { $DebugMPriv(ElemEnd,$i) < $DebugMPriv(elmnumber) } continue
	}

	#.central.s process escape escape view layers entities ElementsShow Elements $i escape
	lappend elemsfront $i
	#if { [llength $elemsfront] > $maxbuf } {
	#    .central.s process escape escape view layers entities ElementsShow Elements $elemsfront escape
	#    set elemsfront ""
	#}
    }
    #if { [llength $elemsfront] } {
	.central.s process escape escape view layers entities ElementsShow Elements $elemsfront escape
    #}
    .central.s process escape escape utilities variables FastSelection $fastselection escape

    ::GidUtils::EnableGraphics
    .central.s process escape escape view redraw escape
}


proc DebugMesherPostUpdate3 {} {
    global DebugMPriv 
    #WarnWinText "DebugMesherPostUpdate3"
    #return
    
    if { ![info exists DebugMPriv(elmnumberprev)] || $DebugMPriv(elmnumber) != $DebugMPriv(elmnumberprev) } {
	if { [info exists DebugMPriv(drawlist)] && $DebugMPriv(drawlist) != "" } {
	    drawopengl draw -deletelists "$DebugMPriv(drawlist) 1"
	    set DebugMPriv(drawlist) ""
	}
	set DebugMPriv(drawlist) [drawopengl draw -genlists 1]
	drawopengl draw -newlist $DebugMPriv(drawlist) compile

	set elemstoremovenext ""
	for { set i 1 } { $i <= $DebugMPriv(NumElems) } { incr i } {
	    if { ![info exists DebugMPriv(ElemStart,$i)] } {
		if { $i > $DebugMPriv(elmnumber) } break
	    } else {
		if { $DebugMPriv(ElemStart,$i) > $DebugMPriv(elmnumber) } continue
		if { $DebugMPriv(ElemEnd,$i) < $DebugMPriv(elmnumber) } continue
	    }
	    if { [info exists DebugMPriv(ElemEnd,$i)] && $DebugMPriv(elmnumber) == [expr $DebugMPriv(ElemEnd,$i)-1] }  {
		lappend elemstoremovenext $i
		continue
	    }
	    if { [string equal -nocase [lindex [.central.s info project] 6] "normal"] } {
		DrawElem $i
	    } elseif { [string equal -nocase [lindex [.central.s info project] 6] "postprocess"] } {
		DrawRenderElem $i
		DrawElem $i
	    } elseif { [string equal -nocase [lindex [.central.s info project] 6] "polygons"] } {
		DrawRenderElem $i
		DrawElem $i
	    } else {
		DrawRenderElem $i
		#DrawElem $i
	    }
	}
	#to draw specially the front elements to disappear in the next step
	foreach i $elemstoremovenext {
	    DrawElem $i {1 0 0}
	    DrawRenderElem $i {0.7 0.7 1.0}
	}
	
	drawopengl draw -endlist
	set DebugMPriv(elmnumberprev) $DebugMPriv(elmnumber)
    }   
    drawopengl draw  -call $DebugMPriv(drawlist)
}

proc DrawElem { num { color "0 0 0" } } {
    global DebugMPriv

    drawopengl draw -color $color -begin lineloop
    foreach inode $DebugMPriv(FrontElem,$num) {
	drawopengl draw -vertex $DebugMPriv(Node,$inode)
    }
    drawopengl draw -end
}

proc DrawRenderElem { num { color "0.8 1.0 1.0" } } {
    global DebugMPriv
    drawopengl draw -polygonmode frontandback fill -color $color  -begin triangles
    foreach inode $DebugMPriv(FrontElem,$num) {
	drawopengl draw -vertex $DebugMPriv(Node,$inode)
    }
    drawopengl draw -end
}

proc DebugMesherPostSearch { w { forward 1 } } {
    global DebugMPriv

    if { $forward == 1 } {
	set forflag -forwards
    } else { set forflag -backwards }


    set ret [$w.text#1 search $forflag -nocase -- $DebugMPriv(search) insert]
    if { $ret == "" } {
	bell
    } else {
	$w.text#1 mark set insert $ret
	$w.text#1 see insert
    }        
}

proc DebugL2FillPost { dir } {
    global DebugMPriv

    $DebugMPriv(w).listbox#1 del 0 end

    set files [lsort -dictionary [glob -nocomplain \
	    [file join $dir "Front-*.mesh"]]]

    if { $files != "" } {
	foreach i $files {
	    set fileid [open $i r]
	    set NumElems [lindex [gets $fileid] 2]
	    close $fileid
	    $DebugMPriv(w).listbox#1 insert end "$NumElems [file tail $i]"
	}
    }
}

proc DebugDirSelectPost {} {
    global DebugMPriv
    for { set i 0 } { $i < [$DebugMPriv(w).text#1 size] } { incr i } {
	if { [$DebugMPriv(w).text#1 sel includes $i] } {
	    DebugL2FillPost [$DebugMPriv(w).text#1 get $i]
	    set DebugMPriv(TemporalDir) [$DebugMPriv(w).text#1 get $i]
	}
    }
}

proc DebugMeshReadPost {} {
    global DebugMPriv

    set NumElems [lindex [$DebugMPriv(w).listbox#1 get active] 0]
    set DebugMPriv(NumElems) $NumElems
    set DebugMPriv(NumSteps) $NumElems
    $DebugMPriv(w).scale#1 conf -to $DebugMPriv(NumSteps)

    set filename [file join $DebugMPriv(TemporalDir) \
		[lindex [$DebugMPriv(w).listbox#1 get active] 1]]
#    if { [lindex [.central.s info Mesh] 0] == 0 } {
#        .central.s process escape escape escape escape files meshread \"$filename\"        
#    } else {
#        .central.s process escape escape escape escape files meshread Erase \"$filename\"
#    }
    DebugMeshReadMeshFile $filename

    set DebugMPriv(elmnumber) 1
}

proc DebugMeshReadMeshFile { filename } {
    global DebugMPriv

    if [catch {set archivo [open $filename r]} f] {
	WarnWinText "Error: Cannot open '$filename': $f"
	return
    }
    #WarnWinText "Reading '$filename'..."
    ::GidUtils::SetWarnLine [_ "Reading '%s" $filename']...
    set nend 0
    set NumNodes 0
    set NumElems 0
    set NumLine 0
    set ToRead "BeginNodes" 
    while { ![ eof $archivo ] } {
	gets $archivo aa
	incr NumLine
	switch $ToRead {
	    "Mesh" {
		# MESH    dimension 3 ElemType Triangle Nnode 3
	    }
	    "BeginNodes" {
		if { [string equal -nocase $aa "Coordinates"] } { set ToRead "Nodes" }
	    }
	    "Nodes" {
		if { [string equal -nocase $aa "End Coordinates"] } {
		    set ToRead "BeginElems" 
		} else {
		    set nitem [scan $aa  " %i %lf %lf %lf" num x y z]
		    if { $nitem < 3 } {
		        WarnWinText "Error reading node in line $NumLine ='$aa'"
		        break
		    } elseif { $nitem == 3 } {
		        set z 0
		    }
		    set DebugMPriv(Node,$num) "$x $y $z"
		    incr NumNodes
		}
	    }
	    "BeginElems" {
		if { [string equal -nocase $aa "Elements"] } { set ToRead "Elements" }
	    }
	    "Elements" {
		if { [string equal -nocase $aa "End Elements"] } {
		    set ToRead "EOF" 
		    break
		} else {
		    set nitem [scan $aa  " %i %i %i %i %i # ini=%i end=%i " num n1 n2 n3 nmat nini nend]
		    if { $nitem != 7 } {
		        WarnWinText "Error reading element in line $NumLine ='$aa'"
		        break
		    }
		    set DebugMPriv(FrontElem,$num) "$n1 $n2 $n3"
		    set DebugMPriv(ElemStart,$num) $nini
		    set DebugMPriv(ElemEnd,$num) $nend
		    incr NumElems
		}
	    }
	}

    }    
    close $archivo

    set DebugMPriv(NumNodes) $NumNodes
    set DebugMPriv(NumElems) $NumElems
    set DebugMPriv(NumSteps) $nend
    $DebugMPriv(w).scale#1 conf -to $DebugMPriv(NumSteps)

    #WarnWinText "Readed $DebugMPriv(NumNodes) nodes, $DebugMPriv(NumElems) elements, num steps=$DebugMPriv(NumSteps)"
    ::GidUtils::SetWarnLine [_ "Debug Mesh: Readed %s nodes, %s elements" $DebugMPriv(NumNodes) $DebugMPriv(NumElems)]

    catch {
	set DebugMPriv(elmnumber) 1
    }

    #only is released if DebugMPriv(drawopengl) exists
    ReleaseCallbackRedraw
    set DebugMPriv(drawopengl) [drawopengl register DebugMesherPostUpdate3]
}

proc DebugSendMesherPost {} {
    global DebugMPriv
    
    

    if { [.central.s info Mesh] == 0 } {
	.central.s process escape escape escape escape \
		*****MESHVOL [file join $DebugMPriv(TemporalDir) \
		[lindex [$DebugMPriv(w).listbox#1 get active] 1]]
    } else {
	.central.s process escape escape escape escape \
		*****MESHVOL yes [file join $DebugMPriv(TemporalDir) \
		[lindex [$DebugMPriv(w).listbox#1 get active] 1]]
    }
}

proc DebugSendDebuggidfilePost {} {
    global DebugMPriv

    set pid [exec emacsclient [file join $DebugMPriv(TemporalDir) debuggidfile] > /dev/null &]
    exec kill $pid
}


proc DebugMesherPost { { w .wdevm } } {
    global DebugMPriv tcl_platform env

    set NumElems [.central.s info Mesh MaxNumElements]
    set DebugMPriv(NumSteps) $NumElems 
    set DebugMPriv(NumElems) $NumElems
    catch { destroy $w }

    toplevel $w
    wm title $w [_ "Debug mesher post"]
    set pare .gid
    #         set x [expr [winfo x $pare]+[winfo width $pare ]/2- \
	    #                    [winfo reqwidth $w]/2]
    set x 0
    set y [expr [winfo y $pare]+[winfo height $pare ]/2- \
		   [winfo reqheight $w]]
    wm geom $w +$x+$y
    debugmesher2_ui $w

    $w.scale#1 conf -from 1
    $w.scale#1 conf -to $DebugMPriv(NumSteps)

    set DebugMPriv(w) $w
    
#    set layerslist [.central.s info layers]    
#    if { [lsearch $layerslist ElementsHide] == -1 } {
#        .central.s process layer new ElementsHide escape
#    }
#    if { [lsearch $layerslist ElementsShow] == -1 } {
#        .central.s process layer new ElementsShow escape
#    }
#    .central.s process layer Off ElementsHide ToUse ElementsHide escape escape
   
    set DebugMPriv(lastelmnumber) $NumElems
    trace variable DebugMPriv(elmnumber) w DebugMesherPostUpdateT

    if {$tcl_platform(platform) == "windows"} {
      set dirs [glob -nocomplain [file join $env(TEMP) gid*]]
    } else {
      set dirs [eval exec [concat ls -1dt [glob /tmp/gid*]]]
    } 

    foreach i $dirs {
	$w.text#1 ins end $i
    }

    set DebugMPriv(TemporalDir) [lindex [.central.s info Project] 9]
    DebugL2FillPost $DebugMPriv(TemporalDir)



    bind $w.listbox#1 <1> " focus $w.listbox#1"
    bind $w.listbox#1 <g> DebugSendMesherPost
    bind $w.listbox#1 <m> DebugMeshReadPost
    bind $w <e> DebugSendDebuggidfilePost

    menu $w.debugmenu

    $w.debugmenu add command -label "Mesh read" -command DebugMeshReadPost
    $w.debugmenu add command -label "Generate mesh" -command DebugSendMesherPost
    $w.debugmenu add command -label "Send file to emacs" -command DebugSendDebuggidfilePost

    
    bind $w.listbox#1 <3> "tk_popup $w.debugmenu %X %Y"

    bind $w.listbox#1 <Double-1> {
	catch { 
	    foreach i [split [selection get] \n] {
		if { $i == "" } { continue }
		#.central.s process escape escape escape escape *****MESHVOL [file join $DebugMPriv(TemporalDir)        [lindex $i 1]]
		#DebugSendMesherPost
		DebugMeshReadPost
	    }
	}
   }

   #.central.s process escape escape escape escape view entities elements escape
   bind $w.text#1 <Double-1> DebugDirSelectPost



   bind $w <Destroy> "ReleaseCallbackRedraw"
}

proc ReleaseCallbackRedraw { } {
    global DebugMPriv
    #WarnWinText "unregister"

    #.central.s process escape escape escape escape view entities elements escape
    if { [info exists DebugMPriv(drawopengl)] && $DebugMPriv(drawopengl) != "" } {
	drawopengl unregister $DebugMPriv(drawopengl)
	catch { unset DebugMPriv }
    }
}

#----------------------DEBUGMESHER ALONE---------------------------
if { $DebugMesherRunningAlone == 1 } {

    proc .central.s { args } { 
	switch [lindex $args 1] {
	    "Project"  {
		return "0 0 0 0 MESHUSE" 
	    }
	    "Mesh" {
		return 0
	    }
	}
    }

    DebugMesherPost .wdevm 
} 
#-----------------------------------------------------------------
