AnsweredAssumed Answered

[VBA] get points coordinates

Question asked by Benoît Varret on Dec 4, 2019
Latest reply on Dec 6, 2019 by Josh Brady


I'm trying to get the coordinates of specific sketch points back into Excel. Here is the minimal working example. Copy paste it into an excel vba module. You will need to activate "SldWorks 2016 Type Library" under "Tools > References…"


vValue is suppose to be a triplet of coordinates. If you run the script you'll get (0;0;0). If you run the script step by step (F8 shortcut) and manually select a point in the model just before  the line `vValue = swSelMgr.GetSelectionPoint2(1, -1)` you'll get the exact coordinates. What's wrong ? There is no error.







Option Explicit

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As IModelDoc2
Dim oDimension As Object
Dim swSelMgr As SldWorks.SelectionMgr

Dim lErrors As Long, lWarnings As Long
Dim bStatus As Boolean
Dim vValue As Variant

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True

'Set the path to a SolidWorks part
Set swModel = swApp.OpenDoc6("C:\PATH\TO\MY\FILE.SLDPRT", swDocPART, 0, "", lErrors, lWarnings)
'Set the name of the point. You can get it thanks to the measurement tools in SW
bStatus = swModel.Extension.SelectByID2("Point1@Sketch1", "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)

Set swSelMgr = swModel.SelectionManager
'When step by step, try here to manually select a point in the model
vValue = swSelMgr.GetSelectionPoint2(1, -1)

Debug.Print vValue(0)
Debug.Print vValue(1)
Debug.Print vValue(2)

End Sub