6 Replies Latest reply on Jul 27, 2016 4:39 PM by John Alexander

    Transform Model Coordinates to Sketch Coordinates Macro

    Cody Brown

      Hi,

       

      I am writing a macro in which the user clicks on a hole (already inserted) and then inserts a ring around the hole. I know this can be done with a library feature, but I want to be able to insert several rings at once without having to navigate through my library features each time. I have code to get the location where the users clicks in 3D space (model coordinates), but I need to transform these coordinates into sketch coordinates as the axes are changed when you enter a sketch. I've seen sample code on how to go the other way (from sketch coordinates to model coordinates: 2014 SOLIDWORKS API Help - Transform Sketch to Model Example (VBA) ), but I do not see any examples on how to go from model coordinates to sketch coordinates. Below is my code so far, which is supposed to insert a ring at a user-specified location:

      --------------------------------------------------------------------------------------------------------------------

      Private Sub CommandButton1_Click()

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Dim Comp As SldWorks.Component2

      Dim bRet As Boolean

      Dim Point As Variant

      Dim SelMgr As SldWorks.SelectionMgr

       

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc

      Set SelMgr = Part.SelectionManager

       

      Unload frmRing

       

      While SelMgr.GetSelectedObjectCount < 1

          DoEvents   'Program pauses until user selects something in solidworks

      Wend

       

      Set Part = swApp.ActiveDoc

      Set SelMgr = Part.SelectionManager

      'Get xyz of selecteion (in meters)

      Point = SelMgr.GetSelectionPoint2(1, -1)

      'Print xyz

      Debug.Print "X= " & Point(0)

      Debug.Print "Y= " & Point(1)

      Debug.Print "Z= " & Point(2)

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("", "FACE", Point(0), Point(1), Point(2), False, 0, Nothing, 0)

      Part.SketchManager.InsertSketch True

      boolstatus = Part.Extension.SelectByID2("", "FACE", Point(0), Point(1), Point(2), False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Dim skSegment As Object

      Set skSegment = Part.SketchManager.CreateCircleByRadius(Point(0), Point(1), Point(2), 0.5 * 0.0254)  'This line needs the transformed points

      Part.SetPickMode

      Part.ClearSelection2 True

       

      End Sub

      -----------------------------------------------------------------------------------------------------------------------------------

       

      Any ideas on how to transform the model coordinates (Point(0), Point(1), and Point(2)) to sketch coordinates? I'm a beginner-intermediate programmer, so please explain any code you provide as I want to make sure I fully understand it so I can maintain it. I recorded some of the macro, so there may be a little bit of extraneous code above.

       

      Thanks!

        • Re: Transform Model Coordinates to Sketch Coordinates Macro
          John Alexander

          From your VBA example link:

           

          6. Get the model-to-sketch transform for this sketch. Calling IMathTransform::Inverse provides a IMathTransform from the sketch coordinates to the model coordinates. The IMathPoint is then moved into the model coordinates by calling IMathPoint::MulitplyTransform(MathTransform).

           

          The example is actually inverting the transform to be Sketch -> Model. It does this by getting the ModelToSketch transform and calling Invert on it. To get the ModelToSketch transform, just omit the inversion.

           

           

              Set MathTrans = SketchFeature.ModelToSketchTransform

              'Get the inversion of the transform

              'Edit - omit this line: Set MathTrans = MathTrans.Inverse

           

           

          Also from the example, your points are transformed by calling MultiplyTransform on them with the ModelToSketchTransform as an argument.

          Set MathP = MathP.MultiplyTransform(MathTrans)

            • Re: Transform Model Coordinates to Sketch Coordinates Macro
              Cody Brown

              Thanks for the quick reply! I tried what you suggested and that worked, however it only works for points. Any idea on how I could apply this to a hole? For example, if I click on the outer edge of a hole, is there a way to modify this code to get the sketch point of the center of the hole?

               

              Edit: I also found an example showing how to locate the center of a hole and create a 3D sketch of a point there, but the transformation example only works with a 2D sketch of a point. So another option would be to get the transformation to work for a 3D sketch of a point, but I'm not sure how to do that.

                • Re: Transform Model Coordinates to Sketch Coordinates Macro
                  John Alexander

                  Usually features in the Solidworks API will (eventually) depend on MathPoints. By eventually, I mean they will depend on features that depend on features that contain or are defined by MathPoints. Sometimes, they will directly report their coordinates and you have to build the math points from those coordinates (if you want to use the functionality that MathPoints provide, like multiplying transforms). For instance, SketchPoints report their X, Y, Z value with respect to the sketch coordinate system.

                   

                  2012 SOLIDWORKS API Help - ISketchPoint Interface Members

                   

                  For hole features in a part, I'm not familiar enough to give you a straight answer without working at it. I know that the location of the hole will be driven by a sketch (which is absorbed by the hole feature). Any SketchPoints in that sketch will become insertion points for instances of the hole feature. Therefore, if you want to know about a hole's position, all you need is that sketch. I'm not sure how to get the sketch associated with a hole feature but I'm 99% certain that there is a relationship that will be exposed in the API somehow. Try searching the Solidworks API help site for hole features.

                   

                  Once you have a SketchPoint from the hole feature's sketch, you can get the X,Y, and Z values reported with respect to the sketch's coordinate system. You can take those values, build a mathpoint from them, and call the MultiplyTransform function that it provides.

                   

                  2012 SOLIDWORKS API Help - IMathPoint Interface Members

                   

                  If your question is broader than just holes, the answer varies. Many features in Solidworks will eventually depend on MathPoints - but not always. Because of this, every time you want to do something novel (to you), it is usually a matter of reading through the API and planning how to transform information from one thing to another. When writing Solidworks macros in general, a significant portion of time is spent reading the API documentation or guessing and experimenting.

                    • Re: Transform Model Coordinates to Sketch Coordinates Macro
                      Cody Brown

                      Thanks for your reply and advice. I am only using holes, not any other features. If I am able to get the model coordinates of the hole's center, is there a way to transform those coordinates without using sketch points? The code below from the original example I linked to requires an object to use MathTrans. So maybe there is a way to replace MathTrans with an array of coordinates or something like that?

                      -------------------

                      Set SketchFeature = selMgr.GetSelectedObject6(1, 0)

                      Set SketchFeature = SketchFeature.GetSpecificFeature2

                      ...

                      Set MathTrans = SketchFeature.ModelToSketchTransform

                      ...

                      Set MathP = MathP.MultiplyTransform(MathTrans)

                      -------------

                      I've looked a little bit at the links you provided already. I'll continue to look at them some more, but hopefully you or someone else will have more suggestions on how to translate specific model coordinates (like (1,2,3)) into sketch coordinates without sketch points.

                        • Re: Transform Model Coordinates to Sketch Coordinates Macro
                          Cody Brown

                          I think I found something much simpler that will work for what I need. Instead of using sketch points, I can apply a concentric relation to the hole and circle via vba. I was trying to draw a circle with its center at the hole's center, so this will effectively do the same thing. Since you answered my original question, I'll mark it as correct.

                           

                          Thanks again for the help and advice!

                          • Re: Transform Model Coordinates to Sketch Coordinates Macro
                            John Alexander

                            I haven't tested this, can't guarantee that it will compile but it should give you a rough idea of what I'm suggesting.

                             

                            1. You need a reference to the sketch that you create for the circle so that you can get the ModelToSketchTransform. I'm not sure how that is accessed - probably something through the SketchManager like "GetActiveSketch".

                             

                            2. You can build a MathPoint using the MathUtility accessed through the Application.Sldworks object. This object can be transformed into the sketch coordinates. You can then use its X, Y, Z values stored in MathPoint.ArrayData to assign to the center point of the new sketch circle.

                             

                            Private Sub CommandButton1_Click()

                                 Dim swApp As Object

                                 Dim Part As Object

                                 Dim boolstatus As Boolean

                                 Dim longstatus As Long, longwarnings As Long

                                 Dim Comp As SldWorks.Component2

                                 Dim bRet As Boolean

                                 Dim Point As Variant

                                 Dim SelMgr As SldWorks.SelectionMgr

                             

                                 Set swApp = Application.SldWorks

                                 Set Part = swApp.ActiveDoc

                                 Set SelMgr = Part.SelectionManager

                             

                                 Unload frmRing

                                 

                                 While SelMgr.GetSelectedObjectCount < 1

                                      DoEvents   'Program pauses until user selects something in solidworks

                                 Wend

                                 

                                 Set Part = swApp.ActiveDoc

                                 Set SelMgr = Part.SelectionManager

                                 'Get xyz of selecteion (in meters)

                                 Point = SelMgr.GetSelectionPoint2(1, -1)

                                 'Print xyz

                                 Debug.Print "X= " & Point(0)

                                 Debug.Print "Y= " & Point(1)

                                 Debug.Print "Z= " & Point(2)

                             

                                 Set Part = swApp.ActiveDoc

                                 boolstatus = Part.Extension.SelectByID2("", "FACE", Point(0), Point(1), Point(2), False, 0, Nothing, 0)

                                 Part.SketchManager.InsertSketch True

                                 boolstatus = Part.Extension.SelectByID2("", "FACE", Point(0), Point(1), Point(2), False, 0, Nothing, 0)

                                 Part.ClearSelection2 True

                                 Dim skSegment As Object

                             

                                 Dim swSketch as Sldworks.Sketch

                                 set swSketch = 'Get a reference to the sketch you just created - not sure off the top of my head

                                

                                 Dim swMathUtility as Sldworks.MathUtility

                                 Dim swMathPoint as sldworks.MathPoint

                                 Dim DoubleArray(2) as Double

                                

                                 DoubleArray(0) = Point(0)

                                 DoubleArray(1) = Point(1)

                                 DoubleArray(2) = Point(2)

                             

                                 Set swMathUtility = swApp.GetMathUtility

                                 Set swMathPoint = swMathUtility.CreatePoint(DoubleArray)

                                 Set swMathPoint = swMathPoint.MultiplyTransform(swSketch.ModelToSketchTransform)

                             

                                 Point = swMathPoint.ArrayData

                             

                                 Set skSegment = Part.SketchManager.CreateCircleByRadius(Point(0), Point(1), Point(2), 0.5 * 0.0254)  'This line gets the transformed points

                                 Part.SetPickMode

                                 Part.ClearSelection2 True

                            End Sub

                            ----------------