# Library functions for the scripting library (scrib)
#
# File: scrib/lib/flanges.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------

##

proc scrib_lib_flanges_find { {max_flange_depth 10} {max_curvature 20} \
	{min_flange_depth 2} {surfs ""} {flange_notch_depth 0} {ignore_tol 0}} {

    if {$surfs == ""} {
	set outer_curves [scrib_lib_geo_find_part_boundary_single]
    } else {
	set outer_curves [scrib_lib_geo_find_part_boundary_single $surfs]
    }
#mess "outer_curves are $outer_curves\n"
    set flanges ""
    set valid_bounds ""
    foreach curve $outer_curves {
	set surf [ic_geo_incident curve $curve 1]
	if {[ic_surface_curvature $surf] > $max_curvature} {
#mess "max_curvature failed for $surf\n"
	    continue
	}
	if {[lsearch $valid_bounds $surf] == -1} {	lappend valid_bounds $surf }
	if ![info exists crv_length($surf)] {set crv_length($surf) 0}
	set crv_length($surf) [expr $crv_length($surf) + [ic_geo_crv_length $curve]]
	if {[expr $crv_length($surf) * $max_flange_depth] < [ic_geo_srf_area $surf]} {
#mess "max_area failed for $surf\n"
	    continue
	}
	if {[expr $crv_length($surf) * $min_flange_depth] > [ic_geo_srf_area $surf]} {
#mess "min_area failed for $surf\n"
	    continue	
	}
	if {[lsearch $flanges $surf] == -1} {lappend flanges $surf}
    }
mess "flanges are found to be $flanges\n"
#exclude dumb flange surfaces 
    set good_flanges ""
    foreach surf $flanges {
	#at least one corner needs to be attached to a single edge
	set bounds [scrib_lib_feature_find_opposite_side $surf "" 1]
	set corners [concat [lindex $bounds 0] [lindex $bounds 2] [lindex $bounds 4] \
		[lindex $bounds 6] ]
	set OK 0
	foreach p $corners {
	    foreach cc [ic_geo_incident point $p 1] {
		if {[llength [ic_geo_incident curve $cc 1]] == 1} {
		    set OK 1
		    break
		}
	    }
	    if $OK break
	}
	if $OK {lappend good_flanges $surf}
    }
     set flanges $good_flanges
mess "good flanges are found to be $flanges\n"
#Find connected regions
    set regions [scrib_lib_geo_make_conn_regions surface $flanges]
    set ret ""
#Jason, This is where it should cut out the flanges, and then make proper regions of flanges.
#I guess the return would need to somehow make any split region now two regions...
    if {$flange_notch_depth != 0} {

if 0 {
	set new_regions ""
	foreach region $regions {
	    set ret [scrib_lib_flanges_cut_out_notches $region $flange_notch_depth]
	    if {$ret != $region} {
		mess "something's happening, return is $ret"  red
	    } else {
		lappend new_regions $region
	    }
	}
	set regions $new_regions
}
    }
#Now it goes and assigns mesh sizes to regions...
    foreach region $regions {
#mess "region is $region\n"
	#find all outer_curves attached to this region.
    	set outer_att_c ""
	set other_att_c ""
	foreach s $region {
	    foreach c [lsort -unique [ic_geo_boundary surface $s 1 1]] {
		if {[lsearch $outer_curves $c] != -1} {
		    lappend outer_att_c $c
		} else {
		    lappend other_att_c $c
		}
	    }
	}
	set inner_att_c [scrib_lib_utils_list_remove_duplicated $other_att_c]
	
	#Need to check to see if we have just one closed region.
	if {[llength $outer_att_c] == [llength $outer_curves] } {
	    if {$surfs == ""} {
		mess "All outer flanges are attached" red
		mess "These are not supported yet" red
		continue
	    }
	}
#Find four corners
#mess "outer_att_c is $outer_att_c\n" debug
#mess "inner_att_c is $inner_att_c\n" debug
#mess "--------------------------\n" debug
	set ofl [scrib_lib_geo_order_conn_curves $outer_att_c 1] 
#mess "ofl is $ofl\n" debug
	set outer_att_c [lindex $ofl 0]
	set first_pt [lindex $ofl 1]
	set last_pt [lindex $ofl 2]
	set used_pts [lindex $ofl 3]

	set ifl [scrib_lib_geo_order_conn_curves $inner_att_c 1 $first_pt ] 
#mess "ifl is $ifl\n" debug
	set inner_att_c [lindex $ifl 0]
	set in_used_pts [lindex $ifl 3]	

#The start point is either the ifp or the ofp; the last_pt is either the ilp or the olp
#				sidec
# 	 ifp x-----------------------------------x ilp
# sidef     |                                            |      sidel
# 	ofp x-----------------------------------x olp
#				sideo
	set ifp ""
	set ofp ""
	set olp ""
	set ilp ""
	set sidef ""
	set sidel ""
	set sidec ""
	set sideo ""
#Find ifp and ofp and find ilp and olp
	set first_surf [ic_geo_incident curve [lindex $outer_att_c 0] 1]		
	set fscp [scrib_lib_feature_find_opposite_side $first_surf "" 1]
	if {$fscp == "" } {
	    mess "not getting 4 corners for surface $first_surf\n"
	    continue
	    #should really probably subtract this surface from fillet region, and then try next one on list
	}
#mess "--------------------------\n" debug
#mess "fscp is $fscp\n" debug
	set last_surf [ic_geo_incident curve [lindex $outer_att_c end] 1]
	set lscp [scrib_lib_feature_find_opposite_side $last_surf "" 1]
	if {$lscp == "" } {
	    mess "not getting 4 corners for surface $last_surf\n"
	    continue
	    #should really probably subtract this surface from fillet region, and then try next one on list
	}
#mess "--------------------------\n" debug
#mess "lscp is $lscp\n" debug

	set opf ""
	foreach p $used_pts {
	    if {[lsearch $fscp $p] == -1} continue
	    lappend opf $p
	}
#mess "opf is $opf\n" debug
	set ipf ""
	foreach p $in_used_pts {
	    if {[lsearch $fscp $p] == -1} continue
	    lappend ipf $p	
	}
#mess "ipf is $ipf\n" debug
	set opl ""
	foreach p $used_pts {
	    if {[lsearch $lscp $p] == -1} continue
	    lappend opl $p
	}
#mess "opl is $opl\n" debug
	set ipl ""
	foreach p $in_used_pts {
	    if {[lsearch $lscp $p] == -1} continue
	    lappend ipl $p	
	}
#mess "ipl is $ipl\n" debug
	if {[lindex $opf 0] != $first_pt} {mess "first_pt $first_pt should be same as [lindex $opf 0]\n" red}
	if {[lindex $ipf 0] != $first_pt} {mess "first_pt $first_pt should be same as [lindex $ipf 0]\n" red}
	if {[lindex $opl end] != $last_pt} {mess "last_pt $last_pt should be same as [lindex $opl end]\n" red}
	if {[lindex $ipl end] != $last_pt} {mess "last_pt $last_pt should be same as [lindex $ipl end]\n" red}

#mess "opf length is [llength $opf]\n" debug
	if {[llength $opf] == 2} {
	    set ofp $first_pt
	    set ifp [lindex $ipf 1]
	    foreach c $inner_att_c {
		lappend sidef $c
		if {[lsearch [ic_geo_boundary curve $c 0 1] $ifp] != -1} {break}
	    }
	}

	if {[llength $opf]  == 3} { 
	    set ifp $first_pt
	    set ofp [lindex $opf 1]
	    foreach c $outer_att_c {
		lappend sidef $c
		if {[lsearch [ic_geo_boundary curve $c 0 1] $ofp] != -1} {break}
	    }
	}

	if {([llength $opf]  == 4) || ([llength $ipf]  == 4)} {
#mess "All Points are on one boundary\n--------------\n" debug
#mess "fscp is $fscp\n" debug
#mess "need to add support of flanges where all points are on one boundary\n" red
#continue
	    set sing_sides [scrib_lib_flanges_orient_sides $fscp]
#mess "sing_sides is $sing_sides\n" debug
	    set sing_sides [scrib_lib_flanges_check_sides $sing_sides $ignore_tol]
#mess "sing_sides is $sing_sides\n" debug

	    set ifp [lindex $sing_sides 0]
	    set sidef [lindex $sing_sides 1]
	    set ofp [lindex $sing_sides 2]	   
	    set sideo [lindex $sing_sides 3]
	    set olp [lindex $sing_sides 4]
	    set sidel [lindex $sing_sides 5]
 	    set ilp [lindex $sing_sides 6]
	    set sidec [lindex $sing_sides 7]
#mess "flanges found to be $region, where four corners are $ifp $ofp $olp $ilp\n" debug
#mess "sidef is $sidef; sidel is $sidel; sideo is $sideo; sidec is $sidec\n" debug
	    if {$flange_notch_depth != 0} {
		if [scrib_lib_geo_curve_proximity_tol $sideo $sidec $flange_notch_depth] continue
	    }
	    lappend ret [list $region $ifp $sidef $ofp $sideo $olp $sidel $ilp $sidec]	    
	    continue
	}
	
	if {[llength $opf] > 4} {mess "Should not be getting more than 4 values for $opf\n" red}
if {[llength $opf] < 2} {mess "Should not be getting less than 2 values for $opf\n" red}
#
#mess "opl length is [llength $opl]\n" debug
if {[llength $opl] == 2} { 
	    set olp $last_pt
	    set ilp [lindex $ipl 1]
	    set getem 0
	    foreach c $inner_att_c {
		if $getem {lappend sidel $c}
		if {[lsearch [ic_geo_boundary curve $c 0 1] $ilp] != -1} {set getem 1}
	    }
	}

	if {[llength $opl]  == 3} {
	    set ilp $last_pt
	    set olp [lindex $opl 1]
	    set getem 0
	    foreach c $outer_att_c {
		if $getem {lappend sidel $c}
		if {[lsearch [ic_geo_boundary curve $c 0 1] $olp] != -1} {set getem 1}
	    }
	}
#mess "last_pt is $last_pt first_pt is $first_pt; outer_att_c is \
# $outer_att_c; inner_att_c is $inner_att_c\n" debug
	if {[llength $ipf] > 4} { mess "Should not be getting more than 4 values for $ipf\n" red }
	if {[llength $ipf] < 2} { mess "Should not be getting less than 2 values for $ipf\n" red }
#Find side surfaces
#side surfaces are those between ifp and ofp; and ilp and olp
	set sidec ""
	foreach c $inner_att_c {
	    if {[lsearch $sidef $c] != -1} continue
	    if {[lsearch $sidel $c] != -1} continue
	    lappend sidec $c
	}

	set sideo ""
	foreach c $outer_att_c {
	    if {[lsearch $sidef $c] != -1} continue
	    if {[lsearch $sidel $c] != -1} continue
	    lappend sideo $c
	}

	if {$sideo == ""} {mess "sideo for $region, was not found\n" red ; continue}
	if {$sidec == ""} {mess "sidec for $region, was not found\n" red ; continue}
	if {$sidef == ""} {mess "sidef for $region, was not found\n" red ; continue}
	if {$sidel == ""} {mess "sidel for $region, was not found\n" red ; continue}
	
	if {$ifp == ""} {mess "ifp was not set properly\n" red ; continue}
	if {$ofp == ""} {mess "ofp was not set properly\n" red ; continue}
	if {$ilp == ""} {mess "ilp was not set properly\n" red ; continue}
	if {$olp == ""} {mess "olp was not set properly\n" red ; continue}

#mess "flanges found to be $region, where four corners are $ifp $ofp $olp $ilp\n" debug
#mess "sidef is $sidef; sidel is $sidel; sideo is $sideo; sidec is $sidec\n" debug

#mess "region found to be $region\n"
	if {$flange_notch_depth != 0} {
	    if [scrib_lib_geo_curve_proximity_tol $sideo $sidec $flange_notch_depth] continue
	}
	lappend ret [list $region $ifp $sidef $ofp $sideo $olp $sidel $ilp $sidec]
    }
#mess "ret is $ret\n"
#Find cutout features
#Match the front curves to back curves.
return $ret
}

proc scrib_lib_flanges_orient_sides {opposite_side_ret} {
#The start point is either the ifp or the ofp; the last_pt is either the ilp or the olp
#				sidec
# 	 ifp x-----------------------------------x ilp
# sidef     |                                            |      sidel
# 	ofp x-----------------------------------x olp
#				sideo

#mess "opposite_side_ret is $opposite_side_ret\n"
    set key_num ""
    set crv_length_num_pt ""
    set length 0
    set i 1
    foreach {point curve} $opposite_side_ret {
	foreach c $curve {
	    if {[llength [ic_geo_incident curve $c 1]] == 1} break
	    set length [expr $length + [ic_geo_crv_length $c]]
	}
	lappend crv_length_num_pt [list $curve $length $i $point]
	incr i		
    }
    set crv_length_num_pt [lsort -index 1 -decreasing -real $crv_length_num_pt]
    set pointer [lindex $crv_length_num_pt 0]
    set num [lindex $pointer 2]
    set crvs [lindex $pointer 0]
    set pt1 [lindex $pointer 3]
    set num1 [expr $num * 2 - 2]
    set num2 [expr $num1 + 2]
    if {$num2 > 7} {set num2 [expr $num2 - 8]}
    foreach c $crvs {
	foreach cc [ic_geo_incident point [lindex $opposite_side_ret $num2] 1] {
	    if {$c == $cc} {set key_num $num2}
	}
    }
    if {$key_num == ""} {set key_num $num1}
    set key_num [expr $key_num - 1]
    set ret  [concat [lreplace $opposite_side_ret 0 $key_num] \
	[lreplace $opposite_side_ret [expr $key_num + 1] end]]

    set OK 1
    foreach curve [lindex $ret 3] {
	if {[llength [ic_geo_incident curve $curve 1]] != 1 } {
	    set OK 0
	    break
	}
    }
#mess "OK is $OK\n --------------\n" debug
    if $OK  {
	return $ret
    } else {
	return [list [lindex $ret 0] [lindex $ret 7] [lindex $ret 6] [lindex $ret 5] \
	    [lindex $ret 4] [lindex $ret 3] [lindex $ret 2] [lindex $ret 1]]
    }
}



#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_flange_find_ali {} {
global mv_set
  set l_min    5
  set l_max    10
  set flng     ""
  set outer    ""
  set sngl_crv ""
  set sngl_srf ""
  set srfs [ic_geo_get_objects surface]
  foreach sr $srfs { 
    catch {ic_geo_boundary surface $sr 1 1} bnd
    if {$bnd == ""} continue
    set outer [concat $outer $bnd]
    }
  set crvs [ic_geo_get_objects curve]
  if {$crvs == ""} return
  foreach cr $crvs {
    if {[lsearch $outer $cr] == -1} continue
    set srf_bnd [ic_geo_incident curve $cr 1]
    if {[llength $srf_bnd] == 1} {
       lappend sngl_crv $cr
       lappend s_srf_nm $srf_bnd
       if {[lsearch $sngl_srf $srf_bnd] == -1} {
          lappend sngl_srf $srf_bnd}
       }
    }
  foreach srf $sngl_srf {
    set plc [lsearch $mv_set(srf_nm) $srf]
    set edg [lindex  [lindex $mv_set(srf_info) $plc] 1]
    set sng_e 0
    foreach i "0 1 2 3" {
      set sd [lindex $edg $i]
      set lng 0
      set sngl 1
      foreach s $sd {
        set lng [expr $lng+[ic_geo_crv_length $s]]
        if {[lsearch $sngl_crv $s] == -1} {set sngl 0}
        }
      if {$sngl == 1} {set sng_e 1}
      set lng_$i $lng
      }
    if {$sng_e == 1} {
      if {$lng_0 > $l_min && $lng_0 < $l_max} {
        if {[lsearch $flng $srf] == -1} {lappend flng $srf}
        }
      if {$lng_1 > $l_min && $lng_1 < $l_max} {
              if {[lsearch $flng $srf] == -1} {lappend flng $srf}
        }
      if {$lng_2 > $l_min && $lng_2 < $l_max} {
              if {[lsearch $flng $srf] == -1} {lappend flng $srf}
        }
      if {$lng_3 > $l_min && $lng_3 < $l_max} {
              if {[lsearch $flng $srf] == -1} {lappend flng $srf}
        }
      }
    }
  if {$flng == ""} return
  set conn_crv [scrib_lib_geo_make_conn_loops $sngl_crv]
  foreach srf $flng {
     set crvtr [ic_surface_curvature $srf]
     if {$crvtr >= 180} {set crvtr [expr $crvtr-180]}
     if {$crvtr < 1 && $crvtr > -1 } {
        lappend srf_flat $srf
        } else { lappend srf_crvt $srf}
     }
  foreach srf $srf_crvt {
     set plc [lsearch $
     foreach
  
     }
  ic_geo_set_family curve   A_C names $sngl_crv
  ic_geo_set_family surface A_S names $s_srf_nm
     
  ic_geo_set_family surface AAA names $flng
  update_geom_visible surface p 
}


##

proc scrib_lib_flanges_assign_free_curves {surfs free_curves back_curves} {
#mess "free_curves are $free_curves\n" debug
#mess "back_curves are $back_curves\n" debug
    foreach surf $surfs {
	set free ""
	set locked ""
	set bounds [ic_geo_boundary surface $surf 0 1]
	foreach c $free_curves {
	    if {[lsearch $bounds $c] == -1} continue
	    lappend free $c
	}
	foreach c $back_curves {
	    if {[lsearch $bounds $c] == -1} continue
	    lappend locked $c
	}
	set number [scrib_lib_meshdirect_get_number_of_els $locked]
#mess "number on locked/back curves is $number\n" debug
	if {$number < 1} {
	    mess "number is $number\n" red
	    return
	}
	set free_number [scrib_lib_meshdirect_get_number_of_els $free]
	if {$number == $free_number} continue
#mess "surf $surf has non-equal mesh number\n" debug
#	set length 0
#	foreach c $locked {
#	    set length [expr $length + [ic_geo_crv_length $c]]
#	}
	set length_free 0
	foreach c $free {
	    set length_free [expr $length_free + [ic_geo_crv_length $c]]
	}
	foreach c $free {
	    ic_set_meshing_params curve $c emax [expr $length_free / $number]
	}
	
    }
}

## Cut out the notches on the given <em>surfs</em> with a width of <em>notch_width</em>

# source ~/43_dir/scrib/lib/flanges.tcl
# scrib_lib_flanges_cut_out_notches E174935 60
# scrib_lib_flanges_cut_out_notches E170769 60
# scrib_lib_flanges_cut_out_notches "E174935 E170769" 60

proc scrib_lib_flanges_cut_out_notches {surfs notch_width} {
    # author: jka, 10 july 2003
    
    # get the critical point foreach surface
    
    set crit_points [ic_geo_flanges_notch_critical_points $surfs]
    if {"" == $crit_points} {
	mess "Warning: could not find any notches in the flange surface(s) \"$surfs\"\n"
	return $surfs
    }
    #mess "debug: for surface \"$surfs\" got critical points: $crit_points\n"

    set result_surfs ""
    
    # foreach surfacename and critical point...
    
    foreach {sname pt} $crit_points {	
	
	mess "Cutting surface $sname based on critical point $pt\n"
	
	# get the vector of the length of the surface. we have to do this
	# geometrically instead of uv-param based, since who knows what
	# the paramaterization will look like. again, this function makes
	# all sorts of assumptions about this surface being a flange
	
	set corners [lindex [ic_geo_surface_topological_corners $sname] 1]
	if {$corners == ""} {continue}	
	set corner0loc [ic_geo_get_point_location [lindex $corners 0]]
	set corner1loc [ic_geo_get_point_location [lindex $corners 1]]
	set corner2loc [ic_geo_get_point_location [lindex $corners 2]]
	set corner3loc [ic_geo_get_point_location [lindex $corners 3]]
	
	set edge01len [ic_geo_pnt_dist $corner0loc $corner1loc]
	set edge12len [ic_geo_pnt_dist $corner1loc $corner2loc]
	
	set lengthvec ""
	if {$edge01len > $edge12len} {
	    set lengthvec [ic_geo_vec_diff $corner0loc $corner1loc]
	    set widthvec  [ic_geo_vec_diff $corner1loc $corner2loc]
	} else {
	    set lengthvec [ic_geo_vec_diff $corner1loc $corner2loc]
	    set widthvec  [ic_geo_vec_diff $corner0loc $corner1loc]
	}
	# fixme: these vecs should be computed from both edges in each dir, not just one!! --jka fixme
	
	# in each direction along the length of the surface go notch_width/2
	#     resulting in var two_splits
	
	set notch_offset [expr $notch_width / 2]
	set lengthdir [ic_geo_vec_nrm $lengthvec]
	
	set splitoneloc [ic_geo_vec_sum $pt [ic_geo_vec_smult $lengthdir   $notch_offset ]]
	set splittwoloc [ic_geo_vec_sum $pt [ic_geo_vec_smult $lengthdir "-$notch_offset"]]
	
	#mess "isolated notch between $splitoneloc and $splittwoloc\n"
	
	set family "FLANGE_NOTCH_TRIM_CRVS"
	ic_geo_new_family $family
	
	set sname_result_surfs $sname
	
	set surfs_to_try $sname
	
	foreach splitloc [list $splitoneloc $splittwoloc] {
	    
	    set p1 [ic_geo_vec_sum $splitloc [ic_geo_vec_smult $widthvec  "3"]]
	    set p2 [ic_geo_vec_sum $splitloc [ic_geo_vec_smult $widthvec "-3"]]
	    
	    # --> create tmp curves which we will project to the surface
	    
	    set tmpcrv [ic_geo_cre_line $family "TMP_FLANGE_CRV" $p1 $p2]
	    #mess "tmpcrv: $tmpcrv from $p1 to $p2\n"
	    
	    # --> project them to get the triming surface
	    
	    foreach surf $surfs_to_try {
		catch {ic_geo_project_curve_to_surface $tmpcrv $surf [ic_geo_new_name curve "FLANGE_CURVE"] $family} curve
		if {"" != $curve} {
		    #mess "projected curve: $curve\n"
		    break
		}
	    }
	    
	    # --> now cut surface with this new curve
	    
	    if {"" != $curve} {
		
		set new_trim_ents ""
		set new_surfs ""
		foreach surf $surfs_to_try {
		    
		    catch {ic_geo_trim_surface $surf $curve} new_trim_ents
		    
		    # get the nuw surface out of this new_trim_ents list
		    # so that we can try to trim against it too
		    #		    
		    # FLANGE_CURVE0 { { point { FLANGE_NOTCH_TRIM_CRVS.18 ... } }  { curve { E174935e0.21 ...} }  { surface { E174935.cut.0 } } }
		    
		    foreach ent_type_data $new_trim_ents {
			if {"surface" == [lindex $ent_type_data 0]} {
			    set new_surfs [lindex $ent_type_data 1]
			    eval lappend surfs_to_try $new_surfs
			    break
			}
		    }
		    if {"" != $new_surfs} { break }
		}
		
		if {"" != $new_surfs} { eval lappend sname_result_surfs $new_surfs }
		
	    } else {		
		mess "Error: could not trim surface $sname with curve $curve\n" red
	    }
	    
	    # --> delete the tmp curve
	    
	    ic_delete_geometry curve names $tmpcrv
	}
	
	lappend result_surfs $sname_result_surfs
    }
    
    # this returned list contains all given surfs and all new.
    
    return $result_surfs
}

proc scrib_lib_flanges_check_sides {sing_sides {ignore_tol 0}} {

    set ifp [lindex $sing_sides 0]
    set sidef [lindex $sing_sides 1]
    set ofp [lindex $sing_sides 2]	   
    set sideo [lindex $sing_sides 3]
    set olp [lindex $sing_sides 4]
    set sidel [lindex $sing_sides 5]
    set ilp [lindex $sing_sides 6]
    set sidec [lindex $sing_sides 7]

    if {[scrib_lib_flanges_llength_valid_curves $sidef $ignore_tol] == 3} {
#mess "trying to modify $sidef\n" debug
	set f_check [scrib_lib_flanges_adjust_connector_points $ifp $sidef]
#mess "f_check is $f_check\n" debug
	set sidef [lindex $f_check 0]
	set ifp [lindex $f_check 1]
 	set sidec [concat $sidec [lindex $f_check 2]]
	if {[lindex $f_check 2] == ""} {
	    set f_check [scrib_lib_flanges_adjust_connector_points $ofp $sidef 1]
#mess "f_check is $f_check\n" debug
#mess "f_check is $f_check\n"
	    set sidef [lindex $f_check 0]
	    set ofp [lindex $f_check 1]
 	    set sideo [concat $sideo [lindex $f_check 2]]
	}
    }
    if {[scrib_lib_flanges_llength_valid_curves $sidel $ignore_tol] == 3} {
#mess "trying to modify $sidel\n" debug
	set l_check [scrib_lib_flanges_adjust_connector_points $ilp $sidel]
#mess "l_check is $l_check\n" debug
	set sidel [lindex $l_check 0]
	set ilp [lindex $l_check 1]
	set sidec [concat $sidec [lindex $l_check 2]] 
	if {[lindex $l_check 2] == ""} {
	    set l_check [scrib_lib_flanges_adjust_connector_points $olp $sidel 1]
#mess "l_check is $l_check\n" debug
#mess "l_check is $l_check\n"
	    set sidel [lindex $l_check 0]
	    set olp [lindex $l_check 1]
 	    set sideo [concat $sideo [lindex $l_check 2]]
	}
    }

return [list $ifp $sidef $ofp $sideo $olp $sidel $ilp $sidec]

}

proc scrib_lib_flanges_adjust_connector_points {ixp sidex {allow_open 0}} {
#				sidec
# 	 ifp x-----------------------------------x ilp
# sidef     |                                            |      sidel
# 	ofp x-----------------------------------x olp
#				sideo
    set new_sidex ""
    set new_sidec_addon ""
    set new_ixp $ixp
    foreach curve $sidex {
	if {[lsearch [ic_geo_boundary curve $curve 0 1] $ixp] == -1} {
	    lappend new_sidex $curve
	    continue
	}
	if !$allow_open {
	    if {[llength [ic_geo_incident curve $curve 1]] < 2 } {
		lappend new_sidex $curve
		continue
	    }
	}
	foreach p [ic_geo_boundary curve $curve 0 1] {
	    if {$p == $ixp} continue
	    set new_ixp $p
	}
	set new_sidec_addon $curve	
    }
    return [list $new_sidex $new_ixp $new_sidec_addon]
}

proc scrib_lib_flanges_llength_valid_curves {curves ignore_tol} {
     set long_curves ""
     foreach c $curves {
	if {[ic_geo_crv_length $c] > $ignore_tol} {lappend long_curves $c}
    }
    return [llength $long_curves]
}

proc scrib_lib_flanges_get_valid_curves {curves ignore_tol} {
     set long_curves ""
     foreach c $curves {
	if {[ic_geo_crv_length $c] > $ignore_tol} {lappend long_curves $c}
    }
    return $long_curves
}


