
# This code creates the user interface for nspacking.
#
# Stuff for NS packing.
# Copyright (c) 2000 Pacific Marketing and Consulting, Inc,
#       	and ICEM Systems, GmbH.  All rights reserved.
# Authors:  Wayne A. Christopher, wayne@icemcfd.com
# 	    Jari Spaete, jsp@icemcfd.de
#

proc edit_nspack {} {
global nspVar
    
  if ![ic_geo_is_loaded] {
    mess "Please load a geometry file.\n" red
    file_loadtetin "" 0
    if ![ic_geo_is_loaded] {
      return
    }
  }

  if ![info exists nspVar(InOutLast)] {
    set nspVar(InOutLast) IN
  }
  
  set w .nspacking  
  if [winfo exists $w] { top_destroy $w }  
  
  if ![info exists nspVar(NS_PACKING)] {
    nsp_SetParameter 0
    disp_change_overall_visible subface
  } else {
    nsp_ShowSubfaces
  }  
  set buttons {
    {"Accept" "nsp_Done"}
    {"Clear" "nsp_Clear"}
    {"Dismiss" "nsp_Cancel"}    
  }
  set win [form_init $w "NS packing" "" $buttons] 
  if {$win != ""} {
    form_frame $win.m ridge 3 "0 0"
    form_frame $win.m.f flat 3 "0 0"
    form_frame $win.m.f.1 flat 3 "0 0"
    form_frame $win.m.f.2 flat 3 "1 0"
    form_frame $win.m.f.3 flat 3 "2 0"
    form_frame $win.m.i flat 3 "1 0"
    form_frame $win.m.i.a sunken 4 "0 0"
    form_frame $win.m.i.a.1 flat 3 "0 0"
    form_frame $win.m.i.a.2 flat 3 "1 0"
    form_frame $win.m.i.a.3 flat 3 "2 0"
    form_frame $win.m.i.a.4 flat 3 "3 0"
    form_frame $win.m.i.a.5 flat 3 "4 0"
    form_frame $win.m.i.a.6 flat 3 "5 0"
    form_frame $win.m.i.a.7 flat 3 "6 0"
    form_frame $win.m.i.a.8 flat 3 "7 0"
    form_frame $win.m.i.a.9 flat 3 "8 0"
    form_frame $win.m.b flat 3 "2 0"
    form_frame $win.m.g flat 3 "3 0"
    form_frame $win.m.g.1 flat 3 "0 0"
    form_frame $win.m.g.2 flat 3 "1 0"
    form_frame $win.m.h flat 3 "4 0"
    form_label $win.m.f.1.in_out "Select meshing law parameters" "0 0"
    form_radiobutton $win.m.f.2.inside "Inside" nspVar(InOut) IN "0 0" "" "nsp_SetInOut $win.m.i.a.1.law $win.m.i.a"
    form_radiobutton $win.m.f.2.outside "Outside" nspVar(InOut) OUT "0 1" "" "nsp_SetInOut $win.m.i.a.1.law $win.m.i.a"
    form_label $win.m.i.a.1.select "Meshing law" "0 0"  
    set menu $win.m.i.a.1.law
    set nspVar(DefaultMenuCommand) "nsp_SetMeshingLaw $nspVar(MeshLaw,Default) $menu"
    set entries $menu.menu
    form_menubutton $menu "Default" $entries "0 1"
    $menu configure -bd 2 -width 16 -relief raised -padx 3 -pady 3
    menu $entries -tearoff 0
    $entries add command -label "Default" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Default) $menu"
    $entries add command -label "Uniform" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Uniform) $menu"
    $entries add command -label "Hyperbolic" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Hyperbolic) $menu"
    $entries add command -label "Geometric" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Geometric) $menu"
    $entries add command -label "Bigeometric" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Bigeometric) $menu"
    $entries add command -label "Exponential" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Exponential) $menu"
    $entries add command -label "Biexponential" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Biexponential) $menu"
    $entries add command -label "Poisson" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,Poisson) $menu"
    $entries add command -label "GLL" -command "nsp_SetMeshingLaw $nspVar(MeshLaw,GLL) $menu"    
    form_label $win.m.i.a.2.s "Spacing 1" "0 0"
    form_entry $win.m.i.a.2.e nspVar(Space1) float "0 1"
    bind $win.m.i.a.2.e <KeyRelease> nsp_SetInOutParameter  
    form_label $win.m.i.a.3.s "Spacing 2" "1 0"
    form_entry $win.m.i.a.3.e nspVar(Space2) float "1 1"
    bind $win.m.i.a.3.e <KeyRelease> nsp_SetInOutParameter 
    form_label $win.m.i.a.4.s "Spacing 1-2" "2 0"
    form_entry $win.m.i.a.4.e nspVar(Space1-2) float "2 1"
    bind $win.m.i.a.4.e <KeyRelease> nsp_SetInOutParameter 
    form_label $win.m.i.a.5.s "Spacing 2-2" "3 0"
    form_entry $win.m.i.a.5.e nspVar(Space2-2) float "3 1"
    bind $win.m.i.a.5.e <KeyRelease> nsp_SetInOutParameter  
    form_label $win.m.i.a.6.s "Ratio" "4 0"
    form_entry $win.m.i.a.6.e nspVar(Ratio) float "4 1"
    bind $win.m.i.a.6.e <KeyRelease> nsp_SetInOutParameter 
    form_checkbutton $win.m.i.a.7.c "Compute number of layers" nspVar(ComputeLayers) \
   	"5 0" "" "nsp_ComputeLayers"
    form_label $win.m.i.a.8.s "Number of layers" "6 0"
    form_entry $win.m.i.a.8.e nspVar(LayersNumber) float "6 1"
    bind $win.m.i.a.8.e <KeyRelease> nsp_SetInOutParameter  
    form_checkbutton $win.m.i.a.9.c "Interpolation" nspVar(Interpolation) \
    	"7 0" "" "" 
    form_label $win.m.b.s "First layer height" "0 0"
    form_entry $win.m.b.e nspVar(FirstLayerHeight) float "0 1"

    form_button $win.m.g.2.select "Select subface(s)" "nsp_SelectSubfaces" "0 0" ""
    form_checkbutton $win.m.h.extend "Extend subface" nspVar(SubfaceExtend) \
    	"1 0" "" "nsp_ExtendSubface"
    form_finish +800+400 1 0
  }
  
  $menu configure -text $nspVar(Name,MeshLaw)  
  if [string match "IN" $nspVar(InOutLast)] {
    $win.m.i.a configure -relief sunken
    $win.m.i.a.7.c configure -state disabled -fg gray
  } elseif [string match "OUT" $nspVar(InOutLast)] {
    $win.m.i.a configure -relief raised
    $win.m.i.a.7.c configure -state normal -fg black
    nsp_ComputeLayers
  }
  set nspVar(Win,Main) $win
}

proc nsp_SelectSubfaces {} {
global nspVar nspList nspImages str_type_visible
  
  if $nspVar(SubfaceExtend) {
    mess "Select only one subface to extend.\n"
  } 

  nsp_ClearView
  set mode subface
  
  set str_type_visible($mode) 1
  etm_ConvertSubfaces 1
  disp_set_visible $mode 1
  set picked [select_struct_surf $mode 0]
  disp_set_visible $mode 0
  etm_ConvertSubfaces 0
  
  set list {}
  catch { unset nspImages }
  foreach item $picked {
    lappend list $item
    ic_str_highlight $mode $item 1 gray
    ic_str_set_nums_visible $mode "$item 1"   
    set str_number_visible($mode,$item) 1
    lappend nspImages $item
    if ![info exists done] {
      set done 1
      set nspVar(SubfaceExtendSelected) $item
    }
  }
  set nspList(SelectedSubfaces) [lsort $list]
  if {[llength $list ] > "1"} {
    set nspVar(SubfaceExtendSelected) ""
    set nspVar(SubfaceExtend) 0
  }  
  
  if $nspVar(SubfaceExtend) {
    if {[llength $list] == "1"} {
      if ![string match "" $nspVar(SubfaceExtendSelected)] {
        ic_str_scanp_move_zone subface $nspVar(SubfaceExtendSelected) 0 $nspVar(SubfaceExtend)
      }
    } else {
      set nspVar(SubfaceExtendSelected) ""
      set nspVar(SubfaceExtend) 0
    }
  }
}

proc nsp_ExtendSubface {} {
global nspList nspVar 
  
  if {![info exists nspList(SelectedSubfaces)] || [llength $nspList(SelectedSubfaces)] == "0"} {
    return
  }
  
  if {[llength $nspList(SelectedSubfaces)] > "1" && $nspVar(SubfaceExtend)} {
    mess "You selected more than one subface to extend. Only one subface is allowed.\n"
    set nspVar(SubfaceExtend) 0
  } else {
    ic_str_scanp_move_zone subface $nspVar(SubfaceExtendSelected) 0 $nspVar(SubfaceExtend)
  }
}

proc nsp_ComputeLayers {} {
global nspVar 

  set win $nspVar(Win,Main)
  if $nspVar(ComputeLayers) {
    $win.m.i.a.8.s configure -fg gray
    $win.m.i.a.8.e configure -state disabled -fg gray
  } else {
    $win.m.i.a.8.s configure -fg black
    $win.m.i.a.8.e configure -state normal -fg black
  }
}

proc nsp_SetMeshingLaw { what win } {
global nspVar

  foreach item [array names nspVar MeshLaw,*] {
    if {$what == $nspVar($item)} {
      set nspVar(Name,MeshLaw) [ lindex [ split $item , ] 1 ]
      set nspVar(Selected,MeshLaw,$nspVar(InOut)) $what
      set nspVar(Name,MeshLaw,$nspVar(InOut)) $nspVar(Name,MeshLaw)
      $win configure -text $nspVar(Name,MeshLaw)
      break
    }
  }
}

proc nsp_Done {} {
    global nspVar nspList file_name tool_dir env

    if {![info exists nspList(SelectedSubfaces)] || \
	[llength $nspList(SelectedSubfaces)] == "0"} {
	mess "You selected no subface(s) for NS packing.\n"
	return
    } 

    set nspList(InsideLayers) \
	"$nspVar(LayersNumber,IN) $nspVar(Selected,MeshLaw,IN) \
  	 $nspVar(Space1,IN) $nspVar(Space2,IN) $nspVar(Space1-2,IN) \
  	 $nspVar(Space2-2,IN) $nspVar(Ratio,IN) $nspVar(Interpolation,IN)"
    set nspList(OutsideLayers) \
	"$nspVar(LayersNumber,OUT) $nspVar(Selected,MeshLaw,OUT) \
  	 $nspVar(Space1,OUT) $nspVar(Space2,OUT) $nspVar(Space1-2,OUT) \
  	 $nspVar(Space2-2,OUT) $nspVar(Ratio,OUT) $nspVar(Interpolation,OUT)"
  
    set last [ic_str_num_entities domain]
    for {set i 0} {$i < $last} {incr i} {
      lappend list 1
    }
    set nspList(SelectedDomains) $list

    top_destroy .nspacking
    set nspVar(NS_PACKING) 1
    set etm_dir $file_name(mesh_dir)
    mess "Starting NS packing monitor ...\n"
    mess "Use Interrupt button to cancel NS packing.\n"

    busy 1
    ic_str_save_previous_nodes
    mess "Start NS packing ...\n"    
    ic_str_nspacking $etm_dir $nspList(SelectedDomains) \
      	$nspList(SelectedSubfaces) $nspList(InsideLayers) \
      	$nspList(OutsideLayers) $nspVar(SubfaceExtend) \
      	$nspVar(FirstLayerHeight) $nspVar(ComputeLayers) 
    mess "End NS packing ...\n"
    catch { interrupt finish }
    nsp_Unset
    beep
    .v.view redraw
    busy 0
}

proc nsp_Cancel {} {
  
  nsp_Unset
  top_destroy .nspacking
}

proc nsp_Clear {} {
global nspVar
  
  set curr $nspVar(InOut)
  set last $nspVar(InOutLast)
  nsp_Unset
  set nspVar(InOut) $curr
  set nspVar(InOutLast) $last
  eval $nspVar(DefaultMenuCommand)
}

proc nsp_Unset {} {
global nspList nspImages

  nsp_ClearView
  nsp_SetParameter 0
  catch { unset nspImages }
  catch { unset nspList(SelectedSubfaces) }
  catch { unset nspList(InsideLayers) }
  catch { unset nspList(OutsideLayers) }
}

proc nsp_SetInOut { win frame } {
global nspVar

  set w $nspVar(Win,Main)
  if [string match "OUT" $nspVar(InOut)] {
    if [string match "OUT" $nspVar(InOutLast)] {
      return
    } 
    $frame configure -relief raised
    $w.m.i.a.7.c configure -state normal -fg black
    nsp_ComputeLayers
  } elseif [string match "IN" $nspVar(InOut)] {
    if [string match "IN" $nspVar(InOutLast)] {
      return
    }    
    $frame configure -relief sunken
    $w.m.i.a.7.c configure -state disabled -fg gray
    $w.m.i.a.8.s configure -fg black
    $w.m.i.a.8.e configure -state normal -fg black
  } 
  set nspVar(Space1,$nspVar(InOutLast)) $nspVar(Space1)
  set nspVar(Space2,$nspVar(InOutLast)) $nspVar(Space2)
  set nspVar(Space1-2,$nspVar(InOutLast)) $nspVar(Space1-2)
  set nspVar(Space2-2,$nspVar(InOutLast)) $nspVar(Space2-2)
  set nspVar(Ratio,$nspVar(InOutLast)) $nspVar(Ratio)
  set nspVar(LayersNumber,$nspVar(InOutLast)) $nspVar(LayersNumber)
  set nspVar(Interpolation,$nspVar(InOutLast)) $nspVar(Interpolation)
  
  set nspVar(Space1) $nspVar(Space1,$nspVar(InOut))
  set nspVar(Space2) $nspVar(Space2,$nspVar(InOut))
  set nspVar(Space1-2) $nspVar(Space1-2,$nspVar(InOut))
  set nspVar(Space2-2) $nspVar(Space2-2,$nspVar(InOut))
  set nspVar(Ratio) $nspVar(Ratio,$nspVar(InOut))
  set nspVar(LayersNumber) $nspVar(LayersNumber,$nspVar(InOut))
  set nspVar(Interpolation) $nspVar(Interpolation,$nspVar(InOut))
  
  set nspVar(InOutLast) $nspVar(InOut)
  $win configure -text $nspVar(Name,MeshLaw,$nspVar(InOut))
}

proc nsp_SetInOutParameter {} {
global nspVar

  set nspVar(Space1,$nspVar(InOut)) $nspVar(Space1)
  set nspVar(Space2,$nspVar(InOut)) $nspVar(Space2)
  set nspVar(Space1-2,$nspVar(InOut)) $nspVar(Space1-2)
  set nspVar(Space2-2,$nspVar(InOut)) $nspVar(Space2-2)
  set nspVar(Ratio,$nspVar(InOut)) $nspVar(Ratio)
  set nspVar(LayersNumber,$nspVar(InOut)) $nspVar(LayersNumber)
  set nspVar(Interpolation,$nspVar(InOut)) $nspVar(Interpolation)
}

proc nsp_SetParameter { what } {
global nspVar nspImages nspList

  if { $what == 0 } {
    set nspVar(MeshLaw,Default) 0
    set nspVar(MeshLaw,Hyperbolic) 1
    set nspVar(MeshLaw,Bigeometric) 2
    set nspVar(MeshLaw,Geometric) 3
    set nspVar(MeshLaw,Exponential) 5
    set nspVar(MeshLaw,Biexponential) 4
    set nspVar(MeshLaw,Poisson) 6
    set nspVar(MeshLaw,Uniform) 7
    set nspVar(MeshLaw,GLL) 8
    set nspVar(Name,MeshLaw) Default

    set nspVar(Space1) 0.0
    set nspVar(Space2) 0.0
    set nspVar(Space1-2) 0.0
    set nspVar(Space2-2) 0.0
    set nspVar(Ratio) 1.1
    set nspVar(FirstLayerHeight) 0.01
    set nspVar(LayersNumber) 20
    set nspVar(Interpolation) 0
    
    set nspVar(InOut) IN
    set nspVar(InOutLast) $nspVar(InOut)
    set nspVar(Selected,MeshLaw,IN) $nspVar(MeshLaw,Default)
    set nspVar(Selected,MeshLaw,OUT) $nspVar(MeshLaw,Default)
    set nspVar(Name,MeshLaw,IN) $nspVar(Name,MeshLaw)
    set nspVar(Name,MeshLaw,OUT) $nspVar(Name,MeshLaw)
    set nspVar(Space1,IN) $nspVar(Space1)
    set nspVar(Space1,OUT) $nspVar(Space1)
    set nspVar(Space2,IN) $nspVar(Space2)
    set nspVar(Space2,OUT) $nspVar(Space2)
    set nspVar(Space1-2,IN) $nspVar(Space1-2)
    set nspVar(Space1-2,OUT) $nspVar(Space1-2)
    set nspVar(Space2-2,IN) $nspVar(Space2-2)
    set nspVar(Space2-2,OUT) $nspVar(Space2-2)
    set nspVar(Ratio,IN) $nspVar(Ratio)
    set nspVar(Ratio,OUT) $nspVar(Ratio)
    set nspVar(LayersNumber,IN) 20
    set nspVar(LayersNumber,OUT) 5
    set nspVar(Interpolation,IN) $nspVar(Interpolation)
    set nspVar(Interpolation,OUT) $nspVar(Interpolation)
    
    set nspVar(SubfaceExtend) 0
    set nspVar(SubfaceExtendSelected) ""
    
    set nspVar(NS_PACKING) 0
    set nspVar(ComputeLayers) 1
    
    catch { unset nspImages }
    catch { unset nspList(SelectedSubfaces) }
  } 
}

proc nsp_ClearView {} {
global nspImages

  if [info exists nspImages] {
    foreach item $nspImages {
      ic_str_highlight subface $item 0
      ic_str_set_nums_visible subface "$item 0"
    }
  } else {
    nsp_SetParameter 0
  }
}

proc nsp_ShowSubfaces {} {
global nspImages

  if [info exists nspImages] {
    foreach item $nspImages {
      ic_str_highlight subface $item 1 gray
      ic_str_set_nums_visible subface "$item 1"
    }
  } else {
    nsp_SetParameter 0
  }
}
