# Script file for the scripting library (scrib)
#
# File: scrib/scripts/mesh_assembly.tcl
#
# $Revision: 408467 $ on $Date: 2005-06-20 16:09:28 -0400 (Mon, 20 Jun 2005) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------

proc app_sm_assemble_uns {{extension uns}} {    
    #
    # foreach file with the proper file extension
    #
    set all_uns [glob -nocomplain *.$extension]
    set offset [glob -nocomplain *_offset.$extension]
    
    #set all_uns [scrib_lib_exec_filenames_validate $all_uns]
    foreach i $all_uns {
	
	if {[string range $i end-9 end] == "offset.$extension"} continue
	set base [file root $i]
	if {[lsearch $offset $base\_offset.$extension] == -1} {
		mess "offset of $i was not found\n" blue
		lappend offset $i
	}
    }
    foreach f $offset {
	ic_uns_load $f
    }
}

#-------------------------------------------------------------

proc app_sm_assemble_tin {{extension tin}} {    
    #
    # foreach file with the proper file extension
    #
    set all_tin [glob -nocomplain *.$extension]
    set meshable [glob -nocomplain *_meshable.$extension]
    
    #set all_tin [scrib_lib_exec_filenames_validate $all_tin]
    foreach i $all_tin {
	
	if {[string range $i end-11 end] == "meshable.$extension"} continue
	set base [file root $i]
	if {[lsearch $meshable $base\_meshable.$extension] == -1} {
		mess "meshable of $i was not found\n" blue
		lappend meshable $i
	}
    }
    foreach f $meshable {
	ic_load_tetin [list "$f"]
    }
}

#-------------------------------------------------------------

proc app_sm_assemble_bc {{extension fbc}} {
 
    #
    # foreach file with the proper file extension
    #
    set all_bc [glob -nocomplain *.$extension]
    foreach f $all_bc {
	ic_boco_load $f
	#we could create a thickness file generation step here.
    }
}
#
# -------------------------------------------------
#Main executable
if {![info exists proc_scrib_data(dir)] || ("" == $proc_scrib_data(dir))} {
    set proc_scrib_data(dir) [pwd]
}
if ![file exists $proc_scrib_data(export_dir)] {file mkdir $proc_scrib_data(export_dir) }

if [info exists proc_scrib_data(export_file)] {
    set assembly_nas [file join $proc_scrib_data(export_dir) $proc_scrib_data(export_file)]
} else {
    set assembly_nas [file join $proc_scrib_data(export_dir) [file root $assembly_mesh].nas]
}
set assembly_mesh [file join $proc_scrib_data(export_dir) [file root $proc_scrib_data(export_file)].uns]
set assembly_tin [file root $assembly_mesh].tin
set assembly_fbc [file root $assembly_mesh].fbc
set welded_mesh [file rootname $assembly_mesh]_welded.uns
set welded_tin  [file rootname $welded_mesh].tin
set welded_fbc  [file rootname $welded_mesh].fbc
#
#-------------------------------------------------------
# 
# --> merge the mesh files into a mesh assembly assembly_mesh
#
#---->first delete temporary files that should have been cleaned up
scrib_lib_uns_remove_tempfiles $proc_scrib_data(wdir)

if ![file exists $assembly_mesh] {
    app_sm_assemble_uns
    if [ic_uns_is_loaded] {
	ic_save_unstruct $assembly_mesh
    } else {
	mess "no assembled mesh to save...returning.\n" red
	return
    }
} 
#
# --> merge the tetin files into a geometry assembly_tin
#
if ![ file exists $assembly_tin] {
    if {$proc_scrib_data(original_geom) == 1} {
	app_sm_assemble_tin
	if [ic_geo_is_loaded] {
	    ic_save_tetin $assembly_tin
	} else {
	    mess "no assembled tetin to save...returning.\n" red
	    return
	}	
    } else {
	ic_geo_import_mesh $assembly_mesh 1 1
	ic_save_tetin $assembly_tin
    }
}
#
# --> merge up the family boco file into assembly_fbc
#
if ![ file exists $assembly_fbc] {
    app_sm_assemble_bc
    ic_boco_save_atr $assembly_fbc
}
#
# --> Manipulate the Nastran params file
#
# copy the canonical to the local
#
set renew_params_file 1
set local_params_file [scrib_lib_output_nastran_params_local $proc_scrib_data(wdir) $renew_params_file]
#
# add the materials to it
#
set mat_map_file [scrib_lib_offset_material_mapping_filename $proc_scrib_data(wdir)]
scrib_lib_offset_param_shell_properties $local_params_file $mat_map_file
#
# (later, we'll add rbe3 props to it)
#
#--> Unload everything
#
if [ic_geo_is_loaded] {ic_unload_tetin}
if [ic_uns_is_loaded] {ic_unload_mesh}
if [ic_boco_is_loaded] {ic_boco_unload}
#
# -->  AI*Env has a problem if you don't load tetin, then mesh, then fbc
#
ic_load_tetin [list "$assembly_tin"]
ic_load_unstruct $assembly_mesh
ic_boco_load $assembly_fbc
#
# weld up the assembly
#
if {![info exists proc_scrib_data(welding_file)] || \
	![file exists $proc_scrib_data(welding_file)]} {
    mess "cannot run welding script without a proper weld file. Please set a valid weld file.\n" red
    set mode ""
    set proc_scrib_data(name) assembled
} else {
    if ![info exists proc_scrib_data(welding_mode)] {
	mess "no welding mode set!\n" red
	#	    scrib_lib_output_nastran $assembly_mesh $assembly_fbc $assembly_nas $proc_scrib_data(nasform)
	set proc_scrib_data(name) assembled
	set mode ""
    } else {
	set mode $proc_scrib_data(welding_mode) ;# from app_*/config.tcl or gui-field
    }
}

if {$mode != ""} {
    
    set args ""
    
    if {[info exists proc_scrib_data(welding_report)] && ("" != $proc_scrib_data(welding_report))} {
	append args "-report_file $proc_scrib_data(welding_report) "
    }
    if {[info exists proc_scrib_data(welding_maxproject)] && ("" != $proc_scrib_data(welding_maxproject))} {
	append args "-maxprojection $proc_scrib_data(welding_maxproject) "
    } else {
	append args "-maxprojection 50 "
    }    
    if {[info exists proc_scrib_data(welding_splitting)] && ("" != $proc_scrib_data(welding_splitting))} {
	append args "-element_splitting $proc_scrib_data(welding_splitting) "
    }
    
    eval scrib_lib_welding_spot_main $mode $proc_scrib_data(welding_file) $args
    
    # this will save the settings to the tetin file
    
    if {"" != $welded_tin} { ic_save_tetin $welded_tin }
    #
    # save the welded mesh
    #
    ic_save_unstruct $welded_mesh
    ic_boco_save_atr $welded_fbc
    
    set proc_scrib_data(name) assembled_welded
}
#
# jka: 07.Oct.2002: added this, I think rightly
#scrib_lib_utils_ensure_same_families
#
if [file exists $welded_mesh] {
    scrib_lib_output_nastran $welded_mesh     $welded_fbc $assembly_nas $proc_scrib_data(nasform)
} else {
    scrib_lib_output_nastran $assembly_mesh $assembly_fbc $assembly_nas $proc_scrib_data(nasform)
}
#
# load up the params file if we're interactive
#
if {[info exists proc_scrib_data(interactive)] && (0 != $proc_scrib_data(interactive))} {
    scrib_lib_exec_med_display_load_params $local_params_file
}
#
# --> write out a report
#
ic_reports_write_mesh_report All [file root $assembly_mesh].html -format html -mesher "Batch Surface"


