sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    How to get the absolute coordinates of faces?

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 2.5k 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.
    • lbsswuL Offline
      lbsswu
      last edited by

      I want to get the absolute center coordinates of faces. But I find the coordinates are affected by the edit mode. Two images in the attachment is an illustration. The first image shows model in edit mode, while the second does not. My goal is to get the absolute coordinate of the center of the red face. However, in the first image, the center is Point3d(-9.0624, -22.0221, 63.6455); in the second image, the center is Point3d(-0.178652, -0.258835, 1.39058).

      The first coordnate is what I wanted. But this required me to use mouse to clik the model several times. Could anyeone tell me if there are some methods to get the absolute coordinate without mouse clik?


      test1.PNG


      test2.PNG

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

        I assume you have somehow got the center of the face inside its 'context' - let's call it pt.
        You have a reference to the face's 'container' [a group or instance].
        You can get the transformation of that - e.g. tr=instance.transformation.
        You can apply a transformation to a point thus:
        pt.transform!(tr)

        You can also do the inverse - e.g. you have a point outside of the context and want to convert it as if it's inside the context...
        pt.transform!(tr.inverse)

        I'm unsure about which way round you want to do this, but somewhere in this is your answer...

        TIG

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

          Here's what I've got.

          def self.currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation
              return child_pos unless (parent.class == Sketchup;;Group) and (parent.class == Sketchup;;ComponentInstance)
              tra = parent.transformation.origin.to_a
              xaxis = parent.transformation.xaxis.to_a
              yaxis = parent.transformation.yaxis.to_a
              zaxis = parent.transformation.zaxis.to_a
              # my experimented formule
              external_pos[x] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0]
              external_pos[y] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1]
              external_pos[z] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2]
              #external_pos = child_pos.transform(parent.transformation).to_a #alternative sketchup formula
              return external_pos
          end
          

          Copy to editor to unwrap the code lines.
          It basically shows how that transform formula works
          By the way If you have sketchy physics, you can see play with something similiar here: Navigation System

          1 Reply Last reply Reply Quote 0
          • lbsswuL Offline
            lbsswu
            last edited by

            @tig said:

            I assume you have somehow got the center of the face inside its 'context' - let's call it pt.
            You have a reference to the face's 'container' [a group or instance].
            You can get the transformation of that - e.g. tr=instance.transformation.
            You can apply a transformation to a point thus:
            pt.transform!(tr)

            You can also do the inverse - e.g. you have a point outside of the context and want to convert it as if it's inside the context...
            pt.transform!(tr.inverse)

            I'm unsure about which way round you want to do this, but somewhere in this is your answer...

            I split my 3D car model in several parts, e.g. door, wheel, etc. I want to locate their center no matter the user change the view point.

            Use the top of the car as an example, I built a "car_top" group, and used the group as a container, but still did not get the correct coordinate.

            1 Reply Last reply Reply Quote 0
            • lbsswuL Offline
              lbsswu
              last edited by

              @anton_s said:

              Here's what I've got.

              def self.currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation
              >     return child_pos unless (parent.class == Sketchup;;Group) and (parent.class == Sketchup;;ComponentInstance)
              >     tra = parent.transformation.origin.to_a
              >     xaxis = parent.transformation.xaxis.to_a
              >     yaxis = parent.transformation.yaxis.to_a
              >     zaxis = parent.transformation.zaxis.to_a
              >     # my experimented formule
              >     external_pos[x] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0]
              >     external_pos[y] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1]
              >     external_pos[z] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2]
              >     #external_pos = child_pos.transform(parent.transformation).to_a #alternative sketchup formula
              >     return external_pos
              > end
              

              Copy to editor to unwrap the code lines.
              It basically shows how that transform formula works
              By the way If you have sketchy physics, you can see play with something similiar here: Navigation System

              Thanks for your code. But I still did not get the coordinate that I want, the return coordinate is equal to the child's coordinate. The type of the child is Sketchup::Face, and the type of its parent is Sketchup::ComponentDefinition. I made a group like this:

              
              model = Sketchup.active_model;
              groups = [];
              model.definitions.each{|d| groups << d if d.group? }
              entities = model.entities;
              selection = model.selection;
              pgrp = [];
              groups.each{|d| pgrp << d.instances[0] if d.instances[0].name=="car_top" }
              tmpents = pgrp[0].entities;
              #print pgrp[0]
              faces = tmpents.find_all{|entity|entity.class==Sketchup;;Face}
              ctr = faces[8].bounds.center
              print ctr
              pos = self.currentPos(pgrp[0], ctr)
              print pos
              
              

              pgrp[0] is a group that contain faces[8]. May be there are some bugs...

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

                Sorry, rewritten it with errors. 😳 Here's a better version:

                def currentPos(parent, child_pos) #Get the position of the point relative to parent's transformation
                  return false if (parent.class != Sketchup;;Group) and (parent.class != Sketchup;;ComponentInstance)
                  tra = parent.transformation.origin.to_a
                  xaxis = parent.transformation.xaxis.to_a
                  yaxis = parent.transformation.yaxis.to_a
                  zaxis = parent.transformation.zaxis.to_a
                  external_pos = Array.new(3)
                  # my experimented formule
                  external_pos[0] = tra[0] + child_pos[0]*xaxis[0] + child_pos[1]*yaxis[0] + child_pos[2]*zaxis[0]
                  external_pos[1] = tra[1] + child_pos[0]*xaxis[1] + child_pos[1]*yaxis[1] + child_pos[2]*zaxis[1]
                  external_pos[2] = tra[2] + child_pos[0]*xaxis[2] + child_pos[1]*yaxis[2] + child_pos[2]*zaxis[2]
                  #external_pos = child_pos.transform(parent.transformation).to_a # alternative sketchup formula
                  return external_pos
                end
                

                Copy to nowpad++ to unwrap all lines
                Note its no more self.currentPos, its just currentPos

                1 Reply Last reply Reply Quote 0
                • liquid98L Offline
                  liquid98
                  last edited by

                  Hi Ibswu,

                  Check this:

                  http://sketchucation.com/forums/viewtopic.php?f=180&t=48769

                  -- Liquid

                  Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

                  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