SketchUp Plugin: Sine Wave

Just a quick little plugin to draw a sine curve in SketchUp.

Draws a sine curve at the Origin and in a Group. User is asked for amplitude, length, and number of segments.

sine_wave.rb



1 comment:

Lunkwill said...

Here's an enhancement that adds cosine and tangent, and changes the amplitude and frequency to be floats instead of ints.

# Copyright (C) 2010 Jim Foltz
# Draw a Sine wave
require "sketchup"

module JF
def self.clamp(y, max)
if y > max
y = max
end
if y < -max
y = -max
end
return y
end

def self.draw_sine_wave
prompts = %w( Length Amplitude Segments Function )
defaults = [10.0, 5.0, 24, "Sine"]
list = ['','','',"Sine|Cosine|Tan"]
title = "Parameters"

ret = UI.inputbox(prompts, defaults, list, title)
return unless ret
#a = 10.0.feet
#b = 250.0.feet
b = ret[0].to_l
a = ret[1].to_l
segments = ret[2].to_l
f = ret[3]
#b, a, segments, f = ret.map{|e| e.to_l}
pts = []
0.step(b, b/segments) do |x|
y = 0.0
case f
when 'Sine'
y = a * Math::sin( (x / b) * 2 * Math::PI)
when 'Cosine'
y = a * Math::cos( (x / b) * 2 * Math::PI)
when 'Tan'
y = Math::tan( (x / b) * 2 * Math::PI)
y = clamp(y, a)
end
pts << [x, y]
end
Sketchup.active_model.start_operation("Trig Function")
grp = Sketchup.active_model.active_entities.add_group
entities = grp.entities
entities.add_edges pts
Sketchup.active_model.commit_operation

end

end
unless file_loaded? "jf_trig.rb"
UI.menu("Plugins").add_item("Trig functions") { JF.draw_sine_wave }
file_loaded "jf_trig.rb"
end

Popular Posts (Last 30 Days)