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

#set debug 1
set model $proc_scrib_data(model)

#I think the following is causing all kinds of problems, because name gets set incorrectly, 
#at least, I see in current_med_script name being set to [file root $model].uns, but in 
#clean files name is not set... -BEN

#if ![info exists proc_scrib_data(name)] {
#    set proc_scrib_data(name) [file rootname $model]
#}
set proc_scrib_data(name) [file rootname $model]
set proc_scrib_data(cadfile) [file join $proc_scrib_data(cad_dir) $model]

if ![info exists proc_scrib_data(dir)] {
    set proc_scrib_data(dir) [pwd]
}
if ![info exists proc_scrib_data(thickness)] {
    set proc_scrib_data(thickness) 0
}

set interim_mesh $proc_scrib_data(name).uns
set interim_geom  [file join $proc_scrib_data(dir) $proc_scrib_data(name).tin]
set meshable_geom [file join $proc_scrib_data(dir) $proc_scrib_data(name)_meshable.tin]

set offset_mesh $proc_scrib_data(name)_offset.uns
set offset_geom $proc_scrib_data(name)_offset.tin

set out_boco    $proc_scrib_data(name).fbc
if ![info exists proc_scrib_data(export_dir)] {
    set proc_scrib_data(export_dir) "."
} elseif ![file isdirectory $proc_scrib_data(export_dir)] {
    file mkdir $proc_scrib_data(export_dir)
}
set export_file [file join $proc_scrib_data(export_dir) $proc_scrib_data(name).nas]
#
# get the nastran pid from this cad part file name if possible
#
scrib_lib_glue_assembly_filename_naspid_get $proc_scrib_data(name)
if {1 == $proc_scrib_data(naspid)} {
    mess "Check your nastran pid settings: could not get a nastran pid from the filename $proc_scrib_data(name)\n" red
    set badnaspid 1
}
mess "Nastran Shell pid for file $proc_scrib_data(name) is $proc_scrib_data(naspid)\n" blue

set thisscript_fam [scrib_lib_assembly_famname_get $proc_scrib_data(naspid)]

#
#
# --> import the cad file to a tetin
#
switch $proc_scrib_data(cadsys) {
    catia {
	if [info exists proc_scrib_data(layers)] {
	    scrib_lib_catia_import $proc_scrib_data(cadfile) \
		$interim_geom $proc_scrib_data(layers)

	    if ![llength [ic_geo_get_objects surface ""]] {
		mess "no surfaces in layers $proc_scrib_data(layers) trying noshow area\n" red
		ic_unload_tetin
		scrib_lib_catia_import $proc_scrib_data(cadfile) \
		    $interim_geom $proc_scrib_data(layers)		0 1
	    }	    

	} else {
	    scrib_lib_catia_import $proc_scrib_data(cadfile) \
		$interim_geom 
	}
    }
    default {
	#
	# try a more general command
	#
	set command "scrib_lib_$proc_scrib_data(cadsys)_import"
	if {$command == [info commands $command]} {
	    $command $proc_scrib_data(cadfile) $interim_geom
	} else {
	    mess "mesh_quad does not support cad system $proc_scrib_data(cadsys). cannot get command $command" red
	    return
	}
    }
}
#
#
# --> load the tetin
#
scrib_lib_utils_putdebug "Loading tetinfile $interim_geom...\n"
ic_load_tetin [list "$interim_geom"]
#
#============ CHECK IF EMPTY =========================
#
# --> check to see that there are surfaces in the layers
#
if ![llength [ic_geo_get_objects surface ""]] {
    set place $proc_scrib_data(name)
    if [info exists proc_scrib_data(layers)] {
	append place " in layers $proc_scrib_data(layers)"
    }
    mess "No surfaces in $place...returning\n" red
    #
    ## xx ?? note nastran specificity xx ## ??
    set n [open [file join $proc_scrib_data(export_dir) $proc_scrib_data(name)_no_surfs.nas] "w"]
    puts $n "no surfaces in $place\n"
    close $n
    return
}
#
#========== PREPARE GEOMETRY =======================
#
# --> and do some stuff to the tetin object
#
ic_set_meshing_params global 0 gref 1 gmax 64 gnat $proc_scrib_data(nat_size) gnatref 10 gcgap 1
ic_geo_fixup 1
#if $proc_scrib_data(ignore_angle) {
#    ic_build_topo $proc_scrib_data(ignore_tol) -angle \
#	$proc_scrib_data(ignore_angle) -filter_points -filter_curves
#} else {
    if $proc_scrib_data(bt) {ic_build_topo $proc_scrib_data(ignore_tol) }
#}
ic_geo_delete_unattached
ic_set_meshing_params surface_all {} emax $proc_scrib_data(mesh_size)
#scrib_lib_utils_putdebug "curves after ic_geo_delete_unattached: [ic_geo_get_objects curve ""]\n"
#
#--> tetin is now ready to be meshed.
#

#due to some problems in mat pt locations. I am deleting all of them.
#    foreach mp [ic_geo_get_objects material] {
#	ic_delete_geometry material names $mp
#    }

ic_save_tetin $meshable_geom
#
#============= MESH ====================
#
    set args [ list run_cutter 1 log tetra_cmd.log \
	    fix_non_manifold 1 run_smoother 1 write_volume 1 smoother_n_iter 20 \
	    smoother_aspect 0.4 run_coarsener 5 coarsen_aspect 0.1 n_processors 4 ]
    if ![llength [ic_geo_get_objects material]] {
	lappend args auto_vol
	lappend args 1
    }
    mess "args are $args\n"
    if [catch {ic_run_tetra $meshable_geom $interim_mesh $args} res] {
	mess "res is $res\n"
    }
    
    ic_load_unstruct $interim_mesh
    if {0 >= [ic_count_nodes]} {
	mess "Error mesh $interim_mesh has no nodes\n" red
	return 1
    }
    
    if [lindex [ic_uns_diagnostic subset all diag_type uncovered] 0] {
	mess "There are problems with this mesh\n" red
	file delete $interim_mesh
	return
    }


#
#============= CLEAN MESH ====================
#
#    ic_uns_merge_nodes all $proc_scrib_data(ignore_tol)
    ic_uns_swap_edges_auto all 0 10
    if {[info exists proc_scrib_data(ignore_angle)] && (0 < $proc_scrib_data(ignore_angle))} {
	#
	# we want to explude the SHADOW family (scrib_lib_welding_shadow_family_name) from the list
	#
	set remove_fams [ic_uns_list_families]
	set ind [lsearch $remove_fams [scrib_lib_welding_shadow_family_name $proc_scrib_data(naspid)]]
	set remove_fams [lreplace $remove_fams $ind $ind]
	#	
	scrib_lib_quad_remove_bars_by_feature_angle $remove_fams {NDOE BAR_2 LINE_2 TRI_3 QUAD_4} \
	    $proc_scrib_data(ignore_angle)
    }
    ic_uns_coarsen all $proc_scrib_data(coarsen_quality) {} $proc_scrib_data(coarsen_dev) \
	$proc_scrib_data(coarsen_iters) 1 0 $proc_scrib_data(coarsen_size)
    ic_smooth_elements upto 1 iterations 5 laplace 1 float TETRA_4 smooth TRI_3
    ic_smooth_elements upto $proc_scrib_data(coarsen_quality) iterations 10 \
	smooth TETRA_4 smooth TRI_3
    if {$proc_scrib_data(quadratic) != 1 } {set proc_scrib_data(quadratic) 0}
    if $proc_scrib_data(quadratic)   {ic_change_linear_quadratic iters 10 project 1 max_dev_half 0.1666666 \
	max_dev_nproj 30.0 max_dev_chord 0.35 check_proj 0 max_leap 3.5 midface 0 refine 0 \
	all_elem 1 name {}
    }

ic_save_unstruct $interim_mesh
scrib_lib_output_nastran $interim_mesh $out_boco $export_file $proc_scrib_data(nasform)

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




























































































