
# This code creates the user interface for the mesh editor.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Al Magnuson,	awm@icemcfd.com
#
# Based on ddn_med.tcl in this same directory

proc ug_copymove_part {copy} {
    global file_name
    
    set part [ug_select_part both 1]
    if {$part == ""} return
    set dir [lindex $part 0]
    set selected [lindex $part 1]
    set filename [lindex $part 2]
    
    set newdir ""
    if {($dir == "ugparts") || ($dir == "./ugparts")} {
	set newdir $file_name(ugparts_dir)
	if ![file exists $newdir] { ic_mkdir $newdir }
    } else {
	set newdir ugparts
	if ![file exists $newdir] { ic_mkdir $newdir }
    }
    if $copy { set word Copy } else { set word Move }
    if [tk_dialog .delete $word \
	    "$word $selected from $dir to $newdir?" \
	    "" -1 "Yes" "No" ] return
    
    set newfile $newdir/[file tail $filename ]
    if ![file exists $newdir] { ic_mkdir $newdir }
    if [file exists ./ugparts] {
	if $copy {
	    ic_cp $filename $newfile
	} else {
	    ic_mv $filename $newfile
	}
    } else {
        mess "No such directory: ./ugparts\n" red
    }
    mess "Done.\n"
}

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

proc ug_delete_part {} {
    set part [ug_select_part both 1]
    if {$part != ""} {
	if [tk_dialog .delete "Delete" \
		"Delete [lindex $part 1] \
		\nin directory [lindex $part 0]?" \
		"" -1 "Yes" "No" ] return
	ug_delete_part_int [lindex $part 0] [lindex $part 1]
    }
}

proc ug_delete_part_int {dir name} {
    set file $dir/$name
    if {$file == ""} { error "No part named $name exists in $dir" }
    
    ic_rm $file
    # update the part list
    mess "Done.\n"
}

proc ug_select_part {which exists} {
    global file_name parts_dir_local
    
    return [ug_select_part_full $which $exists]
    
    switch $which {
	both {
	    if [file exists ./ugparts] {
		set parts_dir_local [tk_dialog .wpd Select \
					 "Select part from project directory\
					($file_name(ugparts_dir)) or ./ugparts?" \
					 "" -1 "project dir" "./ugparts"]
	    } else {
		set parts_dir_local 0
	    }
	}
	local {
	    set parts_dir_local 1
	}
	project {
	    set parts_dir_local 0
	}
    }   
    
    if $parts_dir_local {
	set dir ./ugparts
    } else {
	set dir $file_name(ugparts_dir)
    }
    set partname [ug_pick_part $dir $parts_dir_local $exists]
    if {$partname == ""} { return "" }
    set file $dir/$partname
    if {$file == ""} {
	if $exists {
	    error "You must select an existing part"
	} else {
	    set file $dir/$partname
	}
    }
    return [list $dir $partname $file]
}

proc ug_run {what {partname ""} {dir ""} {extra_cmds ""} {batchug 0} {do_after 1}} {
    global file_name env use_xwindows ug_options parts_dir_local
    global is_win

    set saved_ug_base_dir ""
    set saved_ug_lic_file ""
    #
    #  This is a restart file used by the default launch script.
    mess "Launching Unigraphics using $ug_options(lnch)\n"
    if [string match $ug_options(lnch_def) $ug_options(lnch)] {
	#
	#  Prepare options for the default launch script
	if [string match ug_options(base_dir) "not defined"] {
	    mess "UGII_BASE_DIR Environment Variable not set.  Please set\n" red
	    mess "this environment variable to the Unigraphics installation\n" red
	    mess "directory.\n" red
	    file_options
	    return
	} else {
	    if [info exists env(UGII_BASE_DIR)] {
		set saved_ug_base_dir $env(UGII_BASE_DIR)
	    }
	    set env(UGII_BASE_DIR) $ug_options(base_dir)
	    mess "    UGII_BASE_DIR     = $env(UGII_BASE_DIR)\n"
	}
	if [string match ug_options(lic) "not defined"] {
	    mess "UGII_LICENSE_FILE Environment Variable not set.  Please set\n" red
	    mess "this environment variable to the Unigraphics license file or server\n" red
	    file_options
	    return
	} else {
	    if [info exists env(UGII_LICENSE_FILE)] {
		set saved_ug_lic_file $env(UGII_LICENSE_FILE)
	    }
	    set env(UGII_LICENSE_FILE) $ug_options(lic)
	    mess "    UGII_LICENSE_FILE = $env(UGII_LICENSE_FILE)\n"
	}
	if {$partname == ""} {
	    set stuff [ug_select_part both 0]
	    if {$stuff == ""} return
	    set dir [lindex $stuff 0]
	    set partname [lindex $stuff 1]
	}
        if {$dir == ""} {
	    if $parts_dir_local {
	        set dir ./ugparts
	    } else {
	        set dir $file_name(ugparts_dir)
	    }
        }
        if [file exists $partname] {
	    mess "    Retrieving UG file $partname\n"
	    set pn "-retrieve=[ug_partname $partname]"
	    #
	    #  Build macro
	    set ugstfile [cmd_tempfile ugstart .macro]
	    set fp [open $ugstfile w]
 	    puts $fp "Unigraphics V15.0.2.2"
 	    puts $fp "Macro File:  $ugstfile"
 	    puts $fp "Macro Version 5.00"
 	    puts $fp "Graphics Driver:   XWI"
 	    puts $fp "Created by awm on Sat Aug  5 07:48:00 2000"
 	    puts $fp "Devices: None"
 	    puts $fp "Part Name Display Style: \$FILENAME"
 	    puts $fp "Selection Parameters 0 2 0.250000 0"
 	    puts $fp "Display Parameters 1.000000 8.396900 6.351501 -1.000000 -0.756410 1.000000 0.756410"
 	    puts $fp "*****************"
 	    puts $fp "RESET"
 	    puts $fp "MENU, 0, ICEMCFD UG_GATEWAY_MAIN_MENUBAR ! "
 	    puts $fp "DIALOG_BEGIN \"ICCONFIG\""
	    close $fp
	    set cmdargs "-key=$ugstfile"
        } else {
	    mess "    Creating new UG file $partname\n"
	    set pn ""
	    set ugstfile [cmd_tempfile ugstart .macro]
	    set fp [open $ugstfile w]
 	    puts $fp "Unigraphics V15.0.2.2"
 	    puts $fp "Macro File:  $ugstfile"
 	    puts $fp "Macro Version 5.00"
 	    puts $fp "Graphics Driver:   XWI"
 	    puts $fp "Created by awm on Sat Aug  5 07:54:23 2000"
 	    puts $fp "Devices: None"
 	    puts $fp "Part Name Display Style: \$FILENAME"
 	    puts $fp "Selection Parameters 0 2 0.250000 0"
 	    puts $fp "Display Parameters 1.000000 8.396900 6.351501 -1.000000 -0.756410 1.000000 0.756410"
	    puts $fp "*****************"
	    puts $fp "RESET"
	    puts $fp "MENU, 0, UG_FILE_NEW UG_GATEWAY_MAIN_MENUBAR !"
	    puts $fp "FILE_BOX -2, $partname  0 ! New Part File"
            puts $fp "SET_VALUE: 0 ! FSB item"
	    close $fp
	    set cmdargs "-key=$ugstfile"
        }
    } else {
	#
	#  Prepare environment variables for user specified launch script
	#
	if [info exists env(ICMIF_ACN)] {
	    set env(UGII_VENDOR_DIR) $env(ICMIF_ACN)/lib/ug/ug$ug_options(ver)_vendor_dir
	} else {
	    set env(UGII_VENDOR_DIR) $env(ICEM_ACN)/dif/ug/lib/ug/ug$ug_options(ver)_vendor_dir
	}
	mess "Using Mesher Interface for Unigraphics version $ug_options(ver)\n"
    }
    
           
    set ug_path $ug_options(lnch)
    
    #
    #  Might want a macro here to launch the mesher interface
    if [string match $ug_options(lnch_def) $ug_options(lnch)] {
	ic_run_application_command . "" $ug_path "$cmdargs \"$pn\""
	if {$saved_ug_base_dir != ""} {
	    set env(UGII_BASE_DIR) $saved_ug_base_dir
	}
	if {$saved_ug_lic_file != ""} {
	    set env(UGII_BASE_DIR) $saved_ug_lic_file
	}
    } else {
	ic_run_application_command . "" $ug_path ""
    }
}    

proc ug_options {{tt ""} {hd 1}} {
    global ug_options
    
}

proc ug_pick_part {dir islocal exists} {
    global part_selected
    
    set pts ""
    foreach pp [ug_list_parts $dir] {
	lappend pts [lindex $pp 0]
    }
    if ![lmember $pts $part_selected] { set part_selected "" }
    
    if $islocal {
	set place ./ugparts
    } else {
	set place PROJECT_DIR/ugparts
    }
    if $exists {
	set mess "Select a part from $place."
    } else {
	set mess "Select a part from $place\nor enter a new name."
    }
    set p [choose_one $mess $pts $part_selected]
    if {$p != ""} { set part_selected $p }
    return $p
}

proc ug_list_parts {dir} {
    global env
        
    set parts [glob -nocomplain $dir/*.prt]
    return [lsort $parts]
}

proc ug_partname {pn} {
    global is_win
    
    if $is_win {
	set a [regsub -all " " $pn _ pn]
	set b [regsub -all "\t" $pn _ pn]
	if {$a || $b} {
	    mess "Warning: on windows UG part names can't have spaces.\n" red
	}
	return $pn
    } else {
	return $pn
    }
}

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

proc ug_select_part_full {which exists} {
    global file_name parts_dir_local part_selected
    global ug_part_selected_local ug_part_selected_project
    
    set d [form_init .select_ug_part_$which "Select ug part" file \
	      "accept cancel"]
    if {$d != ""} {
	set applist list
	if {(($which == "local") || ($which == "both")) && \
		[file exists ./ugparts]} {
	    form_listbox $d.local "Parts in\n./ugparts" "" \
		"ug_select_part_full_set $d local" \
		"form_accept_hack .select_ug_part_$which; list" \
		{20x10} {1 0 -cols 2}
	    form_label $d.locall "Part:" {2 0}
	    form_entry $d.locale ug_part_selected_local "" {2 1}
	    $d.locale configure -width 15
	    set mgeo "0 0 -cols 2"
	}
	if {($which == "project") || ($which == "both")} {
	    form_listbox $d.project "Parts in\nPROJECT_DIR/ugparts" "" \
		"ug_select_part_full_set $d project" \
		"form_accept_hack .select_ug_part_$which; list" \
		{20x10} {1 2 -cols 2}
	    form_label $d.projectl "Part:" {2 2}
	    form_entry $d.projecte ug_part_selected_project "" {2 3}
	    $d.projecte configure -width 15
	    set mgeo "0 2 -cols 2"
	}
	if {$which == "both"} {
	    set mgeo "0 0 -cols 4"
	    set applist "appropriate list"
	}
	form_label $d.lab "Select a part from the $applist or\nenter\
		a new part name below." $mgeo
	
	form_finish +300+300
    }
    
    set ug_part_selected_local ""
    set ug_part_selected_project ""
        
    if [winfo exists .select_ug_part_$which.main.local] {
	set pts_l ""
	if [file exists ./ugparts] {
	    foreach pp [ug_list_parts ./ugparts] {
		lappend pts_l "[lindex $pp 0] \([lindex $pp 2]\)"
	    }
	}
	form_listbox_set .select_ug_part_$which.main.local $pts_l
    }
    if [winfo exists .select_ug_part_$which.main.project] {
	set pts_p ""
	if [file exists $file_name(ugparts_dir)] {
	    foreach pp [ug_list_parts $file_name(ugparts_dir)] {
		lappend pts_p "[lindex $pp 0] \([lindex $pp 2]\)"
	    }
	}
	form_listbox_set .select_ug_part_$which.main.project $pts_p
    }
    
    if ![form_wait .select_ug_part_$which] { return "" }
    
    if {$ug_part_selected_local != ""} {
	set dir ./ugparts
	set partname $ug_part_selected_local
	set parts_dir_local 1
    } elseif {$ug_part_selected_project != ""} {
	set dir $file_name(ugparts_dir)
	set partname $ug_part_selected_project
	set parts_dir_local 0
    } else {
	return ""
    }
    
    set part_selected $partname
    
    set file $dir/$partname
    if {$file == ""} {
	if $exists {
	    error "You must select an existing part"
	} else {
	    set file $dir/$partname
	    if ![file exists $dir] { ic_mkdir $dir }
	}
    }
    return [list $dir $partname $file]
}

proc ug_select_part_full_set {d which name} {
    global ug_part_selected_local ug_part_selected_project
    
    switch $which {
	local {
	    set snum [lindex $name end]
	    regsub -all {\(} $snum "" snum
	    regsub -all {\)} $snum "" snum
	    regsub -all " \{\}" [lreplace $name end end ""] "" name
	    set ug_part_selected_local $name
	    set ug_part_selected_project ""
	    if [winfo exists $d.project] {
		form_listbox_select $d.project ""
	    }
	}
	project {
	    set snum [lindex $name end]
	    regsub -all {\(} $snum "" snum
	    regsub -all {\)} $snum "" snum
	    regsub -all " \{\}" [lreplace $name end end ""] "" name
	    set ug_part_selected_project $name
	    set ug_part_selected_local ""
	    if [winfo exists $d.local] {
		form_listbox_select $d.local ""
	    }
	}
    }
}

# --------

proc nice_partname {} {
    global part_selected
    if {$part_selected == ""} {return anonymous_part}
    set pn $part_selected
    regsub -all " |\t" $pn _ pn
    regsub -all {\\} $pn {\\\\} pn
    return [string tolower $pn]
}
