sketchucation logo sketchucation
    • Login
    đŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Getting global coordinates

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 2 Posters 800 Views 2 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.
    • B Offline
      bizello
      last edited by

      Hi,
      I woul like to write a script to get the midpoint global coordinate from the line current selected.
      The task is store this global coordinates (x,y,z) inside variables, and retrive them as reference point to insert objects or draw lines from them.
      I search on Sketchup ruby API Documentation but I didn't found.
      Someone knows how can I do this?
      Thanks!
      Sergio Bizello

      Sérgio A.Bizello

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

        An Edge has two Vertices, or you can use its Start and End Vertices.
        You can then find the Positions of these (==Points).
        You use the Point Offset method on a Point towards the other Point,
        by half the Distance between these two Points
        That is the mid-point you want...

        
        edge=ss[0] ### say, or however you set/get the 'edge'
        point0=edge.start.position
        point1=edge.end.position
        vector=point1-point0
        distance=point0.distance(point1)
        halfway=distance/2
        point_mid=point0.offset(vector,halfway)
        
        

        I've made it with perhaps more variables and steps than you need, for clarity...

        To store these use...

        model.set_attribute("MyLines",edge.to_s,point_mid.to_a)
        

        To recover these use

        ### assuming 'edge' is selected somehow...
        mid_edge=model.get_attribute("MyLines",edge.to_s,[])
        if mid_edge[2]###=z
          ### do stuff with this point [x,y,z]
        else
          puts edge.to_s+" mid-point not available."
        end#if
        

        TIG

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

          Thank you TIG,

          But I'm begginer on Sketchup API and Ruby programming, so I would like to know how can I apply this code on a selected line.
          I will explained better, my task has two parts:

          1. I select a line, part of a wall, and store this line to do the second part;
          2. To detect and store the midpoint global coordinate to use as reference point (You already did this, very grateful again).
            So, if you tell me if there is a good book about Ruby and Sketchup API programming I woul be very been thankful.
            I 've seen a lot of sites on internet, all of them very good but I like books a lot and would like to have one.
            Grateful

          Sérgio Bizello

          Sérgio A.Bizello

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

            To get the line info let's assume you first select it and then run the tool [the other way - when you run the tool and then select the line is more complicated!]

            Here is a def to do what you want - I have tried to explain what each bit does...

            
            def getlinemidpoint()
              model=Sketchup.active_model
              ss=model.selection
              if ss[0] and ss[0].class== Sketchup;;Edge
                edge= ss[0]
              else
                UI.messagebox("You Must Select an Edge.")
                return nil
              end#if
              point0= edge.start.position
              point1= edge.end.position
              vector= point1 - point0
              distance= point0.distance(point1)
              halfway= distance/2
              ### could also use; halfway= edge.length / 2
              point_mid= point0.offset(vector,halfway)
              UI.messagebox("The Selected Edge Mid-Point is ["+point_mid.x.to_s+", "+point_mid.y.to_s+", "+point_mid.z.to_s+"].")
              ### now do what you want with this info... e.g.
              model.set_attribute("MyLines", edge.to_s, point_mid.to_a)
            end#def
            
            

            TIG

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

              TIG, thanks a lot!
              The script worked very well. So, I will include it inside the main script.
              I wont forget to speak folds its aid.
              Really very thanks
              Sérgio

              Sérgio A.Bizello

              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