Class: JennCad::Path

Inherits:
Thing
  • Object
show all
Defined in:
lib/jenncad/features/path.rb

Instance Attribute Summary collapse

Attributes inherited from Thing

#anchors, #calc_h, #calc_x, #calc_y, #calc_z, #csize, #diameter, #fn, #name, #opts, #parent, #parts, #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(args) ⇒ Path

Returns a new instance of Path.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/jenncad/features/path.rb', line 195

def initialize(args)
  @thing = store_thing_from(args[:part])
  @part = new_thing
  @angle = args[:a] || 0
  @current_angle = args[:a] || 0
  @direction = 0
  @name = args[:name] || "Path"
  @calc_h = @part.z
  @dir = 0
  @lpos = {x: 0, y: 0, l: 0, a: 0}
  @coords = []
  @steps = []
  @parts = [new_thing]

  super(args)
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def angle
  @angle
end

#current_angleObject

Returns the value of attribute current_angle.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def current_angle
  @current_angle
end

#directionObject

Returns the value of attribute direction.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def direction
  @direction
end

#elementsObject

Returns the value of attribute elements.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def elements
  @elements
end

#lposObject

Returns the value of attribute lpos.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def lpos
  @lpos
end

#thingObject

Returns the value of attribute thing.



194
195
196
# File 'lib/jenncad/features/path.rb', line 194

def thing
  @thing
end

Instance Method Details

#add_lpos(lpos) ⇒ Object



307
308
309
310
311
312
313
# File 'lib/jenncad/features/path.rb', line 307

def add_lpos(lpos)
  #puts "x: #{lpos[:x]}, y: #{lpos[:y]}"
  [:x, :y].each do |key|
    @lpos[key] += lpos[key]
  end
  @coords << lpos
end

#assembleObject



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/jenncad/features/path.rb', line 324

def assemble
  @steps.each_with_index do |step, i|
    if @steps[i+1] && @steps[i+1].class == Line && step.class == RoundCorner
      step.set_angles(@steps[i+1].current_angle, @steps[i+1].angle, @steps[i+1].direction)
    end
  end
  res = nil
  @steps.each do |step|
    if step.kind_of? Transformation
      res.transformations << step
    else
      res += step.part
    end
  end
  set_heights_for_auto_extrude([res], self)
  res.transformations << @transformations
  Aggregation.new(@name, res)
end

#centerObject



319
320
321
322
# File 'lib/jenncad/features/path.rb', line 319

def center
  @steps << Move.new(x: @lpos[:x] * -1, y: @lpos[:y] * -1)
  self
end

#corner(args) ⇒ Object



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
# File 'lib/jenncad/features/path.rb', line 222

def corner(args)
  od = args[:od]
  id = args[:id]
  if id.nil? && od
    id = new_thing.d
    #puts "od: #{od} , thing d #{new_thing.d} new id #{id}"
  end
  if od.nil? && id
    od = id + new_thing.d
  end
  rc = RoundCorner.new(
    start_point: { x: @lpos[:x], y: @lpos[:y] },
    end_point: { x: @lpos[:x], y: @lpos[:y] },
    od: od,
    id: id,
    ccw: args[:ccw],
    from: args[:from],
    to: args[:to],
    a: args[:a],
    direction: @direction,
    thing: thing,
  )

  _, ox, oy, x, y = rc.positions
  @lpos[:x] += x + ox
  @lpos[:y] += y + oy

  @steps << rc
end

#line(args) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/jenncad/features/path.rb', line 252

def line(args)
  if args[:l]
    args[:a] ||= 0
    if args[:a] > 0
      @direction = 1
    elsif args[:a] < 0
      @direction = -1
    else
      @direction = 0
    end

    @angle += args[:a]
    @current_angle = args[:a]

    l = args[:l]
    gamma = 90
    alpha = @angle
    beta = gamma - alpha

    case
    when alpha == 90, alpha == -270
      x = l
      y = 0
    when alpha == 180, alpha == -180
      x = 0
      y = -l
    when alpha == 270, alpha == -90
      x = -l
      y = 0
    when alpha == 360, alpha == 0
      x = 0
      y = l
    else
      y = l/Math::sin(radians(gamma)) * Math::sin(radians(beta))
      x = y/Math::sin(radians(beta)) * Math::sin(radians(alpha))
    end
  elsif args[:x] || args[:y]
    x = args[:x] || 0
    y = args[:y] || 0

    dx = @lpos[:x] - x
    dy = @lpos[:y] - y

    l = Math::sqrt(dx*dx + dy*dy)
    alpha = Math::atan2(x, y)*180/Math::PI
  else
    puts "Error in line(): Please specify either :l, :x or :y"
    return
  end

  @steps << Line.new(start_point: { x: @lpos[:x], y: @lpos[:y] }, end_point: { x: @lpos[:x] + x, y: @lpos[:y] + y  }, angle: @angle, current_angle: @current_angle, direction: @direction, l: l, thing: thing)

  add_lpos({x: x, y: y})
end

#new_thingObject



218
219
220
# File 'lib/jenncad/features/path.rb', line 218

def new_thing
  Marshal.load(@thing)
end

#posObject



315
316
317
# File 'lib/jenncad/features/path.rb', line 315

def pos
  [@lpos[:x], @lpos[:y]]
end

#store_thing_from(thing) ⇒ Object



212
213
214
215
216
# File 'lib/jenncad/features/path.rb', line 212

def store_thing_from(thing)
  r = Marshal.load(Marshal.dump(thing)) # make sure we don't edit the object in memory
  r.transformations = []
  Marshal.dump(r)
end