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!
SolidworksApi macros