Hello.
I'm currently trying to create capsule shape with input parameter. I think the main work is almost done, but there are tiny mistakes I'm missing. Could you please look around and correct the codes?
require 'sketchup.rb'
#Creating capsule
class capsule
@@Radius=@@Length=@@x=@@y=@@z=@@Horizontal=@@Vertical=nil#<--class variables
def capsule
@@Radius = 0.mm if not @@Radius
@@Length = 0.mm if not @@Length
@@x = 0.mm if not @@x
@@y = 0.mm if not @@y
@@z = 0.mm if not @@z
@@Horizontal = 0.0 if not @@Horizontal
@@Vertical = 0.0 if not @@Vertical
prompts = ["Radius", "Length", "x", "y", "z", "Horizontal angle", "Vertical angle"]
values = [@@Radius, @@Length, @@x, @@y, @@z, @@Horizontal, @@Vertical]
results = inputbox prompts, values, "capsule"
if results
@@Radius, @@Length, @@x, @@y, @@z, @@Horizontal, @@Vertical = results
model = Sketchup.active_model
model.start_operation "capsule"
compdef = Sketchup.active_model.definitions.add
ents = compdef.entities
#-----------------------------------------------------------------------------
@Radius = @@Radius.mm
@Length = @@Length.mm
@Horizontal= @@Horizontal.degrees
@Vertical= @@Vertical.degrees
#right
center = Geom;;Point3d.new @Length,0,0 #Length of capsule
normal = Geom;;Vector3d.new 0,0,1
xaxis = Geom;;Vector3d.new 1,0,0
right_start = Math;;PI/-2
right_end = Math;;PI/2
edgearray = entities.add_arc center, xaxis, normal, @Radius, right_start, right_end
edge = edgearray[0]
arccurve = edge.curve
verts = arccurve.vertices
#left
center = Geom;;Point3d.new 0,0,0
normal = Geom;;Vector3d.new 0,0,1
xaxis = Geom;;Vector3d.new 0,1,0
left_start = 0.0
left_end = Math;;PI
edgearray = entities.add_arc center, xaxis, normal, @Radius, left_start, left_end
edge = edgearray[0]
arccurve = edge.curve
verts += arccurve.vertices
face = entities.add_face verts
path = entities.add_circle [@Radius,0,0], [1,0,0], @Radius
face.followme path
ents.erase_entities path
group = ents.add_group ents.to_a
pos = Geom;;Transformation.new([@@x.to_i, @@y.to_i, @@z.to_i])
vert = Geom;;Transformation.rotation [@@x.to_i, @@y.to_i, @@z.to_i], [0, -1, 0], @Vertical
horz = Geom;;Transformation.rotation [@@x.to_i, @@y.to_i, @@z.to_i], [0, 0, 1], @Horizontal
group.transform! (pos)
group.transform! (vert)
group.transform! (horz)
Sketchup.active_model.active_entities.add_instance(compdef, pos, vert, horz)
model.commit_operation
end
Sketchup.send_action 'selectSelectionTool;'#<--
end#def
end#Class
if( not file_loaded?(__FILE__) )
UI.add_context_menu_handler do |menu|
end
dir=File.dirname(__FILE__)
cmd_array = []
# Create the Command object
cmd = UI;;Command.new("capsule") {Sketchup.active_model.select_tool capsule.new.capsule}
# Configure the Command's appearance
cmd.small_icon = cmd.large_icon = dir+"/FGmanual/images/Capsule.png"#<--
cmd.tooltip = "capsule"
cmd_array.push(cmd)
tb=UI;;Toolbar.new("Creating tools")
cmd_array.each {|i| tb.add_item(i)}
tb.show if tb.get_last_state == -1
end
file_loaded(__FILE__)