class SolidRuby::SolidRubyObject

Attributes

attributes[RW]
children[RW]
siblings[RW]
transformations[RW]

Public Class Methods

alias_attr(long, short=nil) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 163
def self.alias_attr(long, short=nil)
  short ||= long[0].downcase.to_sym
  @@attr_aliases ||= {}
  @@attr_aliases[self.name] ||= {}
  @@attr_aliases[self.name][short] = long

  self.class_eval {
    define_method long do
      @attributes[long]
    end
  }
end
new(*attributes) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 23
def initialize(*attributes)
  @transformations = []
  @siblings = []
  @attributes = attributes.flatten
  @attributes = @attributes[0] if @attributes[0].is_a? Hash
  @debug_obj = false

  @@attr_aliases ||= {}
  @@attr_aliases[self.class.name] ||= {}
  @@attr_aliases[self.class.name].each do |k, v|
    @attributes[v] ||= @attributes.delete(k) unless @attributes[k].nil?
  end
end

Public Instance Methods

&(obj) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 113
def &(obj)
  @siblings << obj
  self
end
debug() click to toggle source
# File lib/solidruby/solidruby_object.rb, line 118
def debug
  @debug_obj = true
  self
end
debug?() click to toggle source
# File lib/solidruby/solidruby_object.rb, line 123
def debug?
  @debug_obj
end
mirror(args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 66
def mirror(args)
  @transformations ||= []
  @transformations << Mirror.new(args)
  self
end
place(args={}) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 79
def place(args={})
  return self if args.nil? || self.nil? || args[:onto].nil?

  onto = args[:onto]
  face = args[:face] || :top
  edge = args[:edge] || :center
  corner = args[:corner] || :center
  face_offset = args[:face_offset] || 0
  edge_offset = args[:edge_offset] || 0
  corner_offset = args[:corner_offset] || 0

  if onto.respond_to? :get_point_on
    move_to = onto.get_point_on(
      face: face,
      edge: edge,
      corner: corner,
      face_offset: face_offset,
      edge_offset: edge_offset,
      corner_offset: corner_offset
    )

    if (self.respond_to? :get_point_on)
      move_me = self.get_point_on(face: :center, edge: :center, corner: :center)
      move_to[:x] -= move_me[:x]
      move_to[:y] -= move_me[:y]
      move_to[:z] -= move_me[:z]
    end

    self.translate(move_to)
  end

  self
end
rotate(args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 37
def rotate(args)
  # always make sure we have a z parameter; otherwise RubyScad will produce a 2-dimensional output
  # which can result in openscad weirdness
  args[:z] = 0 if args[:z].nil?
  @transformations ||= []
  @transformations << Rotate.new(args)
  self
end
rotate_around(point, args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 46
def rotate_around(point, args)
  x = point.x
  y = point.y
  z = point.z
  translate(x: -x, y: -y, z: -z).rotate(args).translate(x: x, y: y, z: z)
end
save(filename, start_text = nil) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 156
def save(filename, start_text = nil)
  file = File.open(filename, 'w')
  file.puts start_text unless start_text.nil?
  file.puts scad_output
  file.close
end
scad_output()
Alias for: walk_tree
scale(args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 72
def scale(args)
  args = { v: args } if args.is_a?(Numeric) || args.is_a?(Array)
  @transformations ||= []
  @transformations << Scale.new(args)
  self
end
to_rubyscad() click to toggle source
# File lib/solidruby/solidruby_object.rb, line 152
def to_rubyscad
  ''
end
translate(args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 53
def translate(args)
  return self if (args[:x] || 0) == 0 && (args[:y] || 0) == 0 && (args[:z] || 0) == 0
  @transformations ||= []
  @transformations << Translate.new(args)
  self
end
union(args) click to toggle source
# File lib/solidruby/solidruby_object.rb, line 60
def union(args)
  @transformations ||= []
  @transformations << Union.new(args)
  self
end
walk_tree() click to toggle source
# File lib/solidruby/solidruby_object.rb, line 127
def walk_tree
  res = ''

  @transformations.reverse.each do |trans|
    res += trans.walk_tree
  end
  res += '#' if self.debug?
  res += to_rubyscad.to_s + "\n"
  @siblings.each do |s|
    res += s.walk_tree
  end
  res
end
Also aliased as: scad_output
walk_tree_classes() click to toggle source
# File lib/solidruby/solidruby_object.rb, line 143
def walk_tree_classes
  res = []
  @transformations.reverse.each do |trans|
    res += trans.walk_tree_classes
  end
  res << self.class
  res
end