Class: SolidRuby::SolidRubyObject

Inherits:
Object
  • Object
show all
Defined in:
lib/solidruby/solidruby_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attributes) ⇒ SolidRubyObject

Returns a new instance of SolidRubyObject.



23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



18
19
20
# File 'lib/solidruby/solidruby_object.rb', line 18

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



20
21
22
# File 'lib/solidruby/solidruby_object.rb', line 20

def children
  @children
end

#siblingsObject

Returns the value of attribute siblings.



21
22
23
# File 'lib/solidruby/solidruby_object.rb', line 21

def siblings
  @siblings
end

#transformationsObject

Returns the value of attribute transformations.



19
20
21
# File 'lib/solidruby/solidruby_object.rb', line 19

def transformations
  @transformations
end

Class Method Details

.alias_attr(long, short = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/solidruby/solidruby_object.rb', line 165

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

Instance Method Details

#&(obj) ⇒ Object



115
116
117
118
# File 'lib/solidruby/solidruby_object.rb', line 115

def &(obj)
  @siblings << obj
  self
end

#debugObject



120
121
122
123
# File 'lib/solidruby/solidruby_object.rb', line 120

def debug
  @debug_obj = true
  self
end

#debug?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/solidruby/solidruby_object.rb', line 125

def debug?
  @debug_obj
end

#mirror(*args) ⇒ Object



70
71
72
73
# File 'lib/solidruby/solidruby_object.rb', line 70

def mirror(*args)
  @transformations << Mirror.new(*args)
  self
end

#place(args = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/solidruby/solidruby_object.rb', line 81

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) ⇒ Object



37
38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
51
# 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) ⇒ Object



158
159
160
161
162
163
# File 'lib/solidruby/solidruby_object.rb', line 158

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

#scale(args) ⇒ Object



75
76
77
78
79
# File 'lib/solidruby/solidruby_object.rb', line 75

def scale(args)
  args = { v: args } if args.is_a?(Numeric) || args.is_a?(Array)
  @transformations << Scale.new(args)
  self
end

#to_rubyscadObject



154
155
156
# File 'lib/solidruby/solidruby_object.rb', line 154

def to_rubyscad
  ''
end

#translate(args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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
  if @transformations.last.is_a? Translate
    @transformations.last.x += args[:x] || 0
    @transformations.last.y += args[:y] || 0
    @transformations.last.z += args[:z] || 0
  else
    @transformations << Translate.new(args)
  end
  self
end

#union(args) ⇒ Object



65
66
67
68
# File 'lib/solidruby/solidruby_object.rb', line 65

def union(args)
  @transformations << Union.new(args)
  self
end

#walk_treeObject Also known as: scad_output



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/solidruby/solidruby_object.rb', line 129

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

#walk_tree_classesObject



145
146
147
148
149
150
151
152
# File 'lib/solidruby/solidruby_object.rb', line 145

def walk_tree_classes
  res = []
  @transformations.reverse.each do |trans|
    res += trans.walk_tree_classes
  end
  res << self.class
  res
end