6 Replies Latest reply on Feb 23, 2017 2:36 PM by Eric Bunn

    Trouble getting true (model) coordinates of sketch point

    Matt Martens

      I am having trouble getting the model coordinates of a specific known sketch point. I would like to do this without having to manually select the point I need, i.e. using only the API to find the point and extract the data. I have tried 4 or 5 methods using ISelectionMgr, ISketchPoint, & IMeasure to extract the x, y, & z values but the transform is never correct for all 3 components. The only method that transforms correctly every time requires the user to select the point.


      Any and all help is greatly appreciated.

        • Re: Trouble getting true (model) coordinates of sketch point
          Matt Martens

          I solved this problem after alot of frustration. The key was to assign the x, y, & z properties of the ISketchPoint object to a double array instead of a variant for the transform.

            • Re: Trouble getting true (model) coordinates of sketch point
              Keith Rice

              This issue is known by SolidWorks API Dev team, FYI:


              SPR 860142 : CreatePoint Method (IMathUtility) fails when pass Parameter Array as an argument.


              If you email apisupport@solidworks.com and ask them to add you to the notification list, you'll be notified when the bug is resolved.



              SolidWorks API Tutorials

                • Re: Trouble getting true (model) coordinates of sketch point
                  Matt Martens

                  Thanks Keith.


                  I was trying to locate the XYZ coordinates by doing an inverse IMathTransform on the coordinates of the ISketchPoint object. The issue was when I would obtain the object through the ISelectionMgr by using the code to select it, I was getting coordinates of (0, 0, 0). The only way I was getting the correct coordinates with ISelectionMgr was to mouse select the point. Then, when I was accessing the point object by ISketch::GetSketchPoints and reading the X, Y, & Z properties into a variant array, the transform was coming out incorrect.


                  Once I figured out to read the properties into a array of type double, the transform was correct.


                  The reason I was using a variant array in the first place is the sample code in the API help example "Transform Coordinates from Sketch to Model Space Example (VBA)".  I eventually found an example - forgive me but right now I am unable to find it - in the API help that used the array of double type and obviously by now you know that solved the problem.


                  By the way, when I searched for the SPR in the knowledge base, I couldn't find it.



                  The example I found my eventual solution in was "Get x,y,z Locations of Points in Sketch Example (VBA)".

              • Re: Trouble getting true (model) coordinates of sketch point
                Eric Bunn



                I know it has been some time since you posted this.  Is it possible to publish the code that involves the solution to this?  I have a macro that gets the tess points of a sketch segment inside of a block instance which is inserted into a sketch.  The returned tess points are in the blocks original sketch space coordinates.  I want to transform the coordinates to model space and the only way that I've found to accomplish this was actually creating a temporary point, measuring it, and then deleting it.  The measurements returned to me are in model space.  Looking to shortcut this.


                Thanks in advance,


                  • Re: Trouble getting true (model) coordinates of sketch point
                    Matt Martens

                    I created a function within a much larger macro that utilized it.


                    'transform a sketch point's coordinates into model space

                    Private Function GetModelCoordinates(swSketch As SldWorks.Sketch, pointNum As Integer) As Variant

                      Dim swMathUtil As SldWorks.MathUtility

                      Dim swMathPt As SldWorks.MathPoint

                      Dim swMathTrans As SldWorks.MathTransform

                      Dim ptCoord(2) As Double

                      Dim ptObj As Object


                      Set ptObj = swSketch.GetSketchPoints2(pointNum - 1)

                      ptCoord(0) = ptObj.X: ptCoord(1) = ptObj.Y: ptCoord(2) = ptObj.Z

                      Set swMathUtil = swApp.GetMathUtility

                      Set swMathPt = swMathUtil.CreatePoint(ptCoord)

                      Set swMathTrans = swSketch.ModelToSketchTransform.Inverse

                      Set swMathPt = swMathPt.MultiplyTransform(swMathTrans)

                      GetModelCoordinates = swMathPt.ArrayData

                    End Function