
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Stuff to handle the lists of parts in the manager.

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

proc select_part {win num list} {
    global partname partfile sheetname
    unselect_part
    set partnum [$win nearest $num]
    set part [lindex $list $partnum]
    if {$part == ""} return
    set partname [lindex $part 0]
    set partfile [lindex $part 1]
    set sheetname [lindex $part 2]
    $win select set $partnum
    infomsg "Partfile is now $partfile"
}

proc P {} { uplevel #0 source ../iceman/partlist.tcl }

# Locate and select the part.  Returns 0 if it wasn't there.

proc locate_part {name parts_list list} {
    global partname sheetname partfile batch
    unselect_part
    set n 0
    foreach i $parts_list {
	if {![string compare [string tolower $name] \
		[string tolower [lindex $i 0]]]} {
	    set partname \"[lindex $i 0]\"
	    set partfile [lindex $i 1]
	    set sheetname [lindex $i 2]
	    if !$batch {
		$list select set $n
	    }
	    return 1
	}
	incr n
    }
    set partname ""
    set partfile ""
    set sheetname ""
    return 0
}

proc set_part_partfile {partf} {
    global partname sheetname partfile local_parts global_parts
    
    set n 0
    foreach i $local_parts {
	if {$partf == [lindex $i 1]} {
	    unselect_part
	    set partname \"[lindex $i 0]\"
	    set partfile [lindex $i 1]
	    set sheetname [lindex $i 2]
	    .parts.l.list select set $n
	    return 1
	}
	incr n
    }
    set n 0
    foreach i $global_parts {
	if {$partf == [lindex $i 1]} {
	    unselect_part
	    set partname \"[lindex $i 0]\"
	    set partfile [lindex $i 1]
	    set sheetname [lindex $i 2]
	    .parts.g.list select set $n
	    return 1
	}
	incr n
    }
    return 0
}

proc part_is_mulcad {} {
    global partfile
    return [string match "mulcad/*" $partfile]
}

proc part_is_comak {} {
    global partfile
    return [string match "objects/*" $partfile]
}

# Which is d for ddn, m for mulcad, k for comak, and - for other.
# If callup is 1, then run icemup first.  This is useful for moving parts, etc.

proc update_partlist {which callup} {
    global place global_parts local_parts comak_parts
    global partname sheetname partfile confname geoname
    global partlist_inactivecolor batch
    
    if !$batch {
	.parts.g.list delete 0 end
	.parts.l.list delete 0 end
	.parts.k.list delete 0 end
    }
    if $callup {
	infomsg "Updating DDN directories..."
    }
    disable_interface
    
    set global_parts [list_parts parts $callup]
    set local_parts [list_parts $place/parts $callup]
    set comak_parts [list_parts objects/$confname/$geoname $callup]
    if !$batch {
	foreach p $global_parts {
	    .parts.g.list insert end \
		[format "%s (%s)" [lindex $p 0] [lindex $p 2]]
	}
	foreach p $local_parts {
	    .parts.l.list insert end \
		[format "%s (%s)" [lindex $p 0] [lindex $p 2]]
	}
	foreach p $comak_parts {
	    .parts.k.list insert end \
		[format "%s (%s)" [lindex $p 0] [lindex $p 2]]
	}
    }
    
    cmd_rm .ddn_parts
    set ff [open .ddn_parts w]
    foreach p $global_parts {
	puts $ff "[lindex $p 0] [lindex $p 2]"
    }
    close $ff
    
    if {($partfile != "") && ![file exists $partfile]} {
	# This means the part was deleted.
	set partfile ""
	set partname ""
	set sheetname ""
    } else { if {($partfile == "") && ($partname != "")} {
	# The part should have been created.
	case $which in {
	    d {
		if {![locate_part $partname $global_parts \
		      .parts.g.list]} {
			  warningmsg "DDN didn't create the part."
		      }
	    }
	    m {
		if {![locate_part $partname $local_parts \
		      .parts.l.list]} {
			  warningmsg "MULCAD didn't create the part."
		      }
	    }
	    k {
		if {![locate_part $partname $comak_parts \
		      .parts.k.list]} {
			  warningmsg "Comak didn't create the part."
	      }
	  }
      }
}   }

    if $callup {infomsg "Done."}
    enable_interface
}

proc update_iges {} {
    .parts.i.list delete 0 end
    set iparts [glob -nocomplain iges/*]
    foreach ip $iparts {
	set f [file tail $ip]
	if {($f != "iges_list") && ($f != "_tmp_ddn_part_")} {.parts.i.list insert end $f}
    }
}

proc update_dxf {} {
    .parts.d.list delete 0 end
    set iparts [glob -nocomplain dxf/*]
    foreach ip $iparts {
 	set f [file tail $ip]
 	if {$f != "dxf_list"} {.parts.d.list insert end $f}
    }
}

proc update_tvda {} {
    .parts.v.list delete 0 end
    set vparts [glob -nocomplain tvda/*]
    foreach vp $vparts {
	set f [file tail $vp]
	if {$f != "tvda_list"} {.parts.v.list insert end $f}
    }
}

proc update_set {} {
    .parts.s.list delete 0 end
    set sparts [glob -nocomplain set/*]
    foreach sp $sparts {
	set f [file tail $sp]
	if {$f != "set_list"} {.parts.s.list insert end $f}
    }
}
 
proc update_prebfc {} {
    .parts.p.list delete 0 end
    set pparts [glob -nocomplain prebfc/*]
    foreach ip $pparts {
	set f [file tail $ip]
	if {$f != "prebfc_list"} {.parts.p.list insert end $f}
    }
}

proc unselect_part {} {
    global partname sheetname partfile batch
    set partname ""
    set partfile ""
    set sheetname ""
    if !$batch {
	.parts.l.list select clear 0 end
	.parts.g.list select clear 0 end
	.parts.k.list select clear 0 end
    }
}

# Return the pathname of this part, if we know about it, else return "".

proc find_part {pname} {
    global global_parts local_parts comak_parts
    foreach p $global_parts {
	if {[lindex $p 0] == $pname} {return [lindex $p 1]}
    }
    foreach p $local_parts {
	if {[lindex $p 0] == $pname} {return [lindex $p 1]}
    }
    foreach p $comak_parts {
	if {[lindex $p 0] == $pname} {return [lindex $p 1]}
    }
    return ""
}

proc pick_part {pinfo} {
    global partname sheetname waiting_for_part icem_fluent
    
    set waiting_for_part 1
    set pname [query_enter_or_waitvar .query "Input the name of a new\
	    part, or select an existing one from the $pinfo list below." {}]
    set waiting_for_part 0
    if {($pname == "*cancel*") || ($pname == "")} {return 0}
    if {$pname == "*okay*"} {return 1}

    set sname 1
    set partname $pname
    set sheetname $sname
    return 1
}

proc partfile_is_binary {name} {
    set fd [open $name]
    set line [read $fd 80]
    close $fd
    if {([string first "\n" $line] >= 0) && ([string first "\0" $line] == -1)} {
	return 0
    } {
	return 1
    }
}

set complained_once_partinfo 0

proc list_parts {place callup} {
    global env ddn_directory is_win
    
    if ![file exists $place] return
    
    if {$is_win || ([file exists $place/$ddn_directory] && \
	    ![file size $place/$ddn_directory])} {
	cmd_rm $place/$ddn_directory
    }
    if ![file exists $place/$ddn_directory] { set callup 1 }
    
    return [list_parts_ddndir $place $callup]
}

proc list_parts_ddndir {place callup} {
    global env
    
    if $callup {
	set stuff [check_exec $env(ICEM_ACN)/bin/ddndir -w $place]
    } else {
	set stuff [check_exec $env(ICEM_ACN)/bin/ddndir -r $place]
    }
    if {$stuff == ""} return
    
    set warns [lindex $stuff 2]
    if {$warns != ""} {
	puts $warns
    }
    
    set parts ""
    foreach ent [lindex $stuff 1] {
	foreach line $ent {
	    regsub -all {\\} $line {\\\\} line
	    switch [lindex $line 0] {
		type  { set type [lindex $line 1] }
		name  { set name [string trim [lindex $line 1]] }
		sheet { set sheet [lindex $line 1] }
		file  { set file [string trim [lindex $line 1]] }
	    }
	}
	if {$type != 1} continue
	lappend parts [list $name $place/$file $sheet]
    }
    return [lsort $parts]
}
