Module: SUMD

Defined in:
lib/su_info/sumd_common.rb

Overview

Common methods for SUMD modules

Since this loads for all SUMD modules, variable & method names are chosen to allow them to be removed from the SketchUp symbol list.

Class Method Summary collapse

Class Method Details

.sumd_file_read(n) ⇒ String?

Returns file contents

Parameters:

  • n (String)

    file name with extension

Returns:

  • (String, nil)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/su_info/sumd_common.rb', line 29

def SUMD.sumd_file_read(n)
  @ret = nil
  if (n.slice(-3,3) == '.md')
    d = File.expand_path(@path_md, @rb_dir)  || @path_temp
  elsif (n.slice(-4,4) == '.txt')
    d = File.expand_path(@path_txt, @rb_dir) || @path_temp
  elsif (n.slice(-3,3) == '.rb')
    d = @rb_dir
  end
  if File.directory?(d)
    @full_name = "#{d}#{File::SEPARATOR}#{n}"
    if File.exists?(@full_name)
      @ret = File.read(@full_name)
    else
      UI.messagebox("File #{@full_name} does not exist\n\nExiting!")
    end
  else
    UI.messagebox("Directory #{d} does not exist\n\nExiting!")
  end
  @ret
end

.sumd_file_write(sumd_name, s) ⇒ String?

Writes output file, returns full file name if successful

Parameters:

  • sumd_name (String)

    file name suffix

  • s (String)

    file contents

Returns:

  • (String, nil)

    full file name



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/su_info/sumd_common.rb', line 56

def SUMD.sumd_file_write(sumd_name, s)
  filename = nil
  if (sumd_name.slice(-4,4) == '.txt')
    d = File.expand_path(@path_txt, @rb_dir) || @path_temp
    n = "su#{@su_major}_#{sumd_name}"
  elsif (sumd_name.slice(-3,3) == '.md')
    d = File.expand_path(@path_md, @rb_dir)  || @path_temp
    n = "SketchUp_#{@su_major}_#{sumd_name.gsub(/ /,'_')}"
    str = sumd_name.sub(/\.md/, '')
#     tt = "#{@title_su} #{str}".gsub(/_/, '  ' )
    tt = "#{@title_su} #{str}"
    s.sub!( /^# @title Template.*/, "# @title #{tt}" )
    s.sub!( /TOC #{str}/, "SketchUp #{@toc_su} #{str}" )

  else
    UI.messagebox('Unknown extension!')
    return nil
  end
  if (File.directory?(d))
    filename = "#{d}#{File::SEPARATOR}#{n}"
    f = File.open(filename, "w")
    if (f)
      f.write(s)
      f.close
    end
  else
    UI.messagebox("Directory #{d} does not exist!")
  end
  filename
end

.sumd_generated_by(n, v)

Returns 'Generated by...' string for md files

Parameters:

  • n (String)

    module name

  • v (Float)

    version



91
92
93
94
95
96
97
# File 'lib/su_info/sumd_common.rb', line 91

def SUMD.sumd_generated_by(n, v)
  # create file hdr
  t = Time.now.gmtime
  date =  t.strftime("%Y-%m-%d at %I:%M:%S %p") + " GMT"
  "Generated with [#{n}] v#{v.to_s}, on #{date},\n" \
  "using SketchUp v#{Sketchup.version} & Ruby v#{RUBY_VERSION}."
end

.sumd_get_ruby_hash(hash, sym = nil) ⇒ Boolean

Loads a hash with either native Ruby constants or symbols

Parameters:

  • hash (Hash)

    loaded with file contents

  • sym (Boolean, nil) (defaults to: nil)

    true if symbols, nil for constants

Returns:

  • (Boolean)

    true if hash loaded



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/sumd_common.rb', line 104

def SUMD.sumd_get_ruby_hash(hash, sym = nil)
  @ret = false
  d = File.expand_path(@path_txt, @rb_dir) || @path_temp
  if File.directory?(d)
    if sym
      @f_name = "#{d}#{File::SEPARATOR}native_ruby_syms.txt"
    else
      @f_name = "#{d}#{File::SEPARATOR}native_ruby_cnsts.txt"
    end
    if File.exists?(@f_name)
      @a_ruby = File.read(@f_name).split(/[\r\n\f]+/)
      @a_ruby.each { |k| hash[k] = nil }
      @ret = true
    else
      UI.messagebox("File #{@f_name} does not exist\n\nExiting!")
    end
  else
    UI.messagebox("Directory #{d} does not exist\n\nExiting!")
  end
  @ret
end

.sumd_str_to_em(a) ⇒ Float

Returns approx em width

Parameters:

  • a (Array<String>, String)

    array/string to check

Returns:

  • (Float)

    max em for array column 0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/su_info/sumd_common.rb', line 129

def SUMD.sumd_str_to_em(a)
  max = 0.0
#    puts "#{a.class}  #{a[0]}"
  a.each { |row|
    s = ( (String === row) ? row : row[0] )
    len = s.length - 1
    n = 0.0
    0.upto(len) { |i|
      n += case s[i,1]
      when /[mW]/        then 0.98
      when /[M]/         then 0.87
      when /[fIijlrt]/   then 0.35
      when /[HNOQU]/     then 0.80
      when /[ADGRVw]/    then 0.75
      when /[_BCKoX]/    then 0.67
      when /[sJ]/        then 0.47
      when /[acEL]/      then 0.53
      else                    0.62
      end
    }
    max = [max, n].max
  }
  ( (10.0 * max).round )/ 10.0
end