class SolidRuby::Assemblies::Pipe

Attributes

bent_segments[RW]
pipe[RW]
sum_x[RW]
sum_y[RW]
x[RW]
y[RW]

Public Class Methods

new(args = {}) click to toggle source

Warning: sum_x and sum_y are both a quick hack at the moment

                         They will ONLY work on bends if you do same thing in the other direction
                                 for example
                                 pipe.cw(20,30)
                                 pipe.ccw(20,30)
This might be fixed in the future.
# File lib/solidruby/assemblies/pipe.rb, line 26
def initialize(args = {})
  # parameters
  @diameter = args[:diameter] || 1
  @fn = args[:fn] || 64
  @bent_segments = args[:bent_segments] || 128
  @line_rotation = args[:line_rotation] || 0 # z rotation in case needed with fn values

  # variable initialization
  @pipe = nil
  @sum_x = 0
  @sum_y = 0
end

Public Instance Methods

apply_rotation(obj) click to toggle source

This will be called on bent, so this library can work with rectangle pipes, if you overwrite this and let it rotate z by 90

# File lib/solidruby/assemblies/pipe.rb, line 48
def apply_rotation(obj)
  obj
end
ccw(radius, angle, color = nil) click to toggle source

go counter clockwise

# File lib/solidruby/assemblies/pipe.rb, line 70
def ccw(radius, angle, color = nil)
  return false if angle > 360
  # since bent can only do up to 90°, splitting it up in chunks in order to grow it
  return ccw(radius, 90, color) + ccw(radius, angle - 90, color) if angle > 90

  if @pipe.nil?
    @pipe = bent_ccw(radius, angle)
    @pipe = @pipe.color(color) unless color.nil?
  else
    rotated_pipe = @pipe.rotate(z: angle)
    pipe_piece = bent_ccw(radius, angle)
    pipe_piece = pipe_piece.color(color) unless color.nil?
    @pipe = pipe_piece + rotated_pipe.translate(x: x, y: y + radius)
  end
end
cw(radius, angle, color = nil) click to toggle source

go clockwise

# File lib/solidruby/assemblies/pipe.rb, line 53
def cw(radius, angle, color = nil)
  return false if angle > 360
  # since bent can only do up to 90°, splitting it up in chunks in order to grow it
  return cw(radius, 90, color) + cw(radius, angle - 90, color) if angle > 90

  if @pipe.nil?
    @pipe = bent_cw(radius, angle)
    @pipe = @pipe.color(color) unless color.nil?
  else
    rotated_pipe = @pipe.rotate(z: -angle)
    pipe_piece = bent_cw(radius, angle)
    pipe_piece = pipe_piece.color(color) unless color.nil?
    @pipe = pipe_piece + rotated_pipe.translate(x: x, y: y - radius)
  end
end
inner_shape() click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 43
def inner_shape
  nil
end
line(length, color = nil) click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 86
def line(length, color = nil)
  @pipe = if @pipe.nil?
            create_line(length, color)
          else
            @pipe.translate(x: length) + create_line(length, color)
          end
  @sum_x += length
end
shape() click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 39
def shape
  circle(d: @diameter, fn: @fn)
end

Private Instance Methods

bent_ccw(radius, angle) click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 129
def bent_ccw(radius, angle)
  res = apply_rotation(shape).translate(x: radius).rotate_extrude(fn: @bent_segments)
  res -= apply_rotation(inner_shape).translate(x: radius).rotate_extrude(fn: @bent_segments) unless inner_shape.nil?

  len = radius + @diameter / 2.0
  @x = Math.sin(radians(angle)) * len
  @y = Math.cos(radians(angle)) * len
  cut = polygon(points: [[0, 0], [0, len], [@x, @y]]).scale(2)

  # for working with it
  len = radius #- @diameter / 2.0
  @x = Math.sin(radians(angle)) * len
  @y = -1 * Math.cos(radians(angle)) * len

  @sum_x += @x
  @sum_y += @y

  res *= cut.linear_extrude(h: 100).translate(z: -50)
  res = res.mirror(y: 1)
  # Positioning it on 0

  res.translate(y: radius)
end
bent_cw(radius, angle) click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 106
def bent_cw(radius, angle)
  res = apply_rotation(shape).translate(x: radius).rotate_extrude(fn: @bent_segments)
  res -= apply_rotation(inner_shape).translate(x: radius).rotate_extrude(fn: @bent_segments) unless inner_shape.nil?

  len = radius + @diameter / 2.0
  @x = Math.sin(radians(angle)) * len
  @y = Math.cos(radians(angle)) * len
  cut = polygon(points: [[0, 0], [0, len], [@x, @y]]).scale(2)

  # for working with it
  len = radius #- @diameter / 2.0
  @x = Math.sin(radians(angle)) * len
  @y = Math.cos(radians(angle)) * len

  @sum_x += @x
  @sum_y += @y

  res *= cut.linear_extrude(h: 100).translate(z: -50)

  # Positioning it on 0
  res.translate(y: -radius)
end
create_line(length, color = nil) click to toggle source
# File lib/solidruby/assemblies/pipe.rb, line 97
def create_line(length, color = nil)
  res = shape.linear_extrude(h: length)
  if inner_shape
    res -= inner_shape.linear_extrude(h: length + 0.2).translate(z: -0.1)
  end
  res = res.color(color) if color
  res.rotate(z: @line_rotation).rotate(y: 90)
end