class SolidRuby::RubyScadBridge

Constants

BACKGROUND_STR
CIRCLE_STR
COLOR_STR
CUBE_STR
CYLINDER_STR
DEBUG_STR
DIFFERENCE_STR
DISABLE_STR
ECHO_STR
END_BLOCK
FA_STR
FN_STR
FP_P
FS_STR
HULL_STR
IMPORT_STR
INCLUDE_STR
INTERSECTION_STR
LINEAR_EXTRUDE_STR
MINKOWSKI_STR
MIRROR_STR
MULTMATRIX_STR
PAD
POLYGON_STR
POLYHEDRON_STR
PROJECTION_STR
RENDER_STR
ROOT_STR
ROTATE_EXTRUDE_STR
ROTATE_STR
SCALE_STR
SPHERE_STR
SQUARE_STR
START_BLOCK
SURFACE_STR
TAB_SIZE
TEXT_STR
TRANSLATE_STR
UNION_STR
USE_STR

Public Class Methods

extended(_mod) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 338
def self.extended(_mod)
  start_output
end
included(_mod) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 342
def self.included(_mod)
  start_output
end
start_output() click to toggle source
# 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

Public Instance Methods

background() { || ... } click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 133
def background
  format_output BACKGROUND_STR
  yield if block_given?
end
circle(args = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 153
def cube(args = {})
  format_command CUBE_STR, args
end
cylinder(args = {}) click to toggle source
# 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() { || ... } click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 138
def debug
  format_output DEBUG_STR
  yield if block_given?
end
delete_from(hash, *keys) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 281
def delete_from(hash, *keys)
  keys.each { |k| hash.delete(k) }
end
difference(&block) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 109
def difference(&block)
  format_command DIFFERENCE_STR, &block
end
disable() { || ... } click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 148
def disable
  format_output DISABLE_STR
  yield if block_given?
end
dxf_cross(_args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 368
def dxf_cross(_args = {})
  0.0
end
dxf_dim(_args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 372
def dxf_dim(_args = {})
  0.0
end
echo(*args) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 87
def echo(*args)
  format_output ECHO_STR % { string: args.join(', ') }
end
end_all_blocks() click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 305
def end_all_blocks
  end_block while @@tab_level > 0
end
end_block() click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 301
def end_block
  format_output END_BLOCK
end
fa(value) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 67
def fa(value)
  format_output FA_STR % { value: value }
end
fn(value) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 75
def fn(value)
  format_output FN_STR % { value: value }
end
format_block(output_str) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 321
def format_block(output_str)
  output_str
end
format_command(cmd_str, args = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 317
def format_output(str)
  str
end
format_value(var) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 71
def fs(value)
  format_output FS_STR % { value: value }
end
hull(&block) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 129
def hull(&block)
  format_command HULL_STR, &block
end
import(args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 105
def import(args = {})
  format_command IMPORT_STR, args
end
include_scad(file) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 79
def include_scad(file)
  format_output INCLUDE_STR % { file: file }
end
intersection(&block) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 117
def intersection(&block)
  format_command INTERSECTION_STR, &block
end
linear_extrude(args = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 125
def minkowski(&block)
  format_command MINKOWSKI_STR, &block
end
mirror(args = {}, &block) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 230
def multmatrix(args = {}, &block)
  format_command MULTMATRIX_STR, args, &block
end
new_line() click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 293
def new_line
  format_output "\n"
end
polygon(args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 181
def polygon(args = {})
  format_command POLYGON_STR, args
end
polyhedron(args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 165
def polyhedron(args = {})
  format_command POLYHEDRON_STR, args
end
projection(args = {}, &block) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 91
def projection(args = {}, &block)
  format_command PROJECTION_STR, args, &block
end
raw_output(str) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 313
def raw_output(str)
  str
end
render(args = {}, &block) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 121
def render(args = {}, &block)
  format_command RENDER_STR, args, &block
end
root() { || ... } click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 143
def root
  format_output ROOT_STR
  yield if block_given?
end
rotate(args = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 309
def space_string(str, tab_level)
  ((' ' * TAB_SIZE) * tab_level) + str
end
sphere(args = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 169
def square(args = {})
  format_command SQUARE_STR, args
end
start_block() click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 297
def start_block
  format_output START_BLOCK
end
surface(args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 185
def surface(args = {})
  format_command SURFACE_STR, args
end
text(args = {}) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 205
def text(args = {})
  format_command TEXT_STR, args
end
translate(args = {}, &block) click to toggle source
# 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) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 113
def union(&block)
  format_command UNION_STR, &block
end
use(file) click to toggle source
# File lib/solidruby/rubyscad_bridge.rb, line 83
def use(file)
  format_output USE_STR % { file: file }
end
vector_input(args, element) click to toggle source
# 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