Module: SolidRuby::ScrewThreads

Included in:
SolidRuby
Defined in:
lib/solidruby/screw_thread.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: ScrewThread

Instance Method Summary collapse

Instance Method Details

#create_bolts(face, obj1, obj2, 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
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
167
168
169
# File 'lib/solidruby/screw_thread.rb', line 79

def create_bolts(face, obj1, obj2, args = {})
  # make a obj1-=obj2 with bolts corresponding to the height of obj1

  if face.nil? || obj1.nil? || obj2.nil?
    raise 'usage: create_bolts(face,obj1,obj2,args={})  - args can include (obj1.)height and bolt_height'
    return
  end

  # we need to know obj1 height (if not supplied by user)
  #TODO: Rescuing like this is not good practise
  height ||= args[:height]
  case face.to_s
  when 'top'
    height ||= obj1.z rescue nil
  when 'bottom'
    height ||= obj1.z rescue nil
  when 'left'
    height ||= obj1.x rescue nil
  when 'right'
    height ||= obj1.x rescue nil
  when 'front'
    height ||= obj1.y rescue nil
  when 'back'
    height ||= obj1.y rescue nil
  end
  height ||= obj1.height rescue nil

  if height.nil?
    raise "the object we're substracting from doesn't have a height defined; please define manually"
    return
  end

  # lets check if the obj2 responds to the threads_[face] method

  meth = "threads_#{face}"

  unless obj2.respond_to?(meth)
    raise "The object you're trying to get bolts from doesn't supply any on the face '#{face}'. Please add a method #{meth} to this object"
    return
  end
  holes = obj2.send(meth)

  return if holes.nil?

  # let the user either define bolt_heights as integer, array or none (will be guessed)
  if args[:bolt_height].is_a? Array
    bolt_heights = args[:bolt_height]
    if bolt_heights.size != holes.size
      raise "object has #{holes.size} threads for bolts but you supplied #{bolt_heights.size}"
      return
    end
  else
    bolt_heights = []
    holes.each do |hole|
      bolt_heights << if args[:bolt_height]
                        args[:bolt_height]
                      else
                        (height + hole.depth).floor
                      end
    end
  end

  ret = []
  holes.each_with_index do |hole, i|
    bolt = Bolt.new(hole.size, bolt_heights[i], washer: args[:washer])
    case face
    when 'top'
      bolt.transformations << Rotate.new(x: 180)
      bolt.transformations << Translate.new(x: hole.x, y: hole.y, z: hole.z + height)
    when 'bottom'
      bolt.transformations << Translate.new(x: hole.x, y: hole.y, z: hole.z)
    when 'left'
      bolt.transformations << Rotate.new(y: 90)
      bolt.transformations << Translate.new(x: hole.x, y: hole.y, z: hole.z)
    when 'right'
      bolt.transformations << Rotate.new(y: -90)
      bolt.transformations << Translate.new(x: hole.x + height, y: hole.y, z: hole.z)
    when 'front'
      bolt.transformations << Rotate.new(x: -90)
      bolt.transformations << Translate.new(x: hole.x, y: hole.y, z: hole.z)
    when 'back'
      bolt.transformations << Rotate.new(x: 90)
      bolt.transformations << Translate.new(x: hole.x, y: hole.y + height, z: hole.z)
    end
    bolt.transformations += obj2.transformations unless obj2.transformations.nil?

    ret << bolt
  end

  ret
end