8 Replies Latest reply on Aug 4, 2017 11:05 AM by Dave Paul

    Y and Z Swapped After Transform

    Dave Paul

      I have transformed a sketch point with the transform of the feature.  I'm so close to the data that I need..., but the Y and Z coordinates are swapped.

      I am working on transforming the data using a plane that is in the right coordinate system, but I don't know if that's the right way to go.

      How do I get the coordinates to come out in the right orientation?

       

      swSketchPoints = MathP.ArrayData
      Set MathTrans = swSketchFeature.ModelToSketchTransform
      Set MathTrans = MathTrans.Inverse
      Set MathP = MathP.MultiplyTransform(MathTrans)
      swSketchPoints = MathP.ArrayData
      

       

      Converted Point = -10.8951928610316, -0.1400000756, -2.91935812849468

      Should be  = -10.8951928610316, -2.91935812849468, -0.1400000756

        • Re: Y and Z Swapped After Transform
          John Alexander
          Should be  = -10.8951928610316, -2.91935812849468, -0.1400000756

           

          How are you determining those numbers? Are you using the Measure tool in the part/assembly or is it some other method?

           

          Could you show a screenshot of your feature tree with the sketch in question?

            • Re: Y and Z Swapped After Transform
              Dave Paul

              Hey John,

              Thanks for your help.  Here is the feature tree and the data I'm using to determine the position of the sketch point.  I just want to flip the coordinates around so it's using the standard "X to the right", "Y up" configuration.

                • Re: Y and Z Swapped After Transform
                  John Alexander

                  I just wanted to see that to make sure we weren't in an assembly.

                   

                  On the first line of your sample code

                   

                  swSketchPoints = MathP.ArrayData

                   

                  you assign MathP's arrayData to a variant swSketchPoints. Where does MathP come from?

                   

                  Normally it would look something like this (without the Dims):

                   

                  set swSketchPoint = swSketch.getSketchPoints(0) 'this is just a dummy example, there are several ways of getting this reference but it is important that the sketch point belong to the sketch that is being used to transform the point's coordinates.

                   

                  set swMathPoint = swMathUtility.CreatePoint(swSketchPoint.GetCoords) 'create the math point from the sketch point's coordinates (there might be an intermediate step here if CreatePoint is finicky)

                   

                  set swMathPoint = swMathPoint.MultiplyTransform(swSketch.ModelToSketchTransform.Inverse) 'this is the same transform that you performed in your example, just abbreviated to one line

                   

                  vPointData = swMathPoint.arrayData() 'vPointData should be a variant array containing 3 doubles: x,y,z of the sketch point measured with respect to the model's coordinate system

                    • Re: Y and Z Swapped After Transform
                      John Alexander

                      I just noticed something in your screenshot, are you trying to go from the sketch point inside Sketch40 to the model's coordinate system or are you trying to go from Sketch40 to Sketch66 (which is being edited)?

                       

                      If the latter, you need to add an additional transform from the model into Sketch66.

                       

                      [Sketch40] -> [Model] -> [Sketch66]

                       

                      Dim swSketch40 as Sldworks.Sketch

                      Dim swSketch66 as Sldworks.Sketch

                      Dim swSketch40toSketch66Transform as sldworks.mathtransform

                       

                      set swSketch40toSketch66Transform = swSketch40.modeltoSketchTransform.inverse.multiplyTransform(swSketch66.ModelToSketchTransform)

                       

                      I'm not certain if having Sketch66 open for editing has any impact on the ModelToSketchTransform.

                        • Re: Y and Z Swapped After Transform
                          Dave Paul

                          John,

                          Thank you for the reply.  Sketch66 was just a sketch that I had open to place the dimensions that verified the location of the hole point.  Sorry for the confusion.

                          Thanks for the ideas. I'll give them a try.

                          • Re: Y and Z Swapped After Transform
                            Dave Paul

                            To answer your question about the "MathP" variable, it's built from the sketch point for the hole placement.

                             

                            boolstatus = swModel.Extension.SelectByID2(strName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)
                            Set swHoleFeature = swSelMgr.GetSelectedObject5(1)
                            Set Component = swSelMgr.GetSelectedObjectsComponent2(1)
                            swModel.ClearSelection2 True
                            Set swHoleFeatureData = swHoleFeature.GetDefinition
                            swHoleSketchPoints = swHoleFeatureData.GetSketchPoints
                            Debug.Print swHoleFeatureData.GetSketchPointCount
                            For Each pt In swHoleSketchPoints
                                Set swSketchPoint = pt
                                Set swHoleSketchFeature = swSketchPoint.GetSketch
                                pt.Select4 False, Nothing
                            Next
                            PointCoords(0) = swSketchPoint.X
                            PointCoords(1) = swSketchPoint.Y
                            PointCoords(2) = swSketchPoint.Z
                            Set MathUtil = swApp.GetMathUtility
                            Set MathP = MathUtil.CreatePoint(PointCoords)
                            swHoleSketchPoints = MathP.arrayData
                            
                              • Re: Y and Z Swapped After Transform
                                John Alexander

                                 

                                I tried your sample and it seems to work for me. The hole feature I used is driven by a sketch that is not aligned with the part's coordinate system so it is definitely performing a non-identity Transform to get the observed result.

                                 

                                Option Explicit

                                 

                                 

                                Sub main()

                                    Dim swApp       As SldWorks.SldWorks

                                    Dim swModel     As SldWorks.ModelDoc2

                                    Dim swSelMgr    As SldWorks.SelectionMgr

                                   

                                    Dim MathUtil    As SldWorks.MathUtility

                                    Dim MathP       As SldWorks.MathPoint

                                   

                                    Dim swHoleFeature       As SldWorks.Feature

                                    Dim swHoleFeatureData   As SldWorks.WizardHoleFeatureData2

                                    Dim swComponent         As SldWorks.Component2

                                   

                                    Dim swHoleSketchFeature As SldWorks.Sketch

                                    Dim swSketchPoint   As SldWorks.SketchPoint

                                    Dim swHoleSketchPoints  As Variant

                                   

                                    Dim PointCoords(2)   As Double

                                    Dim pt As Variant

                                   

                                    Dim strName    As String

                                    Dim boolstatus As Boolean

                                   

                                   

                                   

                                    Set swApp = Application.SldWorks

                                    Set swModel = swApp.ActiveDoc

                                   

                                    Set swSelMgr = swModel.SelectionManager

                                   

                                    strName = "" 'hole feature name

                                    boolstatus = swModel.Extension.SelectByID2(strName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

                                   

                                    Set swHoleFeature = swSelMgr.GetSelectedObject5(1)

                                    Set swComponent = swSelMgr.GetSelectedObjectsComponent2(1)

                                    swModel.ClearSelection2 True

                                    Set swHoleFeatureData = swHoleFeature.GetDefinition

                                    swHoleSketchPoints = swHoleFeatureData.GetSketchPoints

                                    Debug.Print swHoleFeatureData.GetSketchPointCount

                                    For Each pt In swHoleSketchPoints

                                        Set swSketchPoint = pt

                                        Set swHoleSketchFeature = swSketchPoint.GetSketch

                                        pt.Select4 False, Nothing

                                    Next

                                    PointCoords(0) = swSketchPoint.X

                                    PointCoords(1) = swSketchPoint.Y

                                    PointCoords(2) = swSketchPoint.Z

                                    Set MathUtil = swApp.GetMathUtility

                                    Set MathP = MathUtil.CreatePoint(PointCoords)

                                    swHoleSketchPoints = MathP.ArrayData

                                   

                                    Set MathP = MathP.MultiplyTransform(swHoleSketchFeature.ModelToSketchTransform.Inverse)

                                   

                                    Debug.Print PointCoords(0)

                                    Debug.Print PointCoords(1)

                                    Debug.Print PointCoords(2)

                                   

                                    Debug.Print MathP.ArrayData(0)

                                    Debug.Print MathP.ArrayData(1)

                                    Debug.Print MathP.ArrayData(2)

                                End Sub

                                  • Re: Y and Z Swapped After Transform
                                    Dave Paul

                                    I wrote a transform by hand that simply swapped the Y and Z components.  I applied it to the transform answer that the macro was giving me and it seems to work in all situations.

                                     

                                    Dim Xform(15) As Double
                                    Dim vXform As Variant
                                    Dim MathXform  As SldWorks.MathTransform
                                       
                                    Xform(0) = 1#: Xform(1) = 0#: Xform(2) = 0#
                                    Xform(3) = 0#: Xform(4) = 0#: Xform(5) = 1#
                                    Xform(6) = 0#: Xform(7) = 1#: Xform(8) = 0#
                                    Xform(9) = 0#: Xform(10) = 0#: Xform(11) = 0#
                                    Xform(12) = 1#
                                    Xform(13) = 0#: Xform(14) = 0#: Xform(15) = 0#
                                    vXform = Xform
                                    
                                    Set MathXform = MathUtil.CreateTransform(vXform)
                                    Set MathP = MathP.MultiplyTransform(MathXform)