
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc pipe_model_write {infile} {
    global default_pipe_radius pipe_mesh_params
    
    set pipes [db_list_objects pipe]
    if {$pipes == ""} { error "There are no pipe/wire objects" }
    
    set np 1
    foreach obj $pipes {
	if ![$obj si_value active] continue
	set sh [$obj getval body_shape]
	set n [$sh getval nverts]
	
	for {set i 1} {$i <= $n} {incr i} {
	    set pt [$sh getval vert$i]
	    set ww [$sh getval radius$i]
	    set rc [$sh getval rcurve$i]
	    if {$ww == 0} { set ww $default_pipe_radius }
	    
	    if ![info exists point_record($pt)] {
		set point_record($pt) [list $np $ww $rc]
		set pt_num($np) $pt
		incr np
	    } else {
		set np1 [lindex $point_record($pt) 0]
		set ww1 [lindex $point_record($pt) 1]
		set rc1 [lindex $point_record($pt) 2]
		if {$ww > $ww1} { set ww1 $ww }
		if {$rc > $rc1} { set rc1 $rc }
		set point_record($pt) [list $np1 $ww1 $rc1]
	    }
	}
    }
    
    set fp [open $infile w]
    
    if $pipe_mesh_params(on) {
	foreach p [ array names pipe_mesh_params ] {
	    if {($p != "on") && ($pipe_mesh_params($p) != 0)} {
		puts $fp "param $p $pipe_mesh_params($p)"
	    }
	}
    }
    
    for {set i 1} {$i < $np} {incr i} {
	set pt $pt_num($i)
	set pr $point_record($pt)
	puts $fp "point $i $pt [lindex $pr 1] [lindex $pr 2]"
    }
    foreach obj $pipes {
	if ![$obj si_value active] continue
	set sh [$obj getval body_shape]
	set n [$sh getval nverts]
	if [$sh getval eccentric_flag] { set ef " -e" } else { set ef "" }
	
	for {set i 1} {$i < $n} {incr i} {
	    set pt1 [$sh getval vert$i]
	    set pt2 [$sh getval vert[expr $i + 1]]
	    set v1 $point_record($pt1)
	    set v2 $point_record($pt2)
	    puts $fp "pipe$ef [lindex $v1 0] [lindex $v2 0]"
	}
    }
    close $fp
    
    mess "Wrote the pipe model to \"$infile\"\n"
}

proc pipe_model_read {tetfile} {
    auto_load_projlib $tetfile 1 1 1 1 1 0
}

proc pipe_model_clear {} {
    delete_projlib
}

proc pipe_model_defrad {} {
    global default_pipe_radius
    
    set r [question "Default pipe radius?" $default_pipe_radius]
    if {$r != ""} {
	set default_pipe_radius $r
    }
}

proc pipe_model_create {output_mesh} {
    global bindir env grid_hexa_interactive
    
    cmd_mkdir [directory tetin]
    set infile [directory tetin]/pipe_in
    set tetfile [directory tetin]/tetin_pipe
    set bocofile [directory tetin]/family_boco_pipe
    set blockfile [directory tetin]/blocking_pipe
    set replayfile [directory tetin]/replay_pipe
    set outfile [directory autohex]/grid_output
    pipe_model_write $infile
    
    cmd_rm $tetfile
    cmd_rm $bocofile
    cmd_rm $blockfile
    cmd_rm $replayfile
    cmd_rm $outfile
    
    #set pipes $env(ICEM_ACN)/icemcfd/pipes
    set pipes [file join $env(ICEM_ACN) icemcfd pipes]
    if [info exists env(PIPES_PATH)] {
	set pipes $env(PIPES_PATH)
    }
    regsub -all \\\\ $pipes / pipes
    set cmd "\"$pipes\" \"$infile\" -b \"$blockfile\" -t \"$tetfile\" -w \"$outfile\" \
		-r \"$replayfile\" -f \"$bocofile\" -o -trans"
    if $grid_hexa_interactive {
	append cmd " -nomesh"
    }
    
    mess "Running: $cmd\n"
    catch {eval exec $cmd} res
    mess "$res\n"
    
    if 0 {
	# Don't use the builtin version
	run_pipes $infile $blockfile $tetfile $replayfile $bocofile \
	    $output_mesh
    }
    
    pipe_model_read $tetfile
}

