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



163
164
165
166
167
168
169
170
171
172
173
174
# 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

Instance Method Details

#&(obj) ⇒ Object



113
114
115
116
# File 'lib/solidruby/solidruby_object.rb', line 113

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

#debugObject



118
119
120
121
# File 'lib/solidruby/solidruby_object.rb', line 118

def debug
  @debug_obj = true
  self
end

#debug?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/solidruby/solidruby_object.rb', line 123

def debug?
  @debug_obj
end

#mirror(args) ⇒ Object



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

def mirror(args)
  @transformations ||= []
  @transformations << Mirror.new(args)
  self
end

#place(args = {}) ⇒ Object



79
80
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
# 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) ⇒ 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



156
157
158
159
160
161
# 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

#scale(args) ⇒ Object



72
73
74
75
76
77
# 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_rubyscadObject



152
153
154
# File 'lib/solidruby/solidruby_object.rb', line 152

def to_rubyscad
  ''
end

#translate(args) ⇒ Object



53
54
55
56
57
58
# 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) ⇒ Object



60
61
62
63
64
# File 'lib/solidruby/solidruby_object.rb', line 60

def union(args)
  @transformations ||= []
  @transformations << Union.new(args)
  self
end

#walk_treeObject Also known as: scad_output



127
128
129
130
131
132
133
134
135
136
137
138
139
# 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

#walk_tree_classesObject



143
144
145
146
147
148
149
150
# 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