Module: RubyScad
- Defined in:
- lib/rubyscad/version.rb,
lib/rubyscad/RubyScad.rb
Constant Summary collapse
- VERSION =
"1.0.7"- START_BLOCK =
"{"- END_BLOCK =
"}"- TAB_SIZE =
3- PAD =
0.01
- CUBE_STR =
"cube(%<args>s);"- SPHERE_STR =
"sphere(%<args>s);"- CYLINDER_STR =
"cylinder(%<args>s);"- POLYHEDRON_STR =
"polyhedron(%<args>s);"- SQUARE_STR =
"square(%<args>s);"- CIRCLE_STR =
"circle(%<args>s);"- POLYGON_STR =
"polygon(%<args>s);"- TRANSLATE_STR =
"translate(%<args>s)"- ROTATE_STR =
"rotate(%<args>s)"- SCALE_STR =
"scale(%<args>s)"- MIRROR_STR =
"mirror(%<args>s)"- MULTMATRIX_STR =
"multmatrix(%<args>s)"- COLOR_STR =
"color(%<args>s)"- UNION_STR =
"union(%<args>s)"- DIFFERENCE_STR =
"difference(%<args>s)"- INTERSECTION_STR =
"intersection(%<args>s)"- RENDER_STR =
"render(%<args>s)"- MINKOWSKI_STR =
"minkowski(%<args>s)"- HULL_STR =
"hull(%<args>s)"- BACKGROUND_STR =
'%'- DEBUG_STR =
'#'- ROOT_STR =
'!'- DISABLE_STR =
'*'- IMPORT_STR =
"import(%<args>s);"- SURFACE_STR =
"surface(%<args>s);"- LINEAR_EXTRUDE_STR =
"linear_extrude(%<args>s)"- ROTATE_EXTRUDE_STR =
"rotate_extrude(%<args>s)"- PROJECTION_STR =
"projection(%<args>s)"- INCLUDE_STR =
"include <%<file>s>"- USE_STR =
"use <%<file>s>"- ECHO_STR =
"echo(%<string>s);"- FA_STR =
"$fa = %<value>s;"- FS_STR =
"$fs = %<value>s;"- FN_STR =
"$fn = %<value>s;"
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
- #translate(args = {}, &block) ⇒ Object
- #union(&block) ⇒ Object
- #use(file) ⇒ Object
- #vector_input(args, element) ⇒ Object
Class Method Details
.extended(mod) ⇒ Object
333 334 335 |
# File 'lib/rubyscad/RubyScad.rb', line 333 def self.extended(mod) start_output end |
.included(mod) ⇒ Object
337 338 339 |
# File 'lib/rubyscad/RubyScad.rb', line 337 def self.included(mod) start_output end |
.start_output ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/rubyscad/RubyScad.rb', line 320 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
125 126 127 128 |
# File 'lib/rubyscad/RubyScad.rb', line 125 def background() format_output BACKGROUND_STR yield if block_given? end |
#circle(args = {}) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/rubyscad/RubyScad.rb', line 165 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
221 222 223 224 225 226 227 228 229 |
# File 'lib/rubyscad/RubyScad.rb', line 221 def color(args={}, &block) unless args.include?(:color) args[:color] = [args.fetch(:r, 0), args.fetch(:g, 0), args.fetch(:b, 0), args.fetch(:a, 1)].to_s else args[:color] = "\"#{args[:color]}\"" end delete_from(args, :r, :g, :b, :a) format_command(COLOR_STR, args[:color], &block) end |
#cube(args = {}) ⇒ Object
145 146 147 |
# File 'lib/rubyscad/RubyScad.rb', line 145 def cube(args={}) format_command CUBE_STR, args end |
#cylinder(args = {}) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rubyscad/RubyScad.rb', line 181 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
130 131 132 133 |
# File 'lib/rubyscad/RubyScad.rb', line 130 def debug() format_output DEBUG_STR yield if block_given? end |
#delete_from(hash, *keys) ⇒ Object
256 257 258 |
# File 'lib/rubyscad/RubyScad.rb', line 256 def delete_from(hash, *keys) keys.each { |k| hash.delete(k) } end |
#difference(&block) ⇒ Object
101 102 103 |
# File 'lib/rubyscad/RubyScad.rb', line 101 def difference(&block) format_command DIFFERENCE_STR, &block end |
#disable ⇒ Object
140 141 142 143 |
# File 'lib/rubyscad/RubyScad.rb', line 140 def disable() format_output DISABLE_STR yield if block_given? end |
#dxf_cross(args = {}) ⇒ Object
362 363 364 |
# File 'lib/rubyscad/RubyScad.rb', line 362 def dxf_cross(args={}) return 0.0 end |
#dxf_dim(args = {}) ⇒ Object
366 367 368 |
# File 'lib/rubyscad/RubyScad.rb', line 366 def dxf_dim(args={}) return 0.0 end |
#echo(*args) ⇒ Object
79 80 81 |
# File 'lib/rubyscad/RubyScad.rb', line 79 def echo(*args) format_output ECHO_STR % {string: args.join(', ')} end |
#end_all_blocks ⇒ Object
291 292 293 |
# File 'lib/rubyscad/RubyScad.rb', line 291 def end_all_blocks() end_block while @@tab_level > 0 end |
#end_block ⇒ Object
287 288 289 |
# File 'lib/rubyscad/RubyScad.rb', line 287 def end_block() format_output END_BLOCK end |
#fa(value) ⇒ Object
59 60 61 |
# File 'lib/rubyscad/RubyScad.rb', line 59 def fa(value) format_output FA_STR % {value: value} end |
#fn(value) ⇒ Object
67 68 69 |
# File 'lib/rubyscad/RubyScad.rb', line 67 def fn(value) format_output FN_STR % {value: value} end |
#format_block(output_str) ⇒ Object
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rubyscad/RubyScad.rb', line 268 def format_block(output_str) format_output output_str.concat(' ') if block_given? start_block yield end_block else new_line unless output_str.include?(';') end end |
#format_command(cmd_str, args = {}, &block) ⇒ Object
231 232 233 234 235 236 237 238 |
# File 'lib/rubyscad/RubyScad.rb', line 231 def format_command(cmd_str, args={}, &block) unless args.kind_of? String arg_str = args.collect { |k, v| "#{format_key(k)} = #{format_value(v)}" }.join(', ') else arg_str = args end format_block cmd_str % {args: arg_str}, &block end |
#format_key(key) ⇒ Object
240 241 242 243 244 |
# File 'lib/rubyscad/RubyScad.rb', line 240 def format_key(key) key = key.to_s key.prepend('$') if key.match("^f[asn]$") key end |
#format_output(str) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/rubyscad/RubyScad.rb', line 307 def format_output(str) @@prev_output ||= "" @@tab_level ||= 0 str.lines do |l| l.concat("\n") if l.match('[;\}\{>]') @@tab_level-=1 if(l.include?('}')) && @@tab_level > 0 l = space_string(l, @@tab_level) if @@prev_output.include?("\n") raw_output(l) @@tab_level+=1 if(l.include?('{')) @@prev_output = l end end |
#format_value(var) ⇒ Object
246 247 248 249 250 251 252 253 254 |
# File 'lib/rubyscad/RubyScad.rb', line 246 def format_value(var) if var.is_a?(Vector) or var.is_a?(Matrix) return var.to_a.to_s elsif var.is_a? String return '"' + var + '"' else return var.to_s end end |
#fs(value) ⇒ Object
63 64 65 |
# File 'lib/rubyscad/RubyScad.rb', line 63 def fs(value) format_output FS_STR % {value: value} end |
#hull(&block) ⇒ Object
121 122 123 |
# File 'lib/rubyscad/RubyScad.rb', line 121 def hull(&block) format_command HULL_STR, &block end |
#import(args = {}) ⇒ Object
97 98 99 |
# File 'lib/rubyscad/RubyScad.rb', line 97 def import(args={}) format_command IMPORT_STR, args end |
#include_scad(file) ⇒ Object
71 72 73 |
# File 'lib/rubyscad/RubyScad.rb', line 71 def include_scad(file) format_output INCLUDE_STR % {file: file} end |
#intersection(&block) ⇒ Object
109 110 111 |
# File 'lib/rubyscad/RubyScad.rb', line 109 def intersection(&block) format_command INTERSECTION_STR, &block end |
#linear_extrude(args = {}, &block) ⇒ Object
87 88 89 90 |
# File 'lib/rubyscad/RubyScad.rb', line 87 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
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/rubyscad/RubyScad.rb', line 343 def lookup(x, points) xmin, xmax = [0.0, 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 return points[xmin] + (((x - xmin) * (points[xmax] - points[xmin])) / (xmax - xmin)) end |
#minkowski(&block) ⇒ Object
117 118 119 |
# File 'lib/rubyscad/RubyScad.rb', line 117 def minkowski(&block) format_command MINKOWSKI_STR, &block end |
#mirror(args = {}, &block) ⇒ Object
212 213 214 215 |
# File 'lib/rubyscad/RubyScad.rb', line 212 def mirror(args={}, &block) vector_input(args, :v) format_command MIRROR_STR, args, &block end |
#multmatrix(args = {}, &block) ⇒ Object
217 218 219 |
# File 'lib/rubyscad/RubyScad.rb', line 217 def multmatrix(args={}, &block) format_command MULTMATRIX_STR, args, &block end |
#new_line ⇒ Object
279 280 281 |
# File 'lib/rubyscad/RubyScad.rb', line 279 def new_line format_output "\n" end |
#polygon(args = {}) ⇒ Object
173 174 175 |
# File 'lib/rubyscad/RubyScad.rb', line 173 def polygon(args={}) format_command POLYGON_STR, args end |
#polyhedron(args = {}) ⇒ Object
157 158 159 |
# File 'lib/rubyscad/RubyScad.rb', line 157 def polyhedron(args={}) format_command POLYHEDRON_STR, args end |
#projection(args = {}, &block) ⇒ Object
83 84 85 |
# File 'lib/rubyscad/RubyScad.rb', line 83 def projection(args={}, &block) format_command PROJECTION_STR, args, &block end |
#raw_output(str) ⇒ Object
299 300 301 302 303 304 305 |
# File 'lib/rubyscad/RubyScad.rb', line 299 def raw_output(str) if @@output_file File.open(@@output_file, 'a') { |f| f.print(str) } else print str end end |
#render(args = {}, &block) ⇒ Object
113 114 115 |
# File 'lib/rubyscad/RubyScad.rb', line 113 def render(args={}, &block) format_command RENDER_STR, args, &block end |
#root ⇒ Object
135 136 137 138 |
# File 'lib/rubyscad/RubyScad.rb', line 135 def root() format_output ROOT_STR yield if block_given? end |
#rotate(args = {}, &block) ⇒ Object
197 198 199 200 |
# File 'lib/rubyscad/RubyScad.rb', line 197 def rotate(args={}, &block) vector_input(args, :a) format_command ROTATE_STR, args, &block end |
#rotate_extrude(args = {}, &block) ⇒ Object
92 93 94 95 |
# File 'lib/rubyscad/RubyScad.rb', line 92 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
207 208 209 210 |
# File 'lib/rubyscad/RubyScad.rb', line 207 def scale(args={}, &block) vector_input(args, :v) format_command SCALE_STR, args, &block end |
#space_string(str, tab_level) ⇒ Object
295 296 297 |
# File 'lib/rubyscad/RubyScad.rb', line 295 def space_string(str, tab_level) ((' '*TAB_SIZE)*tab_level) + str end |
#sphere(args = {}) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/rubyscad/RubyScad.rb', line 149 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
161 162 163 |
# File 'lib/rubyscad/RubyScad.rb', line 161 def square(args={}) format_command SQUARE_STR, args end |
#start_block ⇒ Object
283 284 285 |
# File 'lib/rubyscad/RubyScad.rb', line 283 def start_block() format_output START_BLOCK end |
#surface(args = {}) ⇒ Object
177 178 179 |
# File 'lib/rubyscad/RubyScad.rb', line 177 def surface(args={}) format_command SURFACE_STR, args end |
#translate(args = {}, &block) ⇒ Object
202 203 204 205 |
# File 'lib/rubyscad/RubyScad.rb', line 202 def translate(args={}, &block) vector_input(args, :v) format_command TRANSLATE_STR, args, &block end |
#union(&block) ⇒ Object
105 106 107 |
# File 'lib/rubyscad/RubyScad.rb', line 105 def union(&block) format_command UNION_STR, &block end |
#use(file) ⇒ Object
75 76 77 |
# File 'lib/rubyscad/RubyScad.rb', line 75 def use(file) format_output USE_STR % {file: file} end |
#vector_input(args, element) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/rubyscad/RubyScad.rb', line 260 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 |