proc meshingoptionsdialog { } {

    set w .options_dlg
    
    if {[winfo exists .options_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {

	toplevel $w

#	global options.meshsize

	tixNoteBook $w.nb -ipadx 6 -ipady 6
	
	$w.nb add general -label "General" -underline 0
	$w.nb add meshsize -label "Mesh Size"   -underline 0
	$w.nb add chartopt -label "STL Charts" -underline 0
	$w.nb add optimizer -label "Optimizer"   -underline 0
	$w.nb add insider -label "Insider"   -underline 0
	$w.nb add debug -label "Debug"   -underline 0


	pack $w.nb -expand yes -fill both -padx 5 -pady 5 -side top	


	# General meshing options

	set f [$w.nb subwidget general]

	set finevals { 1 2 3 4 5 }
	set finelabs(1) "very coarse" 
	set finelabs(2) "coarse" 
	set finelabs(3) "moderate" 
	set finelabs(4) "fine" 
	set finelabs(5) "very fine" 

	tixOptionMenu $f.fine -label "Mesh granularity : " \
	    -options {
		label.width  19
		label.anchor e
		menubutton.width 15
	    } 


	foreach finev $finevals {
	    $f.fine add command $finev -label $finelabs($finev)
	}
	$f.fine config -variable meshoptions.fineness
	$f.fine config -command { setgranularity }
	global meshoptions.fineness
#	setgranularity ${meshoptions.fineness}
	pack $f.fine


	


	set mgsteps { ag me ms os mv ov }
	set mgsteplabel(ag) "Analyze Geometry"
	set mgsteplabel(me) "Mesh Edges"
	set mgsteplabel(ms) "Mesh Surface"
	set mgsteplabel(os) "Optimize Surface"
	set mgsteplabel(mv) "Mesh Volume"
	set mgsteplabel(ov) "Optimize Volume"

	
	tixOptionMenu $f.first -label "First Step : " \
	    -options {
		label.width  19
		label.anchor e
		menubutton.width 15
	    } 

	tixOptionMenu $f.last -label "Last Step : " \
	    -options {
		label.width  19
		label.anchor e
		menubutton.width 15
	    } 

	foreach step $mgsteps {
	    $f.first add command $step -label $mgsteplabel($step)
	    $f.last add command $step -label $mgsteplabel($step)
	}

	$f.first config -variable meshoptions.firststep 
	$f.last config  -variable meshoptions.laststep 

	pack $f.first $f.last
	




	set msg(1) "Least"
	set msg(2) "Little"
	set msg(3) "Moderate"
	set msg(4) "Much"
	set msg(5) "Most"
	
	tixOptionMenu $f.msg -label "Print Messages : " \
	    -options {
		label.width  19
		label.anchor e
		menubutton.width 15
	    } 

	foreach step { 1 2 3 4 5 } {
	    $f.msg add command $step -label $msg($step)
	}

	$f.msg config -variable options.printmsg 
	pack $f.msg
	





	

	checkbutton $f.parthread -text "Parallel meshing thread" \
	    -variable options.parthread
	checkbutton $f.second -text "Second order elements" \
	    -variable options.secondorder
	checkbutton $f.quad -text "Quad dominated" \
	    -variable options.quad -command {
		if { ${options.quad} } {
		    set meshoptions.laststep os
		}
	    }
	checkbutton $f.invtets -text "Invert volume elements" \
	    -variable options.inverttets
	checkbutton $f.invtrigs -text "Invert surface elements" \
	    -variable options.inverttrigs

	pack $f.parthread $f.second $f.quad $f.invtets $f.invtrigs 



	tixControl $f.elementorder -label "Element order: " -integer true \
	    -variable options.elementorder -min 1 -max 20 \
	    -options {
		entry.width 2
		label.width 20
		label.anchor e
	    }	

        pack $f.elementorder


	tixControl $f.memory -label "Large Memory \[MB\]: " -integer true \
	    -variable options.memory -min 0 -max 2000 \
	    -options {
		entry.width 5
		label.width 20
		label.anchor e
	    }	

	global userlevel
	if { $userlevel >= 3} { pack $f.memory }


	# Mesh - Size options
	set f [$w.nb subwidget meshsize]

	tixControl $f.meshsize -label "max mesh-size: " -integer false \
	    -variable options.meshsize -min 1e-9 -max 1e6 \
	    -options {
		entry.width 6
		label.width 25
		label.anchor e
	    }	

	tixControl $f.grading -label "mesh-size grading: " -integer false \
	    -variable options.grading -min 0.1 -max 1 -step 0.1 \
	    -options {
		entry.width 6
		label.width 25
		label.anchor e
	    }	
	
	pack $f.meshsize $f.grading



	frame $f.msf 
	pack $f.msf

 	tixLabelEntry $f.msf.ent -label "mesh-size file: "  \
 	    -labelside top \
 	    -options {  
 		entry.textVariable options.meshsizefilename 
 		entry.width 35
 		label.width 25
 		label.anchor w
 	    }	
 	button $f.msf.btn -text "Browse" -command {
	    global options.meshsizefilename
	    set types {
		{"Meshsize file"   {.msz}	} }
	    set options.meshsizefilename [tk_getOpenFile -filetypes $types -initialfile ${options.meshsizefilename}]
	}

 	pack $f.msf.ent -side left -expand yes -fill x -anchor s -padx 4 -pady 4
 	pack $f.msf.btn -side left -anchor s -padx 4 -pady 4


	label $f.lab -text "Additional mesh size restrictions:"

	#csg-meshsize options

	frame $f.csg -relief groove -borderwidth 3
	pack $f.csg -fill x


	frame $f.csg.curv
	pack $f.csg.curv -anchor w

	scale $f.csg.curv.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable options.curvaturesafety
	label $f.csg.curv.la -text "Elements per curvature radius"
	pack $f.csg.curv.sc $f.csg.curv.la -side left 

	frame $f.csg.elen
	pack $f.csg.elen -anchor w
	scale $f.csg.elen.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable options.segmentsperedge
	label $f.csg.elen.la -text "Elements per edge"
	pack $f.csg.elen.sc $f.csg.elen.la -side left
	

	#stl-meshsize options

	frame $f.stl -relief groove -borderwidth 3
	pack $f.stl -fill x

	frame $f.stl.r2
	pack $f.stl.r2 -anchor w
	scale $f.stl.r2.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable stloptions.resthchartdistfac
	checkbutton $f.stl.r2.bu -text "STL - chart distance" \
	    -variable stloptions.resthchartdistenable
	pack $f.stl.r2.sc $f.stl.r2.bu -side left
	
	frame $f.stl.r6
	pack $f.stl.r6 -anchor w
	scale $f.stl.r6.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable stloptions.resthlinelengthfac
	checkbutton $f.stl.r6.bu -text "STL - line length" \
	    -variable stloptions.resthlinelengthenable
	pack $f.stl.r6.sc $f.stl.r6.bu -side left
	
	frame $f.stl.r3
	pack $f.stl.r3 -anchor w
	scale $f.stl.r3.sc -orient horizontal -length 200 -from 0.2 -to 8 \
	    -resolution 0.1 -variable stloptions.resthcloseedgefac
	checkbutton $f.stl.r3.bu -text "STL/IGES/STEP - close edges" \
	-variable stloptions.resthcloseedgeenable
	pack $f.stl.r3.sc $f.stl.r3.bu -side left
	
	frame $f.stl.r1
	pack $f.stl.r1 -anchor w
	scale $f.stl.r1.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable stloptions.resthsurfcurvfac
	checkbutton $f.stl.r1.bu -text "STL - surface curvature" \
	    -variable stloptions.resthsurfcurvenable
	pack $f.stl.r1.sc $f.stl.r1.bu -side left

	frame $f.stl.r3b
	pack $f.stl.r3b -anchor w
	scale $f.stl.r3b.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable stloptions.resthedgeanglefac
	checkbutton $f.stl.r3b.bu -text "STL - edge angle" \
	-variable stloptions.resthedgeangleenable
	pack $f.stl.r3b.sc $f.stl.r3b.bu -side left
	
	frame $f.stl.r5
	pack $f.stl.r5 -anchor w
	scale $f.stl.r5.sc -orient horizontal -length 200 -from 0.2 -to 5 \
	    -resolution 0.1 -variable stloptions.resthsurfmeshcurvfac
	checkbutton $f.stl.r5.bu -text "STL - surface mesh curv" \
	    -variable stloptions.resthsurfmeshcurvenable
	pack $f.stl.r5.sc $f.stl.r5.bu -side left
	
	
	checkbutton $f.stl.recalch -text "STL - Recalc mesh size for surface optimization" \
	    -variable stloptions.recalchopt
	pack $f.stl.recalch

	button $f.stl.calch -text "Calc New H" -command { redraw; Ng_STLCalcLocalH }
	pack $f.stl.calch






	set f [$w.nb subwidget chartopt]


	label $f.lab1 -text "Yellow Edges Angle (°)"
	scale $f.scale1 -orient horizontal -length 300 \
	    -from 0 -to 90 -resolution 1  -tickinterval 10 \
	    -variable  stloptions.yangle 

	pack $f.lab1 $f.scale1

	label $f.lab2e -text "Edge Corner Angle (°)"
	scale $f.scale2e -orient horizontal -length 360 -from 0 -to 180 \
	    -resolution 1  -tickinterval 20 \
	    -variable  stloptions.edgecornerangle 
	pack $f.lab2e $f.scale2e
	
	label $f.lab2 -text "Chart Angle (°)"
	scale $f.scale2 -orient horizontal -length 360 -from 0 -to 180 \
	    -resolution 1  -tickinterval 20 \
	    -variable  stloptions.chartangle 
	pack $f.lab2 $f.scale2
	
	label $f.lab2b -text "Outer Chart Angle (°)"
	scale $f.scale2b -orient horizontal -length 360 -from 0 -to 180 \
	    -resolution 1  -tickinterval 20 \
	    -variable  stloptions.outerchartangle 
	pack $f.lab2b $f.scale2b




	# Optimization options
	
	set f [$w.nb subwidget optimizer]

	


	tixControl $f.os2d -label "Surface opt steps: " -integer true \
	    -variable options.optsteps2d -min 0 -max 99 -step 1 \
	    -options {
		entry.width 3
		label.width 25
		label.anchor e
	    }	

	tixControl $f.os3d -label "Volume opt steps: " -integer true \
	    -variable options.optsteps3d -min 0 -max 99 -step 1 \
	    -options {
		entry.width 3
		label.width 25
		label.anchor e
	    }	
	
	tixControl $f.elw -label "Element size weight: " -integer false \
	    -variable options.elsizeweight -min 0 -max 1 -step 0.1 \
	    -options {
		entry.width 3
		label.width 25
		label.anchor e
	    }	

	tixControl $f.wem -label "Worst element measure: " -integer false \
	    -variable options.opterrpow -min 1 -max 10 -step 1 \
	    -options {
		entry.width 3
		label.width 25
		label.anchor e
	    }	

	pack $f.os2d $f.os3d $f.elw $f.wem
	
	frame $f.badellimit
	pack $f.badellimit -fill x
	label $f.badellimit.lab -text "bad element criterion";
	scale $f.badellimit.scale -orient horizontal -length 150 \
	    -from 160 -to 180 -resolution 1 \
	-variable options.badellimit
	pack $f.badellimit.scale $f.badellimit.lab -side right -anchor s


	# insider options
	set f [$w.nb subwidget insider]
	


	checkbutton $f.localh -text "Use Local Meshsize" \
	    -variable options.localh
	checkbutton $f.delauney -text "Use Delaunay" \
	    -variable options.delaunay
	checkbutton $f.checkoverlap -text "Check Overlapping" \
	    -variable options.checkoverlap
	checkbutton $f.checkcb -text "Check Chart Boundary" \
	    -variable options.checkchartboundary
	checkbutton $f.blockfill -text "Do Blockfilling" \
	    -variable options.blockfill

	pack  $f.localh  $f.delauney $f.checkoverlap  $f.blockfill $f.checkcb   -anchor w



	
	# debugging options
	set f [$w.nb subwidget debug]

	frame $f.cb
	pack $f.cb -side top

	

	checkbutton $f.cb.slowchecks -text "Slow checks" \
	    -variable debug.slowchecks -command { Ng_SetDebugParameters }
	checkbutton $f.cb.debugoutput -text "Debugging outout" \
	    -variable debug.debugoutput -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltexline -text "Halt on exising line" \
	    -variable debug.haltexistingline  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltoverlap -text "Halt on Overlap" \
	    -variable debug.haltoverlap  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltsuc -text "Halt on success" \
	    -variable debug.haltsuccess  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltnosuc -text "Halt on no success" \
	    -variable debug.haltnosuccess  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltlargequal -text "Halt on large quality class" \
	    -variable debug.haltlargequalclass  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltseg -text "Halt on Segment:" \
	    -variable debug.haltsegment  -command { Ng_SetDebugParameters }
	checkbutton $f.cb.haltnode -text "Halt on Node:" \
	    -variable debug.haltnode  -command { Ng_SetDebugParameters }


	pack $f.cb.slowchecks $f.cb.debugoutput $f.cb.haltexline $f.cb.haltoverlap $f.cb.haltsuc $f.cb.haltnosuc $f.cb.haltlargequal  $f.cb.haltseg   $f.cb.haltnode 

	frame $f.cb.hf
	pack $f.cb.hf -pady 5
	checkbutton $f.cb.hf.cb -text "Halt on Face:" \
	    -variable debug.haltface  -command { Ng_SetDebugParameters }
	entry $f.cb.hf.ent -textvariable debug.haltfacenr -width 5  
	pack $f.cb.hf.cb $f.cb.hf.ent -side left 

	checkbutton $f.cb.showactivechart -text "Show Active Meshing-Chart" \
	    -variable stloptions.showactivechart -command { Ng_SetVisParameters; redraw }




	pack $f.cb.showactivechart
	

	frame $f.segs
	pack $f.segs -pady 5
	label $f.segs.lab1 -text "P1:";
	entry $f.segs.ent1 -width 8 -relief sunken \
	    -textvariable debug.haltsegmentp1 
	label $f.segs.lab2 -text "P2:";
	entry $f.segs.ent2 -width 8 -relief sunken \
	    -textvariable debug.haltsegmentp2 
	pack $f.segs.lab1 $f.segs.ent1 $f.segs.lab2 $f.segs.ent2  -side left



	frame $f.cont -relief groove -borderwidth 3
	pack $f.cont 
	#-fill x 
	
	checkbutton $f.cont.multidrawing -text "Draw Meshing" \
	    -variable multithread_drawing 
	pack $f.cont.multidrawing
	
	checkbutton $f.cont.multitestmode -text "Meshing Testmode" \
	-variable multithread_testmode 
	pack $f.cont.multitestmode
	
	button $f.cont.goon -text "Go On" -command { set multithread_pause 0 }
	pack $f.cont.multidrawing $f.cont.multitestmode $f.cont.goon -side left -expand yes
	



	global userlevel
	if { $userlevel < 3} {
	    $w.nb delete insider
	    $w.nb delete debug
	}



# 	tixButtonBox $w.bbox -orientation horizontal
# 	$w.bbox add ok    -text Apply  -underline 0 -width 5 \
# 	    -command { 
# 		[.options_dlg.nb subwidget meshsize].meshsize invoke
# 		[.options_dlg.nb subwidget meshsize].grading invoke
# 		[.options_dlg.nb subwidget optimizer].os2d invoke
# 		[.options_dlg.nb subwidget optimizer].os3d invoke
# 		[.options_dlg.nb subwidget optimizer].elw invoke
# 		[.options_dlg.nb subwidget optimizer].wem invoke
		
# 		Ng_SetMeshingParameters 
# 	    }
	
# 	$w.bbox add close -text Done   -underline 0 -width 5 \
# 	    -command {
# 		[.options_dlg.nb subwidget meshsize].meshsize invoke
# 		[.options_dlg.nb subwidget meshsize].grading invoke
# 		[.options_dlg.nb subwidget optimizer].os2d invoke
# 		[.options_dlg.nb subwidget optimizer].os3d invoke
# 		[.options_dlg.nb subwidget optimizer].elw invoke
# 		[.options_dlg.nb subwidget optimizer].wem invoke
		
# 		Ng_SetMeshingParameters
# 		destroy .options_dlg
# 	    }
	
# 	pack $w.bbox -side bottom -fill x

	
	frame $w.bu
	pack $w.bu -fill x -ipady 3

	button $w.bu.apl -text "Apply" -command { 
	    [.options_dlg.nb subwidget meshsize].meshsize invoke
	    [.options_dlg.nb subwidget meshsize].grading invoke
	    [.options_dlg.nb subwidget optimizer].os2d invoke
	    [.options_dlg.nb subwidget optimizer].os3d invoke
	    [.options_dlg.nb subwidget optimizer].elw invoke
	    [.options_dlg.nb subwidget optimizer].wem invoke

	    Ng_SetMeshingParameters 
	    Ng_SetDebugParameters
	}

	button $w.bu.ok -text "Done" -command {
	    [.options_dlg.nb subwidget meshsize].meshsize invoke
	    [.options_dlg.nb subwidget meshsize].grading invoke
	    [.options_dlg.nb subwidget optimizer].os2d invoke
	    [.options_dlg.nb subwidget optimizer].os3d invoke
	    [.options_dlg.nb subwidget optimizer].elw invoke
	    [.options_dlg.nb subwidget optimizer].wem invoke

	    Ng_SetMeshingParameters
	    Ng_SetDebugParameters
	    wm withdraw .options_dlg
#	    destroy .options_dlg
	}

	pack  $w.bu.apl $w.bu.ok -side left -expand yes
    
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Meshing Options"
	focus .options_dlg
    }
}


meshingoptionsdialog
wm withdraw .options_dlg












proc geometryoptionsdialog { } {


    set w .geometry_dlg
    
    if {[winfo exists .geometry_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {

	toplevel $w
	
	global geooptions
	
	Ng_GeometryOptions get

	checkbutton $w.drawcsg -text "Draw Geometry" \
	-variable geooptions.drawcsg 
	pack $w.drawcsg

	frame $w.fac
	pack $w.fac -pady 5
	label $w.fac.lab -text "Facets:";
	entry $w.fac.ent -width 8 -relief sunken \
	    -textvariable geooptions.facets
	pack $w.fac.lab $w.fac.ent  -side left
	
 
	frame $w.det
	pack $w.det -pady 5
	label $w.det.lab -text "Detail:";
	entry $w.det.ent -width 8 -relief sunken \
	    -textvariable geooptions.detail
	pack $w.det.lab $w.det.ent  -side left
	
	frame $w.cox
	pack $w.cox -pady 5
	label $w.cox.lab -text "min/max x:";
	entry $w.cox.ent1 -width 8 -relief sunken \
	    -textvariable geooptions.minx
	entry $w.cox.ent2 -width 8 -relief sunken \
	    -textvariable geooptions.maxx
	pack $w.cox.lab $w.cox.ent1 \
	    $w.cox.ent2  -side left
	
	frame $w.coy
	pack $w.coy -pady 5
	label $w.coy.lab -text "min/max y:";
	entry $w.coy.ent1 -width 8 -relief sunken \
	    -textvariable geooptions.miny
	entry $w.coy.ent2 -width 8 -relief sunken \
	    -textvariable geooptions.maxy
	pack $w.coy.lab $w.coy.ent1 \
	    $w.coy.ent2  -side left
	
	frame $w.coz
	pack $w.coz -pady 5
	label $w.coz.lab -text "min/max z:";
	entry $w.coz.ent1 -width 8 -relief sunken \
	    -textvariable geooptions.minz
	entry $w.coz.ent2 -width 8 -relief sunken \
	    -textvariable geooptions.maxz
	pack $w.coz.lab $w.coz.ent1 \
	    $w.coz.ent2  -side left
	


# 	tixButtonBox $w.bbox -orientation horizontal
# 	$w.bbox add ok    -text Apply  -underline 0 -width 5 \
# 	    -command { Ng_GeometryOptions set }
	
# 	$w.bbox add close -text Done   -underline 0 -width 5 \
# 	    -command { 
# 		Ng_GeometryOptions set
# 		destroy .geometry_dlg
# 	    }
# 	pack $w.bbox -side bottom -fill x
	

 	frame $w.bu
	pack $w.bu -fill x -ipady 3


 	button $w.bu.app -text "Apply" -command {
 	    Ng_GeometryOptions set
 	}
 	button $w.bu.ok -text "Done" -command {
 	    Ng_GeometryOptions set
 	    destroy .geometry_dlg
 	}
 	pack  $w.bu.app $w.bu.ok -side left -expand yes
    

	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Geometry options"
	focus $w
    }
}

#
#
#  Viewing dialog
#
#
proc viewingoptionsdialog { } {

    global userlevel

    set w .viewopts_dlg
    
    if {[winfo exists .viewopts_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w
 



	tixNoteBook $w.nb -ipadx 6 -ipady 6
	
	$w.nb add general -label "general" -underline 0
	$w.nb add stl -label "STL" -underline 0
	$w.nb add occ -label "IGES/STEP" -underline 0
	$w.nb add mesh -label "Mesh"   -underline 0
	$w.nb add light -label "Light"   -underline 0
	$w.nb add edges -label "Edges"   -underline 0


	pack $w.nb -expand yes -fill both -padx 5 -pady 5 -side top	





	# general
	set f [$w.nb subwidget general]

	checkbutton $f.backcol -text "White Background" \
	-variable viewoptions.whitebackground \
	-command { Ng_SetVisParameters; redraw }

	checkbutton $f.cross -text "Draw Coordinate Cross" \
	-variable viewoptions.drawcoordinatecross \
	-command { Ng_SetVisParameters; redraw }

	checkbutton $f.color -text "Draw Color-bar" \
	-variable viewoptions.drawcolorbar \
	-command { Ng_SetVisParameters; redraw }

	checkbutton $f.netgen -text "Draw Netgen-logo" \
	-variable viewoptions.drawnetgenlogo \
	-command { Ng_SetVisParameters; redraw }

	pack $f.backcol $f.cross $f.color $f.netgen

# 	checkbutton $f.stereo -text "Stereo View" \
# 	-variable viewoptions.stereo \
# 	-command { Ng_SetVisParameters; redraw }
# 	pack $f.stereo

	# stl geometry 
	set f [$w.nb subwidget stl]

	frame $f.show -relief groove -borderwidth 3
	pack $f.show
	checkbutton $f.show.showtrias -text "Show STL-Triangles" \
	    -variable stloptions.showtrias -command { Ng_SetVisParameters; redraw }
	pack $f.show.showtrias -anchor w
	
	checkbutton $f.show.showfilledtrias -text "Show Filled Triangles" \
	    -variable stloptions.showfilledtrias -command { Ng_SetVisParameters; redraw }
	pack $f.show.showfilledtrias -anchor w
	
	checkbutton $f.show.showactivechart -text "Show Active Meshing-Chart" \
	    -variable stloptions.showactivechart -command { Ng_SetVisParameters; redraw }
	pack $f.show.showactivechart -anchor w
	
	checkbutton $f.show.showedges -text "Show Edges" \
	    -variable stloptions.showedges -command { Ng_SetVisParameters; redraw }
	pack $f.show.showedges -anchor w
	
	frame $f.special -relief groove -borderwidth 3
	pack $f.special
	checkbutton $f.special.showmarktrias -text "Show Chart Triangles" \
	    -variable stloptions.showmarktrias \
	    -command {set stldoctor.showfaces 0; Ng_STLDoctor; Ng_SetVisParameters; redraw }
	pack $f.special.showmarktrias -side left

	checkbutton $f.special.showfaces -text "Show Faces" \
	    -variable stldoctor.showfaces \
	    -command {set stloptions.showmarktrias 0; Ng_STLDoctor; Ng_SetVisParameters; redraw}    
	pack $f.special.showfaces -side left

	frame $f.fn -relief groove -borderwidth 3
	pack $f.fn
	label $f.fn.lab3 -text "Chart/Face number:"
	scale $f.fn.scale3 -orient horizontal -length 200 -from 0 -to 200 \
	    -resolution 1  -tickinterval 50 \
	    -command { Ng_SetVisParameters; redraw } -variable  stloptions.chartnumber 
	pack $f.fn.lab3 $f.fn.scale3 -side left
	
	frame $f.fo -relief groove -borderwidth 3
	pack $f.fo
	label $f.fo.lab -text "Chart/Face Offset:";
	entry $f.fo.ent -width 5 -relief sunken \
	    -textvariable stloptions.chartnumberoffset
	pack $f.fo.lab $f.fo.ent -side left

	frame $f.mt
	pack $f.mt -fill x
	checkbutton $f.mt.bu -text "Show Marked (Dirty) Triangles" \
	    -variable stldoctor.showmarkedtrigs \
	    -command {Ng_STLDoctor; redraw}    
	pack $f.mt.bu

	frame $f.ep
	pack $f.ep -fill x
	checkbutton $f.ep.bu -text "show edge corner points" \
	    -variable stldoctor.showedgecornerpoints \
	    -command {Ng_STLDoctor; redraw}    
	pack $f.ep.bu

	frame $f.stt
	pack $f.stt -fill x
	checkbutton $f.stt.bu -text "show touched triangle chart" \
	    -variable stldoctor.showtouchedtrigchart \
	    -command {set stldoctor.showfaces 0; set stloptions.showmarktrias 1; \
			  Ng_STLDoctor; Ng_SetVisParameters; redraw}    
	pack $f.stt.bu

	frame $f.sml
	pack $f.sml -fill x
	checkbutton $f.sml.bu -text "draw meshed edges" \
	    -variable stldoctor.drawmeshededges \
	    -command {Ng_STLDoctor;}    
	pack $f.sml.bu
	
	
	frame $f.sm
	pack $f.sm -fill x
	checkbutton $f.sm.bu -text "select with mouse" \
	    -variable stldoctor.selectwithmouse
	pack $f.sm.bu
	
	frame $f.st -relief groove -borderwidth 3
	pack $f.st -fill x
	label $f.st.lab -text "Select triangle by number";
	entry $f.st.ent -width 5 -relief sunken \
	    -textvariable stldoctor.selecttrig
	pack $f.st.ent $f.st.lab -side left -expand yes
	
	frame $f.vc -relief groove -borderwidth 3
	pack $f.vc -fill x
	checkbutton $f.vc.bu -text "show vicinity" \
	    -variable stldoctor.showvicinity \
	    -command {Ng_STLDoctor vicinity; redraw}
	label $f.vc.lab -text "vicinity size";
	scale $f.vc.sc -orient horizontal -length 200 -from 0 -to 200 \
	    -resolution 1 -variable stldoctor.vicinity \
	    -command { Ng_STLDoctor vicinity; redraw }
	pack $f.vc.bu $f.vc.lab $f.vc.sc -expand yes
	


	# IGES/STEP
	set f [$w.nb subwidget occ]
	
	checkbutton $f.occshowsurfaces -text "Show surfaces " \
	    -variable occoptions.showsurfaces \
	    -command { Ng_SetOCCVisParameters; redraw }

	checkbutton $f.occshowedges -text "Show edges " \
	    -variable occoptions.showedges \
	    -command { Ng_SetOCCVisParameters; redraw }

	frame $f.deflection -relief groove -borderwidth 3
	pack $f.deflection -fill x
	button $f.deflection.lab -text "Rebuild visualization data" \
	    -command {
		Ng_SetOCCVisParameters
		Ng_OCCCommand buildvisualizationmesh
		redraw
	    }

	tixControl $f.deflection.ent -label "Visualization smoothness" -integer false \
	    -variable occoptions.deflection -min 0.1 -max 3 -step 0.1 \
	    -options { entry.width 3 } \
	    -command { Ng_SetOCCVisParameters }

	pack $f.deflection.ent $f.deflection.lab -side left  -expand yes

	pack $f.occshowsurfaces $f.occshowedges





	# mesh options
	set f [$w.nb subwidget mesh]

	checkbutton $f.showcolor -text "Colored Meshsize Visualization" \
	    -variable viewoptions.colormeshsize \
	    -command { Ng_SetVisParameters; redraw }


	checkbutton $f.showfilledtrigs -text "Show filled triangles" \
	-variable viewoptions.drawfilledtrigs \
	-command { Ng_SetVisParameters; redraw }
	
	checkbutton $f.showedges -text "Show edges" \
	-variable viewoptions.drawedges \
	-command { Ng_SetVisParameters; redraw }
	

	checkbutton $f.showoutline -text "Show Triangle Outline" \
	    -variable viewoptions.drawoutline \
	    -command { Ng_SetVisParameters; redraw }


	tixControl $f.subdiv -label "Subdivision" -integer true \
            -variable visoptions.subdivisions -min 0 -max 5 \
	    -options { entry.width 2 } \
	    -command { Ng_SetVisParameters; Ng_Vis_Set parameters; Ng_SetNextTimeStamp; redraw }
	
	
	checkbutton $f.showbadels -text "Show bad elements" \
	    -variable viewoptions.drawbadels \
	    -command { Ng_SetVisParameters; redraw }



	checkbutton $f.showprisms -text "Show prisms" \
	    -variable viewoptions.drawprisms \
	    -command { Ng_SetVisParameters; redraw }

	checkbutton $f.showpyramids -text "Show pyramids" \
	    -variable viewoptions.drawpyramids \
	    -command { Ng_SetVisParameters; redraw }

	checkbutton $f.showhexes -text "Show hexes" \
	    -variable viewoptions.drawhexes \
	    -command { Ng_SetVisParameters; redraw }

	frame $f.fshrink
	label $f.fshrink.lab -text "Shrink elements"
	scale $f.fshrink.scale -orient horizontal -length 200 -from 0 -to 1.0001 \
	    -resolution 0.01  -tickinterval 0.25 \
	    -command { Ng_SetVisParameters; redraw } -variable  viewoptions.shrink


	checkbutton $f.showidentified -text "Show identified points" \
	    -variable viewoptions.drawidentified \
	    -command { Ng_SetVisParameters; redraw }

	checkbutton $f.showmetispartition -text "Show METIS Partition" \
	    -variable viewoptions.drawmetispartition \
	    -command { Ng_SetVisParameters; redraw }

	checkbutton $f.showpointnumbers -text "Show Point-numbers" \
	    -variable viewoptions.drawpointnumbers \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showedgenumbers -text "Show Edge-numbers" \
	    -variable viewoptions.drawedgenumbers \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showfacenumbers -text "Show Face-numbers" \
	    -variable viewoptions.drawfacenumbers \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showelementnumbers -text "Show Element-numbers" \
	    -variable viewoptions.drawelementnumbers \
	    -command { Ng_SetVisParameters; redraw }
	
	# label $f.showdomainlab -text "Domain Surface"
#	scale $f.showdomain -orient horizontal -length 100 -from 0 -to 50 \
	    -resolution 1 -variable  viewoptions.drawdomainsurf    \
	    -command { Ng_SetVisParameters; redraw } \
	    -label "Domain Surface" 

	tixControl $f.showdomain -label "Show surface of domain" -integer true \
            -variable viewoptions.drawdomainsurf -min 0 -max 50 \
	    -options { entry.width 2 } \
	    -command { Ng_SetVisParameters; Ng_Vis_Set parameters; redraw }
    
    
    

	if {$userlevel == 3} {
	    frame $f.center -relief groove -borderwidth 3
	    pack $f.center -fill x
	    button $f.center.lab -text "Set Center Point" \
		-command { Ng_SetVisParameters; Ng_Center; redraw }
	    entry $f.center.ent -width 5 -relief sunken \
		-textvariable viewoptions.centerpoint 
	    pack $f.center.ent $f.center.lab -side left  -expand yes
	    
	    frame $f.drawel -relief groove -borderwidth 3
	    pack $f.drawel -fill x
	    button $f.drawel.lab -text "Draw Element" \
		-command { Ng_SetVisParameters; Ng_ZoomAll; redraw }
	    entry $f.drawel.ent -width 5 -relief sunken \
		-textvariable viewoptions.drawelement 
	    pack $f.drawel.ent $f.drawel.lab -side left  -expand yes
	}

        pack $f.showfilledtrigs
	pack $f.showoutline $f.subdiv $f.showedges  $f.showbadels 
	# pack $f.showdomainlab 
	pack $f.showdomain 
	pack $f.showpointnumbers 
	pack $f.showedgenumbers $f.showfacenumbers $f.showelementnumbers 
	pack $f.showmetispartition


	frame $f.frametets
	checkbutton $f.frametets.showtets -text "Show Tets in domain " \
	    -variable viewoptions.drawtets \
	    -command { Ng_SetVisParameters; redraw }
	tixControl $f.frametets.showtetsdomain -label "" -integer true \
	    -variable viewoptions.drawtetsdomain -min 0 -max 50 \
	    -options { entry.width 2 } \
	    -command { Ng_SetVisParameters; redraw }

	pack $f.frametets
	pack $f.frametets.showtets $f.frametets.showtetsdomain -side left


	if {$userlevel == 3} {
	    pack $f.showcolor    $f.showpyramids $f.showprisms $f.showhexes $f.showidentified

	    pack $f.fshrink 
	    pack $f.fshrink.lab $f.fshrink.scale -side left
	}	
	
	if {$userlevel == 3} {
	    frame $f.framecurveproj
	    checkbutton $f.framecurveproj.showcurveproj -text "Show curved edge projection " \
		-variable viewoptions.drawcurveproj \
		-command { Ng_SetVisParameters; redraw }
	    tixControl $f.framecurveproj.showcurveprojedge -label "" -integer true \
		-variable viewoptions.drawcurveprojedge -min 1 -max 99999 \
		-options { entry.width 5 } \
		-command { Ng_SetVisParameters; redraw }

	    pack $f.framecurveproj
	    pack $f.framecurveproj.showcurveproj $f.framecurveproj.showcurveprojedge -side left
	}
	
	




	# light options
	set f [$w.nb subwidget light]
	
	label $f.lab1 -text "Ambient Light"
	scale $f.scale1 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	    -command { Ng_SetVisParameters; redraw } -variable  viewoptions.light.amb 
	label $f.lab2 -text "Diffuse Light"
	scale $f.scale2 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	    -command { Ng_SetVisParameters; redraw } -variable  viewoptions.light.diff 
	label $f.lab3 -text "Specular Light"
	scale $f.scale3 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	    -command { Ng_SetVisParameters; redraw } -variable  viewoptions.light.spec 
	label $f.lab4 -text "Material Shininess"
	scale $f.scale4 -orient horizontal -length 300 -from 0 -to 128 \
	    -resolution 1  -tickinterval 32 \
	    -command { Ng_SetVisParameters; redraw } -variable  viewoptions.mat.shininess 
	label $f.lab5 -text "Material Transparency"
	scale $f.scale5 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	-command { Ng_SetVisParameters; redraw } -variable  viewoptions.mat.transp 
	
	pack $f.lab1 $f.scale1 $f.lab2 $f.scale2 $f.lab3 $f.scale3 $f.lab4 $f.scale4 $f.lab5 $f.scale5
	




	# edges options
	set f [$w.nb subwidget edges]

	checkbutton $f.showedges -text "Show Edges" \
	    -variable viewoptions.drawededges \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showpoints -text "Show Points" \
	    -variable viewoptions.drawedpoints \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showpointnrs -text "Show Points Nrs" \
	    -variable viewoptions.drawedpointnrs \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.showtang -text "Show CP Tangents" \
	    -variable viewoptions.drawedtangents \
	    -command { Ng_SetVisParameters; redraw }
	checkbutton $f.drawedgenrs -text "Show Edge Nrs" \
	    -variable viewoptions.drawededgenrs \
	    -command { Ng_SetVisParameters; redraw }
	
	pack $f.showedges $f.showpoints $f.showpointnrs $f.showtang $f.drawedgenrs

	frame $f.center -relief groove -borderwidth 3
	pack $f.center -fill x
	button $f.center.lab -text "Set Center Point" \
	    -command { Ng_SetVisParameters; Ng_Center; redraw }
	entry $f.center.ent -width 5 -relief sunken \
	    -textvariable viewoptions.centerpoint 
	pack $f.center.ent $f.center.lab -side left  -expand yes
	


	frame $f.f1
	pack $f.f1 -pady 5
	label $f.f1.lab -text "SpecPoint Veclen"
	entry $f.f1.ent -width 5 -relief sunken -textvariable viewoptions.specpointvlen
	pack $f.f1.lab $f.f1.ent
	



	
	frame $w.bu
	pack $w.bu -fill x -ipady 3


	button $w.bu.done -text "Done" -command {
	    Ng_SetVisParameters;
	    redraw
	    destroy .viewopts_dlg
	}
	button $w.bu.apply -text "Apply" -command {
	    Ng_SetVisParameters;
	    redraw
	}
	pack $w.bu.apply $w.bu.done -expand yes -side left
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Viewing options"
	focus $w
    }
}








#
#
#  clipping dialog
#
# 
proc clippingdialog { } {

    set w .clipping_dlg
    
    if {[winfo exists .clipping_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {

	toplevel $w


	label $w.lab1 -text "Normal x"
	scale $w.scale1 -orient horizontal -length 300 -from -1 -to 1 \
	    -resolution 0.01  -tickinterval 0.5 \
	    -variable  viewoptions.clipping.nx \
	    -command { 
		Ng_SetVisParameters; 
		if { ${viewoptions.clipping.enable} == 1 } { redraw };
		Ng_SetVisParameters }
	label $w.lab2 -text "Normal y"
	scale $w.scale2 -orient horizontal -length 300 -from -1 -to 1 \
	    -resolution 0.01  -tickinterval 0.5 \
	    -variable  viewoptions.clipping.ny \
	    -command { 
		Ng_SetVisParameters; 
		if { ${viewoptions.clipping.enable} == 1 } { redraw }; 
		Ng_SetVisParameters }
	label $w.lab3 -text "Normal z"
	scale $w.scale3 -orient horizontal -length 300 -from -1 -to 1 \
	    -resolution 0.01  -tickinterval 0.5 \
	    -variable  viewoptions.clipping.nz \
	    -command { 
		Ng_SetVisParameters; 
		if { ${viewoptions.clipping.enable} == 1 } { redraw }; 
		Ng_SetVisParameters }
	label $w.lab4 -text "Distance"
	scale $w.scale4 -orient horizontal -length 300 -from -1 -to 1.001 \
	    -resolution 0.0001  -tickinterval 0.5 \
	    -variable  viewoptions.clipping.dist \
	    -command { 
		Ng_SetVisParameters; 
		if { ${viewoptions.clipping.enable} == 1 } { redraw }; 
		Ng_SetVisParameters }	
	
	pack $w.lab1 $w.scale1 $w.lab2 $w.scale2 $w.lab3 $w.scale3 $w.lab4 $w.scale4 

	
	checkbutton $w.cb1 -text "Enable clipping" \
	    -variable viewoptions.clipping.enable \
	    -command { Ng_SetVisParameters; redraw } 
	
	pack $w.cb1
	

	
	frame $w.bu
#	pack $w.bu -fill x
	pack $w.bu -fill x -ipady 3

	button $w.bu.cancle -text "Done" -command "destroy $w"
	pack $w.bu.cancle  -expand yes
	
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Clipping Plane"
	#    grab $w
	focus $w


#	$w.scale1 configure -command { puts "call1b"; Ng_SetVisParameters; redraw } 
#	puts "after"


    }
}





#
#  refinement dialog
#
#
proc refinementdialog { } {

    set w .refinement_dlg
    
    if {[winfo exists .refinement_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {

	toplevel $w

	
	tixControl $w.meshsize -label "max mesh-size: " -integer false \
	    -variable options.meshsize -min 1e-6 -max 1e6 \
	    -options {
		entry.width 6
		label.width 25
		label.anchor e
	    }	

	pack $w.meshsize

	global localh
	set localh 1
	tixControl $w.loch -label "local mesh-size: " -integer false \
	    -variable localh -min 1e-6 -max 1e6 \
	    -options {
		entry.width 6
		label.width 25
		label.anchor e
	    }	
	
	pack $w.loch
	
	
	button $w.restface -text "Restrict H at face"  \
	    -command {
		.refinement_dlg.meshsize invoke
		.refinement_dlg.loch invoke
		Ng_RestrictH face $localh
	    }
	button $w.restedge -text "Restrict H at edge"  \
	    -command {
		.refinement_dlg.meshsize invoke
		.refinement_dlg.loch invoke
		Ng_RestrictH edge $localh
	    }
	button $w.restelement -text "Restrict H at element"  \
	    -command {
		.refinement_dlg.meshsize invoke
		.refinement_dlg.loch invoke
		Ng_RestrictH element $localh
	    }
	button $w.restpoint -text "Restrict H at point"  \
	    -command {
		.refinement_dlg.meshsize invoke
		.refinement_dlg.loch invoke
		Ng_RestrictH point $localh
	    }


	pack $w.restface $w.restedge $w.restelement $w.restpoint



	button $w.anisoedge -text "Declare Anisotropic edge"  \
	    -command {
		Ng_Anisotropy edge 
	    }
	pack $w.anisoedge
	

	frame $w.bu
	pack $w.bu -fill x -ipady 3


	button $w.bu.cancle -text "Done" -command "destroy .refinement_dlg"
	button $w.bu.refine -text "Refine"  \
	    -command { 
#		Ng_BisectCopyMesh; 
		set oldnp 0; set newnp $status_np; 
		while { $oldnp < $newnp } {
		    set level [expr $level+1]
		    Ng_Bisect; 
		    Ng_HighOrder ${options.elementorder}
		    Ng_ReadStatus;
		redraw; 
		    set oldnp $newnp
		    set newnp $status_np
		    puts "oldnp $oldnp newnp $newnp"
		}
	    }	 
	button $w.bu.zrefine -text "Z-Refine"  \
	    -command { Ng_ZRefinement; Ng_ReadStatus; redraw; }
   
	pack $w.bu.zrefine $w.bu.refine $w.bu.cancle  -expand yes -side left
		
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Select Refinement"
	focus $w
    }
}




#
#  boundcondessing dialog
#
#
proc bcpropdialog { } {

    set w .bcprop_dlg
    
    if {[winfo exists .bcprop_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
    } {
	toplevel $w
	
	frame $w.face  -borderwidth 3
	pack $w.face -fill x
	label $w.face.lab -text "face index:"
	label $w.face.ent -text 1 -padx 4
	button $w.face.next -text "next" -command {
	    set w .bcprop_dlg;	
	    set facenr [$w.face.ent cget -text]
	    if {$facenr == [Ng_BCProp getnfd]} {
		set facenr 1 
	    } {
		set facenr [expr $facenr + 1]
	    }
	    $w.face.ent configure -text $facenr
	    Ng_BCProp setactive $facenr
	    set bcnr [Ng_BCProp getbc $facenr]
	    $w.bc.ent delete 0 end
	    $w.bc.ent insert 0 $bcnr

	    redraw
	} 
	button $w.face.prev -text "prev" -command {
	    set w .bcprop_dlg;	
	    set facenr [$w.face.ent cget -text]
	    if {$facenr == 1} {
		set facenr [Ng_BCProp getnfd]
	    } {
		set facenr [expr $facenr - 1]
	    }
	    $w.face.ent configure -text $facenr
	    Ng_BCProp setactive $facenr
	    set bcnr [Ng_BCProp getbc $facenr]
	    $w.bc.ent delete 0 end
	    $w.bc.ent insert 0 $bcnr

	    redraw
	} 
	
	
	pack $w.face.lab $w.face.ent $w.face.prev $w.face.next  -side left  
	
	frame $w.bc  -borderwidth 3
	pack $w.bc -fill x
	label $w.bc.lab -text "bc property:"
	entry $w.bc.ent -width 5 -relief sunken 
	button $w.bc.but -text "change" -command { 
	    set w .bcprop_dlg;	    
	    Ng_BCProp setbc [$w.face.ent cget -text] [$w.bc.ent get]; 
	}
	button $w.bc.but2 -text "all" -command { 
	    set w .bcprop_dlg;	    
	    Ng_BCProp setall [$w.bc.ent get]; 
	}
	pack $w.bc.lab $w.bc.ent $w.bc.but $w.bc.but2 -side left  -expand yes
	

	frame $w.bu
	pack $w.bu -fill x -ipady 3

	button $w.bu.close -text "Close" -command { destroy .bcprop_dlg }

	pack $w.bu.close  -expand yes -side left
		
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Boundary Conditions"
    }

    focus $w 

    set facenr [Ng_BCProp getactive]
    $w.face.ent configure -text $facenr
    
    set bcnr [Ng_BCProp getbc $facenr]
    $w.bc.ent delete 0 end
    $w.bc.ent insert 0 $bcnr

}




#
#  METIS dialog
#
#
proc METISdialog { } {

    set w .metis_dlg
    set w.parts 64
    
    if {[winfo exists .metis_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
    } {
	toplevel $w
	
	frame $w.a -borderwidth 0
	frame $w.b -borderwidth 0
	pack $w.a $w.b

	label $w.a.lab -text "Number of partitions:"
	entry $w.a.ent -textvariable w.parts -width 4 -relief sunken

	button $w.b.start -text "Start METIS" -command { 
	    Ng_Metis ${w.parts}
	    redraw
	}
	button $w.b.cancel -text "Cancel" -command { destroy .metis_dlg }
	pack $w.a.lab $w.a.ent -side left  -expand yes
	pack $w.b.start $w.b.cancel -side left


	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "METIS Partitioning"
	focus $w
 
    }
}



#
#  STL dialog
#
proc stloptionsdialog { } {

    set w .stlopts_dlg
    
    if {[winfo exists .stlopts_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w

	tixNoteBook $w.nb -ipadx 6 -ipady 6
	#	$w config -bg gray
	#	$w.nb subwidget nbframe config -backpagecolor gray
	
	# Create the two tabs on the notebook. The -underline option
	# puts a underline on the first character of the labels of the tabs.
	# Keyboard accelerators will be defined automatically according
	# to the underlined character.	
	#
	
# 	$w.nb add chartopt -label "Chart Options" -underline 0
# 	#$w.nb add meshsize   -label "Mesh Size"   -underline 0
# 	pack $w.nb -expand yes -fill both -padx 5 -pady 5 -side top	


# 	set f [$w.nb subwidget chartopt]


# 	label $f.lab1 -text "Yellow Edges Angle (°)"
# 	scale $f.scale1 -orient horizontal -length 300 \
# 	    -from 0 -to 90 -resolution 1  -tickinterval 10 \
# 	    -variable  stloptions.yangle 

# 	pack $f.lab1 $f.scale1

# 	label $f.lab2e -text "Edge Corner Angle (°)"
# 	scale $f.scale2e -orient horizontal -length 360 -from 0 -to 180 \
# 	    -resolution 1  -tickinterval 20 \
# 	    -variable  stloptions.edgecornerangle 
# 	pack $f.lab2e $f.scale2e
	
# 	label $f.lab2 -text "Chart Angle (°)"
# 	scale $f.scale2 -orient horizontal -length 360 -from 0 -to 180 \
# 	    -resolution 1  -tickinterval 20 \
# 	    -variable  stloptions.chartangle 
# 	pack $f.lab2 $f.scale2
	
# 	label $f.lab2b -text "Outer Chart Angle (°)"
# 	scale $f.scale2b -orient horizontal -length 360 -from 0 -to 180 \
# 	    -resolution 1  -tickinterval 20 \
# 	    -variable  stloptions.outerchartangle 
# 	pack $f.lab2b $f.scale2b
	
#	frame $f.r4
#	pack $f.r4 -anchor w
#	scale $f.r4.sc -orient horizontal -length 200 -from 0.1 -to 10 \
#	    -resolution 0.1 -variable stloptions.resthatlasfac
#	checkbutton $f.r4.bu -text "Restrict h for Calc Atlas (Faster)" \
#	    -variable stloptions.resthatlasenable
#	pack $f.r4.sc $f.r4.bu -side left
	

	#set f [$w.nb subwidget meshsize]

	

#    checkbutton $w.seat -text "Use Searchtrees" \
#	-variable stloptions.usesearchtree
#   pack $w.seat




	frame $w.bu
#    pack $w.bu
	pack $w.bu -fill x -ipady 3

# -fill x

    button $w.bu.apply -text "Apply" -command { redraw; Ng_GenerateMesh 1 2}
	button $w.bu.cancle -text "Done" -command { destroy .stlopts_dlg }
    pack $w.bu.cancle  $w.bu.apply  -side left -expand yes
    

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w
	wm title $w "STL Options"
#    grab $w
	focus $w
    }
}

proc stldoctordialog { } {

#üd
    set wd .stldoctor_dlg

    if {[winfo exists .stldoctor_dlg] == 1} {
	wm withdraw $wd
	wm deiconify $wd
	focus $wd 
    } {
	
    toplevel $wd

    tixNoteBook $wd.nb -ipadx 6 -ipady 6
	
    $wd.nb add general -label "General" -underline 0
    $wd.nb add topology -label "Edit Topology"  -underline 5
    $wd.nb add edges -label "Edit Edges"   -underline 5
    $wd.nb add normals -label "Edit Normals"   -underline 5
    $wd.nb add advanced -label "Advanced"   -underline 0


    pack $wd.nb -expand yes -fill both -padx 5 -pady 5 -side top	


    # GENERAL *****************************

    set f [$wd.nb subwidget general]


    frame $f.show
    pack $f.show -fill x
    checkbutton $f.show.showtrias -text "Show STL-Triangles" \
	-variable stloptions.showtrias -command { Ng_SetVisParameters; redraw }
    pack $f.show.showtrias -anchor w
    
    checkbutton $f.show.showfilledtrias -text "Show Filled Triangles" \
	-variable stloptions.showfilledtrias -command { Ng_SetVisParameters; redraw }
    pack $f.show.showfilledtrias -anchor w

    set selmodevals { 0 1 2 3 4 }
    set selmodelabs(0) "triangle" 
    set selmodelabs(1) "edge" 
    set selmodelabs(2) "point" 
    set selmodelabs(3) "line" 
    set selmodelabs(4) "line cluster" 

    tixOptionMenu $f.selmode -label "Double Click selects :" \
	-options {
	    label.width  19
	    label.anchor e
	    menubutton.width 15
	} 

    foreach selmodev $selmodevals {
	$f.selmode add command $selmodev -label $selmodelabs($selmodev)
    }
    $f.selmode config -variable stldoctor.selectmode
    $f.selmode config -command { Ng_STLDoctor }
    global stldoctor.selectmode
    pack $f.selmode

    frame $f.sm
    pack $f.sm -fill x
    checkbutton $f.sm.bu -text "select with mouse" \
	-variable stldoctor.selectwithmouse
    pack $f.sm.bu 

    frame $f.st -relief groove -borderwidth 3
    pack $f.st -fill x
    label $f.st.lab -text "Select triangle by number";
    entry $f.st.ent -width 5 -relief sunken \
	-textvariable stldoctor.selecttrig
    pack $f.st.ent $f.st.lab -side left -expand yes

    frame $f.vc -relief groove -borderwidth 3
    pack $f.vc -fill x
    checkbutton $f.vc.bu -text "show vicinity" \
	-variable stldoctor.showvicinity \
	-command {Ng_STLDoctor vicinity; redraw}
    label $f.vc.lab -text "vicinity size";
    scale $f.vc.sc -orient horizontal -length 200 -from 0 -to 200 \
	-resolution 1 -variable stldoctor.vicinity \
	-command { Ng_STLDoctor vicinity; redraw }
    pack $f.vc.bu $f.vc.lab $f.vc.sc -expand yes

    frame $f.ge -relief groove -borderwidth 3
    pack $f.ge -fill x
    button $f.ge.neighbourangles -text "calc neighbourangles" -command {Ng_STLDoctor neighbourangles}
    button $f.ge.showcoords -text "show coords of touched triangle" -command {Ng_STLDoctor showcoords}
    button $f.ge.moveptm -text "move point to middle of trianglepoints" -command {Ng_STLDoctor movepointtomiddle; redraw}
    button $f.ge.destroy0trigs -text "destroy 0-volume triangles" -command {Ng_STLDoctor destroy0trigs}
    pack $f.ge.neighbourangles $f.ge.showcoords $f.ge.moveptm $f.ge.destroy0trigs -expand yes 


    button $f.ge.cancle -text "Done" -command {destroy .stldoctor_dlg }
    pack $f.ge.cancle -expand yes

    # TOPOLOGY ********************
    set f [$wd.nb subwidget topology]

    frame $f.oc -relief groove -borderwidth 3
    pack $f.oc -fill x
    button $f.oc.bu -text "invert orientation of selected trig" -command {Ng_STLDoctor invertselectedtrig; redraw }
    button $f.oc.bu2 -text "orient after selected trig" -command {Ng_STLDoctor orientafterselectedtrig; redraw }
    pack $f.oc.bu $f.oc.bu2 -side left  -expand yes

    button $f.toperr -text "mark inconsistent triangles" -command {Ng_STLDoctor marktoperrortrigs; redraw }

    button $f.deltrig -text "delete selected triangle" -command {Ng_STLDoctor deleteselectedtrig; redraw }
    button $f.geosmooth -text "geometric smoothing" -command {Ng_STLDoctor smoothgeometry; redraw }

    pack $f.toperr $f.deltrig $f.geosmooth





    # EDGES ***********************
    set f [$wd.nb subwidget edges]


    frame $f.be -relief groove -borderwidth 3 
    pack $f.be -fill x
    label $f.be.lab -text "build edges with yellow angle:";
    scale $f.be.sc -orient horizontal -length 200 -from 0 -to 100 \
	-resolution 0.5
    $f.be.sc config -variable stloptions.yangle 
    $f.be.sc config -command { Ng_SetSTLParameters; Ng_STLDoctor buildedges; redraw }
    label $f.be.lab2 -text "continue edges with yellow angle:";
    scale $f.be.sc2 -orient horizontal -length 200 -from 0 -to 100 \
	-resolution 0.5
    $f.be.sc2 config -variable stloptions.contyangle 
    $f.be.sc2 config -command { Ng_SetSTLParameters; Ng_STLDoctor buildedges; redraw }



    button $f.be.buildedges -text "Build Edges" -command {Ng_STLDoctor buildedges; redraw}
    pack $f.be.lab $f.be.sc $f.be.lab2 $f.be.sc2 $f.be.buildedges -expand yes

    frame $f.se
    pack $f.se -fill x
    checkbutton $f.se.bu -text "show excluded" \
	-variable stldoctor.showexcluded \
	-command {Ng_STLDoctor; redraw}
    pack $f.se.bu 

    # edgeselectmode ******

    set edgeselmodevals { 0 1 2 3 4 }
    set edgeselmodelabs(0) "no change" 
    set edgeselmodelabs(1) "undefined" 
    set edgeselmodelabs(2) "confirmed" 
    set edgeselmodelabs(3) "candidate"
    set edgeselmodelabs(4) "excluded"

    tixOptionMenu $f.edgeselmode -label "Double Click sets edge :" \
	-options {
	    label.width  19
	    label.anchor e
	    menubutton.width 15
	} 

    foreach edgeselmodev $edgeselmodevals {
	$f.edgeselmode add command $edgeselmodev -label $edgeselmodelabs($edgeselmodev)
    }
    $f.edgeselmode config -variable stldoctor.edgeselectmode
    $f.edgeselmode config -command { Ng_STLDoctor }
    global stldoctor.edgeselectmode
    pack $f.edgeselmode

    # edge buttons

    frame $f.edg -relief groove -borderwidth 3
    pack $f.edg -fill x

#    checkbutton $f.edg.bu -text "use external edges" \
#	-variable stldoctor.useexternaledges \
#	-command {Ng_STLDoctor; redraw}
#   pack $f.edg.bu -expand yes


    frame $f.edg.f0
    pack $f.edg.f0
    button $f.edg.f0.confirmedge -text "confirm" -command {Ng_STLDoctor confirmedge; redraw}
    button $f.edg.f0.candidateedge -text "candidate" -command {Ng_STLDoctor candidateedge; redraw}
    button $f.edg.f0.excludeedge -text "exclude" -command {Ng_STLDoctor excludeedge; redraw}
    button $f.edg.f0.undefinededge -text "undefined" -command {Ng_STLDoctor undefinededge; redraw}
    pack $f.edg.f0.confirmedge $f.edg.f0.candidateedge $f.edg.f0.excludeedge $f.edg.f0.undefinededge  -side left

    frame $f.edg.fa
    pack $f.edg.fa
    button $f.edg.fa.setallundefined -text "all undefined" -command {Ng_STLDoctor setallundefinededges; redraw}
    button $f.edg.fa.erasecandidates -text "candidates to undefined" -command {Ng_STLDoctor erasecandidateedges; redraw}
    pack $f.edg.fa.setallundefined $f.edg.fa.erasecandidates -side left


    frame $f.edg.fb
    pack $f.edg.fb
    button $f.edg.fb.confirmcandidates -text "candidates to confirmed" -command {Ng_STLDoctor confirmcandidateedges; redraw}
    button $f.edg.fb.confirmedtocandidates -text "confirmed to candidates" -command {Ng_STLDoctor confirmedtocandidateedges; redraw}
    pack $f.edg.fb.confirmcandidates $f.edg.fb.confirmedtocandidates -side left

    frame $f.edg.f1
    frame $f.edg.f2
    frame $f.edg.f3
    frame $f.edg.f4
    pack $f.edg.f1 $f.edg.f2 $f.edg.f3 $f.edg.f4

    button $f.edg.f1.exportedges -text "export edges" -command {Ng_STLDoctor exportedges}
    button $f.edg.f1.importedges -text "import edges" -command {Ng_STLDoctor importedges; redraw}
    button $f.edg.f1.saveedgedata -text "save edgedata" \
	-command { 
	    set types {
		{"Netgen Edgedata"   {.ned} } 
	    }
	    set file [tk_getSaveFile -filetypes $types -defaultextension ".ned"]
	    if {$file != ""} {
		Ng_STLDoctor saveedgedata $file
	}
    }

    button $f.edg.f1.loadedgedata -text "load edgedata" \
	-command { 
	    set types {
		{"Netgen Edgedata"  {.ned} }
	    }
	    set file [tk_getOpenFile -filetypes $types -defaultextension ".ned"]
	    if {$file != ""} {
		Ng_STLDoctor loadedgedata $file 
		puts "loading done"
		
		redraw
		
#		wm title . [concat "NETGEN - " $file]
	    }
	} 

    button $f.edg.f1.importAVLedges -text "import AVL edges" \
	-command {
	    set types {{"Edge file"  {.edg }}}

	    set file [tk_getOpenFile -filetypes $types -defaultextension ".edg"]
	    if {$file != ""} {
		Ng_STLDoctor importexternaledges $file; 
	    }
	}

    pack $f.edg.f1.importAVLedges $f.edg.f1.loadedgedata $f.edg.f1.saveedgedata -side left

#    button $f.edg.f1.buildedges -text "build external edges" -command {Ng_STLDoctor buildexternaledges; redraw}
    frame $f.edg2 -relief groove -borderwidth 3
    pack $f.edg2 -fill x


#    button $f.edg2.addlonglines -text "make long lines candidates (% of diam)" -command {Ng_STLDoctor addlonglines; redraw}
    label $f.edg2.lab -text "length (%):"
    scale $f.edg2.sc -orient horizontal -length 200 -from 0 -to 100 \
	-resolution 0.5 \
        -variable stldoctor.longlinefact 

 #   button $f.edg2.deletedirtyedges -text "make dirty edges candidates" -command {Ng_STLDoctor deletedirtyedges; redraw}
    button $f.edg2.undoedge -text "undo last edge change" -command {Ng_STLDoctor undoedgechange; redraw}
    
 #   pack $f.edg2.addlonglines $f.edg2.deletedirtyedges -expand yes
 #   pack $f.edg2.lab $f.edg2.sc -side left
    pack $f.edg2.undoedge -expand yes



    # NORMALS ***********************
    set f [$wd.nb subwidget normals]

    frame $f.dt -relief groove -borderwidth 3
    pack $f.dt -fill x
    label $f.dt.lab -text "dirty triangle factor";
    entry $f.dt.ent -width 5 -relief sunken \
	-textvariable stldoctor.dirtytrigfact
    pack $f.dt.ent $f.dt.lab -side left  -expand yes

    frame $f.srt -relief groove -borderwidth 3
    pack $f.srt -fill x
    button $f.srt.bu -text "smooth reverted triangles geometric" -command {Ng_STLDoctor smoothrevertedtrigs; redraw }
    entry $f.srt.ent -width 5 -relief sunken \
	-textvariable stldoctor.smoothangle
    pack $f.srt.ent $f.srt.bu -side left  -expand yes

    frame $f.bdt -relief groove -borderwidth 3
    pack $f.bdt -fill x
    button $f.bdt.bu -text "mark dirty triangles" -command {Ng_STLDoctor markdirtytrigs; redraw }
    button $f.bdt.bu2 -text "smooth dirty triangles normal" -command {Ng_STLDoctor smoothdirtytrigs; redraw }
    pack $f.bdt.bu $f.bdt.bu2 -side left  -expand yes

    
    frame $f.sno -relief groove -borderwidth 3
    pack $f.sno
    
    label $f.sno.labrough -text "rough"
    scale $f.sno.scsmooth -orient horizontal -length 100 -from 0 -to 0.8 \
	-resolution 0.01 -variable stldoctor.smoothnormalsweight \
	-command { Ng_SetSTLParameters }
    label $f.sno.labsmooth -text "smooth"
    button $f.sno.smoothnormals -text "smooth normals" -command { Ng_STLDoctor smoothnormals; redraw}



    pack $f.sno.labrough $f.sno.scsmooth $f.sno.labsmooth $f.sno.smoothnormals -side left -padx 5

    frame $f.no -relief groove -borderwidth 3
    pack $f.no -fill x

    button $f.no.marknonsmoothnormals -text "mark non-smooth triangles" -command {Ng_STLDoctor marknonsmoothnormals; redraw}
    button $f.no.calcnormals -text "calculate normals from geometry" -command {Ng_STLDoctor calcnormals; redraw}

    pack  $f.no.marknonsmoothnormals $f.no.calcnormals -expand yes


    # ADVANCED **************************
    set f [$wd.nb subwidget advanced]


    frame $f.sc
    pack $f.sc -fill x
    checkbutton $f.sc.bu -text "spiral check" \
	-variable stldoctor.spiralcheck \
	-command {Ng_STLDoctor;}    
    checkbutton $f.sc.bu2 -text "cone check" \
	-variable stldoctor.conecheck \
	-command {Ng_STLDoctor;}    
    pack $f.sc.bu $f.sc.bu2


    tixControl $f.gtol -label "load-geometry tolerance factor" -integer false \
	-variable stldoctor.geom_tol_fact \
	-options {
	    entry.width 8
	    label.width 30
	    label.anchor e
	}	
    pack $f.gtol

    button $f.adap -text "Apply" -command {
	[.stldoctor_dlg.nb subwidget advanced].gtol invoke
	Ng_STLDoctor; 
    }
    pack $f.adap -expand yes

#    frame $f.gtol -relief groove -borderwidth 3
#    pack $f.gtol -fill x
#    label $f.gtol.lab -text "Geometry-Load-Tolerance-Factor";
#    entry $f.gtol.ent -width 5 -relief sunken \
#	-textvariable stldoctor.geom_tol_fact
#   pack $f.gtol.lab $f.gtol.ent -side left -expand yes

    #*******************************    
    wm withdraw $wd
    wm geom $wd +100+100
    wm deiconify $wd
    wm title $wd "STL Doctor"

    focus $wd
}
}





proc meshdoctordialog { } {

    set w .meshdoc_dlg
    global meshdoctor.active

    if {[winfo exists .meshdoc_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w

	set meshdoctor.active 1
	Ng_MeshDoctor;


	frame $w.vis -relief groove -borderwidth 3
	pack $w.vis

	checkbutton $w.vis.showfilledtrigs -text "Show filled triangles" \
	-variable viewoptions.drawfilledtrigs \
	-command { Ng_SetVisParameters; redraw }
	
	checkbutton $w.vis.showedges -text "Show edges" \
	-variable viewoptions.drawedges \
	-command { Ng_SetVisParameters; redraw }
	

	checkbutton $w.vis.showoutline -text "Show Triangle Outline" \
	-variable viewoptions.drawoutline \
	-command { Ng_SetVisParameters; redraw }

	pack $w.vis.showfilledtrigs  $w.vis.showoutline $w.vis.showedges

	tixControl $w.markedgedist -label "Mark edge dist: " -integer true \
	    -min 0 -max 999  \
	    -variable meshdoc.markedgedist \
	    -options {
		entry.width 3
		label.width 20
		label.anchor e
	    } \
	    -command {
		Ng_MeshDoctor markedgedist ${meshdoc.markedgedist}
		redraw
	    }
	pack $w.markedgedist
	
	button $w.deledge -text "Delete marked segments" -command {
	    Ng_MeshDoctor deletemarkedsegments
	    redraw
	}
	pack $w.deledge
	
	button $w.close -text "Close" -command { 
	    set meshdoctor.active 0;
	    Ng_MeshDoctor;
	    destroy .meshdoc_dlg 
	}
	pack $w.close -expand yes
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Mesh Doctor"
    }
}



#
#  Quality viewer
#
proc qualityviewdialog { show } {

    set w .qualityview_dlg
    
    if {[winfo exists .qualityview_dlg] == 1} {

	if { $show == 1 } {
	    wm withdraw .qualityview_dlg
	    wm deiconify $w
	    focus $w 
	} {
	    wm withdraw $w
	}
    } {
	toplevel $w
	
	set c $w.c

	canvas $c -relief raised -width 450 -height 300
	pack $w.c -side top -fill x

	set plotFont {Helvetica 18}
	set smallFont {Helvetica 12}

	$c create line 100 250 400 250 -width 2
	$c create line 100 250 100 50 -width 2

	for {set i 0} {$i <= 10} {incr i} {
	    set x [expr {100 + ($i*30)}]
	    $c create line $x 250 $x 245 -width 2
	    if { [expr {$i % 2}] == 0 } {
		$c create text $x 254 -text [expr 0.1*$i] -anchor n -font $plotFont
	    }
	}

	global qualbar
	global qualbarnull
	global qualbaraxis

	for {set i 0} {$i <= 5} {incr i} {
	    set y [expr {250 - ($i*40)}]
	    $c create line 100 $y 105 $y -width 2

	    global qualbaraxis($i)
	    set qualbaraxis($i) \
		[$c create text 96 $y -text [expr $i*50].0 -anchor e -font $plotFont]
	}

	for {set i 0} {$i < 20} {incr i} {
	    set x1 [expr {100 + ($i*15) + 2}]
	    set x2 [expr {$x1+10}]
	    set y [expr {250 - 10 * $i}]
	    global qualbar($i)
	    set qualbar($i) [$c create rectangle $x1 250 $x2 245 -fill blue]
	    set qualbarnull($i) [$c create text [expr {($x1+$x2)/2}] 245 -text 0 -anchor s -font $smallFont -fill blue]	
	}

	frame $w.bu
	pack $w.bu
	# -fill x
	
	button $w.close -text "Close" \
	    -command { 
		wm withdraw .qualityview_dlg
		set viewqualityplot 0
	    }
	pack $w.close
    
	
	if { $show == 1 } {
	    wm withdraw $w
	    wm geom $w +100+100
	    wm deiconify $w
	    wm title $w "Mesh Quality"
	    focus $w
	}
    }
}


#
#  STL INFO dialog
#
proc STLinfodialog { show } {

    set w .STLinfo_dlg
    
    if {[winfo exists .STLinfo_dlg] == 1} {

	if { $show == 1 } {
	    wm withdraw .STLinfo_dlg
	    wm deiconify $w
	    focus $w 
	} {
	    wm withdraw $w
	}
    } {
	toplevel $w
	
	set c $w.c

	canvas $c -relief raised -width 450 -height 300
	pack $w.c -side top -fill x

	set plotFont {Helvetica 18}
	set smallFont {Helvetica 12}

	$c create line 100 250 400 250 -width 2
	$c create line 100 250 100 50 -width 2

	frame $w.bu
	pack $w.bu
	# -fill x
	
	button $w.close -text "Close" \
	    -command { 
		wm withdraw .STLinfo_dlg
		#set STLinfoopen 0
	    }
	pack $w.close
    
	
	if { $show == 1 } {
	    wm withdraw $w
	    wm geom $w +100+100
	    wm deiconify $w
	    wm title $w "STL Geometry Info"
	    focus $w
	}
    }
}





#
#
#   Edit primitive
#
#
proc editprimitivedialog2 { name } {

    global w classname

    set w .ep_dlg
    toplevel .$w

    Ng_GetPrimitiveData $name classname valuelist
        
    
    label $w.lab1 -text "Primitive Name:  $name";
    label $w.lab2 -text "Primitive Class: $classname";
    pack $w.lab1 $w.lab2 -fill x -pady 1m -padx 5m 
    
    frame $w.specific -relief groove

    global spec
    set spec(sphere) { cx cy cz rad }
    set spec(cylinder) { ax ay az bx by bz rad }
    set spec(plane) { px py pz nx ny nz }
    set spec(cone) { ax ay az bx by bz ra rb }
    set spec(brick) { p1x p1y p1z p2x p2y p2z p3x p3y p3z p4x p4y p4z } 
   
    set cnt 0
    foreach field $spec($classname) {

	frame $w.specific.f$cnt 
	pack $w.specific.f$cnt -side top -anchor ne

	label $w.specific.f$cnt.lab -text "$field"
	entry $w.specific.f$cnt.ent -textvariable dataval($cnt) \
	    -width 6 -relief sunken
	pack $w.specific.f$cnt.ent $w.specific.f$cnt.lab -side right
	$w.specific.f$cnt.ent delete 0 end
	$w.specific.f$cnt.ent insert 0 [lindex $valuelist $cnt]
	set cnt [expr $cnt + 1]
    }
    pack $w.specific


    button $w.cancel -text "cancel" -command {
	destroy $w 
    }

    button $w.ok -text "ok" -command {

	set valuelist ""
	set cnt 0
	foreach field $spec($classname) {
	    lappend valuelist $dataval($cnt)
	    set cnt [expr $cnt + 1]
	}
	Ng_SetPrimitiveData $name $valuelist
	destroy $w
    }
    pack  $w.cancel $w.ok -side left -expand yes

    bind $w <Return> { $w.ok  invoke}
    bind $w <Escape> { $w.cancel  invoke}
    

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w

#    grab $w
    focus $w.specific.f0.ent
}



#
#
#      Select primitve to edit
#
#

proc editprimitivedialog { } {
    global w

    set w .ep_dlg
    toplevel $w

    frame $w.frame -borderwidth 5m
    pack $w.frame -side top -expand yes -fill y

    listbox $w.frame.list -yscroll "$w.frame.scroll set" -setgrid 1 -height 12
    scrollbar $w.frame.scroll -command "$w.frame.list yview"
    pack $w.frame.scroll -side right -fill y
    pack $w.frame.list -side left -expand 1 -fill both
    

    Ng_GetPrimitiveList primlist
    foreach el $primlist {
	$w.frame.list insert end $el }

    button $w.cancel -text "cancel" -command { destroy $w }
    button $w.ok -text "ok" -command {
	set name [.ep_dlg.frame.list get active]
	puts "name=($name)"
	destroy $w
	if { $name != "" } { editprimitivedialog2 $name }
    }
    
    bind $w <Escape> { $w.cancel invoke }
    bind $w <Return> { $w.ok invoke }
    

    pack  $w.cancel $w.ok -side left -expand yes

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w

#    grab $w
    focus $w.frame.list
}



#
#
#         Create new primitive
#
#
proc newprimitivedialog { } {

    global w name

    set w .ap_dlg
    
    toplevel $w

    set name ""
    frame $w.f1
    pack $w.f1 -pady 2m
    label $w.f1.lab -text "Primitive Name: ";
    entry $w.f1.ent -width 5 -relief sunken \
	-textvariable name
    pack $w.f1.lab $w.f1.ent -side left
    
    frame $w.frame -borderwidth .5c
    pack $w.frame -side top -expand yes -fill y

    listbox $w.frame.list -yscroll "$w.frame.scroll set" -setgrid 1 -height 8 
    scrollbar $w.frame.scroll -command "$w.frame.list yview"
    pack $w.frame.scroll -side right -fill y
    pack $w.frame.list -side left -expand 1 -fill both
    
    $w.frame.list insert 0 sphere cylinder plane cone brick
    $w.frame.list activate 0
    
    button $w.ok -text "ok" -command {
	Ng_CreatePrimitive [$w.frame.list get active]  $name
	destroy $w
	editprimitivedialog2 $name
    }

    button $w.cancel -text "cancel" -command {
	destroy $w
    }
    
    pack  $w.cancel $w.ok -side left -expand yes -pady 2m


    bind $w <Escape> { $w.cancel invoke }
    bind $w <Return> { $w.ok invoke }

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w

#    grab $w
    focus $w.f1.ent
}






proc newsoliddialog { } {

    global w name val sollist

    set w .ns_dlg
    toplevel $w

    set name ""
    frame $w.f1
    label $w.f1.lab -text "Solid Name: ";
    entry $w.f1.ent -width 5 -relief sunken \
	-textvariable name
    $w.f1.ent delete 0 end
    button $w.f1.getsel -text "Get Selected" -command { 
	$w.f1.ent delete 0 end
	$w.f1.ent insert 0 [$w.f3.list get active]
	$w.bu.get invoke
    }
    pack $w.f1.getsel -side bottom
    pack $w.f1.ent $w.f1.lab -side right


    frame $w.f3 -borderwidth .5c
    listbox $w.f3.list -yscroll "$w.f3.scroll set" -setgrid 1 -height 12
    scrollbar $w.f3.scroll -command "$w.f3.list yview"
    pack $w.f3.scroll -side right -fill y
    pack $w.f3.list -side left -expand 1 -fill both
    
    Ng_GetSolidList sollist
    foreach el $sollist {
	$w.f3.list insert end $el }

    frame $w.f2
    label $w.f2.lab -text "Solid Description: ";
    pack $w.f2.lab


    entry $w.f2.ent -width 100 -relief sunken \
	-textvariable val  -xscrollcommand "$w.f2.scr set"
    scrollbar $w.f2.scr -relief sunken -orient horiz -command \
	"$w.f2.ent xview"
    $w.f2.ent delete 0 end
    pack $w.f2.ent $w.f2.scr -fill x



    frame $w.bu
    button $w.bu.close -text "close" -command {
	destroy $w
    }

    button $w.bu.get -text "get data" -command {
	Ng_GetSolidData $name val
    }

    button $w.bu.set -text "set data" -command {
	Ng_SetSolidData $name $val
    }

    pack $w.bu.get $w.bu.set $w.bu.close -side left 


    pack $w.bu -pady 5 -side bottom                     ;# buttons
    pack $w.f2 -pady 5 -side bottom                     ;# edit field
    pack $w.f1 -pady 5 -side left                       ;# name
    pack $w.f3 -side left -expand yes -fill y           ;# listbox



    bind $w <Escape> { $w.bu.close invoke }

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w

#    grab $w
    focus $w
}







#
#  Edit top level objects
#
#


proc toplevelproperties { w solname surfname } {

    global properties

    Ng_TopLevel getprop $solname $surfname properties


    set w .tlprop_dlg

    if {[winfo exists $w] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w
    
	label $w.lab1 -text "Red"
	scale $w.scale1 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	    -command { Ng_TopLevel setprop $solname $surfname properties; redraw } -variable  properties(red)
	
	label $w.lab2 -text "Green"
	scale $w.scale2 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	-command { Ng_TopLevel setprop $solname $surfname properties; redraw } -variable  properties(green)
	
	label $w.lab3 -text "Blue"
	scale $w.scale3 -orient horizontal -length 300 -from 0 -to 1 \
	    -resolution 0.01  -tickinterval 0.2 \
	    -command { Ng_TopLevel setprop $solname $surfname properties; redraw } -variable  properties(blue)

	
	pack $w.lab1 $w.scale1 $w.lab2 $w.scale2 $w.lab3 $w.scale3

	checkbutton $w.cb4 -text "Visible" \
	    -command { Ng_TopLevel setprop $solname $surfname properties; redraw } \
	    -variable properties(visible)
	
	checkbutton $w.cb5 -text "Transparent" \
	    -command { Ng_TopLevel setprop $solname $surfname properties; redraw } \
	    -variable properties(transp)
	
	
	pack  $w.cb4 $w.cb5
	
	
	frame $w.bu
	pack $w.bu -fill x
	button $w.bu.ok -text "Ok" -command "destroy .tlprop_dlg"
	pack $w.bu.ok  -expand yes
    
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	focus $w
    }
    wm title $w "Properties $solname $surfname"
}






proc topleveldialog { } {

    global w name val sollist

    set w .tl_dlg
    toplevel $w



    frame $w.sol -borderwidth .5c
    listbox $w.sol.list -yscroll "$w.sol.scroll set" -setgrid 1 -height 12
    scrollbar $w.sol.scroll -command "$w.sol.list yview"
    pack $w.sol.scroll -side right -fill y
    pack $w.sol.list -side left -expand 1 -fill both
    
    Ng_GetSolidList sollist
    foreach el $sollist {
	$w.sol.list insert end $el }
    Ng_GetPrimitiveList sollist
    foreach el $sollist {
	$w.sol.list insert end $el }




    frame $w.sul -borderwidth .5c
    listbox $w.sul.list -yscroll "$w.sul.scroll set" -setgrid 1 -height 12
    scrollbar $w.sul.scroll -command "$w.sul.list yview"
    pack $w.sul.scroll -side right -fill y
    pack $w.sul.list -side left -expand 1 -fill both
    
    Ng_GetSurfaceList sollist
    foreach el $sollist {
	$w.sul.list insert end $el }





    frame $w.topl -borderwidth .5c
    listbox $w.topl.list -yscroll "$w.topl.scroll set" -setgrid 1 -height 12 \
	-command { puts hi }
    scrollbar $w.topl.scroll -command "$w.topl.list yview"
    pack $w.topl.scroll -side right -fill y
    pack $w.topl.list -side left -expand 1 -fill both
    
    Ng_TopLevel getlist sollist
    puts $sollist
    foreach el $sollist {
	set hel "[ lindex $el 0 ]"
	if { [ llength $el ] == 2 } {
	    set hel "[ lindex $el 1 ] on [ lindex $el 0 ]"
	}
	$w.topl.list insert end $hel 
    }


    frame $w.bu

    button $w.bu.close -text "close" -command {
	destroy $w
    }
    button $w.bu.addsol -text "Add Solid" -command {
	set solname [$w.sol.list get active]
	Ng_TopLevel set $solname ""
	Ng_ParseGeometry
	$w.topl.list insert end $solname
    }

    button $w.bu.addsurf -text "Add Surface" -command {
	set solname [$w.sol.list get active]
	set surfname [$w.sul.list get active]
	Ng_TopLevel set $solname $surfname
	Ng_ParseGeometry
	puts "$solname on $surfname"
	$w.topl.list insert end "$surfname on $solname"
    }

    button $w.bu.remsol -text "Remove" -command {
	set solname [$w.topl.list get active]
	set surfname ""
	if { [llength $solname] == 3 } {
	    set surfname [lindex $solname 0]
	    set solname [lindex $solname 2]
	}
	Ng_TopLevel remove $solname $surfname
	Ng_ParseGeometry
	$w.topl.list delete active
    }

    button $w.bu.prop -text "Properties" -command {
	set solname [$w.topl.list get active]
	set surfname ""
	if { [llength $solname] == 3 } {
	    set surfname [lindex $solname 0]
	    set solname [lindex $solname 2]
	}
	toplevelproperties tlp $solname $surfname
    }


    

    pack  $w.bu.close $w.bu.addsol $w.bu.addsurf $w.bu.remsol $w.bu.prop  -side left 


    pack $w.bu -side bottom
    pack $w.sol -side left -expand yes -fill y           ;# listbox
    pack $w.sul -side left -expand yes -fill y           ;# listbox
    pack $w.topl -side left -expand yes -fill y           ;# listbox


    bind $w <Escape> { $w.bu.close invoke }

    wm withdraw $w
    wm geom $w +100+100
    wm deiconify $w

#    grab $w
    focus $w
}






proc topleveldialog2 { } {
    set w .tl2_dlg
    
    if {[winfo exists .tl2_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w

	global name val sollist

	frame $w.topl -borderwidth .5c
	listbox $w.topl.list -yscroll "$w.topl.scroll set" -setgrid 1 -height 12
	scrollbar $w.topl.scroll -command "$w.topl.list yview"
	pack $w.topl.scroll -side right -fill y
	pack $w.topl.list -side left -expand 1 -fill both
	
	Ng_TopLevel getlist sollist
	puts $sollist
	foreach el $sollist {
	    set hel "[ lindex $el 0 ]"
	    if { [ llength $el ] == 2 } {
		set hel "[ lindex $el 1 ] on [ lindex $el 0 ]"
	    }
	    $w.topl.list insert end $hel }
	
	
	frame $w.bu
	
	button $w.bu.close -text "close" -command {
	    destroy .tl2_dlg
	}
	

	button $w.bu.prop -text "Properties" -command {
	    set solname [.tl2_dlg.topl.list get active]
	    set surfname ""
	    if { [llength $solname] == 3 } {
		set surfname [lindex $solname 0]
		set solname [lindex $solname 2]
	    }
	    toplevelproperties tlp $solname $surfname
	}
	
	pack $w.bu.close $w.bu.prop  -side left 
	pack $w.bu -side bottom
	pack $w.topl -side left -expand yes -fill y           ;# listbox
		
	bind .tl2_dlg.topl.list <Double-1> {
	    set solname [.tl2_dlg.topl.list get  @%x,%y]
	    set surfname ""
	    if { [llength $solname] == 3 } {
		set surfname [lindex $solname 0]
		set solname [lindex $solname 2]
	    }
	    toplevelproperties tlp $solname $surfname
	}
	
	bind .tl2_dlg <Escape> { .tl2_dlg.bu.close invoke }
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Top-Level Options"	
	focus $w
    }
}






proc logwindow { } {
    set w .logwindow
    
    if {[winfo exists .logwindow] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {
	toplevel $w

	text $w.edit -yscroll "$w.scrolly set" -setgrid 1 -height 12
	scrollbar $w.scrolly -command "$w.edit yview"	
	pack $w.edit -side left -fill both -expand 1
	pack $w.scrolly -side left -fill both -expand 0

	.logwindow.edit insert end "Netgen Log Window\n"

	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "Netgen Log"	
	focus $w
    }
}
# logwindow


set entities [ ]


proc occdialogbuildtree {} {
    global entities

    set w .occ_dlg
    set hlist [$w.mtre subwidget hlist]

    set entities [Ng_GetOCCData getentities]
    set nrentities [expr [llength $entities]]


    if {$nrentities != 0} {

	$hlist add Topology -itemtype text -text "Topology"
	
	$hlist add Topology/CompSolids   -itemtype text -text "Composite Solids" -data "Composite Solids"
	$hlist add Topology/FreeSolids   -itemtype text -text "Free Solids" -data "Free Solids"
	$hlist add Topology/FreeShells   -itemtype text -text "Free Shells" -data "Free Shells"
	$hlist add Topology/FreeFaces    -itemtype text -text "Free Faces" -data "Free Faces"
	$hlist add Topology/FreeWires    -itemtype text -text "Free Wires" -data "Free Wires"
	$hlist add Topology/FreeEdges    -itemtype text -text "Free Edges" -data "Free Edges"
	$hlist add Topology/FreeVertices -itemtype text -text "Free Vertices" -data "Free Vertices"
	
	set i [expr 0]
	while {$i < $nrentities} {
	    set entity [lindex $entities [expr $i]]
	    incr i 1
	    set entityname [lindex $entities [expr $i]]
	    $hlist add Topology/$entity -text $entityname -data $entityname
	    incr i 1
	    $w.mtre close Topology/$entity
	}
	
	$w.mtre autosetmode
	
	$w.mtre open Topology
	$w.mtre close Topology/CompSolids
	$w.mtre close Topology/FreeSolids
	$w.mtre close Topology/FreeShells
	$w.mtre close Topology/FreeFaces
	$w.mtre close Topology/FreeWires
	$w.mtre close Topology/FreeEdges
	$w.mtre close Topology/FreeVertices
	
	set i [expr 0]
	while {$i < $nrentities} {
	    set entity [lindex $entities [expr $i]]
	    $w.mtre close Topology/$entity
	    incr i 2
	}
	
	set faces [Ng_OCCCommand getunmeshedfaceinfo]    
	set nrfaces [expr [llength $faces]]
	if {$nrfaces >= 2} {
	    $hlist add ErrorFaces -itemtype text -text "Faces with surface meshing error"
	    $w.mtre open ErrorFaces
	    set i [expr 0]
	    while {$i < $nrfaces} {
		set entity [lindex $faces [expr $i]]
		incr i 1
		set entityname [lindex $faces [expr $i]]
		$hlist add ErrorFaces/$entity -text $entityname -data $entityname
		incr i 1
	    }
	}
	

	set faces [Ng_OCCCommand getnotdrawablefaces]    
	set nrfaces [expr [llength $faces]]
	if {$nrfaces >= 2} {
	    $hlist add NotDrawableFaces -itemtype text -text "Faces impossible to visualize"
	    $w.mtre open NotDrawableFaces
	    set i [expr 0]
	    while {$i < $nrfaces} {
		set entity [lindex $faces [expr $i]]
		incr i 1
		set entityname [lindex $faces [expr $i]]
		$hlist add NotDrawableFaces/$entity -text $entityname -data $entityname
		incr i 1
	    }
	}


	$w.mtre autosetmode

	puts "done"
    }
}


proc rebuildoccdialog {} {
    if {[winfo exists .occ_dlg] == 1} {
	[.occ_dlg.mtre subwidget hlist] delete all
	occdialogbuildtree 
    }
}

proc checkoccloaded { } {
    set isoccgeometryloaded [Ng_OCCCommand isoccgeometryloaded]
    if {$isoccgeometryloaded == 0} {
	puts "no IGES/STEP geometry loaded"
	destroy .occ_dlg
    }
}

#proc setocctolerance { } {
#    set w .setocctolerance
#}


proc selectentity { entityname } {
    global entities
    set nrentities [expr [llength $entities]]
    set i [expr 0]
    while {$i < $nrentities} {
	set entitylength []
			
	set entity2 [lindex $entities [expr $i]]
	incr i 1
	set entityname2 [lindex $entities [expr $i]]
	incr i 1
	set entityname2 [string range $entityname2 0 [expr [string length $entityname]-1]]
	
	if {$entityname == $entityname2} {
	    set hlist [.occ_dlg.mtre subwidget hlist]
	    .occ_dlg.mtre open Topology
	    set slashpos [string last "/" $entity2]
	    set entity3 [string range $entity2 0 [expr $slashpos-1]]
	    while {$slashpos != -1} {
		.occ_dlg.mtre open Topology/$entity3
		
		set slashpos [string last "/" $entity3]
		set entity3 [string range $entity3 0 [expr $slashpos-1]]
	    }
	    $hlist selection clear
	    $hlist see Topology/$entity2
	    $hlist selection set Topology/$entity2
	} 
    }	    
}



proc occdialog { } {
	
    uplevel 1 {

    global entities
    set selectvisual geometry
    Ng_SetVisParameters
    redraw

    set w .occ_dlg
    
    if {[winfo exists .occ_dlg] == 1} {
	wm withdraw $w
	wm deiconify $w
	focus $w 
    } {	
	toplevel $w

	tixTree $w.mtre -options { separator "/" }
	pack $w.mtre -fill both -expand yes

	occdialogbuildtree

	set hlist [$w.mtre subwidget hlist]


	set solname {""}

	
	bind $hlist <Double-1> {
	    set oldsolname {$solname}
	    set solname [[.occ_dlg.mtre subwidget hlist] info selection]
	    if {$solname != "" && $oldsolname != $solname } {
		set seppos [string first "/" $solname]
		set rootname [string range $solname 0 [expr $seppos-1]]

		set entityname [[.occ_dlg.mtre subwidget hlist] info data $solname]
		set spacepos [string first " " $entityname]
		set entitytype [string range $entityname 0 [expr $spacepos-1]]
		set helpstring [string range $entityname [expr $spacepos+1] [expr [string length $entityname]-1]]
		set spacepos2 [string first " " $helpstring]
		set entitynumber [string range $helpstring 0 [expr $spacepos2-1]]
		if {$rootname == "Topology"} {
		    Ng_OCCCommand highlightentity $entitytype $entitynumber
		    set selectvisual geometry
		    redraw
		} {
		    set brackpos [string first " (" $entityname]
		    if {$brackpos != -1} {
			set entityname [string range $entityname 0 $brackpos]
		    }

		    selectentity $entityname
		}
	    }
	}
	
	button $w.cl -text "Close" -command {
	    destroy .occ_dlg
	}
	
	button $w.swaporientation -text "Swap orientation" -command {
	    set solname [[.occ_dlg.mtre subwidget hlist] info selection]
	    set entityname [[.occ_dlg.mtre subwidget hlist] info data $solname]
	    set spacepos [string first " " $entityname]
	    set entitytype [string range $entityname 0 [expr $spacepos-1]]
	    set helpstring [string range $entityname [expr $spacepos+1] [expr [string length $entityname]-1]]
	    set spacepos2 [string first " " $helpstring]
	    set entitynumber [string range $helpstring 0 [expr $spacepos2-1]]

	    Ng_OCCCommand swaporientation $entitytype $entitynumber
	    set selectvisual geometry
#	    Ng_SetVisParameters
	    redraw

	    [.occ_dlg.mtre subwidget hlist] delete all
	    occdialogbuildtree	
	}

	button $w.checkentities -text "Analyze geometry" -command {
	    set irregent [Ng_OCCCommand findsmallentities]

	    set w .occ_dlg
	    set hlist [$w.mtre subwidget hlist]
	    
	    $hlist add ProblematicEntities -text "Problematic Entities"
	    $hlist delete offsprings ProblematicEntities

	    set nritems [expr [llength $irregent]]
	    set i [expr 0]
	    while {$i < $nritems} {
		set entity [lindex $irregent [expr $i]]
		incr i 1
		set entityname [lindex $irregent [expr $i]]
		$hlist add ProblematicEntities/$entity -text $entityname -data $entityname
		incr i 1
	    }
	    $w.mtre open ProblematicEntities
	    $w.mtre autosetmode
	}

	checkbutton $w.zoomtohighlightedentity -text "Zoom to highlighted entity" \
	    -variable occoptions.zoomtohighlightedentity \
	    -command {
		Ng_SetOCCVisParameters
		if { ${occoptions.zoomtohighlightedentity} == 1} {
		    set selectvisual geometry
#		    Ng_SetVisParameters
		    Ng_OCCCommand redrawstatus 1
		    redraw
		} {
		    Ng_OCCCommand redrawstatus 0
		}
	    }



	frame $w.healing -relief groove -borderwidth 3

	tixControl $w.healing.tolerance -label "Healing tolerance: " -integer false \
	    -variable occoptions.tolerance -min 1e-9 -max 1e6 \
	    -options {
		entry.width 6
		label.width 25
		label.anchor e
	    }	

	checkbutton $w.healing.fixsmalledges -text "Fix small edges" \
	    -variable occoptions.fixsmalledges
	
	checkbutton $w.healing.fixspotstripfaces -text "Fix spot/strip faces" \
	    -variable occoptions.fixspotstripfaces
	
	checkbutton $w.healing.sewfaces -text "Sew faces" \
	    -variable occoptions.sewfaces
	
	checkbutton $w.healing.makesolids -text "Make solids" \
	    -variable occoptions.makesolids
	
	button $w.healing.heal -text "Heal geometry" -command { 
	    .occ_dlg.healing.tolerance invoke
	    Ng_OCCCommand shapehealing
	    redraw 
	    [.occ_dlg.mtre subwidget hlist] delete all
	    occdialogbuildtree
	}

	pack $w.healing.tolerance $w.healing.fixsmalledges \
	    $w.healing.fixspotstripfaces $w.healing.sewfaces \
	    $w.healing.makesolids -anchor w

	pack $w.healing.heal	





	pack $w.zoomtohighlightedentity -anchor w
	pack $w.checkentities
	pack $w.swaporientation
	pack $w.healing -fill x
	pack $w.cl
 
	
	wm withdraw $w
	wm geom $w +100+100
	wm deiconify $w
	wm title $w "IGES/STEP Topology Explorer/Doctor"
	focus .occ_dlg
    }
}
}
