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

    Generating coordinates with Ruby

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    6 Posts 4 Posters 423 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.
    • K Offline
      kmk111890
      last edited by

      Hi, I'm looking for how to generates coordinate with an equation and connect the coordinates adding line or curve with ruby.

      My idea was

      
      ents = Sketchup.active_model.entities
      pt0 = [a,b,c]
      pt1 = [d,e,f]
      pt2 = [g,h,i]
      
      for k in 0...20
      
      x = eq*pt0.x + eq*pt1.x + eq*pt2.x
      y = eq*pt0.y + eq*pt1.y + eq*pt2.y
      z = eq*pt0.z + eq*pt1.z + eq*pt2.z
      
      pt3 = Geom;;Point3d.new(x, y, z)
      
      end
      
      ents.add_line pt3
      
      

      pt0, pt1, pt2 are basic coordinates. 'eq' means random equation. We can add any equation with a variable 'k'

      I know there are some errors, but don't know how to fix it. I wish 21 different coordinates are generated from the 'for loop' and they connect each other to draw a line. Can you help me?

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

        @kmk111890 said:

        Hi, I'm looking for how to generates coordinate with an equation and connect the coordinates adding line or curve with ruby.

        My idea was

        
        > ents = Sketchup.active_model.entities
        > pt0 = [a,b,c]
        > pt1 = [d,e,f]
        > pt2 = [g,h,i]
        > 
        > for k in 0...20
        > 
        > x = eq*pt0.x + eq*pt1.x + eq*pt2.x
        > y = eq*pt0.y + eq*pt1.y + eq*pt2.y
        > z = eq*pt0.z + eq*pt1.z + eq*pt2.z
        > 
        > pt3 = Geom;;Point3d.new(x, y, z)
        > 
        > end
        > 
        > ents.add_line pt3
        > 
        

        pt0, pt1, pt2 are basic coordinates. 'eq' means random equation. We can add any equation with a variable 'k'

        I know there are some errors, but don't know how to fix it. I wish 21 different coordinates are generated from the 'for loop' and they connect each other to draw a line. Can you help me?

        The .add_line functions requires a start and end. Your code only has an end. The ents.add_line statement should be inside the For loop. If not, only the last "pair" will be used in the .add_line.

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

        http://sdmitch.blogspot.com/

        1 Reply Last reply Reply Quote 0
        • S Offline
          slbaumgartner
          last edited by

          In addition, if you gather the points into an Array you can use Entities#add_edges to create them all in one shot.

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

            Something like this:

            pt0 = [a, b, c]
            pt1 = [d, e, f]
            pt2 = [g, h, i]
            pt3 = Geom;;Point3d.new(0, 0, 0)
            pts = []
            for k in 0...20
                x = eq*pt0.x + eq*pt1.x + eq*pt2.x
                y = eq*pt0.y + eq*pt1.y + eq*pt2.y
                z = eq*pt0.z + eq*pt1.z + eq*pt2.z
                pt3 = Geom;;Point3d.new(x, y, z)
                pts << pt3
            end
            Sketchup.active_model.active_entities.add_edges(pts)
            
            

            Naturally you'll need to define the xyz value references a up to i, and the eq earlier on in the code...
            BUT is this example it currently makes a straight line, k is never used ?
            Is it actually eq ?
            If so then currently the points would just 'step' up by 1" each time !
            I suspect you somehow want to use pt3 iterated in the calculation [that's why I made it outside of the block before it starts changing] ?
            e.g. x = eq*pt0.x + eq*pt1.x + eq*pt2.x**+ eq*pt3.x**
            πŸ˜•

            TIG

            1 Reply Last reply Reply Quote 0
            • K Offline
              kmk111890
              last edited by

              Thank you, sdmitch and TIG. I could partly understand how i have to fix the codes, but not perfect because I showed you code roughly. Maybe, it would be better i show you what i'm doing now. I'm trying to add bezier curve to draw one simple shape. The equation for the quadratic bezier curve represents as shown below

              B(t) = (1-t)(1-t)P0+2(1-t)P1+(t)(t)P2

              With the equation, I tried to generate coordinates which are from the equation and wanted to use 'For loop'. hmmm It looked like i can make the codes by myself, but didn't work well

              ents = Sketchup.active_model.entities
              Length = 1000
              angle = (60)/2
              angle2 = (45)/2
              pt0 = [0,0,0]
              pt1 = [Length.mm, 0, 0]
              aa = 80*0.01
              pt2 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees))
              pt3 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle2.degrees))
              pt4 = [Length.mm*aa, 0, 0]
              pt5 = pt4.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees))
              arcarray = ents.add_arc [0,0,0], [1,0,0],[0,-1,0], Length.mm, 0, angle2.degrees
              arc = arcarray[0]
              arccurve = arc.curve
              verts = arccurve.vertices
              
              t1=0
              x6 = (1-(t1))*(1-(t1))*pt5.x+2*(1-(t1))*(t1)*pt2.x+(t1)*(t1)*pt3.x
              y6 = (1-(t1))*(1-(t1))*pt5.y+2*(1-(t1))*(t1)*pt2.y+(t1)*(t1)*pt3.y
              z6 = (1-(t1))*(1-(t1))*pt5.z+2*(1-(t1))*(t1)*pt2.z+(t1)*(t1)*pt3.z
              
              pt6 = Geom;;Point3d.new(x6, y6, z6)
              
              t2=0.2
              x7 = (1-(t2))*(1-(t2))*pt5.x+2*(1-(t2))*(t2)*pt2.x+(t2)*(t2)*pt3.x
              y7 = (1-(t2))*(1-(t2))*pt5.y+2*(1-(t2))*(t2)*pt2.y+(t2)*(t2)*pt3.y
              z7 = (1-(t2))*(1-(t2))*pt5.z+2*(1-(t2))*(t2)*pt2.z+(t2)*(t2)*pt3.z
              
              pt7 = Geom;;Point3d.new(x7, y7, z7)
              
              t3=0.4
              x8 = (1-(t3))*(1-(t3))*pt5.x+2*(1-(t3))*(t3)*pt2.x+(t3)*(t3)*pt3.x
              y8 = (1-(t3))*(1-(t3))*pt5.y+2*(1-(t3))*(t3)*pt2.y+(t3)*(t3)*pt3.y
              z8 = (1-(t3))*(1-(t3))*pt5.z+2*(1-(t3))*(t3)*pt2.z+(t3)*(t3)*pt3.z
              
              pt8 = Geom;;Point3d.new(x8, y8, z8)
              
              t4=0.6
              x9 = (1-(t4))*(1-(t4))*pt5.x+2*(1-(t4))*(t4)*pt2.x+(t4)*(t4)*pt3.x
              y9 = (1-(t4))*(1-(t4))*pt5.y+2*(1-(t4))*(t4)*pt2.y+(t4)*(t4)*pt3.y
              z9 = (1-(t4))*(1-(t4))*pt5.z+2*(1-(t4))*(t4)*pt2.z+(t4)*(t4)*pt3.z
              
              pt9 = Geom;;Point3d.new(x9, y9, z9)
              
              t5=0.8
              x10 = (1-(t5))*(1-(t5))*pt5.x+2*(1-(t5))*(t5)*pt2.x+(t5)*(t5)*pt3.x
              y10 = (1-(t5))*(1-(t5))*pt5.y+2*(1-(t5))*(t5)*pt2.y+(t5)*(t5)*pt3.y
              z10 = (1-(t5))*(1-(t5))*pt5.z+2*(1-(t5))*(t5)*pt2.z+(t5)*(t5)*pt3.z
              
              pt10 = Geom;;Point3d.new(x10, y10, z10)
              
              t6=1
              x11 = (1-(t6))*(1-(t6))*pt5.x+2*(1-(t6))*(t6)*pt2.x+(t6)*(t6)*pt3.x
              y11 = (1-(t6))*(1-(t6))*pt5.y+2*(1-(t6))*(t6)*pt2.y+(t6)*(t6)*pt3.y
              z11 = (1-(t6))*(1-(t6))*pt5.z+2*(1-(t6))*(t6)*pt2.z+(t6)*(t6)*pt3.z
              
              pt11 = Geom;;Point3d.new(x11, y11, z11)
              
              
              a = ents.add_line pt0, pt1
              b = ents.add_line pt0, pt5
              c = ents.add_curve pt0, pt6, pt7, pt8, pt9, pt10, pt11, pt1
              face = ents.add_face pt6, pt7, pt8, pt9, pt10, pt11, pt1, pt0
              path = ents.add_circle [Length,0,0], [-1,0,0], -Length
              face.followme path
              ents.erase_entities path
              
              face2 = ents.add_face verts
              path2 = ents.add_circle [Length,0,0], [-1,0,0], -Length
              face2.followme path2
              ents.erase_entities path2
              
              group=ents.add_group ents.to_a
              
              
              1 Reply Last reply Reply Quote 0
              • sdmitchS Offline
                sdmitch
                last edited by

                @kmk111890 said:

                Thank you, sdmitch and TIG. I could partly understand how i have to fix the codes, but not perfect because I showed you code roughly. Maybe, it would be better i show you what i'm doing now. I'm trying to add bezier curve to draw one simple shape. The equation for the quadratic bezier curve represents as shown below

                B(t) = (1-t)(1-t)P0+2(1-t)P1+(t)(t)P2

                With the equation, I tried to generate coordinates which are from the equation and wanted to use 'For loop'. hmmm It looked like i can make the codes by myself, but didn't work well

                This seems to work

                ents = Sketchup.active_model.entities
                Length = 1000
                angle = (60)/2
                angle2 = (45)/2
                pt0 = [0,0,0]
                pt1 = [Length.mm, 0, 0]
                aa = 80*0.01
                pt2 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees))
                pt3 = pt1.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle2.degrees))
                pt4 = [Length.mm*aa, 0, 0]
                pt5 = pt4.transform(Geom;;Transformation.rotation(pt0,[0,-1,0],angle.degrees))
                arcarray = ents.add_arc [0,0,0], [1,0,0],[0,-1,0], Length.mm, 0, angle2.degrees
                arc = arcarray[0]
                arccurve = arc.curve
                verts = arccurve.vertices
                pts=[];pts<<pt0
                0.step(1,0.2) do |t1|
                  x = (1-(t1))*(1-(t1))*pt5.x+2*(1-(t1))*(t1)*pt2.x+(t1)*(t1)*pt3.x
                  y = (1-(t1))*(1-(t1))*pt5.y+2*(1-(t1))*(t1)*pt2.y+(t1)*(t1)*pt3.y
                  z = (1-(t1))*(1-(t1))*pt5.z+2*(1-(t1))*(t1)*pt2.z+(t1)*(t1)*pt3.z
                  pts << Geom;;Point3d.new(x, y, z)
                end
                pts<<pt1
                a = ents.add_line pt0, pt1
                b = ents.add_line pt0, pt5
                c = ents.add_curve pts
                face = ents.add_face pts
                path = ents.add_circle [Length,0,0], [-1,0,0], -Length
                face.followme path
                ents.erase_entities path
                
                face2 = ents.add_face verts
                path2 = ents.add_circle [Length,0,0], [-1,0,0], -Length
                face2.followme path2
                ents.erase_entities path2
                
                group=ents.add_group ents.to_a
                
                

                kmk.gif

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

                http://sdmitch.blogspot.com/

                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