#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
#  srf_nm   : Surfaces names
#  srf_info : Name of 4 sides curves
#  arc_nm   : ARC's names
#  arc_info : List of [radius, angle & center's location]
#  fllt_nm  : Fillets names
#  fllt_info: Number of edges - Are edges fillet?
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-

proc scrib_lib_fillet_find_surf {
    {a_min 10} {a_max 170} {l_min 1} {fllt_type 0} {surfs ""} {param 0} {msh_lmt ""}} {
  return [ic_meshdirect_fillet_find_surf $a_min $a_max $l_min $fllt_type $surfs $param $msh_lmt]
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_find {a_min a_max l_min {surfs ""}} {
    ic_meshdirect_fillet_find $a_min $a_max $l_min $surfs
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_find_arc_old {cr a_min a_max l_min} {
global mv_set
  
  set ret_val 0
  #set l_min [expr $r_min*$a_min*0.017453292]
  set lng [ic_geo_crv_length $cr]
  if {$lng < $l_min} {return 0}
  set fam   [ic_geo_get_family curve $cr]
  foreach i "0 1 2 3 4" {
     set p_$i [ic_geo_cre_pnt_on_crv_at_par $fam "" [expr $i*0.25] $cr]
     set l_$i [ic_geo_get_point_location [subst $[subst p_$i]]]
     }
  ic_virtual define point cnt1 "$p_0 $p_2 $p_3 radius 0 angle 0 offset 0"
  ic_virtual define point cnt2 "$p_1 $p_2 $p_3 radius 0 angle 0 offset 0"
  set c1 [lindex [ic_virtual check point cnt1] 0]
  if ![scrib_lib_math_is_point $c1] {
      ic_delete_geometry point names "$p_0 $p_1 $p_2 $p_3 $p_4" 0
      return $ret_val
      }
  set c2 [lindex [ic_virtual check point cnt2] 0]
  if ![scrib_lib_math_is_point $c2] {
      ic_delete_geometry point names "$p_0 $p_1 $p_2 $p_3 $p_4" 0
      return $ret_val
      }
  set x_av [expr ([lindex $c1 0]+[lindex $c2 0])/2.0]
  set y_av [expr ([lindex $c1 1]+[lindex $c2 1])/2.0]
  set z_av [expr ([lindex $c1 2]+[lindex $c2 2])/2.0]
  set cnt  [list $x_av $y_av $z_av]
  set r0   [scrib_lib_math_vector_length [scrib_lib_math_vector_sub $cnt $l_0]]
  set r1   [scrib_lib_math_vector_length [scrib_lib_math_vector_sub $cnt $l_1]]
  set r2   [scrib_lib_math_vector_length [scrib_lib_math_vector_sub $cnt $l_2]]
  set r3   [scrib_lib_math_vector_length [scrib_lib_math_vector_sub $cnt $l_3]]
  set r4   [scrib_lib_math_vector_length [scrib_lib_math_vector_sub $cnt $l_4]]
  set rd_lst [lsort -real [list $r0 $r1 $r2 $r3 $r4]]
  set min  [lindex $rd_lst 0]
  set max  [lindex $rd_lst end]
  if $max  {set rat [expr $min/double($max)]} else {set rat 0}
  set v1   [scrib_lib_math_vector_sub $l_0 $cnt]
  set v2   [scrib_lib_math_vector_sub $l_4 $cnt]
  set ang  [expr [scrib_lib_math_angle_between_vectors $v1 $v2]/0.017453292]
  
  if {$rat > 0.95 && $rat < 1.05} {set arc y} else {set arc n}
  if {$arc == "y"} {
     if {$ang > $a_min && $ang < $a_max} {
        set ret_val [list [expr ($min+$max)/2.0] $ang $lng]
        lappend ret_val $cnt
        }
     }
  ic_delete_geometry point names "$p_0 $p_1 $p_2 $p_3 $p_4" 0
  return $ret_val
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_find_arc {cr a_min a_max l_min {tol -1}} {
  return [ic_meshdirect_fillet_find_arc $cr $a_min $a_max $l_min $tol]
}


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_continues {fllt_type} {
    ic_meshdirect_fillet_continues $fllt_type
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_continues_tri {tri_lst tri_nm sng_nm tri_crv fllt_type} {
    ic_meshdirect_fillet_continues_tri $tri_lst $tri_nm $sng_nm $tri_crv $fllt_type
}

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_crt_loop {} {
global mv_set

  set lp_exst [ic_geo_get_objects loop]
  foreach srf $mv_set(fllt_nm) {
    set edges [ic_geo_boundary surface $srf 0 1]
    set fam   [ic_geo_get_family surface $srf]
    ic_geo_create_loop $srf/0 $fam names [list $edges] 1 "" ""
    }
  set lp_all   [ic_geo_get_objects loop]
  set mv_set(loop_nm) [lrange $lp_all [llength $lp_exst] end]
}


#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-
proc scrib_lib_fillet_find_by_arc {{surfs ""} {l_min 1} {l_max  20} {min_curvature 15} \
	 {max_srf_area 5000} {max_perim 2000} {fllt_type 2} {tol -1}}  {
    return [ic_meshdirect_fillet_find_by_arc $surfs $l_min $l_max $min_curvature \
	$max_srf_area $max_perim $fllt_type $tol]
}


proc scrib_lib_fillet_button_ali { } { 
    mess "Finding fillet buttons...\n\n"    
    
    if [info exists closed_fillets] {
	set fillet_butt_loops $closed_fillets
    } else {
	set fillet_butt_loops [scrib_lib_fillet_find_surf 10 170 \
				   1 2 $free_surfs ]
    }
    set exclusion_surfs ""
    foreach surf [ic_geo_get_objects surface ""] {
	if {[lsearch $free_surfs $surf] == -1} { lappend exclusion_surfs $surf }
    }
    set fillet_loops ""
    foreach fbl $fillet_butt_loops { 
	set inner_outer [scrib_lib_geo_find_internal_outer_loops $fbl 1]
	set outer_c [lindex $inner_outer 0]
	set inner_c [lindex $inner_outer 1]
	if {$inner_c == ""} continue
	set surround_surfs ""
	foreach c $outer_c {
	    foreach s [ic_geo_incident curve $c 1] {
		if {[lsearch $fbl $s] == -1} {lappend surround_surfs $s}
	    }
	}
	set surround_surfs [lsort -unique $surround_surfs]
	lappend fillet_loops  [lindex [scrib_lib_feature_find_button_features \
					   [list $surround_surfs]  FILLET_BUTT_CURVES  FILLET_SURF \
					   $V_bead_mesh_size  0 1 $exclusion_surfs fillet] 0]
    }
    
    #	mess "fillet_loops are $fillet_loops\n"
    
    set free_surfs [scrib_lib_utils_remove_from_grouped $fillet_loops $free_surfs]
}










