19 Replies Latest reply on May 9, 2018 11:38 AM by Jacob Corder

    Get coordinates of point selected in model by user

    Deeksha Gupta

      I'm trying to fetch the coordinates of point shown in picture.

       

      I have tried all three APIs:

      1. SelMgr.GetSelectionPointInSketchSpace(count,-1);
      2. SelMgr.GetSelectionPoint2(count, -1);
      3. SelMgr.GetSelectedObject6(count,-1);

      But all give different result as shown in below message box:

      (Units are same: inches).. please assist.

       

       

       

       

        • Re: Get coordinates of point selected in model by user
          Deepak Gupta

          The value you would get would be in Meters, so use conversion factor to get the desired results in Inches.

            • Re: Get coordinates of point selected in model by user
              Deeksha Gupta

              Deepak,

              All values are in inches itself..but API is giving wrong coordinates.

                • Re: Get coordinates of point selected in model by user
                  Simon Turner

                  Deepak is correct. Take your X value: 0.3168142.

                  Multiply that by 1000 to convert from m to mm and then divide by 25.4 to get back to inches.

                  What answer do you get?

                    • Re: Get coordinates of point selected in model by user
                      Deeksha Gupta

                      Simon,

                      if we convert to inches, only X value is coming correct. for Y and z we get 4.124 and -4.073 respectively.. which are not matching with 3.66 and -4.49

                        • Re: Get coordinates of point selected in model by user
                          Simon Turner

                          I think we'd need to see your model. Can you attach it?

                          • Re: Get coordinates of point selected in model by user
                            Jacob Corder

                            If it is in a sketch, perhaps you need to multiply it by the sketch transform.

                             

                            Create imathpoint

                            Multiply by the sketch transform.

                             

                            The coordinates your getting are always in world coordinates in meters.

                             

                            Try that.

                             

                            If it is not a sketch, well then this is weird.

                              • Re: Get coordinates of point selected in model by user
                                Deeksha Gupta

                                 

                                Jacob,

                                 

                                Thanks a lot for the solution.. I checked and realized the problem is exactly what you have mentioned.

                                 

                                I have used the following code snippet to transform, but still does not give me correct result:

                                 

                                 

                                 

                                double[] origPt = new double[] { 0,0,0};

                                 

                                MathTransform tran = comp.Transform2;

                                 

                                MathPoint mp = MathUtil.CreatePoint(origPt);

                                 

                                double[] arr = mp.ArrayData;

                                 

                                Any comments on this ... 

                                  • Re: Get coordinates of point selected in model by user
                                    Deepak Gupta

                                    Deeksha it might be better if you upload your sample file and full macro codes. This way one can easily debug and can either give you the working macro OR point you to the right direction.

                                      • Re: Get coordinates of point selected in model by user
                                        Deeksha Gupta

                                        Attaching model and code:-

                                         

                                        private static List<double[]> GetPickPoints(ModelDoc2 modDoc)

                                        {

                                         

                                        SldWorks swApp = null;

                                         

                                        Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks;

                                         

                                        MathUtility MathUtil = swApp.GetMathUtility();

                                         

                                        SelectionMgr SelMgr = modDoc.SelectionManager;

                                         

                                        int count = 1;

                                         

                                        List<double[]> lstPickpoints = new List<double[]>();

                                         

                                        Component2 comp = null;

                                         

                                        double[] originPt = new double[] { 0,0,0};

                                         

                                        do

                                        {

                                        MathTransform tran = comp.Transform2;

                                         

                                        MathPoint mp = MathUtil.CreatePoint(originPt);

                                         

                                        double[] arr = mp.ArrayData;

                                         

                                        }

                                        while (comp != null);

                                         

                                        return lstPickpoints;

                                        }

                                         

                                         

                                          • Re: Get coordinates of point selected in model by user
                                            Simon Turner

                                            This VBA macro shows how to use the component transform:

                                             

                                            Option Explicit

                                             

                                            Dim swApp As SldWorks.SldWorks

                                            Dim Part As ModelDoc2

                                            Dim boolstatus As Boolean

                                            Dim longstatus As Long, longwarnings As Long

                                             

                                            Sub main()

                                                Dim mySkPt As SketchPoint

                                                Dim myTran As MathTransform

                                                Dim mPt As MathPoint

                                                Dim myUtil As MathUtility

                                                Dim p(2) As Double

                                                Dim selMgr As SelectionMgr

                                                Dim sa As Variant

                                                Dim myComp As Component2

                                               

                                                Set swApp = Application.SldWorks

                                                Set Part = swApp.ActiveDoc

                                                boolstatus = Part.Extension.SelectByID2("Point72@3DSketch1@1578614_1-1@1578614", "EXTSKETCHPOINT", 0.3168142, 0.1047496, -0.1034542, False, 0, Nothing, 0)

                                             

                                                Set selMgr = Part.SelectionManager

                                                Set mySkPt = selMgr.GetSelectedObject6(1, -1)

                                                Set myComp = selMgr.GetSelectedObjectsComponent4(1, -1)

                                               

                                                Set myTran = myComp.Transform2

                                               

                                                p(0) = mySkPt.X

                                                p(1) = mySkPt.Y

                                                p(2) = mySkPt.Z

                                                sa = p

                                                Set myUtil = swApp.GetMathUtility

                                                Set mPt = myUtil.CreatePoint(sa)

                                                Set mPt = mPt.MultiplyTransform(myTran)

                                                sa = mPt.ArrayData

                                                Debug.Print sa(0) * 1000 / 25.4

                                                Debug.Print sa(1) * 1000 / 25.4

                                                Debug.Print sa(2) * 1000 / 25.4

                                            End Sub

                                            • Re: Get coordinates of point selected in model by user
                                              Jacob Corder

                                              ok buddy here is your problem

                                               

                                              you have a coordinate system you are using as a reference to the meassure tool.  this is why the selection point displayed is not what you are expecting.

                                               

                                              turn off coordinate system 1 as reference and everything lines up perfectly

                                               

                                              your measurements through api are  the correct coordinates in ASSEMBLY SPACE

                                               

                                              change the coordinate system to assembly origin and your numbers match up

                                               

                                              if you need to measure relative to the coordinate system then

                                               

                                              here is the correct code

                                               

                                              Merry Christmas

                                               

                                              Option Explicit

                                               

                                              Dim swApp As SldWorks.SldWorks

                                              Dim Part As ModelDoc2

                                              Dim boolstatus As Boolean

                                              Dim longstatus As Long, longwarnings As Long

                                               

                                              Sub main()

                                                  Dim myTran As MathTransform

                                                  Dim mPt As MathPoint

                                                  Dim myUtil As MathUtility

                                                  Dim p(2) As Double

                                                  Dim selMgr As SelectionMgr

                                                  Dim sa As Variant

                                                  Dim myComp As Component2

                                                  Dim CoordSys As CoordinateSystemFeatureData

                                                  Dim CoordFeat As Feature

                                                  Dim SwAssemblyDoc As AssemblyDoc

                                                  Set swApp = Application.SldWorks

                                                  Set Part = swApp.ActiveDoc

                                                

                                                  Set selMgr = Part.SelectionManager

                                                  Set myComp = selMgr.GetSelectedObjectsComponent4(1, -1)

                                                  Dim SelPt As Variant

                                                  SelPt = selMgr.GetSelectionPoint2(1, -1)

                                                  Set myTran = myComp.Transform2

                                               

                                                  p(0) = SelPt(0) 'mySkPt.X

                                                  p(1) = SelPt(1) '  mySkPt.Y

                                                  p(2) = SelPt(2) 'mySkPt.Z

                                                  sa = p

                                                  Set myUtil = swApp.GetMathUtility

                                                  Set mPt = myUtil.CreatePoint(sa)

                                                  Set mPt = mPt.MultiplyTransform(myTran)

                                                  sa = mPt.ArrayData

                                                  Debug.Print sa(0) * 1000 / 25.4

                                                  Debug.Print sa(1) * 1000 / 25.4

                                                  Debug.Print sa(2) * 1000 / 25.4

                                                   Debug.Print p(0) * 1000 / 25.4

                                                  Debug.Print p(1) * 1000 / 25.4

                                                  Debug.Print p(2) * 1000 / 25.4

                                               

                                               

                                                  Set SwAssemblyDoc = Part

                                               

                                                  Set CoordFeat = SwAssemblyDoc.FeatureByName("Coordinate System1")

                                                  If Not CoordFeat Is Nothing Then

                                                      Set CoordSys = CoordFeat.GetDefinition

                                                      If Not CoordSys Is Nothing Then

                                                          Dim CoordTrans As MathTransform

                                                          Set CoordTrans = CoordSys.Transform

                                                          Dim TransformedPt As MathPoint

                                                          If Not CoordTrans Is Nothing Then

                                                              Set TransformedPt = mPt.MultiplyTransform(CoordTrans.Inverse)

                                                              sa = TransformedPt.ArrayData

                                                              Debug.Print "Relative to Coordinate System X: " & sa(0) * 1000 / 25.4

                                                              Debug.Print "Relative to Coordinate System Y: " & sa(1) * 1000 / 25.4

                                                              Debug.Print "Relative to Coordinate System Z: " & sa(2) * 1000 / 25.4

                                                          End If

                                                      End If

                                                  End If

                                              End Sub

                                                • Re: Get coordinates of point selected in model by user
                                                  Deeksha Gupta

                                                  Thankyou Jacob !

                                                   

                                                  The solution worked for me perfectly.

                                                  • Re: Get coordinates of point selected in model by user
                                                    Balu N.

                                                    Jacob Corder I have used the code as it is but  this line

                                                     

                                                    myComp = selMgr.GetSelectedObjectsComponent4(1, -1)

                                                     

                                                    is getting nothing and its not running further. is there any thing to add as a reference

                                                     


                                                     

                                                     

                                                     


                                                      • Re: Get coordinates of point selected in model by user
                                                        Jacob Corder

                                                        if your writing in VBA, dont you need to use

                                                         

                                                        Set MyComp = selMgr.GetSelectedObjectsComponent4(1, -1)

                                                         

                                                        if .net then you dont use set.

                                                         

                                                        i need to see more code.

                                                         

                                                        if you call GetSelectedObjectsComponent4 and the selected item is a mate for instance,  it will return nothing.

                                                         

                                                         

                                                          • Re: Get coordinates of point selected in model by user
                                                            Balu N.

                                                            Jacob Corder thank you for your quick response. I have fixed that issue. I have one more new problem I have created Coordinate system by using below code

                                                                 boolstatus = doc.Extension.SelectByID2("Point1@3DSketch1", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

                                                                    so(0) = sm.GetSelectedObject6(1, -1)

                                                                    doc.ClearSelection2(True)

                                                                    boolstatus = doc.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, True, 1, Nothing, 0)

                                                                    sx(0) = sm.GetSelectedObject6(1, -1)

                                                                    doc.ClearSelection2(True)

                                                                    boolstatus = doc.Extension.SelectByID2("Right", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                                                                    boolstatus = doc.Extension.SelectByID2("FRONT", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                                                                    doc.InsertAxis2(True)

                                                                    doc.Extension.SelectByID2("Axis1", "AXIS", 0, 0, 0, True, 2, Nothing, 0)

                                                                    sy(0) = sm.GetSelectedObject5(1)

                                                                    'sy(0) = sm.GetSelectedObject6(1, -1)

                                                                    sz(0) = sm.GetSelectedObject6(2, -1)

                                                                    vo = so

                                                                    vx = sx

                                                                    vy = sy

                                                                    vz = sz

                                                             

                                                                    feat = swFeatureManager.CreateCoordinateSystem(so(0), (vx), (vy), (vz))  ' Creating coordinate system with y direction inline to Origin/default Y-direction

                                                            but I want  y direction inline to origin/default X direction

                                                             

                                                                    'feat = doc.FeatureByPositionReverse(0)

                                                                    feat.Name = "CS-50-0-1-1"

                                                                    feat = doc.FeatureByPositionReverse(0)

                                                                    cSysData = feat.GetDefinition

                                                                    doc.ClearSelection2(True)

                                                                    boolstatus = cSysData.AccessSelections(doc, component)

                                                                      when I try to change the direction of coordinate system by selecting reference plane it is not taking, if you have code snippet for changing coordinate system direction (I have only default planes to select) its very helpful