Class: JennCad::Primitives::Slot

Inherits:
Primitive show all
Includes:
CircleIsh
Defined in:
lib/jenncad/primitives/slot.rb

Instance Attribute Summary collapse

Attributes inherited from Primitive

#dimensions

Attributes inherited from Thing

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

Instance Method Summary collapse

Methods included from CircleIsh

#auto_dn!, #handle_fn, #handle_radius_diameter, #radius, #set_anchors_2d

Methods inherited from Primitive

#feed_opts, #handle_diameter, #handle_margins

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(args) ⇒ Slot

Returns a new instance of Slot.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jenncad/primitives/slot.rb', line 6

def initialize(args)
  if args.kind_of?(Array) && args[0].kind_of?(Hash)
    args = args.first
  end
  if args.kind_of? Array
    m = {}
    if args.last.kind_of? Hash
      m = args.last
    end
    args = [:d, :z].zip(args.flatten).to_h
    args.deep_merge!(m)
  end
  args = parse_xyz_shortcuts(args)
  if args[:z].to_d > 0
    args[:h] = args[:z]
  else
    args[:z] = nil
  end

  @opts = {
    d: 0,
    a: 0,
    r: nil,
    x: 0,
    y: 0,
    z: nil,
    d1: nil,
    d2: nil,
    mode: :auto,
    cz: false,
    az: false,
    margins: {
      r: 0,
      d: 0,
      z: 0,
    },
  }.deep_merge!(args)

  super(opts)

  @d = @opts[:d].to_d
  @a = @opts[:a].to_d
  @h = @opts[:h].to_d
  @z = @h
  @x = @opts[:x].to_d
  @y = @opts[:y].to_d

  @r = @opts[:r] || nil
  if @r
    @d = @r * 2
  end
  @fn = @opts[:fn]
  @len_x = @opts[:x]
  @len_y = @opts[:y]
  tx = @opts[:tx] || @opts[:total_x] || nil
  ty = @opts[:ty] || @opts[:total_y] || nil
  if tx
    @len_x = tx - @d
  end
  if ty
    @len_y = ty - @d
  end
  if @opts[:z] && opts[:z].to_d > 0
    @dimensions = [:x, :y, :z]
  else
    @dimensions = [:x, :y]
  end

  set_anchors
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def d
  @d
end

#fnObject

Returns the value of attribute fn.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def fn
  @fn
end

#hObject

Returns the value of attribute h.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def h
  @h
end

#len_xObject

Returns the value of attribute len_x.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def len_x
  @len_x
end

#len_yObject

Returns the value of attribute len_y.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def len_y
  @len_y
end

#rObject

Returns the value of attribute r.



4
5
6
# File 'lib/jenncad/primitives/slot.rb', line 4

def r
  @r
end

Instance Method Details

#czObject



77
78
79
80
81
82
# File 'lib/jenncad/primitives/slot.rb', line 77

def cz
  @opts[:cz] = true
  @transformations << Move.new(z: -@z / 2.0)
  set_anchors
  self
end

#end_vectorObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/jenncad/primitives/slot.rb', line 186

def end_vector
  if @a.to_d == 0.0
    return [@len_x, 0] if @len_x
    return [0, @len_y] if @len_y
  end
  if @len_x
    x = cos(PI*@a/180.0)*@len_x.to_d
    y = sin(PI*@a/180.0)*@len_x.to_d
  else
    x = -1* sin(PI*@a/180.0)*@len_y.to_d
    y = cos(PI*@a/180.0)*@len_y.to_d
  end
  [x,y]
end

#get_modeObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jenncad/primitives/slot.rb', line 127

def get_mode
  if @opts[:d1] && @opts[:d2]
    case @opts[:mode]
    when nil, :auto
      :dia1
    when :cyl
      :cyl
    else
      :dia1
    end
  else
    :default
  end
end

#set_anchorsObject



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
# File 'lib/jenncad/primitives/slot.rb', line 85

def set_anchors
  @anchors = {} # reset anchors
  rad = radius

  if @x > 0
    set_anchor :left, x: - rad
    set_anchor :right, x: @x + rad
  elsif @x < 0
    set_anchor :left, x: @x - rad
    set_anchor :right, x: rad
  else
    set_anchor :left, x: -rad
    set_anchor :right, x: rad
  end
  if @y > 0
    set_anchor :bottom, y: - rad
    set_anchor :top, y: @y + rad
  elsif @y < 0
    set_anchor :bottom, y: @y - rad
    set_anchor :top, y: rad
  else
    set_anchor :bottom, y: -rad
    set_anchor :top, y: rad
  end

  set_anchor :center1, xy: 0
  set_anchor :center2, x: @x, y: @y

  # TODO: figure out if we also want to have "corners"
  # - possibly move it like a cube
  # - points at 45 ° angles might not be that useful unless you can get the point on the circle at a given angle
  # - inner/outer points could be useful for small $fn values

  if @opts[:cz]
    set_anchor :bottom_face, z: -@z/2.0
    set_anchor :top_face, z: @z/2.0
  else
    set_anchor :bottom_face, z: 0
    set_anchor :top_face, z: @z
  end
end

#to_openscadObject



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/jenncad/primitives/slot.rb', line 142

def to_openscad
  mode = get_mode

  opts = @opts.clone
  opts.delete(:color)

  case mode
  when :default
    c1 = ci(opts)
    c2 = ci(opts)
  when :dia1 # new default mode; d1 start dia, d2 end dia
    c1 = ci(opts.merge(d: @opts[:d1]))
    c2 = ci(opts.merge(d: @opts[:d2]))
  when :cyl # old mode; use cylinders
    c1 = cy(opts)
    c2 = cy(opts)
  end

  if @len_x
    c2.move(x:@len_x)
  end
  if @len_y
    c2.move(y:@len_y)
  end
  res = c1 & c2
  if mode != :cyl && @z.to_d > 0
    res = res.e(@z)
  elsif @opts[:az] == true
    # TODO: this needs testing, may not work
    res = res.auto_extrude
  end
  res.inherit_color(self)

  if @a != 0.0
    res = res.rotate(z:@a)
  end
  if @transformations && @transformations.size > 0
    res = UnionObject.new([res])
    res.transformations = @transformations
    return res
  end
  res
end