Module: SolidRuby::Helpers

Included in:
SolidRuby
Defined in:
lib/solidruby/helpers/fillet.rb,
lib/solidruby/helpers/chamfer.rb,
lib/solidruby/helpers/position.rb,
lib/solidruby/helpers/triangle.rb,
lib/solidruby/helpers/rounded_cube.rb

Overview

This file is part of SolidRuby.

SolidRuby is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SolidRuby is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SolidRuby.  If not, see <http://www.gnu.org/licenses/>.

Defined Under Namespace

Classes: Triangle

Instance Method Summary collapse

Instance Method Details

#calculate_point_on(args = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/solidruby/helpers/position.rb', line 168

def calculate_point_on(args = {})
  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

  vert_axis = :z
  horiz_axis = :x
  vert_dir = 1
  horiz_dir = 1

  pos = case face
  when :top
    vert_axis = :y
    horiz_dir = -1
    {x: args[:x] / 2.0, y: args[:y] / 2.0, z: args[:z] + face_offset}
  when :bottom
    vert_axis = :y
    horiz_dir = -1
    vert_dir = -1
    { x: args[:x] / 2.0, y: args[:y] / 2.0, z: -face_offset}
  when :left
    horiz_axis = :y
    { x: face_offset, y: args[:y] / 2.0, z: args[:z] / 2.0 }
  when :right
    horiz_axis = :y
    horiz_dir = -1
    { x: args[:x] + face_offset, y: args[:y] / 2.0, z: args[:z] / 2.0 }
  when :front
    horiz_dir = -1
    { x: args[:x] / 2.0, y: -face_offset, z: args[:z] / 2.0 }
  when :back
    { x: args[:x] / 2.0, y: args[:y] + face_offset, z: args[:z] / 2.0 }
  when :center
    horiz_dir = -1
    { x: args[:x] / 2.0, y: args[:y] / 2.0, z: args[:z] / 2.0 }
  end

  return pos if pos.nil?

  pos[:x] -= args[:x] / 2.0 if args[:centered]
  pos[:y] -= args[:y] / 2.0 if args[:centered]
  pos[:z] -= args[:z] / 2.0 if args[:centered_z]

  args[:transformations].each do |t|
    pos[:x] += t.x
    pos[:y] += t.y
    pos[:z] += t.z
  end

  #pos is now center of the given face, move to the given side
  h_change = case horiz_axis
  when :x
    args[:x] / 2.0
  when :y
    args[:y] / 2.0
  end

  v_change = case vert_axis
  when :y
    args[:y] / 2.0
  else
    args[:z] / 2.0
  end

  case edge
  when :top
    pos[vert_axis] += (v_change * vert_dir)
    pos[vert_axis] += (edge_offset * vert_dir)
  when :bottom
    pos[vert_axis] -= (v_change * vert_dir)
    pos[vert_axis] -= (edge_offset * vert_dir)
  when :left
    pos[horiz_axis] += (h_change * horiz_dir)
    pos[horiz_axis] += (edge_offset * horiz_dir)
  when :right
    pos[horiz_axis] -= (h_change * horiz_dir)
    pos[horiz_axis] -= (edge_offset * horiz_dir)
  end

  case corner
  when :top
    pos[vert_axis] += (v_change * vert_dir)
    pos[vert_axis] += (corner_offset * vert_dir)
  when :bottom
    pos[vert_axis] -= (v_change * vert_dir)
    pos[vert_axis] -= (corner_offset * vert_dir)
  when :left
    pos[horiz_axis] += (h_change * horiz_dir)
    pos[horiz_axis] += (corner_offset * horiz_dir)
  when :right
    pos[horiz_axis] -= (h_change * horiz_dir)
    pos[horiz_axis] -= (corner_offset * horiz_dir)
  end

  pos
end

#chamfer(args = {}) ⇒ Object

Helper method for creating chamfers - basically just an extruded triangle that can be subtracted from an edge



19
20
21
22
23
24
25
26
# File 'lib/solidruby/helpers/chamfer.rb', line 19

def chamfer(args={})
  height = args[:height] || args[:h] || 0
  length = args[:length] || args[:l] || 0

  t = triangle(a: height, alpha: 90, beta: 45)

  return t.linear_extrude(height: length)
end

#corners(face = :top) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/solidruby/helpers/position.rb', line 18

def corners(face = :top)
  [
    {face: face, edge: :top, corner: :left},
    {face: face, edge: :top, corner: :right},
    {face: face, edge: :bottom, corner: :left},
    {face: face, edge: :bottom, corner: :right},
  ]
end

#fillet(args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/solidruby/helpers/fillet.rb', line 17

def fillet(args)
  @radius = args[:r] || args[:radius]
  @height = args[:h] || args[:height]
  @fn = args[:fn] || 64

  res = cube(@radius*2, @radius*2, @height+0.02).translate(z: -0.01) -
  cylinder(r: @radius, h: @height + 0.04, fn: @fn)
    .translate(z: -0.02)

  res.translate(x: -@radius, y: -@radius)
end

#is_horizontal?(face, edge) ⇒ Boolean



37
38
39
# File 'lib/solidruby/helpers/position.rb', line 37

def is_horizontal?(face, edge)
  !is_vertical?(face, edge)
end

#is_vertical?(face, edge) ⇒ Boolean



27
28
29
30
31
32
33
34
35
# File 'lib/solidruby/helpers/position.rb', line 27

def is_vertical?(face, edge)
  if [:top, :bottom].include? face
    false
  elsif [:top, :bottom].include? edge
    false
  else
    true
  end
end

#is_x_dir?(face, edge) ⇒ Boolean



41
42
43
44
45
46
47
48
49
50
# File 'lib/solidruby/helpers/position.rb', line 41

def is_x_dir?(face, edge)
  if is_vertical?(face, edge)
    false
  elsif [:front, :back, :top, :bottom].include?(face) &&
        [:top, :bottom].include?(edge)
    true
  else
    false
  end
end

#is_y_dir?(face, edge) ⇒ Boolean



52
53
54
55
# File 'lib/solidruby/helpers/position.rb', line 52

def is_y_dir?(face, edge)
  return false if is_x_dir?(face, edge) || is_vertical?(face, edge)
  true
end

#normalise_edges(args = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/solidruby/helpers/position.rb', line 57

def normalise_edges(args={})
  faces = {}

  args.each do |key, val|
    if [:front, :back, :left, :right, :top, :bottom].include? key
      val = [val] unless val.is_a? Array
      faces[key] = val
      next
    elsif key == :edges
      if val == :vertical
        faces[:front] = [:left, :right]
        faces[:back] = [:left, :right]
      elsif val == :horizontal
        faces[:front] = [:top, :bottom]
        faces[:right] = [:top, :bottom]
        faces[:left] = [:top, :bottom]
        faces[:back] = [:top, :bottom]
      elsif val == :all
        faces[:front] = [:top, :bottom, :left, :right]
        faces[:right] = [:top, :bottom]
        faces[:left] = [:top, :bottom]
        faces[:back] = [:top, :bottom, :left, :right]
      end
    end
  end
  faces
end

#rounded_cube(args = {}, y = nil, z = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/solidruby/helpers/rounded_cube.rb', line 17

def rounded_cube(args = {}, y = nil, z = nil)
  c = cube(args, y, z)
  r = args[:r]
  sx = c.x - r * 2
  sy = c.y - r * 2
  sz = c.z - r

  res = hull(
    sphere(r: r).translate(z: r),
    sphere(r: r).translate(z: sz),
    sphere(r: r).translate(x: sx, z: sz),
    sphere(r: r).translate(x: sx, y: sy, z: sz),
    sphere(r: r).translate(y: sy, z: sz),
    sphere(r: r).translate(x: sx, z: r),
    sphere(r: r).translate(y: sy, z: r),
    sphere(r: r).translate(x: sx, y: sy, z: r)
  ).translate(x: r, y: r)

  res
end

#translations_for_edge(args = {}) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/solidruby/helpers/position.rb', line 85

def translations_for_edge(args={})
  tolerance = args[:tolerance] || 0.01
  trans = []
  args[:faces].each do |face, edges|
    edges.each do |edge|
      res = {}
      res[:z_rot] = 0
      res[:y_rot] = 0
      res[:x_rot] = 0
      res[:x_trans] = 0
      res[:y_trans] = 0
      res[:length] = args[:z]
      res[:z_trans] = -res[:length]/2.0 - (tolerance*2)

      #position on edge
      if Helpers::is_horizontal?(face, edge)
        res[:y_rot] = 90
        res[:z_trans] = 0
      end

      if is_x_dir?(face, edge)
        res[:length] = args[:x]
        res[:x_trans] = -res[:length] / 2.0 - (tolerance*2)
      elsif is_y_dir?(face, edge)
        res[:length] = args[:y]
        res[:y_trans] = res[:length] / 2.0
        res[:x_rot] = 90
      end

      #rotate to match edge
      rot_matrix = {
        [:top, :top] => 90,
        [:top, :left] => 180,
        [:top, :right] => 90,
        [:top, :bottom] => 180,

        [:bottom, :top] => 270,
        [:bottom, :left] => 270,
        [:bottom, :right] => 0,
        [:bottom, :bottom] => 0,

        [:left, :top] => 180,
        [:left, :left] => 90,
        [:left, :right] => 180,
        [:left, :bottom] => 270,

        [:right, :top] => 90,
        [:right, :left] => 270,
        [:right, :right] => 0,
        [:right, :bottom] => 0,

        [:front, :top] => 180,
        [:front, :left] => 180,
        [:front, :right] => 270,
        [:front, :bottom] => 270,

        [:back, :top] => 90,
        [:back, :left] => 0,
        [:back, :right] => 90,
        [:back, :bottom] => 0,
      }

      res[:z_rot] = rot_matrix[[face, edge]]

      point = args[:onto].get_point_on(face: face, edge: edge)

      if tolerance > 0
        point[:x] = point[:x] > 0 ? point[:x] + tolerance : point[:x] - tolerance
        point[:y] = point[:y] > 0 ? point[:y] + tolerance : point[:y] - tolerance
        point[:z] = point[:z] > 0 ? point[:z] + tolerance : point[:z] - tolerance
      end

      res[:x_trans] += point[:x]
      res[:y_trans] += point[:y]
      res[:z_trans] += point[:z]

      trans << res
    end
  end

  trans
end

#triangle(args) ⇒ Object



164
165
166
# File 'lib/solidruby/helpers/triangle.rb', line 164

def triangle(args)
  Triangle.new(args)
end