
# ICEM CFD solver interface routine for the manager.
#
# Copyright (c) 1993-1995 ICEM Systems.

proc wpatran_write_input {} {
    global confname file_name wpatran_output_path icem_powermesh
    global file_name

    set domain [domain_select $file_name(domains_dir) "Select an unstructured domain." unstruct 0]
    if {$domain == {}} return

    set outfile $file_name(transfer_dir)/wpatran
    set paramfile $file_name(mesh_dir)/wpatran.par
    
    get_boco_topo

    set quadratic 0
    set quad8 0
    set penta15 0
    set hexa20 0
    set pyra13 0
    set swap 0
    set toler 0.0
    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0
    set auto_comp 0
    set quad_out 0
    set auto_pid 0
    set external 0

    if {![dialog .wpatran "Patran" \
          "Please edit the following patran options." {
               {bocofile -type path -title "bocofile" \
                        -help "Boundary Conditions File"}
               {outfile -type path -title "Output file" -help "Output File"}
	       {quadratic -opt {{Yes 1} {No 0}} \
                        -title "Is this a second order elements' grid?"}
               {quad8 -opt {{"QUAD_8" 1} {"QUAD_9" 0}} -active {$quadratic} \
                        -title "Reduce to 8 nodes QUAD elements" }
               {penta15 -opt {{"WEDGE_15" 1} {"WEDGE_18" 0}} -active {$quadratic} \
                        -title "Reduce to 15 nodes WEDGE elements" }
               {hexa20 -opt {{"HEXA_20" 1} {"HEXA_27" 0}} -active {$quadratic} \
                        -title "Reduce to 20 nodes HEXA elements" }
	       {pyra13 -opt {{"PYRA_13" 1} {"PYRA_14" 0}} -active {$quadratic} \
                        -title "Reduce to 13 nodes PYRA elements" }
	       {scale -opt {{"Yes" 1} {"No" 0}} -title "Scaling"}
               {x -type string -active {$scale} -title "x scaling factor" }
               {y -type string -active {$scale} -title "y scaling factor" }
               {z -type string -active {$scale} -title "z scaling factor" }
	       {swap -opt {{"Yes" 1} {"No" 0}} \
			-title "Swap x and y coordinates" \
			-help "For each node, the x and y coordinates are interchanged."}
	       {toler -type string -title "Zero tolerance" \
			-help "Each coordinates smaller than the given value is set to zero."}
	       {auto_comp -opt {{"Yes" 1} {"No" 0}} \
			-title "Automatic creation of named components (packet 21)?" \
		    	-help "Use families, domains or subfaces to create components."}
	       {auto_pid -opt {{"Yes" 1} {"No" 0}} \
			-title "Automatic creation of regions (packet 2)?"\
			-help "Use families, domains or subfaces to create regions."}
	       {quad_out -opt {{"Yes" 1} {"No" 0}} \
		         -title "Write QUAD and volume in packet 2?"}
           {external -opt {{"Yes" 1} {"No" 0}} \
            -title "Use external node numbering?"}
           }]} return
  
    if {![file exists $domain]} {
        errormsg "Domain file $domain doesn't exist"
        return
    }

    # Let's make sure that the params file contains the local coordinate
    # systems from the tetin file.
    if [file exists $paramfile] {
	famboco_edit_load_param_values $paramfile
	# FIXME
	#famboco_after_load_param_callback
    }
    famboco_edit_save_param_values $paramfile
    
    set command "\"$wpatran_output_path\" -dom \"$domain\" -b \"$bocofile\""
    if $quad8   {set command "$command -quad8"}
    if $penta15 {set command "$command -penta15"}
    if $hexa20  {set command "$command -hexa20"}
    if $pyra13  {set command "$command -pyra13"}
    if $scale   {set command "$command -scale $x,$y,$z"}
    if $swap    {set command "$command -swap"}
    if $auto_comp {set command "$command -comp"}
    if $auto_pid {set command "$command -pid"}
    if $quad_out {set command "$command -quad_out"}
    if $external {set command "$command -ex"}
    set command "$command -tol $toler \"$outfile\""
    set command "$command -par \"$paramfile\"" ;# this will always be here

    runxcom ICEM-CFD/wpatran-Interface $command "" 1 0
}

proc wpatran_make_bcicon {fam params} {
    puts "params $params"
    set csystem global
    foreach param $params {
	catch {unset pval}
	foreach {n v} [lrange $param 1 end] { set pval($n) $v }
	switch [lindex $param 0] {
	    DISPL {
		switch $pval(Type) {
		    Translation_X { set use X }
		    Translation_Y { set use Y }
		    Translation_Z { set use Z }
		    Translation_XYZ { set use "X Y Z" }
		    Rotation_X { set use Rot_x }
		    Rotation_Y { set use Rot_y }
		    Rotation_Z { set use Rot_z }
		    Rotation_XYZ { set use "Rot_x Rot_y Rot_z" }
		    All_Displacement { set use "X Y Z Rot_x Rot_y Rot_z" }
		    default { set use "" }
		}
		foreach u $use { set val($u) $pval($u) }
		set csystem [set "pval(Coord. System)"]
	    }
	    T {
		set val(Temperature) $pval(Temperature)
	    }
	}
    }
    
    set return ""
    
    # -----------------------
    
    set dir ""
    foreach i {X Y Z} {
	if [info exists val($i)] {
	    lappend dir $val($i)
	} else {
	    lappend dir 0
	}
    }
    if {$dir != "0 0 0"} {
	lappend return [list arrow "" family_color $dir 3 1 $csystem]
    }
    
    # -----------------------
    
    set dir ""
    foreach i {x y z} {
	if [info exists val(Rot_$i)] {
	    lappend dir $val(Rot_$i)
	} else {
	    lappend dir 0
	}
    }
    if {$dir != "0 0 0"} {
	lappend return [list arrow "" green $dir 2 1 $csystem]
    }

    # -----------------------
    
    if [info exists val(Temperature)] {
	lappend return [list circle "" red 3 1]
    }
    
    return $return
}
