Class: JennCad::Features::Climb

Inherits:
Feature show all
Defined in:
lib/jenncad/features/climb.rb

Instance Attribute Summary

Attributes inherited from Thing

#anchors, #angle, #calc_h, #calc_x, #calc_y, #calc_z, #csize, #diameter, #fn, #name, #opts, #parent, #parts, #pos, #shape, #sits_on, #transformations, #x, #y

Instance Method Summary collapse

Methods inherited from Thing

#anchor, #at, #auto_color, #auto_color!, #auto_extrude, #calculate_center_rotation, #calculated_h, #children_list, #color, #color_or_fallback, #color_parse, #copy_anchor, #copy_anchors, #cut_to, #dbg, #debug?, #find_calculated_h, #fixate, #flip, #flipc, #get_children, #get_contents, #ghost, #has_explicit_color?, #hide, #hl, #inherit_color, #init, #is_2d?, #is_3d?, #mhx, #mhy, #mhz, #mirror, #mix, #miy, #miz, #modify_values, #modify_values!, #move, #movea, #moveai, #moveh, #movei, #multmatrix, #mx, #my, #mz, #on_top_of, #only, #only_color?, #openscad, #openscad_modifier, #option, #parse_xyz_shortcuts, #radians, #referenced_z, #reset, #reset_last_move, #rotate, #rotate_around, #rx, #ry, #rz, #scale, #set_anchor, #set_anchor_from, #set_auto_color, #set_auto_color_for_children, #set_flag, #set_heights_for_auto_extrude, #set_option, #set_parent, #skew, #to_mod, #top_of, #transform, #unset_flag, #z, #z=, #z_margin

Constructor Details

#initialize(opts, block) ⇒ Climb

Returns a new instance of Climb.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jenncad/features/climb.rb', line 3

def initialize(opts, block)
  $log.warn "DEPRECATED feature: climb. Please use x.of (multiples_of example)"

  @opts = {
    offset: :auto,
    step: nil,
    steps: nil,
    bottom: nil,
    top: nil,
    z: nil,
  }.deep_merge!(opts)
  if @opts[:step].nil? && @opts[:steps].nil?
    raise "please define at least one of :step or :steps for climb"
  end
  super(@opts)
  @block = block
end

Instance Method Details

#climb_from_bottom(offset, step, n) ⇒ Object



52
53
54
# File 'lib/jenncad/features/climb.rb', line 52

def climb_from_bottom(offset, step, n)
  n.times.map{ |i| @block.yield.mz(offset+step*i) }.union
end

#climb_from_top(z, offset, step, n) ⇒ Object



56
57
58
# File 'lib/jenncad/features/climb.rb', line 56

def climb_from_top(z, offset, step, n)
  n.times.map{ |i| @block.yield.mz(z-offset-step*i) }.union
end

#get_offset(z) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jenncad/features/climb.rb', line 40

def get_offset(z)
  case offset = @opts[:offset]
  when :auto
    step = get_step(z)
    ((z % step) + step) / 2.0
  when nil, 0, 0.0
    0.0
  else
    offset
  end
end

#get_step(z) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/jenncad/features/climb.rb', line 30

def get_step(z)
  case step = @opts[:step]
  when nil, :auto, 0, 0.0
    steps = @opts[:steps]
    (z  / steps).floor
  else
    step.to_d
  end
end

#to_openscadObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jenncad/features/climb.rb', line 60

def to_openscad
  ref_z = z_or_referenced
  step = get_step(ref_z)
  steps, top, bottom = @opts.values_at(:steps, :top, :bottom)

  offset = get_offset(ref_z)

  lo = (ref_z-offset*2).to_d % step.to_d
  unless lo.to_d == 0.0
    puts "[Warning]: climb has leftover offset #{lo}"
  end

  if steps
    top = steps
    bottom = steps
  end

  unless top or bottom
    climb_from_bottom(offset, step, ((ref_z-offset*2) / step).floor + 1 )
  else
    res = nil
    if top
      res += climb_from_top(ref_z, offset, step, top)
    end
    if bottom
      res += climb_from_bottom(offset, step, bottom)
    end
    res
  end
end

#z_or_referencedObject



21
22
23
24
25
26
27
28
# File 'lib/jenncad/features/climb.rb', line 21

def z_or_referenced
  case z = @opts[:z]
  when nil
    referenced_z.z
  else
    z
  end
end