
# This is the projlib stuff that is shared between med and autohexa.
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc make_projlib {file {l_material 1} {l_surface 1} {l_curve 1} 
		   {l_point 1} {l_loop 1} {l_density 1} {l_quiet 0}} {
    global projlib load_tetin_triangle_tolerance tetin_try_binary
    global tetin_keep_model_params batch_mode fam_visible 
    global proj_last_loaded_objects
    global errorInfo projlib_no_read_boco_file tdv_default
    
    if ![info exists projlib] { set projlib "" }
    
    if ![check_geom_functions_enabled] {
	license_unavailable_warning
	return 0
    }
    
    if {($projlib != "") && [info exists tetin_keep_model_params]} {
	set keepr $tetin_keep_model_params
    } else {
	set keepr 0
    }
    
    set mod 1
    if {$projlib == ""} {
	set projlib [projlib_interface]
	if !$batch_mode {
	    $tdv_default add_image $projlib
	    
	    # The projlib has to have all these tags so we can pick stuff that
	    # is visible only in subsets
	    foreach t {entity point curve surface body material loop} {
		$tdv_default add_tag $projlib geom_$t
	    }
	}
	set mod 0
    }
    if [info exists projlib_no_read_boco_file] {
	$projlib configure -ignore_boco_files $projlib_no_read_boco_file
    }
    catch {unset proj_last_loaded_objects}
    if {$file != ""} {
	foreach t {curve surface point material} {
	    foreach p [$projlib list_objects $t even_if_dormant] {
		set was_there($t,$p) 1
	    }
	}
	busy 1
	if [catch {
	    set ofile [$projlib get -surface_file]
	    set args ""
	    foreach t {material surface curve point loop density} {
		if { [ set l_$t ] == 0 } {
			append args " -skip_$t"
		}
	    }
	    if {$l_quiet} {
	        append args " -quiet"
	    }
	    eval $projlib load_model \"$file\" $load_tetin_triangle_tolerance \
		$tetin_try_binary $keepr $args
	    
	    if {$ofile != ""} { $projlib configure -surface_file "" }
	} err] {
	    mess "$err\n" red
	    puts $errorInfo
	    $projlib delete
	    set projlib ""
	    busy 0
	    return 0
	}
	foreach t {curve surface point material} {
	    set proj_last_loaded_objects($t) ""
	    foreach p [$projlib list_objects $t] {
		if ![info exists was_there($t,$p)] {
		    lappend proj_last_loaded_objects($t) $p
		}
	    }
	}
	foreach fam [$projlib list_families 0] {
	    if ![info exists fam_visible($fam)] {
		set fam_visible($fam) 1
	    }
	}
	if [info exists fam_visible(ORFN)] {
	    set fam_visible(ORFN) 0
	}
	define_geom_objects
	$projlib configure -modified $mod
	$projlib signal_change 1
	busy 0
    } else {
	$projlib initialize
    }
    return 1
}

proc reinit_geom_objects {} {
    global projlib
    
    busy 1
    clear_geom_objects
    define_geom_objects
    busy 0
}

proc delete_projlib {} {
    global projlib uns_cur str_cur
    
    if {$projlib == ""} return
    # must delete bsp images before unload model
    clear_geom_objects    
    $projlib unload_model
    if {(![info exists uns_cur] || ($uns_cur == "")) &&
	(![info exists str_cur] || ($str_cur == ""))} {
	$projlib delete_all_families
    }
    $projlib delete
    set projlib ""
}

proc update_bsp_consistency {} {
    # 05/23/07 VG:
    # Some Undo crashes may be avoided keeping old object numbers.
    # For SP11 some functions with big geometry may be slow now.

    reinit_geom_objects
    return

    # Keep function: May be used if Undo will be updated. 
    # This function is a replacement for clear/define_geom_objects
    # e.g. in ic_build_topo
    # Clear/define are too slow when there are many [invisible] objects
    # Methods:
    # Delete bsp only if NO geometry with this number
    # Create bsp only if there is geometry with this number   
    #
    global projlib batch_mode 

    if {$projlib == ""} return
    #
    if !$batch_mode {
        set pa [tdv_active 0]
        busy 1
    }
    foreach type {surface curve point} {
	set bsps [$projlib check_bsp_consistency $type delete]
	foreach bsp $bsps {
	    $bsp delete
	}
	set nums [$projlib check_bsp_consistency $type create]
	foreach num $nums {
	    set bsp [define_geom_object $type $num]			
	}
	# temporary fix- update color [for dormant]
	foreach bsp [$projlib list_bsp_objects $type only_check_image even_if_dormant] {
	    $bsp configure -only_to_change_color 1
	}		
    }
    foreach type {material loop density body} {
	clear_geom_objects $type
	define_geom_objects $type
    }
    if !$batch_mode {
        busy 0
	update_fam_window
	update_geom_visible all a
 	tdv_active $pa
    }
    $projlib signal_change 1
}

proc clear_geom_objects {{type ""}} {
    global projlib
    
    if {$type == ""} {
	foreach type {surface curve point material density loop body} {
	    clear_geom_objects $type
	}
    } else {
	foreach bsp [$projlib list_bsp_objects $type only_check_image even_if_dormant] {
	    $bsp delete
	}
	clear_fam_visible_prev $type
    }
}

# Clear the fam_visible_prev list so that it objects can be added
# in define_geom_object. This is called from clear_objects.

proc clear_fam_visible_prev { what } {
    global fam_visible fam_visible_prev

    foreach f [array names fam_visible] {
        if [info exists fam_visible_prev($what,$f)]  {
            unset fam_visible_prev($what,$f)
        }
    }
}

proc define_geom_objects {{type ""}} {
    global projlib batch_mode
    
    if {$projlib == ""} return
    if !$batch_mode {set pa [tdv_active 0]}
    if {$type == ""} {
	foreach type {surface curve point material loop density body} {
	    define_geom_objects $type
	}
	if !$batch_mode {
	    catch {
	        # this could also get called from autohex
	        update_fam_window
	        update_geom_visible all a
	    }
	}
    } else {
	if {$type == "body"} {
	    set nums [$projlib list_objects body]
	    foreach num $nums {
	    	define_geom_object body $num
	    }
	    if !$batch_mode {tdv_active $pa}
	    return	
	}
	set nums [$projlib list_objects $type even_if_dormant]
	# first create object with max number 	    
	if [llength $nums] {	    
	    define_geom_object $type [lindex $nums end]
	    foreach num $nums {
	    	define_geom_object $type $num
	    }
    	}
    }
    if !$batch_mode {tdv_active $pa}
}

proc define_geom_objects2 {type new_obj_names} {
    global projlib
    if { $type != "curve" && $type != "point" && $type != "surface" } {
        error "invalid type $type"
    }
    foreach i $new_obj_names {
        set num [$projlib get_number $type $i]
        if { $num < 0 } {
            mess "$num returned from projlib get_number $type $i\n"
            continue
        }
        set obj [$projlib get_object $type $num]
        if {$obj == ""} {
            set obj [define_geom_object $type $num]
        }
        $obj configure -visible 1 -pickable 1
        update_geom_visible_obj $obj
    }
}

proc define_geom_object {type num} {
    global tdv_default
    global projlib fam_visible blank_family_color
    global data_available tetin_load_rename_family batch_mode
    
    if { $num < 0 } {
	error "num = $num < 0"
    }
    set bsp [$projlib get_object $type $num]
    if {$bsp != ""} {return $bsp}
    
    set fam [$projlib get_family $type $num]
    if [info exists tetin_load_rename_family($fam)] {
	set fam $tetin_load_rename_family($fam)
	$projlib set_family $type $num $fam
    }
    if {$fam == ""} {
	# must be a density
	set col $blank_family_color
	set vis [get_geom_visible $type]
    } else {
	if ![info exists fam_visible($fam)] {
	    set fam_visible($fam) 1
	}
	set vis [expr $fam_visible($fam) && [get_geom_visible $type]]
	set col [family_color_for_name $fam]
    }
    set name [$projlib get_name $type $num]
    set bsp [bspline_object -family $fam -name $name -type $type \
		 -visible $vis -color $col -number $num -priority 2]
    $bsp set_object_num $type $num
    $projlib set_bspline_object