Class: CrystalScad::Hardware::Nut

Inherits:
Hardware show all
Defined in:
lib/crystalscad/Hardware.rb

Instance Attribute Summary collapse

Attributes inherited from Assembly

#color, #hardware, #skip, #transformations, #x, #y, #z

Attributes inherited from Primitive

#children

Attributes inherited from CrystalScadObject

#args, #transformations

Instance Method Summary collapse

Methods inherited from Assembly

#*, #+, #-, #add_to_bom, #colorize, get_skip, get_views, #method_missing, #part, #scad_output, #show_hardware, skip, #threads, #transform, view, #walk_tree

Methods inherited from Primitive

#mirror, #rotate, #rotate_around, #scale, #transform, #translate, #union

Methods inherited from CrystalScadObject

#method_missing, #save, #to_rubyscad, #walk_tree, #walk_tree_classes

Constructor Details

#initialize(size, args = {}) ⇒ Nut

Returns a new instance of Nut.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/crystalscad/Hardware.rb', line 213

def initialize(size,args={})
	@size = size
	@type = args[:type] ||= "934"
	@material = args[:material] ||= "8.8"
	@surface = args[:surface] ||= "zinc plated"
	@support = args[:support] ||= false
	@support_layer_height = args[:support_layer_height] ||= 0.2
	@margin = args[:margin] ||= 0.3 # default output margin

	@slot = args[:slot] || nil
	@slot_margin = args[:slot_margin] || 0.5
	@slot_direction = args[:slot_direction] || "z"
	@cylinder_length = args[:cylinder_length] || 0	# for slot only

	@transformations ||= []
	@args = args
	prepare_data
	@height = args[:height] || @height 

	@direction = args[:direction] || @slot_direction 

	@bolt = nil

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CrystalScad::Assembly

Instance Attribute Details

#heightObject

Returns the value of attribute height.



212
213
214
# File 'lib/crystalscad/Hardware.rb', line 212

def height
  @height
end

Instance Method Details

#add_support(layer_height = @support_layer_height) ⇒ Object



295
296
297
298
299
300
301
302
# File 'lib/crystalscad/Hardware.rb', line 295

def add_support(layer_height=@support_layer_height)
	res = cylinder(d:@support_diameter,h:@height-layer_height)
	# on very small nuts, add a support base of one layer height, so the support won't fall over
	if @size < 6 
		res += cylinder(d:@s-1,h:layer_height)
	end	
	res
end

#bolt(length = nil, args = {}) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/crystalscad/Hardware.rb', line 242

def bolt(length=nil, args={})
	return @bolt if @bolt
	@bolt = Bolt.new(@size,length,args)
	case @direction
		when "z"
			bolt.transformations << Rotate.new(x:180)	
			bolt.transformations << Translate.new({z:length	})		
		when "-z"
			bolt.transformations << Translate.new({z:-length+@height})		
		when "-x"
			bolt.transformations << Rotate.new(x:180)	
			bolt.transformations << Translate.new({z:length})		
		when "x"
			bolt.transformations << Translate.new({z:-length+@height})		
		when "-y"
			bolt.transformations << Rotate.new(x:180)	
			bolt.transformations << Translate.new({z:length})		
		when "y"
			bolt.transformations << Translate.new({z:-length+@height})		
	end			
	@bolt.transformations += self.transformations.dup
	
	return @bolt
end

#descriptionObject



238
239
240
# File 'lib/crystalscad/Hardware.rb', line 238

def description
	"M#{@size} Nut, DIN #{@type}, #{@material} #{@surface}"
end

#nut_934(show = true, margin = 0, height_margin = 0) ⇒ Object



345
346
347
348
349
350
351
352
353
354
# File 'lib/crystalscad/Hardware.rb', line 345

def nut_934(show=true,margin=0,height_margin=0)		
	@s += margin
	
	res = cylinder(d:(@s/Math.sqrt(3))*2,h:@height+height_margin,fn:6)
	res -= cylinder(d:@size,h:@height) if show == true
 	if @support
		res -= add_support			
	end 	
	res.color("Gainsboro")
end

#outputObject



331
332
333
334
335
336
337
338
# File 'lib/crystalscad/Hardware.rb', line 331

def output	
	add_to_bom
	if @slot == nil
		return transform(nut_934(false,@margin))			
	else	
		return transform(slot)
	end
end

#prepare_dataObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/crystalscad/Hardware.rb', line 267

def prepare_data
	chart_934 = {2.5=> {side_to_side:5,height:2, support_diameter:2.8}, 
								3 => {side_to_side:5.5,height:2.4, support_diameter:3.5},
							  4 => {side_to_side:7,height:3.2, support_diameter:4.4},
								5 => {side_to_side:8,height:4, support_diameter:5.3},
								6 => {side_to_side:10,height:5, support_diameter:6.3},
								8 => {side_to_side:13,height:6.5, support_diameter:8.3},
							 10 => {side_to_side:17,height:8, support_diameter:10.3},
							 12 => {side_to_side:19,height:10, support_diameter:12.3},

							}
	# for securing nuts
	chart_985 = {
							 3 => {height:4},
							 4 => {height:5},										
							 5 => {height:5},										
							 6 => {height:6},										
							}

	@s = chart_934[@size][:side_to_side]
	@height = chart_934[@size][:height]
	@support_diameter = chart_934[@size][:support_diameter]
	if @type == "985"
		@height = chart_985[@size][:height]			
	end
	
end

#showObject



340
341
342
343
# File 'lib/crystalscad/Hardware.rb', line 340

def show
	add_to_bom
	return transform(nut_934)
end

#slotObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/crystalscad/Hardware.rb', line 304

def slot
	case @slot_direction	
		when "x" 
			pos = {x:@slot}
		when "y"
			pos = {y:@slot}
		when "z"
			pos = {z:@slot}
		when "-x" 
			pos = {x:-@slot}
		when "-y"
			pos = {y:-@slot}
		when "-z"
			pos = {z:-@slot}
		else
		raise "Invalid slot direction #{@slot_direction}"
	end
	res = hull(
		nut_934(false,@margin,@slot_margin),
		nut_934(false,@margin,@slot_margin).translate(pos)
	)				
	if @cylinder_length > 0
		res += cylinder(d:@size+@margin,h:@cylinder_length)			
	end	
	res	
end