class CrystalScad::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
# File lib/crystalscad/Pipe.rb, line 32
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/crystalscad/Pipe.rb, line 54
def apply_rotation(obj)
        return obj           
end
ccw(radius,angle,color=nil) click to toggle source

go counter clockwise

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

        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/crystalscad/Pipe.rb, line 59
def cw(radius,angle,color=nil)
        if angle > 360
                return false                
        end          
        # since bent can only do up to 90°, splitting it up in chunks in order to grow it
        if angle > 90
                return cw(radius,90,color) + cw(radius,angle-90,color)      
        end

        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/crystalscad/Pipe.rb, line 49
def inner_shape
        nil
end
line(length,color=nil) click to toggle source
# File lib/crystalscad/Pipe.rb, line 101
def line(length,color=nil)
        if @pipe == nil
                @pipe = create_line(length,color) 
        else
                @pipe = @pipe.translate(x:length) + create_line(length,color)                               
        end
        @sum_x += length
end
radians(a) 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/crystalscad/Pipe.rb, line 28
def radians(a)
  a/180.0 * Math::PI
end
shape() click to toggle source
# File lib/crystalscad/Pipe.rb, line 45
def shape
        res = circle(d:@diameter,fn:@fn)
end

Private Instance Methods

bent_ccw(radius,angle) click to toggle source
# File lib/crystalscad/Pipe.rb, line 146
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

        return res.translate(y:radius)
end
bent_cw(radius,angle) click to toggle source
# File lib/crystalscad/Pipe.rb, line 122
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

        return res.translate(y:-radius)
end
create_line(length,color=nil) click to toggle source
# File lib/crystalscad/Pipe.rb, line 111
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
        if color
                res = res.color(color)              
        end
        res.rotate(z:@line_rotation).rotate(y:90)
end