# Script file for the scripting library (scrib)
#
# File: scrib/scripts/mesh_tetra.tcl
#
# $Revision: 389238 $ on $Date: 2004-01-08 20:44:05 -0500 (Thu, 08 Jan 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------

global proc_scrib_data env

#
# --> set up vars
#
set V_model $proc_scrib_data(model)
set proc_scrib_data(name) [file rootname $V_model]

if ![info exists proc_scrib_data(dir)] {
    if [info exists proc_scrib_data(wdir)] {
	set proc_scrib_data(dir) $proc_scrib_data(wdir)
    } else {
	set proc_scrib_data(dir) [pwd]
    }
}
foreach nm [array name proc_scrib_data] {
    if [info exists proc_scrib_data($nm)] { set V_$nm $proc_scrib_data($nm) }
}

set interim_mesh  [file join $V_dir [file tail ${V_name}]].uns
set interim_geom  [file join $V_dir [file tail ${V_name}]].tin
set out_boco      [file join $V_dir [file tail ${V_name}]].fbc

if ![info exists V_export_dir] {
    set V_export_dir "."
} elseif ![file isdirectory $V_export_dir] {
    file mkdir $V_export_dir
}
set export_file [file join $V_export_dir [file tail ${V_name}]].nas

if ![info exists V_mesh_size]      { set V_mesh_size       25 }
if ![info exists V_build_topo_tol] { set V_build_topo_tol   0.3 }

#
# --> if there is not already a tetin file, then translate to it from cadsys
#
set translated_from_cadsys 0

if ![file exists $interim_geom] {
    #
    # get the cadsystem name and try import the cad file    
    #
    set V_cadfile [file join $V_cad_dir $V_model]
    
    switch -- $V_cadsys {
	catia   {
	    scrib_lib_catia_import $V_cadfile $interim_geom $V_layers
	}
	default {
	    #
	    # try a more general command
	    #
	    set command "scrib_lib_${V_cadsys}_import"
	    if {$command == [info commands $command]} {
		$command $V_cadfile $interim_geom
	    } else {
		mess "does not support cad system ${V_cadsys}" red
		return
	    }    
	}
    }
    set translated_from_cadsys 1
}

#
# --> load the tetin and see that it has surfaces in it
#

mess "Loading and verifying Tetin file [file tail $interim_geom]...\n\n"

ic_load_tetin [list "$interim_geom"]
if {[llength [ic_geo_get_objects surface ""] ] == 0} {
    mess "no surfaces in layers 12,13,23,203,213,242\n" red
    return
}

#
# --> set meshing paramters globally
#

mess "Setting global mesh parameters...\n\n"

ic_set_meshing_params global 0 gref 1 gmax 64

if $translated_from_cadsys {
    if ![info exists V_build_topo_tol] {
	set V_build_topo_tol ".1"
    }
    catch {ic_build_topo $V_build_topo_tol} res
}

#
# --> create surface edges on the available surfaces
#

mess "Creating surface edges...\n\n"

ic_geo_create_surface_edges [ic_geo_get_objects surface ""]

# ic_geo_delete_unattached ;# this is commmented for some reason

#
# --> set the meshing parameters on surfaces
#

mess "Setting surface mesh parameters...\n\n"

ic_set_meshing_params surface_all {} emax $V_mesh_size

#
# --> run tetra on the geometry
#

mess "Running Tetra...\n\n"

if [scrib_lib_tetra_mesh] {
    mess "there was an error in scrib_lib_tetra_mesh...returning\n" red
}

#
# --> check the quality of the mesh
#

mess "Checking mesh quality...\n\n"

set num_elements [scrib_lib_quality_check2 {
    {"Determinant (Jacobian)" "QUAD_4" "Determinant" 0.0 0.7 } 
    {Warpage "QUAD_4" "Max warpgls" 10.0 180.0 } 
    {"Min quad angle" "QUAD_4" "Min angle" 0.0 45.0 } 
    {"Max quad angle" "QUAD_4" "Max angle" 135.0 180.0 } 
    {"Min tri angle" "TRI_3" "Min angle" 0.0 45.0 } 
    {"Max tri angle" "TRI_3" "Max angle" 135.0 180.0 } 
    {"Max ratio (Maxl/minl)" "TRI_3 QUAD_4" "Max ratio" 5 10 } 
    {"Max ratio (Maxl/minl)" "TRI_3 QUAD_4" "Max ratio" 3 10 }
    {"Max ratio (Maxl/minl)" "TRI_3 QUAD_4" "Max ratio" 2 10 }}]

#
# --> move the mesh into the proper familyname (-- JKA: 30.Sept.02)
#

set tnaspid [scrib_lib_glue_assembly_filename_naspid_get $V_name]
set thisscript_fam [string toupper [scrib_lib_assembly_famname_get $tnaspid]]
mess "Changing mesh families to $thisscript_fam...\n"
scrib_lib_uns_put_mesh_in_family $thisscript_fam

#
# --> save the mesh
#

mess "Saving the mesh as $interim_mesh...\n\n"

ic_save_unstruct $interim_mesh 1 {} {} {} {} 0

#
# --> translate to nastran file
#

set out_boco ./ ;# not sure where this came from? --jka
scrib_lib_output_nastran $interim_mesh $out_boco $export_file $V_nasform

if {[info exists badnaspid] && $badnaspid} {
    mess "\n\n***Part $V_model is returning pid of $badnaspid.\
	    This may be a problem!\nPlease verify that this is correct\n" red
}
