# Script file for the scripting library (scrib)
#
# File: scrib/scripts/mesh_offset.tcl
#
# $Revision: 396133 $ on $Date: 2004-06-30 17:24:02 -0400 (Wed, 30 Jun 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------

set model $proc_scrib_data(model)
#
#The following gives errors, since it seems the name gets set incorrectly
#if ![info exists proc_scrib_data(name)] {
#    set proc_scrib_data(name) [file rootname $model]
#}
set proc_scrib_data(name) [file rootname $model]
set out_boco       $proc_scrib_data(name).fbc
#
set tnaspid [scrib_lib_glue_assembly_filename_naspid_get $proc_scrib_data(name)]
set thisscript_fam [scrib_lib_assembly_famname_get $tnaspid]
#
#================== LOAD THE MESH ===================
#
ic_uns_load $model
#
#================== OFFSET MESH ==================
#
switch $proc_scrib_data(offset) {

    0 {
    #No offset
	mess "No offset defined.  Please define some type of offset.\n" red
	return
    }

    1 {
    #From a file
	scrib_lib_tetin_get_thickness_from_file	
	if {![info exists proc_scrib_data(thickness)] && \
	    ![info exists proc_scrib_data(offset_dir)] } {
		mess "not enough data found for offset\n" red
		return
	}
	if {($proc_scrib_data(offset_dir) == "NO") || ($proc_scrib_data(offset_dir) == "No")} {
	    set proc_scrib_data(offset_dir) no
	}
	if {$proc_scrib_data(offset_dir) != "no"} {
	    if [scrib_lib_uns_reorient_map_consistently_in_direction All $proc_scrib_data(offset_dir)] {
		# then it succeeded and we can offset by normal
		set proc_scrib_data(offset_dir) "normal"
	    }
	}
    }

    2 {
   #From layers
	ic_unload_tetin
	set cadfile [file join $proc_scrib_data(cad_dir) $proc_scrib_data(name).model]
 	set thisextrude_tetin $proc_scrib_data(name)\_extrude.tin
	set stat 0
	switch $proc_scrib_data(cadsys) {
	    catia {
		set stat [scrib_lib_catia_import $cadfile \
		    $thisextrude_tetin $proc_scrib_data(offset_layers) 1]
	    }
	    default {
		mess "does not support cad system $cadsys"
		set stat 1
	    }
	}
	if $stat { return }
	ic_load_tetin [list "$thisextrude_tetin"]
	if [llength [ic_geo_get_objects curve ""]] {
	    set thick_dir [scrib_lib_offset_do_find_curve_dir $thisscript_fam]
	    if {[llength $thick_dir] != 2} {
		mess "There is not proper information for offset.  \
		    Thickness and direction are $thick_dir\n" red
		return
	    }
	    set proc_scrib_data(thickness) [lindex $thick_dir 0]
	    set proc_scrib_data(offset_dir) "normal"
	    #mess "proc_scrib_data(thickness) is $proc_scrib_data(thickness)\n"
	} else {
	    mess "no curves in layers $proc_scrib_data(offset_layers)\n"
	    return
	}   
    }
    default {
	mess "offset $proc_scrib_data(offset) not defined\n" red
	return
    }
}
#
# --> O F F S E T   T H E   M E S H
#
if {$proc_scrib_data(offset_dir) != "no"} {
    mess "Offsetting by thickness of $proc_scrib_data(thickness), in dir $proc_scrib_data(offset_dir)\n" blue
    ic_uns_offset_mesh map all dir $proc_scrib_data(offset_dir) \
	dist [expr $proc_scrib_data(thickness) / 2.0]
} else {
    mess "Not offsetting, but will assign thickness.\n" blue
}
#
# --> S A V E   T H E   O F F S E T   M E S H
#
ic_save_unstruct $proc_scrib_data(offset_mesh)
#
# --> S A V E   T H E   N A S T R A N   F I L E
#
scrib_lib_output_thickness_set_nastran $thisscript_fam \
    $proc_scrib_data(naspid) $proc_scrib_data(thickness) $out_boco 1




