
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
# All the menu-specific stuff, logos, etc go in this file.  This makes it
# easier to create flavored versions of med with different structures.


# Toolbars -----------
after 500 {.v.top.logo.2 configure -text ""}
command_make_toolbar "File commands" 1 {
    "Open project"
    "Save project"
    "Print screen"
}

command_make_toolbar "Editing commands" 1 {
    "Undo operation"
    "Redo operation"
}

command_make_toolbar "Orientation commands" 1 {
    "Home position"
    "Zoom in"
    "Scale to fit"
    { cascade ""
    "Orient positive X" 
    "Orient negative X" 
    "Orient positive Y" 
    "Orient negative Y" 
    "Orient positive Z" 
    "Orient negative Z" 
    "Isometric view" 
    "Reverse orientation" 
    }
}

command_make_toolbar "Tool commands" 1 {
    "Location" 
    "Distance" 
    "Coord systems" 
}

command_make_toolbar "Geometry commands" 2 {
         "CAD functions"
    { cascade data_available(geom)
	"Create topology"
	"Fix gaps (CAD)"
	"Create surface from convex hull"
	"Create midsurface"
    }
    "Change geometry family"
    { cascade data_available(geom)
	"Model mesh params"
	"All family mesh params"
	"Entity mesh params"
	"Curves mesh params"
	"Create material via screen midpoint"
    }
    { cascade data_available(geom)
	"Bound conds2" 	
	"Bolt-holes"
	"Seam-welds"
	"Spot-welds"
    }
}

command_make_toolbar "Geometry surf edit commands" -2 {
     { cascade data_available(geom)
    	"Segment surface"
    	"Segment curve"
    	"Merge surfaces"
    	"Untrim surface"
    	"Rename surface"
    	"Convert surface to facets"
   	"Coarsen surface"
      }

    { cascade data_available(geom)
	"Delete surface triangles"
      	"Create surface triangles"
      	"Move surface triangles to existing surface"
	"Move surface triangles to new surface"
	"Restrict surface triangles"
    }
    { cascade data_available(geom)
    	"Split edges"
    	"Swap edges"
    	"Split surface triangles"
	"Merge surface nodes interactive"
	"Merge surface nodes by tolerance"
	"Move surface nodes to location"
	"Move surface nodes on screen"
	"Move surface nodes on plane"
	"Move surface nodes on line"
	"Move surface nodes in direction"
    }
}

command_make_toolbar "Meshing commands" 2 { 
    { cascade data_available(geom)
	"Mesh (Quad)"
	"Mesh (Triangle)" 
	"Mesh (Tetra)" 
	"Mesh (Rubber band)" 
    }
    { cascade data_available(unstruct)
    	  "Unstructured quality metrics"
	  "Smooth elements"
    }
    "Repair unstructured mesh"
    "Check mesh"
}

command_make_toolbar "Mesh Editing commands" -2 { 
   { cascade data_available(unstruct)
	  "1 Quad -> 2 Tri" 
	  "Tri -> Quad" 
	  "Coarsen elements" 
	  "Refine elements"  
   }
   "Create elements" 
   "Delete selected elements" 
    "Change element family"
   { cascade data_available(unstruct)
	"Split selected edges" 
	"Swap edges (try harder)" 
	"Split selected elements" 
   }
   { cascade data_available(unstruct)
    	"Move unstruct node (invert)" 
    	"Move unstruct node (exact)" 
   }
   { cascade data_available(unstruct)
   	"Merge unstruct nodes (interactive" 
   	"Merge unstruct nodes (selected, tolerance)" 
   }
   { cascade data_available(unstruct)
	"Project unstruct nodes to surfaces"
	"Project unstruct nodes to curves" 
	"Project unstruct nodes to points" 
	"Unproject unstruct nodes" 
   }
}

command_make_toolbar "Solver commands" 2 { 
	"Write nastran"
	"Edit nastran"
	"Run nastran"
}


# Key bindings ---------
command_set_hotkeys {
    F1 "Help"
    z "Zoom in"
    o "Open project"
}


# Pulldown menus ---------

command_make_menu "Help" "" "right button" {}

command_make_menu "File" "" left {
    { "Open" "" cascade {
        { "Open project" }
        { "Load tetin file" }
        { "Load domain file" }
        { "Load family_boco file" }
    } }
    { "Save" "" cascade {
        { "Save project" }
        { "Save tetin file" }
        { "Save domain file" }
        { "Save family_boco file" }
    } }
    { "Save as" "" cascade {
        { "Save project as" }
        { "Save tetin file as" }
        { "Save domain file as" }
        { "Save family_boco file as" }
    } }
    { "Close" "" cascade {
        { "Close project" }
        { "Unload tetin file" }
        { "Unload domain file" }
        { "Unload family_boco file" }
    } }
    { "Delete" "" cascade {
        { "Delete project" }
        { "Delete tetin file" }
        { "Delete domain file" }
        { "Delete family_boco file" }
    } }
    { "Quit" }
    separator
    { "Import geometry" "" cascade {
        { "Import domain file as geometry" }
        { "Import Plot3d as geometry" }
        { "Import Patran as geometry" }
        { "Import STL as geometry" }
        { "Import Fluent as geometry" }
        { "Import Rampant as geometry" }
        { "Import VRML as geometry" }
        { "Import CATIA4 as geometry" }
        { "Import IGES" }
        { "Import DDN" }
        { "Import UG" } 
    } }
    { "Export geometry" "" cascade {
        { "Convert Tetin to IGES" }
        { "Convert Tetin to DDN" }
    } }
    { "Import mesh" "" cascade {
	{ "Import Plot3d as mesh" }
        { "Import STL as mesh" }
        { "Import Ansys as mesh" }
        { "Import Patran as mesh" }
        { "Import Nastran as mesh" }
        { "Import Fluent as mesh" }
        { "Import Starcd as mesh" }
    } }
    { "Export mesh" "" cascade {
 	 { "Select solver" }
   	 { "Solver params" }
   	 { "Solver input" }
   	 { "Start solver" }
    } }
     separator
    { "Print screen" }
    { "Text Editor" }
    { "Shell window in current dir" }
    { "Project Utilities" "" cascade {
    	{ "Convert directory style" }
	{ "Copy project" }
	{ "Rename project" }
	{ "Archive project" }
	{ "Un-archive project" }
    } }
    { "Check system info" }
    { "Configuration" "" cascade {
	    { "Edit pathnames" }
 	    { "Change ICEM_ACN" }
	    { "Clear file locks" }
	    { "License" "" cascade {
		{ "Start license server" }
		{ "Reconnect to license server" }
		{ "Check license usage" }
 	   } }
    } }
    { "Play game" }
}

command_make_menu "Edit" "" left {
    { "Undo operation" }
    { "Redo operation" }
    separator
    { "Edit toolbars" }
    { "Edit options" }
    { "Mouse bindings" }
    { "Coord systems" }
    separator
    { "Transfer facets -> mesh" }
    { "Transfer mesh -> facets" }
}

command_make_menu "View" "" left {
    { "Orient" "" cascade {
	{ "Home position" }
	{ "Zoom in" }
	{ "Scale to fit" }
	{ "Orient positive X" }
	{ "Orient negative X" }
	{ "Orient positive Y" }
	{ "Orient negative Y" }
	{ "Orient positive Z" }
	{ "Orient negative Z" }
	{ "Isometric view" }
	{ "Reverse orientation" }
    } }
    { "Set background" }
    separator
    { "Coord axes" }
    { "Origin marker" }
}

command_make_menu "Geometry" {data_available(geom)} left {
    { "Create" "" cascade {
    	{ "Create surface" "" cascade {
		{"From 2 curves"}
		{ "Create surface from 4 points" }
		{ "Create surface from 8 points (box)" }
		{ "Create midsurface" }
		{ "Create surface from convex hull" }
    	}}
    	{"Create curve" "" cascade {
		{ "Extract curve from surface" }
		{ "Intersect surfaces" }
		{ "Create curve from points" }
    	}}
    	{"Create point" "" cascade {
		{ "Extract points from curve" }
		{ "Create points via screen select" }
		{ "Create points via screen midpoint" }
		{ "Create points via explicit location" }
     	}}
    }}
    { "Repair" "" cascade {
	{ "Create topology" }
	{ "Fix gaps (CAD)" }
    } } 
    { "Edit Geometry" "" cascade {
	{ "Modify surfaces" "" cascade {
 	    { "Segment surface" }		
             { "Untrim surface" }
	    { "Rename surface" }
	    { "Convert surface to facets" }
	    { "Merge surfaces" }
	    { "Coarsen surface" }
	    { "Edit triangles" "" cascade {
		{ "Create surface triangles" }
		{ "Delete surface triangles" }
		{ "Restrict surface triangles" }
		{ "Split surface triangles" }
		{ "Move surface triangles to existing surface" }
		{ "Move surface triangles to new surface" }
	    } }
	    { "Edit edges" "" cascade {
		{ "Split edges" }
		{ "Swap edges" }
	    } }
	    { "Move nodes" "" cascade {
		{ "Move surface nodes to location" }
		{ "Move surface nodes on screen" }
		{ "Move surface nodes on plane" }
		{ "Move surface nodes on line" }
		{ "Move surface nodes in direction" }
	    } }
	    { "Merge nodes" "" cascade {
		{ "Merge surface nodes interactive" }
		{ "Merge surface nodes by tolerance" }
	    } }
	} }
	{ "Modify curves" "" cascade {
	    { "Segment curve" }
	    { "Rename curve" }
	    { "Convert curve to facets" }
	    { "Merge curves" }
	    { "Reapproximate curve" }
	    { "Edit segments" "" cascade {
		{ "Create curve segments" }
		{ "Delete curve segments" }
		{ "Restrict curve segments" }
		{ "Split curve segments" }
		{ "Move to other curve" "" cascade {
		    { "Move curve segments to existing" }
		    { "Move curve segments to new" }
		} }
	    } }
	    { "Move nodes" "" cascade {
		{ "Move curve nodes to location" }
		{ "Move curve nodes on screen" }
		{ "Move curve nodes on surfaces" }
		{ "Move curve nodes on plane" }
		{ "Move curve nodes on line" }
		{ "Move curve nodes in direction" }
	    } }
	    { "Merge curve nodes" }
	} }
	{ "Modify points" "" cascade {
	    { "Rename point" }
	    { "New point position" }
	    { "Screen move point" }
	    { "Merge points" }
	} }
    } }
    { "Delete geometry" data_available(geom) cascade {
	{ "Delete selected geometry" }
	{ "Delete last created objects" }
	{ "Delete duplicate facets" }
	{ "Delete unattached geometry" }
	{ "Delete all visible objects" }
	{ "Delete all blanked objects" }
    } }
    { "Meshing setup" "" cascade {
	{ "Model mesh params" }
	{ "Family mesh params" }
	{ "Entity mesh params" }
	separator
	{ "Loops" "" cascade {
		{ "Create loop via screen select" }
		{ "Create loop from surfaces" }
		{ "Create loop from each surface" }
    	} }
	{"Tetra" "" cascade {   
		{"Material point" "" cascade {
			{ "Create material via screen select" }
			{ "Create material via screen midpoint" }
			{ "Create material via explicit location" }
			separator
	    		{ "Rename material" }
	    		{ "New material position" }
	    		{ "Screen move material" }
		} }
		{ "Density box" "" cascade {
			{ "Create density box"}
	    		{ "Rename density" }
	    		{ "Move density corner" }
		} }
	}}
    } }
    { "Change geometry family" }
    { "Copy/move geometry" "" cascade {
	{ "Copy/move whole model" }
	{ "Copy/move families" }
	{ "Copy/move selected entities" }
    } }
}

command_make_menu "Mesh" {data_available(geom)} left {
    { "Create mesh" "" cascade {
 	   { "Hexa" "" cascade {
		{ "Hexa interactive" }
		{ "Hexa batch" }
	    } }
	    { "Tetra" "" cascade {
		{ "Tetra batch from CAD" }
		{ "Tetra interactive from CAD" }
		{ "Tetra from surface mesh" }
	    } }
 	   { "Prism" }
 	   { "Quad" }
 	   { "AutoHexa" }
    } }
    { "Diagnostics" data_available(mesh) variable_cascade data_available(unstruct) {
	{ 0 "Structured diagnostics" {
	    { "Structured quality metrics" }
	    { "Domain orientation" }
	    { "Structured summary info" }
    	    { "Full topo info" }
	} }
	{ 1 "Diagnostics" {
	    { "Information" "" cascade {
		{ "Unstructured summary info" }
		{ "Unstructured element info" }
		{ "Unstructured node info" }
		{ "Unstructured domain file info" }
		{ "Max vertex degree" }
	    } }
	    { "Which elements" "" cascade {
		{ "Diagnostic elements: All" }
		{ "Diagnostic elements: Active" }
		{ "Diagnostic elements: Subset" }
	    } }
	    { "Unstructured quality metrics" }
	    { "Errors" "" cascade {
		{ "Duplicate elements" }
		{ "Volume orientation" }
		{ "Surface orientation" }
		{ "Uncovered faces" }
		{ "Missing internal faces" }
		{ "Periodic problems" }
	    } }
	    { "Possible problems" "" cascade {
		{ "Overlapping elements" }
		{ "Delaunay violations" }
		{ "Single edges" }
		{ "2 single edges" }
		{ "Multiple edges" }
		{ "Single/multiple edges" }
		separator
		{ "Triangle boxes" }
		{ "Non-manifold vertices" }
		{ "Stand-alone surface mesh" }
		{ "Family boundary edges" }
		{ "Check edges" }
		separator
		{ "Disconnected vertices" }
		{ "Spanning edges" }
	    } }
	} }
    } }
    { "Smooth elements" }
    { "Change element family" }
    { "Copy/move mesh" }
    separator
    { "Mesh directives" data_available(geom) cascade {
	{ "Bolt-holes" }
	{ "Connectors" }
	{ "Seam-welds" }
	{ "Spot-welds" }
    } }
    { "Edit Mesh" data_available(mesh) cascade {
  	  { "Repair unstructured mesh" data_available(mesh) }
	  { "Edit nodes" data_available(mesh)  {
		{ "Move" "" cascade {
			{ "Move unstruct node (invert)" }
			{ "Move unstruct node (no invert)" }
			{ "Move unstruct node (exact)" }
		} }
		{ "Merge" "" cascade {
			{ "Merge unstruct nodes (interactive" }
			{ "Merge unstruct nodes (all, tolerance)" }
			{ "Merge unstruct nodes (selected, tolerance)" }
			{ "Merge unstruct nodes (visible, tolerance)" }
		} }
		{ "Delete unstruct nodes" }
		{ "Project" "" cascade {
			{ "Project unstruct nodes to surfaces" }
			{ "Project unstruct nodes to curves" }
			{ "Project unstruct nodes to points" }
			{ "Project unstruct nodes to current type" }
			{ "Unproject unstruct nodes" }
			{ "Project unstruct nodes automatic" }
			{ "Project unstruct nodes from surves to surfaces" }
		} }
		{ "Split" "" cascade {
			{ "Split nodes manifold" }
			{ "Split nodes non-manifold" }
		} }
		{ "Set projection mode" "" cascade {
			{ "Projection mode: active families" }
			{ "Projection mode: all families" }
			{ "Projection mode: no projection" }
		} }
		{ "Periodic" "" cascade {
			{ "Make nodes periodic" }
			{ "Make nodes not periodic" }
		} }
	    } }
	    { "Edit elements" "data_available(unstruct)" cascade {
		{ "Create elements" }
		{ "Delete" "" cascade {
		    { "Delete selected elements" }
		    { "Delete all blanked elements" }
		} }
		{ "Change element orientation" }
		{ "Split" "" cascade {
		    { "Split selected elements" }
		    { "Split internal faces" }
		    { "Split double walls" }
		    { "Split prisms" }
		} }
		{ "Couplings" "" cascade {
		    { "Create couplings" }
		    { "Clear couplings" }
		} }
		{ "Renumber elements" }
	    } }
	    { "Edit edges" "data_available(unstruct)" cascade {
		{ "Split" "" cascade {
		    { "Split selected edges" }
		    { "Split spanning edges" }
		} }
		{ "Swap" "" cascade {
		    { "Swap edges (simple)" }
		    { "Swap edges (try harder)" }
		    { "Swap edges (automatic)" }
		} }
	    } }
    } }

    { "Refine elements" }
    { "Coarsen elements" }
    { ignore "Change type" data_available(mesh) cascade {
	{ "Prisms -> Tetras" }
	{ "Linear -> Quadratic" }
	{ "Quadratic -> Linear" }
	{ "1 Quad -> 2 Tri" }
	{ "Tri -> Quad" }
	{ "12 Tetra -> 1 Hexa" }
	{ "1 Tetra -> 4 Hexa" }
	{ "1 Prism -> 3 Tetra" }
	{ "All -> Tetra" }
    } }
    { "Merge unstructured meshes" }
    { "Extrude elements" }
    { "Uncouple elements" }
    { "Optimize bandwidth" }
    { "Utilities" data_available(mesh) cascade {
	{ "Edit properties" }
	{ "Make consistent" }
	{ "Check edges" }
    } }
}

command_make_menu "Tools" "" left {
    { "Distance" }
    { "Location" }
    { "Bounding box" } 
    { "Frontal area" }
    separator
    { "Visual3" }
    separator
    { "Bound conds" }
    { "Manage coord. systems" }
    separator
    { "Scripting" "" cascade {
        { "Run script" }
        { "Replay mode" }
        { "View programmer's guide" }
    } }
    separator
    { "Add marker" }
    { "Clear markers" }
    { "Annotations" }
}

command_define "CAD functions" "CAD functions" ccad_icon \
     med_ezcad {
    } no_help always

command_define "Mesh (Tetra)" "Tetra batch from CAD" tetra_mesh_icon \
    "mesher_tetra" {
    } no_help data_available(geom)

command_define "Mesh (Triangle)" "Tri batch from CAD" tri_mesh_icon \
    "mesher_triangle" {
    } no_help data_available(geom)

#command_define "Mesh (Quad)" "Create Paved Mesh (Tri -> Quad)" quad_mesh_icon \
#    "mb_mesh_tri_quad" {
#    } no_help data_available(mesh)

command_define "Mesh (Rubber band)" "Rubber banding Mesh (Quad)" Hexa_mesher_icon \
    "mb_mesher_hexa" {
    } no_help data_available(geom)

command_define "Mesh (Quad)" "Create Paved Mesh (Quad)" quad_mesh_icon \
    "mesher_quad_ai" {
    } no_help data_available(geom)

command_define "Create density box" "Create density box" density \
    "geo_create_density_screen" {
    } no_help data_available(geom)

command_define "Bound conds2" "Bound conds" bc_flag \
    "geo_famboco_edit" {
    } no_help always

#functions that should be in commands_med.tcl
command_define "Curtain surface" "Curtain surface" surf_untrim_icon \
    "geo_create_surface_curtain" {
    } no_help data_available(geom)

command_define "Project to surface" "Project to surface" surf_untrim_icon \
    "geo_create_curve_proj_to_surface" {
    } no_help data_available(geom)

command_define "Split folded surface" "Split folded surface" surf_untrim_icon \
    "geo_split_folded_surface" {
    } no_help data_available(geom)


command_define "Bolt-holes" "Bolt-holes" bc_spider \
    "unstruct_meshdirect_boltspiders" {
    } no_help data_available(geom)
	 
command_define "Connectors" "Connectors" surf_untrim_icon \
    "unstruct_meshdirect_connectors" {
    } no_help data_available(geom)

command_define "Seam-welds" "Seam-welds" bc_seam \
    "unstruct_meshdirect_seamwelds" {
    } no_help data_available(geom)

command_define "Spot-welds" "Spot-welds" bc_spot \
   "unstruct_meshdirect_spotwelds" {
    } no_help data_available(geom)

#functions that should be in commands_med.tcl

#funcs for Nastran solver
command_define "Write nastran" "Write nastran" ansys_listing32x32 \
   "solution_input" {
    } no_help always

command_define "Edit nastran" "Edit nastran" ansys_editor16x16 \
   "notepad " {
    } no_help always

command_define "Run nastran" "Run nastran" ansys_calculator16x16 \
   "solve_nastran" {
    } no_help always


proc run_nastran {} {
global nastran_file 
if ![info exists nastran_file]  {set nastran_file [pwd] }
ic_mess "nastran file is $nastran_file\n"
   set buttons [list [list Solve   "solve_nastran"] \
		  dismiss]

    set d [ form_init .runnastran "Run Nastran" "" $buttons ]
    if { $d != "" } {
        	form_label $d.name "Nastran file" "0 0"
	form_entry $d.file nastran_file string "0 1"
      form_finish bot
    }
}

proc solve_nastran { } {
#global nastran_file
#ic_mess "file is $nastran_file\n"
#destroy .runnastran
global env
#set nastran $env(NAS)\ns01.exe 
#set nfile c:/users/ben/ansys/Catia/Xmember/model/nastran_input.dat
#ic_mess "nastran is $nastran\n"
#ic_mess "nfile is $nfile\n"
#regsub -all \\\\ $nastran / nastran
#regsub -all \\\\ $nfile / nfile
#ic_mess "nastran is $nastran\n"
#ic_mess "nfile is $nfile\n"
#set cmd [list $nastran $nfile ]
set c1 c:\\\\SAS\\\\ns01.exe 
set c2 c:\\\\users\\\\ben\\\\ansys\\\\Catia\\\\Xmember\\\\model\\\\nastran_input.dat
ic_run_application_exec . "" $c1 $c2

set c3 c:\\\\icemcfd\\\\aienvironment\\\\postop2.exe
set args "nastran_input.op2 result_dom"
ic_run_application_exec . "" $c3 $args 
set c4 c:\\\\icemcfd\\\\visual3_2\\\\visual3p\\\\bin\\\\visual3p
#ic_run_application_exec . "" $c4 ""
#c:\\icemcfd\\aienvironment\\postop2.exe nastran_input.op2 nastran_results
#c:\\icemcfd\\visual3_2\\visual3p\\bin\\visual3p


}
#funcs for Nastran solver
global ezcad_is_active
set ezcad_is_active 0

proc mb_mesher_hexa {} {
global env

if [info exists env(ICEM_USE_MENUBAR)] {unset env(ICEM_USE_MENUBAR)}
mesher_hexa
}


proc mesher_quad_ai {} {
    global quad

    set buttons { { Mesh mesher_quad_mesh_ai } \
                  { Reset mesher_quad_reset } { Done mesher_quad_done } }

    set quad(ele_active) [ic_uns_is_loaded]
    set quad(geo_active) [ic_geo_is_loaded]
    set quad(geo_or_ele_active) [expr $quad(ele_active) || $quad(geo_active)]
    # set quad(new_nodes) [expr $quad(element) != 5 || $quad(improvement) > 2]
    # problem with updating ...
    set quad(new_nodes) 1
    set quad(opt) QUAD_dominant
    set quad(loop_opts) "Each_surface"

    set d [ form_init .quad "Surface mesher" "" $buttons ]
    if { $d != "" } {
        set ln 0
        form_label $d.types "Element type" "$ln 0"
        set quad(el_opts) {"Pure_Quad" "Quad_w_one_tri" "QUAD_dominant"  "Pure_tri"}
        form_optionmenu $d.element_type quad(opt) $quad(el_opts) "$ln 1" "" "opt_element"
	$d.element_type configure -width 18

        #form_radiobutton $d.element1 "QUAD_4" quad(element) 4 "$ln 1"
        #incr ln
        #form_radiobutton $d.element2 "QUAD_4 (one TRI_3)" quad(element) 1 "$ln 1"
        #incr ln
        #form_radiobutton $d.element3 "QUAD_4 (several TRI_3)" quad(element) 2 "$ln 1"
        #incr ln
        #form_radiobutton $d.element4 "TRI_3" quad(element) 3 "$ln 1"
        #incr ln
        #form_radiobutton $d.element5 "TRI_3 (STL-like)" quad(element) 5 "$ln 1"

        incr ln
        form_label $d.stypes "Boundaries" "$ln 0"
        #set quad(loops_opt) {"Each_surface" "Family_boundaries" "Selected_surfaces" "Pick_curves"}
        set quad(loops_opt) {"Each_surface" "Pick_curves"}
        form_optionmenu $d.loop_type quad(loop_opts) $quad(loops_opt) "$ln 1" "" ""
	$d.loop_type configure -width 18

        #incr ln
        #form_label $d.element "Patch definition" "$ln 0"
        #form_radiobutton $d.fromgeo "Loops" quad(how) geo "$ln 1" \
         #   {quad(geo_active)}
        #incr ln
        #form_radiobutton $d.fromgeobars "Loops & bars" quad(how) geo+mesh "$ln 1" \
        #    {quad(geo_active) quad(ele_active)}
        #incr ln
        #form_radiobutton $d.frommesh "Bars" quad(how) mesh "$ln 1" \
        #    {quad(ele_active)}
       #incr ln
       # form_checkbutton $d.selection "Mesh selected loop(s)" quad(select) "$ln 0" \
        #    {quad(geo_active) {quad(how) geo geo+mesh}}
      #incr ln
        #form_checkbutton $d.complete "Complete edges" quad(complete_edges) "$ln 0" \
        #   {{quad(how) mesh}}


        incr ln
        form_label $d.iters "Max. # of smoothing steps" "$ln 0" \
            {or {quad(element) 1 2 3 4} {quad(improvement) 3}}
        form_entry $d.itersT quad(iters) int "$ln 1" \
            {or {quad(element) 1 2 3 4} {quad(improvement) 3}}
        incr ln
        form_label $d.conver "Rel. smoothing accuracy" "$ln 0" \
            {quad(iters) quad(new_nodes)}
        form_scale $d.conver_scale quad(conver) "" h 0 .1 .005 "$ln 1" \
            {quad(iters) quad(new_nodes)}
        incr ln
        form_label $d.improvement "Mesh improvement level" "$ln 0" \
            {quad(iters)}
        form_scale $d.improvement_scale quad(improvement) "" h 0 3 1 "$ln 1" \
            {quad(iters)}

        incr ln
        form_label $d.etol "Ignore feature size" "$ln 0" \
            {{quad(element) 0 1 2 3 5}}
        form_entry $d.etolT quad(ele_tol) float "$ln 1" \
            {{quad(element) 0 1 2 3 5}}
  
        incr ln
        form_checkbutton $d.surface_check "Surface projection" quad(proj) "$ln 0" \
            {quad(geo_active) quad(new_nodes)}

        incr ln
        form_label $d.dev "Max. surface deviation" "$ln 0" \
            {quad(proj) quad(new_nodes)}
        form_entry $d.dev_scale quad(dev) float "$ln 1" \
            {quad(proj) quad(new_nodes)}

	incr ln
        form_label $d.violate "Violate surface distance" "$ln 0" \
            {quad(iters) quad(new_nodes)}
        form_entry $d.violateT quad(violate) float "$ln 1" \
            {quad(iters) quad(new_nodes)}

        incr ln
        form_label $d.tol "Looping tolerance" "$ln 0" \
            {quad(geo_active) {quad(how) geo geo+mesh}}
        form_entry $d.tolT quad(geo_tol) float "$ln 1" \
            {quad(geo_active) {quad(how) geo geo+mesh}}
 
  

        #incr ln
        #form_label $d.debug "Debug level" "$ln 0"
        #form_scale $d.debug_scale quad(debug) "" h -1 2 1 "$ln 1"

        form_finish bot
    }
}

proc opt_element {} {
global quad
   set opt {{"Pure_Quad" 4} {"Quad_w_one_tri" 1} {"QUAD_dominant"  2} {"Pure_tri" 3}}
   foreach {n v} $opt {
   	if {$n == $quad(opt)} {set quad(element) $v}
   }
}

proc mesher_quad_mesh_ai {} {
global quad uns_type_visible fam_visible
 ic_mess "quad(loop_opts) is $quad(loop_opts)\n"
   set opt $quad(loop_opts)
   switch $opt {
	Each_surface {
	  set quad(how) geo
	  set selection [ic_geo_get_objects surface ""]
	  set loops ""
	  foreach surf $selection {
		set fam [ic_geo_get_family surface $surf]
		ic_geo_create_loop JUNK.$surf $fam names [ic_geo_boundary surface $surf] 0 $surf
		#ic_geo_create_loop JUNK.$surf $fam names [ic_geo_boundary surface $surf] 0 ""
		lappend loops JUNK.$surf
	  }

	 #mesher_quad_mesh
	set quad(select) 0
  	if ![ic_uns_is_loaded] { ic_uns_create_empty }
	busy 1
       	if [catch {
		ic_quad_from_loops visible $quad(element) $quad(proj) $quad(iters) \
                		$quad(conver) $quad(improvement) 1 $loops $quad(geo_tol) \
                		$quad(ele_tol) 2 $quad(dev) $quad(debug) $quad(violate)
        		} res] { mess "$res\n" red }
        	  busy 0
        	  set quad(ele_active) [ic_uns_is_loaded]
	  foreach t {BAR_2 LINE_2 TRI_3 QUAD_4} {
        		if ![info exists uns_type_visible($t)] {
          		  set uns_type_visible($t) 1
        		}
    	}
   	  
	  foreach surf $selection {ic_delete_geometry loop names JUNK.$surf }	
	ic_save_tetin tetin.tmp
	ic_unload_tetin
	ic_save_unstruct uns.tmp
	ic_unload_mesh
	ic_load_tetin tetin.tmp
	ic_load_unstruct uns.tmp
	
	#exec rm uns.tmp
	}
	Family_boundaries {
	set quad(how) geo

	}
	Selected_surfaces {
	  set quad(how) geo+mesh
	  set selection [geo_select surface]
	  foreach surf $selection {
		set fam [ic_geo_get_family surface $surf]
		#ic_geo_create_loop LOOP.$surf $fam names [ic_geo_boundary surface $surf] 0 $surf
		ic_geo_create_loop LOOP.$surf $fam names [ic_geo_boundary surface $surf] 0 ""
	   }
	   mesher_quad_mesh
	   foreach surf $selection {ic_delete_geometry loop names JUNK.$surf }	   
	}
	Pick_curves {
	   set quad(how) geo+mesh
	   set selection [geo_select curve]
	   ic_geo_create_loop JUNK.curves MESH names $selection 0 {}
	   mesher_quad_mesh
	   ic_delete_geometry loop names JUNK.curves
	}
	default {
	    error "This opt $opt is not supported"
	}
   }

#ic_delete_geometry loop all ""
}








