sketchucation logo sketchucation
    • Login
    ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

    How to draw a cylinder by ruby?

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 7 Posters 2.9k Views
    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.
    • A Offline
      Andreas
      last edited by

      how can I draw a cylinder by ruby-code?

      With best regards from Germany

      Andreas

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        group=Sketchup.active_model.active_entities.add_group()
        ents=group.entities
        edges=ents.add_circle(center_point, normal_vector, radius, segments)
        face=edges[0].face
        face.pushpull(height)
        

        center_point is the point at which the center of the cylinder's bottom face sits...
        normal_vector is the direction of the circular face [which will become the cylinder's bottom face] - typically Z_AXIS...
        radius the circular face's and therefore the cylinder's radius...
        segments the number of segments in the circle and thereby the cylinder's 'side' faces...
        height is the cylinder's height.

        When you have made the cylinder you can use group.explode to return the cylinder's geometry to the active context if desired...

        TIG

        1 Reply Last reply Reply Quote 0
        • B Offline
          Bill_239
          last edited by

          Thank you very much! You provided just enough for me create my first Ruby SketchUp script (it makes cylinders) and I figured out how to integrate it with SketchUp..all in just a few hours..lol! It was an ideal challenge--I feel like I'm off and running! I should mention, for the sake of anyone reading this, that some background I had in O-O programming and some reading I did about Ruby, were invaluable too. I just registered at this site and I am looking forward to learning more about programming for SketchUp here.

          Bill

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            Great, welcome to the site. Good luck with your next Ruby endevor!

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • A Offline
              Andreas
              last edited by

              @tig said:

              group=Sketchup.active_model.active_entities.add_group()
              > ents=group.entities
              > edges=ents.add_circle(center_point, normal_vector, radius, segments)
              > face=edges[0].face
              > face.pushpull(height)
              

              center_point is the point at which the center of the cylinder's bottom face sits...
              normal_vector is the direction of the circular face [which will become the cylinder's bottom face] - typically Z_AXIS...
              radius the circular face's and therefore the cylinder's radius...
              segments the number of segments in the circle and thereby the cylinder's 'side' faces...
              height is the cylinder's height.

              When you have made the cylinder you can use group.explode to return the cylinder's geometry to the active context if desired...

              Sorry, but that doesn´t works:

              center_point = Geom;;Point3d.new(10, 10, 10)
              => Point3d(10, 10, 10)
              vector = Geom;;Vector3d.new(0,0,1)
              => Vector3d(0, 0, 1)
              
              => nil
              radius = 5
              => 5
              segments = 10
              => 10
              height = 2
              => 2
              
              => nil
              group=Sketchup.active_model.active_entities.add_group()
              => #<Sketchup;;Group;0x821f824>
              ents=group.entities
              => #<Sketchup;;Entities;0x821f298>
              
              => nil
              edges=ents.add_circle(center_point, Z_AXIS, radius, segments)
              => [#<Sketchup;;Edge;0x821e190>, #<Sketchup;;Edge;0x821e168>, #<Sketchup;;Edge;0x821e154>, #<Sketchup;;Edge;0x821e140>, #<Sketchup;;Edge;0x821e12c>, #<Sketchup;;Edge;0x821e118>, #<Sketchup;;Edge;0x821e104>, #<Sketchup;;Edge;0x821e0f0>, #<Sketchup;;Edge;0x821e0dc>, #<Sketchup;;Edge;0x821e0c8>]
              
              => nil
              face=edges[0].face
              NoMethodError; undefined method `face' for #<Sketchup;;Edge;0x821e190>
              
              

              With best regards from Germany

              Andreas

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                face=edges[0].faces[0]

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Andreas
                  last edited by

                  @thomthom said:

                  face=edges[0].faces[0]

                  There is also an error:

                  center_point = Geom;;Point3d.new(10, 10, 10)
                  => Point3d(10, 10, 10)
                  vector = Geom;;Vector3d.new(0,0,1)
                  => Vector3d(0, 0, 1)
                  
                  => nil
                  radius = 5
                  => 5
                  segments = 10
                  => 10
                  height = 2
                  => 2
                  
                  => nil
                  group=Sketchup.active_model.active_entities.add_group()
                  => #<Sketchup;;Group;0x82075d0>
                  ents=group.entities
                  => #<Sketchup;;Entities;0x8206d60>
                  
                  => nil
                  edges=ents.add_circle(center_point, Z_AXIS, radius, segments)
                  => [#<Sketchup;;Edge;0x8205adc>, #<Sketchup;;Edge;0x8205ac8>, #<Sketchup;;Edge;0x8205a64>, #<Sketchup;;Edge;0x8205a50>, #<Sketchup;;Edge;0x82059ec>, #<Sketchup;;Edge;0x82059d8>, #<Sketchup;;Edge;0x82059b0>, #<Sketchup;;Edge;0x820599c>, #<Sketchup;;Edge;0x8205988>, #<Sketchup;;Edge;0x8205974>]
                  face=edges[0].faces[0]
                  => nil
                  face.pushpull(height)
                  NoMethodError; undefined method `pushpull' for nil;NilClass
                  
                  

                  With best regards from Germany

                  Andreas

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    edges=ents.add_circle(center_point, Z_AXIS, radius, segments) edges[0].find_faces face=edges[0].faces[0]

                    or (maybe better option)

                    edges=ents.add_circle(center_point, Z_AXIS, radius, segments) face=ents.add_face( edges )

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      We have all gone adrift here... 😳
                      You have a set of edges forming a circular 'ring' - nothing more.
                      We've missing a line of code!
                      To make a face from the edges use
                      face=ents.add_face(edges)
                      then to ensure it's the inner face that the edges delineate use
                      face=edges[0].faces[0]
                      NOW face will be the inner face of the circle - this is useful in case the 'ring' is already drawn over a face etc and you might end up with two faces touching an edge... 🤓

                      EDIT: I see Thomthom beat me to it!

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • B Offline
                        buidoi3003
                        last edited by

                        Hi guys,
                        I use the following code to set the center point = position of the mouse click:

                        def onLButtonDown(flags, x, y, view)
                        puts " x = "
                        puts " y = "
                        puts "--------------------- "
                        centerpoint = Geom::Point3d.new x,y,0

                        However, the (x,y) seems not to be my mouse's click position. It is just random.
                        How could I set the center point to be my mouse click position?

                        1 Reply Last reply Reply Quote 0
                        • D Offline
                          driven
                          last edited by

                          hi
                          have a look at linetool.rb and class CylTool < LineTool in the Example scripts folder in ../SketchUp/Plugins/examples/linetool.rb, or download it if it's not......

                          john

                          learn from the mistakes of others, you may not live long enough to make them all yourself...

                          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