sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Code correction

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 865 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      hanl000
      last edited by

      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__)
      
      
      1 Reply Last reply Reply Quote 0
      • S Offline
        slbaumgartner
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • sdmitchS Offline
          sdmitch
          last edited by

          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)
          

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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 initialize method, that automatically gets called by the new constructor.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • H Offline
              hanl000
              last edited by

              Thank you for giving me advices. The problem has been partly fixed by myself. Maybe, I have to pay attention to studying ruby more

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement