5 Replies Latest reply on Sep 14, 2015 1:18 PM by Josh Brady

    Point Transformation

    Alfonso Cartagena

      I am currently working on transforming sketch point coordinates from one part to another within the same assembly. The current code I have been working with only transforms the point from a sketch to a model.

       

      2015 SOLIDWORKS API Help - Transform Sketch to Model Example (VBA)

       

      Is there an example code of how to transform these points from part to part instead of part to model? If not, how would I go about taking the points I have currently and transforming them into the other part?

       

      Thank you,

      Alfonso

        • Re: Point Transformation
          Josh Brady

          First get transform from source part to assembly. Multiply source coords by part to assy transform. (That's what you have so far). Then, get the same transform for the target part, invert transform, and mutiply.

            • Re: Point Transformation
              Alfonso Cartagena

              Josh,

               

              Thank you for the help! I completely understand what I need to do when it comes to how it would work. I guess what I don't understand is how my code should reflect that. I put the section of code below and referenced where I am confused. How do I tell this code to transform the points into "Part2" for my operation?

               

              Thank you,

              Alfonso

               

               

              Sub main()
              
              
                  
                  'Connect the program to SolidWorks
              
              
                  Set swApp = CreateObject("SldWorks.Application")
              
              
                  Set Model = swApp.ActiveDoc
              
              
                  'Prepare the MathUtility
              
              
                  Set MathUtil = swApp.GetMathUtility
              
              
                  'Get the SelectionMgr
              
              
                  Set selMgr = Model.SelectionManager
              
              
              
                  'Get the sketch from the SelectionMgr
                  
                  boolstatus = Model.Extension.SelectByID2("Sketch6@Part1-1@Assem1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
              
              
                  Set SketchFeature = selMgr.GetSelectedObject6(1, 0)
              
              
                  Set SketchFeature = SketchFeature.GetSpecificFeature2
              
              
                  'Get the sketch points
              
              
                  SketchPoints = SketchFeature.GetSketchPoints2
              
              
                 
                  'Build a coordinate array from the first point in the sketch
              
              
                  PointCoords(0) = SketchPoints(0).X
              
              
                  PointCoords(1) = SketchPoints(0).Y
              
              
                  PointCoords(2) = SketchPoints(0).Z
              
              
              
              
              
                  'Create the new MathPoint from the sketch point data.
              
              
                  'MathP refers to the point location in the sketch coordinates
              
              
                  Set MathP = MathUtil.CreatePoint(PointCoords)
              
              
                 
                  'Display the point coordinates in relation to the sketch origin
              
              
                  SketchPoints = MathP.ArrayData
              
              
                  MsgBox SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)
              
              
              
              
              
              
              
              
              
              
              
               RIGHT HERE IS WHERE I'M LOST.
              
              
              
              
              
              
              
              
              
              
                  'Get the model-to-sketch transform for this sketch
              
              
                  Set MathTrans = SketchFeature.ModelToSketchTransform
              
              
                  'Get the inversion of the transform
              
              
                  Set MathTrans = MathTrans.Inverse
              
              
              
              
              
                  'Multiply the point by the inverse transform
              
              
                  'MathP now refers to the point location in the model coordinates
              
              
                  Set MathP = MathP.MultiplyTransform(MathTrans)
              
              
              
              
              
                  'Display the point coordinates in relation to the model origin
              
              
                  SketchPoints = MathP.ArrayData
                  
                  Debug.Print "Sketch point x, y and z coordinates: "; SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2); ""
                  Debug.Print " "
              
              
              
              
                  MsgBox SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)
              
              
            • Re: Point Transformation
              Josh Brady

              Based on the point where you said you're lost... A transformation matrix (or transform) is a mathematical construct that contains instructions on how to translate Euclidean geometry elements between different coordinate systems.  SolidWorks can generate this transform based on its knowledge about how the coordinate systems are positioned/rotated relative to one another.

               

              Sketches have their own coordinate systems.  Parts have their own coordinate systems.  Assemblies have their own coordinate systems.  In order to find out the coordinates of a sketch point contained by one part, you have to get the path of transforms from where the point is to where you're interested in.  Then, you multiply the point coordinates by the transforms in the correct order to get from where you started to where you want to end up.

               

              Sometimes, the transform you can get from SolidWorks is the wrong direction.  In your example above, there's a line that gets the "model to sketch" transform.  However, you're already at the sketch, and you want to get to the model.  Fortunately, switching the direction of the transform is as easy as getting the inverse of the transform.

               

              If I read your question correctly, your total journey is (roughly) Sketch1@Part1→Part1→Assy1→Part2.  Each of those arrows represents a multiplication of point data by a transform.  Your example code shows the first one of these.  You have to get 2 more.  These will come from Component2 objects in the context of the assembly.

                • Re: Point Transformation
                  Alfonso Cartagena

                  Josh,

                   

                  I understand what the code does. That's not my issue here.

                   

                  Yes, you have the sequence of events in the correct order. At the beginning of this code the sketch is specifically called out and the points are gathered. Going through the next part of code what's confusing to me is that there nothing in the code specifying "Part1" as the ending result. I assumed the coordinates were transformed to the assembly. Once that is established my next issue arises, due to multiple parts within the assembly, how do I call out which part the points are to be transformed to?

                   

                  Sorry for all the confusion, I am new to API.

                   

                  Thank you,

                  Alfonso

                    • Re: Point Transformation
                      Josh Brady

                      SketchFeature.ModelToSketchTransform is what specifies the end result being in the part model's context.  The sketch knows what part model it's in.  That's why I said you need to get the transform from part to assembly.  As far as how to pick the part in the assembly you want to transform to... There are so many ways. Just depends on how your code can know.

                       

                      Sorry... Not trying to be a jerk here, but it sounds like you're further over your head than I have time to teach at the moment.