16 Replies Latest reply on Sep 25, 2017 7:00 AM by John Alexander

    Construct a MathTransform

    Amen Allah Jlili



      Hey guys,

      I have a question about MathTransforms.



      I'm trying to construct a MathTransform that I would otherwise

      get from a "Coordinate System1" feature manually. I have access

      to the face2 where the coordinate System1 sits on.



      How would you guys ago about creating that MathTransform?



      The idea is not to create the math transform and not get

      from the coordinate system for performance purpose.



      Thank you!

        • Re: Construct a MathTransform
          John Alexander

          You want to compute the math transform for a coordinate system located at "Coordinate system 1" without creating the "Coordinate System 1" feature?


          I don't know if this would actually be any faster than using the existing coordinate system feature. I would understand the motivation if the coordinate system doesn't exist yet.


          Transformation matrix data:

               |a b c . n |

                |d e f . o |

                |g h i . p |

                |------. --|

                |j k l . m |


          {j,k,l} is the vector describing the translation. You would need to get the position of that  "Coordinate system 1" with respect to the part's origin. i.e. {x,y,z} = {j,k,l}


          The documentation indicates that {n,o,p} are unused, n=0, o=0, p=0.



          m = 1.0

          I've only ever used this with drawings.



          The 3x3 submatrix (a-i) are the rotational components. I believe each column (or row) in that rotation matrix can be thought of as a unit vector aligned with an axis of the coordinate system being rotated into/out of. I've convinced myself of this because, R I = R leads me to believe that the columns of a rotation matrix can be thought of as the normal vectors aligned with the resulting coordinate system axes. If this isn't true, the following comments about building the rotation matrix are incorrect.


          {a,b,c} - unit vector aligned with x-axis

          {d,e,f,} - unit vector aligned with y-axis

          {g,h,i} - unit vector aligned with z-axis


          I think this is correct, otherwise it would be the transpose of this. i.e. {a,d,g} - x-axis.


          If that green face is planar, you can get its normal vector with respect to the part's origin.

          2012 SOLIDWORKS API Help - Normal Property (IFace2)


          You can assign that to {d,e,f} = {y1,y2,y3}. Where {y1,y2,y3} are the x,y,z components of the IFace.Normal(). I say {d,e,f} because your image shows the Y-axis perpendicular to the surface.


          Next you need to determine either the X-axis or the Z-axis and then compute the last term. How you will determine the X-axis or Z-axis isn't clear from the image. It looks like maybe X  is aligned with that sketch line.


          Assuming you get the x-axis from the sketch line (might have to compute it from startpoint-endpoint), you can get the z-axis as the cross product of your x-axis and y-axis. Because x and y are already unit vectors, this product should still be a unit vector.


          z = x x y


          You could either compute this or build MathVectors for x, and y and set  z = x.cross(y).


          Finally, with the big array of 16 doubles built, you pass it to swMathUtil.CreateMathTransform


          2012 SOLIDWORKS API Help - CreateTransform Method (IMathUtility)


          **If you follow these steps exactly, the result will likely be wrong. It is probably necessary to change the direction of the translation (set all the terms negative), transpose the rotation, or invert the whole thing before you get the transform you are looking for.

          • Re: Construct a MathTransform
            Amen Allah Jlili

            A quick workaround to the question would be to get to use the NormalTo command and then get the MathTransform from the active model view.