Code correction
-
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__) -
Please be more explicit about what makes you think there are "tiny mistakes". I can see various flaws (e.g. a class name should start with a capital letter, also consult Dan Rathbun's frequent posts about proper usage of modules to control namespaces) but I'd rather help you with a specific problem than to write your code for you.
-
As pointed out by slbaumgartner, class names must be capitalized.
Due to Sketchup's trouble with small things, I would multiply @Radius and @Length by 1000 and scale by 0.001 after the capsule is created.
scl=Geom;;Transformation.scaling(ORIGIN,0.001) ents.transform_entities(scl,ents.to_a)edgearray = ents.add_arc and face = ents.add_face so they are inside compdef
Since everything is in compdef, group is not needed.
The horz and vert transformations should also use ORIGIN for the point.
The add_instance requires a definition and a transformation. Since there are three transformations needed, they can be combined, ie
Sketchup.active_model.active_entities.add_instance(compdef,pos*horz*vert) -
Do NOT put spaces between method names and parameter lists:
group.transform! (pos)
should be:
group.transform!(pos)Read up on how to define a class, including use of the built-in
initializemethod, that automatically gets called by thenewconstructor. -
Thank you for giving me advices. The problem has been partly fixed by myself. Maybe, I have to pay attention to studying ruby more
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better đź’—
Register LoginAdvertisement