Class: SolidRuby::RubyScadBridge
- Inherits:
-
Object
- Object
- SolidRuby::RubyScadBridge
- Defined in:
- lib/solidruby/rubyscad_bridge.rb
Constant Summary collapse
- START_BLOCK =
'{'.freeze
- END_BLOCK =
'}'.freeze
- TAB_SIZE =
3- PAD =
0.01
- FP_P =
3- CUBE_STR =
'cube(%<args>s);'.freeze
- SPHERE_STR =
'sphere(%<args>s);'.freeze
- CYLINDER_STR =
'cylinder(%<args>s);'.freeze
- POLYHEDRON_STR =
'polyhedron(%<args>s);'.freeze
- SQUARE_STR =
'square(%<args>s);'.freeze
- CIRCLE_STR =
'circle(%<args>s);'.freeze
- POLYGON_STR =
'polygon(%<args>s);'.freeze
- TRANSLATE_STR =
'translate(%<args>s)'.freeze
- ROTATE_STR =
'rotate(%<args>s)'.freeze
- SCALE_STR =
'scale(%<args>s)'.freeze
- MIRROR_STR =
'mirror(%<args>s)'.freeze
- MULTMATRIX_STR =
'multmatrix(%<args>s)'.freeze
- COLOR_STR =
'color(%<args>s)'.freeze
- UNION_STR =
'union(%<args>s)'.freeze
- DIFFERENCE_STR =
'difference(%<args>s)'.freeze
- INTERSECTION_STR =
'intersection(%<args>s)'.freeze
- RENDER_STR =
'render(%<args>s)'.freeze
- MINKOWSKI_STR =
'minkowski(%<args>s)'.freeze
- HULL_STR =
'hull(%<args>s)'.freeze
- BACKGROUND_STR =
'%'.freeze
- DEBUG_STR =
'#'.freeze
- ROOT_STR =
'!'.freeze
- DISABLE_STR =
'*'.freeze
- IMPORT_STR =
'import(%<args>s);'.freeze
- SURFACE_STR =
'surface(%<args>s);'.freeze
- LINEAR_EXTRUDE_STR =
'linear_extrude(%<args>s)'.freeze
- ROTATE_EXTRUDE_STR =
'rotate_extrude(%<args>s)'.freeze
- PROJECTION_STR =
'projection(%<args>s)'.freeze
- TEXT_STR =
'text(%<args>s);'.freeze
- INCLUDE_STR =
'include <%<file>s>'.freeze
- USE_STR =
'use <%<file>s>'.freeze
- ECHO_STR =
'echo(%<string>s);'.freeze
- FA_STR =
'$fa = %<value>s;'.freeze
- FS_STR =
'$fs = %<value>s;'.freeze
- FN_STR =
'$fn = %<value>s;'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #background ⇒ Object
- #circle(args = {}) ⇒ Object
- #color(args = {}, &block) ⇒ Object
- #cube(args = {}) ⇒ Object
- #cylinder(args = {}) ⇒ Object
- #debug ⇒ Object
- #delete_from(hash, *keys) ⇒ Object
- #difference(&block) ⇒ Object
- #disable ⇒ Object
- #dxf_cross(_args = {}) ⇒ Object
- #dxf_dim(_args = {}) ⇒ Object
- #echo(*args) ⇒ Object
- #end_all_blocks ⇒ Object
- #end_block ⇒ Object
- #fa(value) ⇒ Object
- #fn(value) ⇒ Object
- #format_block(output_str) ⇒ Object
- #format_command(cmd_str, args = {}, &block) ⇒ Object
- #format_key(key) ⇒ Object
- #format_output(str) ⇒ Object
- #format_value(var) ⇒ Object
- #fs(value) ⇒ Object
- #hull(&block) ⇒ Object
- #import(args = {}) ⇒ Object
- #include_scad(file) ⇒ Object
- #intersection(&block) ⇒ Object
- #linear_extrude(args = {}, &block) ⇒ Object
- #lookup(x, points) ⇒ Object
- #minkowski(&block) ⇒ Object
- #mirror(args = {}, &block) ⇒ Object
- #multmatrix(args = {}, &block) ⇒ Object
- #new_line ⇒ Object
- #polygon(args = {}) ⇒ Object
- #polyhedron(args = {}) ⇒ Object
- #projection(args = {}, &block) ⇒ Object
- #raw_output(str) ⇒ Object
- #render(args = {}, &block) ⇒ Object
- #root ⇒ Object
- #rotate(args = {}, &block) ⇒ Object
- #rotate_extrude(args = {}, &block) ⇒ Object
- #scale(args = {}, &block) ⇒ Object
- #space_string(str, tab_level) ⇒ Object
- #sphere(args = {}) ⇒ Object
- #square(args = {}) ⇒ Object
- #start_block ⇒ Object
- #surface(args = {}) ⇒ Object
- #text(args = {}) ⇒ Object
- #translate(args = {}, &block) ⇒ Object
- #union(&block) ⇒ Object
- #use(file) ⇒ Object
- #vector_input(args, element) ⇒ Object
Class Method Details
.extended(_mod) ⇒ Object
338 339 340 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 338 def self.extended(_mod) start_output end |
.included(_mod) ⇒ Object
342 343 344 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 342 def self.included(_mod) start_output end |
.start_output ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 325 def self.start_output @@output_file ||= nil if ARGV[0] && ARGV[0].include?('.scad') @@output_file = ARGV[0] ARGV.shift end if @@output_file File.open(@@output_file, 'w') do |f| f.puts "//created with rubyscad #{VERSION}\n\n" end end end |
Instance Method Details
#background ⇒ Object
133 134 135 136 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 133 def background format_output BACKGROUND_STR yield if block_given? end |
#circle(args = {}) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 173 def circle(args = {}) if args.include?(:d) args[:r] = args[:d] / 2.0 args.delete(:d) end format_command CIRCLE_STR, args end |
#color(args = {}, &block) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 234 def color(args = {}, &block) if args.include?(:color) args[:color] = "\"#{args[:color]}\"" else args[:color] = [args.fetch(:r, 0), args.fetch(:g, 0), args.fetch(:b, 0), args.fetch(:a, 1)].to_s end delete_from(args, :r, :g, :b, :a) format_command(COLOR_STR, args[:color], &block) end |
#cube(args = {}) ⇒ Object
153 154 155 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 153 def cube(args = {}) format_command CUBE_STR, args end |
#cylinder(args = {}) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 189 def cylinder(args = {}) if args.include?(:d) args[:r] = args[:d] / 2.0 args.delete(:d) end if args.include?(:d1) args[:r1] = args[:d1] / 2.0 args.delete(:d1) end if args.include?(:d2) args[:r2] = args[:d2] / 2.0 args.delete(:d2) end format_command CYLINDER_STR, args end |
#debug ⇒ Object
138 139 140 141 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 138 def debug format_output DEBUG_STR yield if block_given? end |
#delete_from(hash, *keys) ⇒ Object
281 282 283 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 281 def delete_from(hash, *keys) keys.each { |k| hash.delete(k) } end |
#difference(&block) ⇒ Object
109 110 111 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 109 def difference(&block) format_command DIFFERENCE_STR, &block end |
#disable ⇒ Object
148 149 150 151 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 148 def disable format_output DISABLE_STR yield if block_given? end |
#dxf_cross(_args = {}) ⇒ Object
368 369 370 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 368 def dxf_cross(_args = {}) 0.0 end |
#dxf_dim(_args = {}) ⇒ Object
372 373 374 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 372 def dxf_dim(_args = {}) 0.0 end |
#echo(*args) ⇒ Object
87 88 89 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 87 def echo(*args) format_output ECHO_STR % { string: args.join(', ') } end |
#end_all_blocks ⇒ Object
305 306 307 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 305 def end_all_blocks end_block while @@tab_level > 0 end |
#end_block ⇒ Object
301 302 303 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 301 def end_block format_output END_BLOCK end |
#fa(value) ⇒ Object
67 68 69 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 67 def fa(value) format_output FA_STR % { value: value } end |
#fn(value) ⇒ Object
75 76 77 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 75 def fn(value) format_output FN_STR % { value: value } end |
#format_block(output_str) ⇒ Object
321 322 323 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 321 def format_block(output_str) output_str end |
#format_command(cmd_str, args = {}, &block) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 244 def format_command(cmd_str, args = {}, &block) if args.is_a? String arg_str = args else arg_str = args.collect do |k, v| "#{format_key(k)} = #{format_value(v)}" end .sort_by{ |x| ['$fn', '$fa', '$fs', 'cen', 'con'].include?(x[0..2]) ? ('z' + x) : x } .join(', ') end format_block cmd_str % { args: arg_str }, &block end |
#format_key(key) ⇒ Object
257 258 259 260 261 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 257 def format_key(key) key = key.to_s key.prepend('$') if key.match('^f[asn]$') key end |
#format_output(str) ⇒ Object
317 318 319 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 317 def format_output(str) str end |
#format_value(var) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 263 def format_value(var) if var.nil? "nil" elsif var.is_a?(Vector) || var.is_a?(Matrix) || var.is_a?(Array) res = [] var.to_a.each do |v| res << format_value(v) end res.to_s.gsub('"', "").gsub('\\', '') elsif var.is_a? String '"' + var + '"' elsif var.is_a? Float "%.#{FP_P}f" % var.round(FP_P).to_s else var.to_s end end |
#fs(value) ⇒ Object
71 72 73 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 71 def fs(value) format_output FS_STR % { value: value } end |
#hull(&block) ⇒ Object
129 130 131 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 129 def hull(&block) format_command HULL_STR, &block end |
#import(args = {}) ⇒ Object
105 106 107 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 105 def import(args = {}) format_command IMPORT_STR, args end |
#include_scad(file) ⇒ Object
79 80 81 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 79 def include_scad(file) format_output INCLUDE_STR % { file: file } end |
#intersection(&block) ⇒ Object
117 118 119 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 117 def intersection(&block) format_command INTERSECTION_STR, &block end |
#linear_extrude(args = {}, &block) ⇒ Object
95 96 97 98 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 95 def linear_extrude(args = {}, &block) str_end = args.include?(:file) ? ';' : '' format_command LINEAR_EXTRUDE_STR.concat(str_end), args, &block end |
#lookup(x, points) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 348 def lookup(x, points) xmin = 0.0 xmax = 0.0 points.keys.sort.reverse_each do |k| if k <= x xmin = k break end end points.keys.sort.each do |k| if k >= x xmax = k break end end return points[xmax] if x == xmax return points[xmin] if x == xmin points[xmin] + (((x - xmin) * (points[xmax] - points[xmin])) / (xmax - xmin)) end |
#minkowski(&block) ⇒ Object
125 126 127 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 125 def minkowski(&block) format_command MINKOWSKI_STR, &block end |
#mirror(args = {}, &block) ⇒ Object
225 226 227 228 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 225 def mirror(args = {}, &block) vector_input(args, :v) format_command MIRROR_STR, args, &block end |
#multmatrix(args = {}, &block) ⇒ Object
230 231 232 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 230 def multmatrix(args = {}, &block) format_command MULTMATRIX_STR, args, &block end |
#new_line ⇒ Object
293 294 295 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 293 def new_line format_output "\n" end |
#polygon(args = {}) ⇒ Object
181 182 183 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 181 def polygon(args = {}) format_command POLYGON_STR, args end |
#polyhedron(args = {}) ⇒ Object
165 166 167 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 165 def polyhedron(args = {}) format_command POLYHEDRON_STR, args end |
#projection(args = {}, &block) ⇒ Object
91 92 93 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 91 def projection(args = {}, &block) format_command PROJECTION_STR, args, &block end |
#raw_output(str) ⇒ Object
313 314 315 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 313 def raw_output(str) str end |
#render(args = {}, &block) ⇒ Object
121 122 123 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 121 def render(args = {}, &block) format_command RENDER_STR, args, &block end |
#root ⇒ Object
143 144 145 146 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 143 def root format_output ROOT_STR yield if block_given? end |
#rotate(args = {}, &block) ⇒ Object
209 210 211 212 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 209 def rotate(args = {}, &block) vector_input(args, :a) format_command ROTATE_STR, args, &block end |
#rotate_extrude(args = {}, &block) ⇒ Object
100 101 102 103 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 100 def rotate_extrude(args = {}, &block) str_end = args.include?(:file) ? ';' : '' format_command ROTATE_EXTRUDE_STR.concat(str_end), args, &block end |
#scale(args = {}, &block) ⇒ Object
220 221 222 223 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 220 def scale(args = {}, &block) vector_input(args, :v) format_command SCALE_STR, args, &block end |
#space_string(str, tab_level) ⇒ Object
309 310 311 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 309 def space_string(str, tab_level) ((' ' * TAB_SIZE) * tab_level) + str end |
#sphere(args = {}) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 157 def sphere(args = {}) if args.include?(:d) args[:r] = args[:d] / 2.0 args.delete(:d) end format_command SPHERE_STR, args end |
#square(args = {}) ⇒ Object
169 170 171 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 169 def square(args = {}) format_command SQUARE_STR, args end |
#start_block ⇒ Object
297 298 299 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 297 def start_block format_output START_BLOCK end |
#surface(args = {}) ⇒ Object
185 186 187 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 185 def surface(args = {}) format_command SURFACE_STR, args end |
#text(args = {}) ⇒ Object
205 206 207 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 205 def text(args = {}) format_command TEXT_STR, args end |
#translate(args = {}, &block) ⇒ Object
214 215 216 217 218 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 214 def translate(args = {}, &block) delete_from(args, :z) if args[:z] == 0 vector_input(args, :v) format_command TRANSLATE_STR, args, &block end |
#union(&block) ⇒ Object
113 114 115 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 113 def union(&block) format_command UNION_STR, &block end |
#use(file) ⇒ Object
83 84 85 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 83 def use(file) format_output USE_STR % { file: file } end |
#vector_input(args, element) ⇒ Object
285 286 287 288 289 290 291 |
# File 'lib/solidruby/rubyscad_bridge.rb', line 285 def vector_input(args, element) unless args.include?(element) args[element] = [args.fetch(:x, 0), args.fetch(:y, 0)] args[element].push(args[:z]) if args.include?(:z) delete_from(args, :x, :y, :z) end end |