# Library functions for the scripting library (scrib)
#
# File: scrib/lib/tetin.tcl
#
# $Revision: 396730 $ on $Date: 2004-07-21 17:11:59 -0400 (Wed, 21 Jul 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# --> if the name of the first proc already exists in interp, return
#
if {"" != [info commands scrib_lib_tetin_import]} { return }
#
# --> procs in this file depend on the following scrib/lib/ files--source them
#     I guess we could do this by proc, too, not sure waht's best
#
if 0 {
    # none required
    set required_files ""
    scrib_lib_setup_source_files $required_files
}

## trivial file copy of model to tetin, implemented transparency.

proc scrib_lib_tetin_import {model tetin} {
    catch {file copy $model $tetin}
    #since I am using this for Nissan, this is where i will set thickness and offset info.
    # this is bad. --jka
    scrib_lib_tetin_get_thickness_from_file
    return
}

## this has got to change...-jka

proc scrib_lib_tetin_get_thickness_from_file {} {
    global proc_scrib_data
    #
    #
    #
    if {[info exists proc_scrib_data(naspid_keystrings)] && \
	    [info exists proc_scrib_data(naspid_nchar)] && \
	    [info exists proc_scrib_data(naspid_offset)]} {
	
	scrib_lib_glue_assembly_filename_naspid_get $proc_scrib_data(name)

    } else {
	# this is the old hard-coded...
	set part_ind [string first PART $proc_scrib_data(name)]
	set proc_scrib_data(naspid) [string range $proc_scrib_data(name) [expr $part_ind + 5]  [expr $part_ind + 9]]
    }
    
    #if the offset file does not exist, set extrude to 0, and skip file loading stuff.
    if ![info exists proc_scrib_data(offset_file)] {set proc_scrib_data(offset_file) ""}
    if ![file exists $proc_scrib_data(offset_file)] {
	if {$proc_scrib_data(offset_file) != ""} {
	    mess "Non existant offset file specified...  Filename $proc_scrib_data(offset_file) does not exist.\n" red
	}
	set proc_scrib_data(offset_file) ""
    }
    if {$proc_scrib_data(offset_file) == ""} {
	set proc_scrib_data(offset) 0
	return
    }
    set proc_scrib_data(thickness) 1
    set proc_scrib_data(offset_dir) ""
    set off_file [open $proc_scrib_data(offset_file) r]
    while {![eof $off_file]}  {
	set line [string trim [gets $off_file]]
	set part_num [lindex $line 0]
	set part_num1 0
	set part_num2 0
	if {[string length $part_num] == 7} {
		set part_num1 [string range $part_num 0 4]
		set part_num2  [string replace $part_num 4 5]
	}
	#mess "part_num is $part_num, part_num1 is $part_num1, part_num2 is $part_num2\n"	
	set std_part_num [scrib_lib_assembly_naspid_standardize $part_num]
	if {($part_num == $proc_scrib_data(naspid) ) \
		|| ($std_part_num == $proc_scrib_data(naspid) ) \
		|| ($part_num1 == $proc_scrib_data(naspid) ) \
		|| ($part_num2 == $proc_scrib_data(naspid) ) } {
		mess "line is $line\n" green
		set length [llength $line]
		set proc_scrib_data(mat_name) [lindex $line [expr $length - 3]]
		set proc_scrib_data(thickness) [lindex $line [expr $length - 2]]
		set proc_scrib_data(offset_dir) [lindex $line [expr $length - 1]]
		if {[string first y $proc_scrib_data(offset_dir)]  != -1 }  {
		    #For examples 12346/5 axis needs to be switched.
		    if {$part_num1 == $proc_scrib_data(naspid) } {
			if {[string first - $proc_scrib_data(offset_dir)]  != -1}  {
			    set proc_scrib_data(offset_dir) y+
			} else {
			    set proc_scrib_data(offset_dir) y-
			}
			mess "switching offset dir to $proc_scrib_data(offset_dir)\n" red
		    }
		}
	}
    }
    mess "naspid is $proc_scrib_data(naspid)\n" blue
    if {$proc_scrib_data(offset_dir) == ""} {
	mess "Offset dir could not be found in $proc_scrib_data(offset_file), so turning off offset\n" red
	set proc_scrib_data(offset) 0
    }
    
    set proc_scrib_data(material_mapping_file) material_name_id_mapping.txt
    
    if {[info exists proc_scrib_data(material_mapping_file)] \
	    && [info exists proc_scrib_data(mat_name)] } {
	
	set proc_scrib_data(matid) [scrib_lib_offset_material_mapping_read \
					$proc_scrib_data(mat_name) \
					$proc_scrib_data(material_mapping_file)]
	mess "material $proc_scrib_data(mat_name) is $proc_scrib_data(matid)\n" blue
    }
    
    #Maybe, also, this is where the pid and material should be set.
    #mess "thickness is $proc_scrib_data(thickness), dir is $proc_scrib_data(offset_dir)\n" blue
}



