home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_geomview.idb / usr / freeware / lib / geomview / doc / example4.tcl.z / example4.tcl
Encoding:
Tcl/Tk script  |  1999-01-26  |  3.5 KB  |  136 lines

  1. #! /usr/local/bin/wish4.0
  2.  
  3. proc readsomething {} {
  4.   if {[gets stdin line] < 0} {
  5.         puts stderr "EOF on input, exiting..."
  6.         exit
  7.   }
  8.   # Translate geomview's lisp-style ()'s into tcl-friendly {} braces.
  9.   regsub -all {\(} $line "\{" line
  10.   regsub -all {\)} $line "\}" line
  11.   # Strip outermost set of braces
  12.   set stuff [lindex $line 0]
  13.   # invoke handler for whichever command we got
  14.   switch [lindex $stuff 0] {
  15.         pick     {handlepick $stuff}
  16.         rawevent {handlekey $stuff}
  17.   }
  18. }
  19.  
  20. # Having asked for notification of these raw events, we report when
  21. # the user pressed these keys in the geomview graphics windows.
  22. proc handlekey {event} {
  23.   global lastincr
  24.   switch [lindex $event 1] {
  25.     32 {msg "Pressed space bar"}
  26.      8 {msg "Pressed backspace key"}    # Backspace: decrement it.
  27.   }
  28. }
  29.  
  30. # Fields of a "pick" response, from geomview manual:
  31. #     (pick COORDSYS GEOMID G V E F P VI EI FI)
  32. #          The pick command is executed internally in response to pick
  33. #          events (right mouse double click).
  34. #
  35. #          COORDSYS = coordinate system in which coordinates of the following
  36. #              arguments are specified.   This can be:
  37. #               world: world coord sys
  38. #               self:  coord sys of the picked geom (GEOMID)
  39. #               primitive: coord sys of the actual primitive within
  40. #                   the picked geom where the pick occurred.
  41. #          GEOMID = id of picked geom
  42. #          G = picked point (actual intersection of pick ray with object)
  43. #          V = picked vertex, if any
  44. #          E = picked edge, if any
  45. #          F = picked face
  46. #          P = path to picked primitive [0 or more]
  47. #          VI = index of picked vertex in primitive
  48. #          EI = list of indices of endpoints of picked edge, if any
  49. #          FI = index of picked face
  50.  
  51. # User picked something.
  52. proc handlepick {pick} {
  53.   global nameof selvert seledge order
  54.   set obj [lindex $pick 2]
  55.   set xyzw [lindex $pick 3]
  56.   set fv [lindex $pick 6]
  57.   set vi [lindex $pick 8] 
  58.   set ei [lindex $pick 9] 
  59.   set fi [lindex $pick 10]
  60.  
  61.   # Report result.
  62.   set w [lindex $xyzw 3]
  63.   set x [expr [lindex $xyzw 0]/$w]
  64.   set y [expr [lindex $xyzw 1]/$w]
  65.   set z [expr [lindex $xyzw 2]/$w]
  66.   set s "$x $y $z "
  67.   if {$vi >= 0} {
  68.         set s "$s  vertex #$vi"
  69.   }
  70.   if {$ei != {}} {
  71.         set s "$s  edge [lindex $ei 0]-[lindex $ei 1]"
  72.   }
  73.   if {$fi != -1} {
  74.         set s "$s  face #$fi ([expr [llength $fv]/4]-gon)"
  75.   }
  76.   msg $s
  77. }
  78.  
  79.  
  80. proc loadobject {fname} {
  81.   if {$fname != ""} {
  82.         puts "(geometry thing < $fname)"
  83.         # Be sure to flush output to ensure geomview receives this now!
  84.         flush stdout
  85.   }
  86. }
  87.  
  88. #
  89. # Display a message on the control panel
  90. #
  91. proc msg {str} {
  92.   global msgtext
  93.   puts stderr $str
  94.   set msgtext $str
  95.   update
  96. }
  97.  
  98.  
  99. # Build "user interface"
  100. # This doesn't need to be an entry box, but we want to be able to
  101. # use X selection to copy text from it.
  102. entry .msg -textvariable msgtext -width 30
  103. pack .msg -fill x -expand true
  104.  
  105. frame .f
  106. label .f.l -text "File to load:"
  107. pack .f.l -side left
  108. entry .f.ent -textvariable fname
  109. pack .f.ent -side left -expand true -fill x
  110. pack .f
  111.  
  112. bind .f.ent <Return> { loadobject $fname }
  113.  
  114. # End UI definition.
  115.  
  116.  
  117. # Call "readsomething" when data arrives from geomview.
  118.  
  119. fileevent stdin readable {readsomething}
  120.  
  121. # Geomview initialization
  122.  
  123. puts {
  124.         (interest (pick primitive))
  125.         (interest (rawevent 32))
  126.         (interest (rawevent 8))
  127.         (geometry thing < hdodec.off)
  128.         (normalization world none)
  129. }
  130. flush stdout
  131.  
  132. wm title . {Sample external module}
  133.  
  134. msg "Click right mouse in graphics window"
  135.