Class: SUMD_TC

Inherits:
Object
  • Object
show all
Defined in:
lib/su_info/Template_Code.rb

Overview

Contains code snippets that are loaded into Template_Guide, and a few other methods. The snippets must be indented 2 spaces, otherwise the code will not parse them.

Methods can be run from the console, eg SUMD_TC.new.ro_1.

Instance Method Summary collapse

Instance Method Details

#coll_1

Code used in Template_Collections



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/su_info/Template_Code.rb', line 210

def coll_1
  am_ents = Sketchup.active_model.entities
  s = 300
  f1 = [ 0, 0, 0 ] , [ 0, s, 0 ] , [ s, s, 0 ] , [ s, 0, 0 ]
  f2 = [ 0, 0, s ] , [ 0, s, s ] , [ s, s, s ] , [ s, 0, s ]
  puts "am_ents.length = #{am_ents.length}"
  am_ents.add_face( f1 )
  puts "am_ents.length = #{am_ents.length}"
  am_ents.add_face( f2 )
  puts "am_ents.length = #{am_ents.length}"
end

#face_1

An example showing use of the Face constants



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/su_info/Template_Code.rb', line 11

def face_1
  # must have a model open with at least one face!
  cns = Sketchup::Face
  face = Sketchup.active_model.entities.grep(cns)[0]
  if (face)
    pt_location = face.classify_point(ORIGIN)
    t = case pt_location
      when cns::PointInside      then 'pt is inside'
      when cns::PointNotOnPlane  then 'pt not on plane'
      when cns::PointOnEdge,
           cns::PointOnVertex    then 'pt on perimeter'
      when cns::PointOutside     then 'pt is outside'
      when cns::PointUnknown     then 'pt error?'
      else 'not trapped by case statement'
    end
  else
    t = 'no face found'
  end
  puts t
end

#len_1

An example showing use of the Length constants



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/su_info/Template_Code.rb', line 34

def len_1
  cns = Length
  h_units  = Hash.new('Unit Unknown')
  h_format = Hash.new('Format Unknown')

  h_units[cns::Centimeter] = 'cm'
  h_units[cns::Feet]       = 'ft'
  h_units[cns::Inches]     = 'in'
  h_units[cns::Meter]      = 'm'
  h_units[cns::Millimeter] = 'mm'

  h_format[cns::Architectural] = 'Architectural'
  h_format[cns::Decimal]       = 'Decimal'
  h_format[cns::Engineering]   = 'Engineering'
  h_format[cns::Fractional]    = 'Fractional'

  om = Sketchup.active_model.options # OptionManager
  op = om['UnitsOptions']            # OptionsProvider
  units =  h_units[  op['LengthUnit']   ]
  format = h_format[ op['LengthFormat'] ]
  puts "Current model units are #{units}"
  puts "Current model format is #{format}"
end

#ro_1

Loads RenderingOptions constants, dumps value and name to the console, then hooks up an observer and outputs info about the callback and what ro's have changes



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/su_info/Template_Code.rb', line 62

def ro_1
  am = Sketchup.active_model
  cns = Sketchup::RenderingOptions

  # create a hash of ro keys
  @@h_ro = {}
  am.rendering_options.each { |k,v|
    val = v.kind_of?(Sketchup::Color) ? v.to_s.slice(-20,20) : v
    @@h_ro[k] = val
  }

  # hash @@roc = RenderingOptions constants
  #   key   is constant value
  #   value is constant name, with some spacing added by RegEx
  @@roc = Hash.new("** No Constant! **")
  # get all the constants, parse names, add to hash
  cns.constants.each { |c|
    text = c.to_s.dup
    if    ( text =~ /^ROPSet/ ) ; text.sub!(/^ROPSet/, 'ROPSet  ')
    elsif ( text =~ /^ROP/    ) ; text.sub!(/^ROP/   , 'ROP     ')
    end
    @@roc[cns.const_get(c)] = text
  }

  # dump hash to console
  cnsl = '-----------------------------------------'
  puts cnsl + '  ro_1()'
  prev = -1
  @@roc.sort.each { |r|
    num = r[0].to_s.rjust(3) + ((prev + 1 != r[0]) ? "*" : " ")
    puts "#{num} #{r[1]}"
    prev = r[0]
  }
  puts cnsl

  # create a RenderingOptionsObserver instance & add callback method
  @obs_ro1 = Sketchup::RenderingOptionsObserver.new
  @obs_ro1.instance_eval {
    @tmr_stopped = true
    def onRenderingOptionsChanged(ro, type)
      # timer puts line breaks between ro changes when multiple ocurr
      if (@tmr_stopped)
        @tmr_stopped = false
        UI.start_timer(0.100) { @tmr_stopped = true ; puts }
      end

      # Loop thru ro's and find changes, load into s_val
      s_val = ''
      ro.each { |k,v|
        val =  v.kind_of?(Sketchup::Color) ? v.to_s.slice(-20,20) : v
        if (@@h_ro[k] != val)
          @@h_ro[k] = val
          val = "%e" % v if (v.class == Float)
          s_val << "#{val.to_s.rjust(20)} #{k}"
        end
      }
      # finally put info to console
      puts "#{type.to_s.rjust(2)} #{@@roc[type].ljust(32)}  #{s_val}"
    end
  }
  # attach the observer
  am.rendering_options.add_observer(@obs_ro1)
end

#ro_2

An example using the ROP constants



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/su_info/Template_Code.rb', line 128

def ro_2
  # create an observer & add callback method
  @obs_ro2 = Sketchup::RenderingOptionsObserver.new
  @obs_ro2.instance_eval {
    def onRenderingOptionsChanged(ro, type)
      cns = ro.class
      suffix = case type
        when cns::ROPDrawHidden
                    'DrawHidden'
        when cns::ROPSetDisplayColorByLayer
                       'DisplayColorByLayer'
        when cns::ROPSetDisplaySketchAxes
                       'DisplaySketchAxes'
        when cns::ROPSetHideConstructionGeometry
                       'HideConstructionGeometry'
        when cns::ROPSetModelTransparency
                       'ModelTransparency'
        when cns::ROPSetRenderMode
                       'RenderMode'
        when cns::ROPSetSectionDisplayMode
                       'SectionDisplayMode'
        when cns::ROPSetTexture
                       'Texture'
        else "Not caught by case statement"
        end
      puts suffix
    end
  }
  # attach it to the Rendering_options of the model
  Sketchup.active_model.rendering_options.add_observer(@obs_ro2)
end

#tool_1

Creates a tool and and callbacks for mouse and KeyDown events



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/su_info/Template_Code.rb', line 162

def tool_1
  @@mse = Proc.new { |up_down_dbl, flags, x, y, view|
    button  = ''
    key_mod = ''
    if (MK_LBUTTON & flags != 0) then button << ', Left'   end
    if (MK_MBUTTON & flags != 0) then button << ', Middle' end
    if (MK_RBUTTON & flags != 0) then button << ', Right'  end

    if (MK_SHIFT   & flags != 0) then key_mod << ', Shift' end
    if (MK_CONTROL & flags != 0) then key_mod << ', Ctrl'  end
    if (MK_ALT     & flags != 0) then key_mod << ', Alt'   end
    s1 = up_down_dbl.ljust(7)
    s2 =  button.sub(/^, /, '').ljust(20)
    s3 = key_mod.sub(/^, /, '')
    puts "Mouse Button #{s1} button = #{s2} keys = #{s3}"
  }
  @tool = Object.new
  @tool.instance_eval {
    def onLButtonDown(*a)        ; @@mse.call('Down'  , *a) ; end
    def onMButtonDown(*a)        ; @@mse.call('Down'  , *a) ; end
    def onRButtonDown(*a)        ; @@mse.call('Down'  , *a) ; end

    def onLButtonUp(*a)          ; @@mse.call('Up'    , *a) ; end
    def onMButtonUp(*a)          ; @@mse.call('Up'    , *a) ; end
    def onRButtonUp(*a)          ; @@mse.call('Up'    , *a) ; end

    def onLButtonDoubleClick(*a) ; @@mse.call('DblClk', *a) ; end
    def onMButtonDoubleClick(*a) ; @@mse.call('DblClk', *a) ; end
    def onRButtonDoubleClick(*a) ; @@mse.call('DblClk', *a) ; end

    def onKeyDown(key, repeat, flags, view)
      # Some binary fun for testing
      t = flags.to_s(2).rjust(16)
      bin = "#{ t[-16,4]} #{ t[-12,4]} " \
            "#{ t[ -8,4]} #{ t[ -4,4]}".rjust(19)
      puts "#{key.to_s.ljust(4)}\t#{flags.to_s.ljust(5)}\t#{bin}"

      k    = key.to_s.rjust(3)
      alt  = (ALT_MODIFIER_MASK       & key != 0).to_s.ljust(5)
      cons = (CONSTRAIN_MODIFIER_MASK & key != 0).to_s.ljust(5)
      copy = (COPY_MODIFIER_MASK      & key != 0).to_s.ljust(5)
      puts "key = #{k}  alt = #{alt}  cons = #{cons}  copy = #{copy}"
    end
  }
  Sketchup.active_model.select_tool(@tool)
end