
# Generic stuff.

# Copyright (c) 1993, 1994 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California

proc model_check {{noisy 1} {from_meshing 0}} {
    global smallest_gap changelist_doit changelist_show
    global dont_perform_gap_check domain already_warned_small_tol
    global smallest_feature smallest_feature_desc
    global app_has_before_solve app_has_verify
    
    set ttol [grid_get_ttol]

    edit_modify_params
    
    if $noisy {
	mess "Checking objects ---------------\n"
    }
    set np 0; set no 0
    
    catch {unset smallest_feature}
    catch {unset smallest_feature_desc}
    
    foreach obj [db_list_objects_recursive] {
	if {[$obj getval mat_lib_path ""] != ""} continue
	set obt [$obj getval obtype]
	
	set vf ""
	if [info exists app_has_before_solve($obt)] {
	    set vf app_before_solve_$obt
	} elseif [info exists app_has_verify($obt)] {
	    set vf app_verify_$obt
	}
	
	if {$vf != ""} {
	    set ret [$vf $obj]
	    if {$ret != ""} {
		mess "[$obj getval obtype] [$obj getval name]:" red
		mess "$ret\n" red
		if {$from_meshing && ([$obj getval obtype] == "part")} {
		    # These are warnings about things touching. 
		    # In many cases they are okay so don't stop here
		} else {
		    incr np
		}
	    }
	}
	incr no
	
	set msp [meshing_part_containing_object $obj]
	foreach sh [db_shapes $obj] {
 	    if {[$sh get -shtype] != "container"} {
		set obj [$sh get -object]
		set ret [$sh verify general]
		if {$ret != ""} {
		    mess "[$obj getval obtype] [$obj getval name]:" red
		    mess "$ret\n" red
		    incr np
		}
		foreach dir {x y z} {
		    set sm [$sh getval dim_$dir]
		    if {($sm > 0) && (![info exists smallest_feature($msp,$dir)] ||
				      ($smallest_feature($msp,$dir) > $sm))} {
			set smallest_feature($msp,$dir) $sm
			set smallest_feature_desc($msp,$dir) [$obj getval name]
			if {[llength [$obj getval section_list]] > 1} {
			    append smallest_feature_desc($msp,$dir) " ([$sh get -name])"
			}
		    }
		}
		lappend shapes_in($msp) $sh
 	    }
 	}
    }
    catch {unset smallest_gap}
    foreach msp [array names shapes_in] {
	set smallest_gap($msp) [check_key_points smgap $shapes_in($msp) $ttol]
    }
    if $noisy {
	mess "Check: $np problem[expr {($np == 1) ? " was" : "s were"}]\
		found for $no object[expr {($no == 1) ? "" : "s"}].\n"
    }
    return $np
}
