sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Distance between a vertex in one group to a vertex in anothe

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 555 Views 3 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.
    • F Offline
      fizgig
      last edited by

      Say I draw a rectangle in sketchup.

      I then select one of the four edges of the rectangle and in the ruby window, I make a variable point at it's start vertex:

      vertex1 = Sketchup.active_model.selection[0].start
      

      No sweat. I then put the rectangle into its own group.

      I do the same for another rectangle (but use the variable vertex2 instead while pointing at one of the new rectangle's edges, then I group the 2nd rectangle into its own new group)

      Now, I want to measure the distance between the two vertices:

      vertex1.position.distance vertex2.position
      

      I get an answer. I then move one of the rectangles by moving its group and rerun the distance command above. No change.

      The reason appears to be that the position method returns a vertex with coordinates relative to the local origin of the respective group it's in. I guess I need to somehow get the answer relative to the global origin. Anyone know how to do that?

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

        I think this is what is actually happening.

        Sketchup.active_model.selection[0] is actually pointing to the first entity in the selection set. Which if you select the face and all edges, then it could turn out to be the edge or a face. So you need to be careful with that, because .start method will not work on a face. So if the selection ever happens to list the face first, then your code will break.

        Also, in general, you will need to be more precise how you are choosing what point. Do you know what corner of the rectangle you want to choose? With the way you described, you will not be able to accurately predict what corner is selected (if it matters).

        So the way I would approach this, is with the group bounding box.

        Make a rectabngle, add it to a group, make another rectangle, add it to a different group. Then find their centers with

        group1_center_point = my_group1.bounds.center group2_center_point = my_group2.bounds.center

        Now anytime the groups move, it is easy to get their centerpoints again.

        Will that begin to help? Instead of centerpoints you can use any of the corners with bounds.corner[] method. Or you can also get a groups axis with my_group.transformation.axis.

        Play with those ideas, see if any of that will work for what you need.

        Chris

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

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

          Something like [untested]...

          
          tranformation1=group1.transformation;tranformation2=group2.transformation
          point1=vertex1.position;point2=vertex2.position
          point1.transform!(tranformation1);point2.transform!(tranformation2)
          distance=point1.distance(point2)
          
          

          or as a one liner...

          
          distance=vertex1.position.transform!(group1.transformation).distance(vertex2.position.transform!(group2.transformation))
          
          

          !!!

          TIG

          1 Reply Last reply Reply Quote 0
          • F Offline
            fizgig
            last edited by

            TIG, I think you nailed it right on the head. Your post caused me to investigate and learn about the transformation object (fascinating stuff)

            After playing around, I came up with:

            vertex1.position.transform!(group1.transformation).distance vertex2.position.transform!(group2.transformation)
            

            This seems to be about the same thing you suggested. And it works!

            1 Reply Last reply Reply Quote 0
            • F Offline
              fizgig
              last edited by

              The rub comes when you want to measure the distance between a vertex inside a group inside a group inside a group to another vertex similarly buried 5 layers deep in another group. You're going to have to back-transform out of every group layer. That's going to be a pain.

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

                'parent'...

                TIG

                1 Reply Last reply Reply Quote 0
                • F Offline
                  fizgig
                  last edited by

                  So was my guess accurate? If I want to get the location of a vertex relative to the overall global coordinates, will I have to back-transform out of every group, parent-group and so forth?

                  If so, I'm having trouble. When I'm playing around with an edge that's part of a componentinstance which is part of a componentinstance, I can't get the transformation of the componentinstance it is in.

                  I do the following command in attempt to make a 3d point object that is located at the starting point of the edge I have selected and back transform it one level:

                  Geom;;Point3d.new(Sketchup.active_model.selection[0].start).transform! (Sketchup.active_model.selection[0].parent.transformation)
                  

                  The trouble is that the parent of this edge is the componentdefintion and not the componentinstance - which makes sense. I just don't know how to get the transformation of the componentinstance that I've currently selected my edge from. My head hurts.

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

                    Something like...

                    ### assuming model, compo'defn, instance etc defined earlier on...
                    transformation = component_definition.transformation * component_definition_instance.transformation
                    this_parent = component_definition_instance.parent
                    while not this_parent == model
                      transformation = transformation * this_parent.transformation
                      this_parent = this_parent.parent
                    end#while
                    ### now have full transformation for the instance no matter how deeply it's nested...
                    
                    

                    ??? Untested - just a thought...
                    Adjust for Edge etc...

                    TIG

                    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