
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
# $Revision$ on $Date$

proc geo_show_edit {what names on {color white}} {
    global geo_show_edit options
    foreach name $names {
	    ic_show_geo_selected $what $name $on $color
	    if $on {
	        if {$what == "surface"} {
		        set geo_show_edit($what,$name) [list simple [ic_geo_get_config_param $what $name simple]]
		        if $options(show_tris) {
		            ic_geo_set_config_param $what $name simple 0
		        }
	        }
	    } elseif [info exists geo_show_edit($what,$name)] {
	        eval ic_geo_set_config_param $what $name $geo_show_edit($what,$name)
	    }
    }
}

proc geo_modify_object_add {what npts item} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1

    ic_undo_group_begin
    set preselect ""
    while 1 {
	    set pts [select_locations $npts "$item vertices" $preselect 1 1 ]
	    if {$pts == ""} break
	    ic_geo_add_segment $what $surf $item $pts
	    if {$npts == 2} {
	        set preselect [list [lindex $pts 1]]
	    }
    }
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
    ic_undo_group_end
}

proc geo_modify_object_delete {what item} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    ic_undo_group_begin
    while 1 {
	set pts [geo_select_segments $what $surf seg]
	if {$pts == ""} break
	ic_geo_delete_segments $what $surf $item $pts
    }
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
    ic_undo_group_end
    ic_geo_delete_if_empty $what $surf
}

proc geo_modify_object_restrict {what item} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    ic_undo_group_begin
    while 1 {
	set pts [geo_select_segments $what $surf seg]
	if {$pts == ""} break
	ic_geo_restrict_segments $what $surf $item $pts
    }
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
    ic_undo_group_end
    ic_geo_delete_if_empty $what $surf
}

proc geo_modify_splitseg {what item how} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    ic_undo_group_begin
    while 1 {
	set pts [geo_select_segments $what $surf seg 0 0 1]
	if {$pts == ""} break
	ic_geo_split_segments $what $surf $item $how $pts
    }
    ic_geo_finish_edit $what $surf
    ic_undo_group_end
    geo_show_edit $what $surf 0
}

proc geo_modify_movenode {what proj_to} {
    global geo_modify
    global tdv_default

    if {$proj_to == "loc"} {
	# This one is different.  Move nodes from all surfaces to a selected
	# location.
	while 1 {
	    set pt [disp_get_location "" 1 "the destination" 1]
	    if {$pt == ""} return
	    set im [tdv_point_im -point $pt -size 3 -color white]
	    $tdv_default add_image $im
	    set nodes [geo_select_segments all_${what}s "" node 0 0 0 0]
	    foreach nn $nodes {
		if 0 {
		    set obj [lindex $nn 0]
		    set name [object_name_of_image $obj]
		} else {
		    set name [lindex $nn 0]
		}
		ic_geo_highlight_segments $what $name remove node all
		ic_geo_move_node $what $name [lindex $nn 1] $pt
		ic_geo_finish_edit $what $name
	    }
	    $im delete
	    if {$nodes == ""} return
	}
    }
    
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    
    switch $proj_to {
	plane {
	    set d [form_init .move_in_plane "Move in plane"]
	    if {$d != ""} {
		form_label $d.norml "Normal" {0 0}
		form_entry $d.norm geo_modify(move_norm) coords {0 1}
		form_button $d.sel3 "Select via 3 points" \
		    "med_get_plane_normal_3pts geo_modify(move_norm)" {1 0 -cols 2}
		form_finish +400+400
	    }
	    if ![form_wait .move_in_plane] {
		geo_show_edit $what $surf 0
		return
	    }
	    eval lappend proj_to $geo_modify(move_norm)
	}
	line {
	    set d [form_init .move_on_line "Move on line"]
	    if {$d != ""} {
		form_label $d.p1l "Point 1" {0 0}
		form_label $d.p2l "Point 2" {1 0}
		form_entry $d.p1 geo_modify(move_pt1) string {0 1}
		form_entry $d.p2 geo_modify(move_pt2) string {1 1}
		$d.p1 configure -width 6; $d.p2 configure -width 6
		form_button $d.p1s "Select" \
		    "med_get_local_location geo_modify(move_pt1)" {0 2}
		form_button $d.p2s "Select" \
		    "med_get_local_location geo_modify(move_pt2)" {1 2}
		form_finish +400+400
	    }
	    if ![form_wait .move_on_line] {
		geo_show_edit $what $surf 0
		return
	    }
	    eval lappend proj_to [ic_coords_into_global $geo_modify(move_pt1)] \
		[ic_coords_into_global $geo_modify(move_pt2)]
	}
	dir {
	    set d [form_init .move_in_dir "Move in direction"]
	    if {$d != ""} {
		form_label $d.vecl "Direction" {0 0}
		form_entry $d.vec geo_modify(move_vec) coords {0 1}
		form_button $d.sel2 "Select via 2 points" \
		    "med_get_line_dir_2pts geo_modify(move_vec)" {1 0 -cols 2}
		form_finish +400+400
	    }
	    if ![form_wait .move_in_dir] {
		geo_show_edit $what $surf 0
		return
	    }
	    eval lappend proj_to $geo_modify(move_vec)
	}
	surface {
	    set on_what [geo_select surface 1 "the constraint surface"]
	    if {$on_what == ""} {
		geo_show_edit $what $surf 0
		return
	    }
	    lappend proj_to $on_what
	}
    }
    
    while 1 {
	set pts [geo_select_segments $what $surf node 1]
	if {[lindex $pts 0] == ""} break
	select_move_geo_nodes $what $surf [lindex $pts 0] \
	    $proj_to [lindex $pts 1]
	ic_geo_highlight_segments $what $surf remove nodes all
    }
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
}

proc geo_modify_mergenode {what} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    if {$what == "curve"} {
       geo_show_edit $what $surf 0
       ic_highlight curve $surf color ""
    } else {
       geo_show_edit $what $surf 1
    }
    # Now select the reference surfaces.
    set refs [geo_select $what 0 "reference ${what}s" 0 0]
    foreach r $refs {
        geo_show_edit $what $r 1 green
    }
    # second selection [preview] unhighlights $surf
    if {$what == "curve"} {
        ic_highlight curve $surf color ""
    }
    #ic_undo_group_begin
    set resume 0
    if ![ic_undo_suspended] {
        ic_save_model_for_undo
        ic_undo_suspend
        set resume 1
    }
    while 1 {
        if {$refs == ""} {
            set pts [geo_select_segments $what $surf node 0 1]
            if {[lindex $pts 0] == ""} break
            ic_geo_merge_nodes $what $surf $pts
        } else {
            set npt [disp_get_location "" 1 "the point" 1]
            if {$npt == ""} break	    
            set pts [geo_select_segments $what $surf node 0 0]
            if {$pts == ""} break
            ic_geo_move_node $what $surf $pts $npt
        }
        ic_geo_highlight_segments $what $surf remove nodes all
    }
    if {$what == "curve"} ic_highlight
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
    foreach r $refs {
        geo_show_edit $what $r 0
    }
    #ic_undo_group_end
    if $resume {
        ic_undo_resume
    }
    ic_geo_delete_if_empty $what $surf
}

proc geo_modify_mergenode_tol {what {tol ""}} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    
    ic_undo_group_begin    
	if {$tol == ""} {     
    set tol [expr [ic_geo_smallest_segment $what $surf] * 0.01]
    set tol [question "Merging tolerance?" $tol]
	}
    if {$tol != ""} {
	ic_geo_merge_nodes_tol $what $surf $tol
    }
    geo_show_edit $what $surf 0
    ic_undo_group_end
    ic_geo_delete_if_empty $what $surf
}

proc geo_modify_splitedge {what} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    
    ic_undo_group_begin
    while 1 {
	set pts [geo_select_segments $what $surf edge 0 0 1]
	if {$pts == ""} break
	ic_geo_split_edges $what $surf $pts
    }
    ic_geo_finish_edit $what $surf
    ic_undo_group_end

    geo_show_edit $what $surf 0
}

proc geo_modify_swapedge {what} {
    set surf [geo_select $what 1 "" 0 1]
    if {$surf == ""} return
    if {[llength $surf] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf 1
    while 1 {
	set pts [geo_select_segments $what $surf edge]
	if {$pts == ""} break
	ic_geo_swap_edges $what $surf $pts
    }
    ic_geo_finish_edit $what $surf
    geo_show_edit $what $surf 0
}

proc geo_modify_moveseg {what item to_new {newfam ""} {name ""}} {
# to_new - flag to move segment to new curve
# newfam - if empty -call family selection dialog
# name   - if not empty - name of new curve [or autoname]
    set surf1 [geo_select $what 1 "the $what to move from" 0 1]
    if {$surf1 == ""} return
    if {[llength $surf1] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf1 1
    if $to_new {
        if {$newfam == ""} {
	        set fam [select_families 0 0 1]
	        if {$fam == ""} {
	            geo_show_edit $what $surf1 0
	            return
	        }
	    } else {
            set fam $newfam
	    }
	    set nf [ic_geo_new_family $fam]
        set surf2 [ic_geo_create_empty_unstruct_${what} $name $fam]
	    if $nf {
	        update_fam_window
	        catch { ::AIEnv::GUIManagement::Tree::AddGroup part $fam }
	    }
    } else {
	    set surf2 [geo_select $what 1 "the $what to move to" 2 1 $surf1]
	    if {$surf2 == ""} {
	        geo_show_edit $what $surf1 0
	        return
	    }
    }
    if !$to_new {
        geo_show_edit $what $surf2 1 yellow
    }
    ic_undo_group_begin
    set reset 0
    while 1 {
	    set pts [geo_select_segments $what $surf1 seg]
	    if {$pts == ""} break
	    set reset 1
	    ic_geo_move_segments $what $surf1 $surf2 $item $pts
    }
    if {$to_new && !$reset} return
    ic_geo_finish_edit $what $surf1
    if $reset { ic_geo_finish_edit $what $surf2 }
    geo_show_edit $what $surf1 0
    if $reset { geo_show_edit $what $surf2 0 }
    ic_undo_group_end
    ic_geo_delete_if_empty $what $surf1
    ic_geo_delete_if_empty $what $surf2
}

proc geo_modify_merge {what} {
    set surf1 [geo_select $what 1 "the $what to merge into" 0 1]
    if {$surf1 == ""} return
    if {[llength $surf1] > 1} {
        mess "You selected more than one $what. Only one is allowed.\n" red
        return
    }
    geo_show_edit $what $surf1 1
    
    set surfs [geo_select $what 0 "" 0 1 $surf1]
    if {$surfs == ""} return
    
    busy 1
    if {$what == "surface"} {
	ic_geo_merge_surfaces $surf1 $surfs
    } else {
	ic_geo_merge_objects $what $surf1 $surfs
    }

    ic_delete_geometry $what names $surfs
    ic_geo_finish_edit $what $surf1
    busy 0
    
    geo_show_edit $what $surf1 0
}

proc geo_modify_merge_edges {} {
    set edge1 [geo_select curve 1 "the curve to merge into" 0 0]
    if {$edge1 == ""} return
    if {[llength $edge1] > 1} {
        mess "You selected more than one curve. Only one is allowed.\n" red
        return
    }   
    geo_show_edit curve $edge1 1
    set surfs [geo_select curve 0 "" 0 0 $edge1]
    if {$surfs == ""} return
    set surfs [concat $edge1 $surfs]
    busy 1
    ic_geo_cre_edge_concat $surfs
    busy 0
    geo_show_edit curve $edge1 0
}

proc old__geo_modify_object_addcrease {what} {
    
    while 1 {
	set surf [geo_select $what 1 "" 0 1]
	if {$surf == ""} return
	
	geo_show_edit $what $surf 1
	
	select_geometry_dragpath $surf geo_modify_addcrease_one \
	    geo_modify_addcrease_release
	
	geo_show_edit $what $surf 0
    }
}

proc old__geo_modify_addcrease_one {surf edgelist pt1 pt2 vec} {
    global projlib
    global tdv_default

    # For now hardwire the curve_frac and line tolerance values.
    $projlib split_edges_project [$surf get -number] $edgelist $vec 0.1 1e-4
    $tdv_default redraw
}

proc old__geo_modify_addcrease_release {surf} {
    global projlib
    
    $projlib split_edges_project_end [$surf get -number]
}

proc geo_modify_point_newpos {} {
    while 1 {
	set obj [geo_select point 1]
	if {$obj == ""} return
	geo_show_edit point $obj 1
	set pt [ic_get_point_location $obj]
	set npt [point_question_location $pt]
	if {$npt == ""} return
	ic_set_point_location $obj $npt
	geo_show_edit point $obj 0
    }
}

proc geo_modify_point_merge {} {
    global merge_point_tol
    
    set tol [question "Tolerance?" $merge_point_tol]
    if {$tol == ""} return
    set merge_point_tol $tol
    
    while 1 {
	set objs [geo_select point 0 "all points to merge"]
	if {$objs == ""} return
	
	if {[llength $objs] < 2} return
	ic_undo_group_begin	
	ic_geo_merge_points_tol $objs $tol
	ic_undo_group_end
    }

}

proc geo_modify_point_screen {} {
    while 1 {
	set objs [geo_select point]
	if {$objs == ""} return
	geo_show_edit point $objs 1
	select_move_geo_points $objs point
	geo_show_edit point $objs 0
    }
}

proc geo_modify_material_newpos {} {
    while 1 {
	set obj [geo_select material 1]
	if {$obj == ""} return
	geo_show_edit material $obj 1
	set pt [ic_get_material_location $obj]
	set npt [point_question_location $pt]
	if {$npt == ""} return
	ic_set_material_location $obj $npt
	geo_show_edit material $obj 0
    }
}

proc geo_modify_material_screen {} {
    while 1 {
	set objs [geo_select material]
	if {$objs == ""} return
	geo_show_edit material $objs 1
	select_move_geo_points $objs material
	geo_show_edit material $objs 0
    }
}

proc geo_modify_body_screen {} {
    while 1 {
	set objs [geo_select body]
	if {$objs == ""} return
	geo_show_edit body $objs 1
	select_move_geo_points $objs body
	geo_show_edit body $objs 0
    }
}

proc geo_modify_object_rename {type} {
    while 1 {
	set obj [geo_select $type 1]
	if {$obj == ""} return
	ic_show_geo_selected $type $obj 1
	set name [question_box .new_name Name \
		      "Enter a new name for the $type.\n" $obj]
	if {($name != "") && ($name != $obj)} {
	    ic_geo_set_name $type $obj $name 1
	    set obj $name
	}
	ic_show_geo_selected $type $obj 0
    }
    
}

