AnsweredAssumed Answered

Help with accessing reference geometry point XYZ data

Question asked by Robert Dank on May 4, 2020
Latest reply on May 5, 2020 by Robert Dank

Hello,

 

Writing a VBA script that will spit out XYZ coordinates for all selected reference points in a part file to an Excel spreadsheet. 

 

Here is the script so far. It fails at line 36 with "Object doesn't support this property or method" which tells me that this is not the right way to get the number of selected reference points. I am also not sure on the way I access the XYZ data for the reference points.

 

So two main unknowns for me:

1) Getting the amount of selected reference geometry points, so I can use this data to repeat a command.

2) How does SolidWorks store XYZ coordinates for reference geometry points?

 

I'm fairly new to VBA for SolidWorks. If someone could help me out here, that would be greatly appreciated!

 

- Robert

 

Dim swApp As Object
Sub main()

Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swSelMgr                As SldWorks.SelectionMgr
Dim swFeat                  As SldWorks.Feature
Dim swRefPt                 As SldWorks.RefPoint
Dim refPtArr As Variant
Dim i As Long
Dim xVal As Double
Dim yVal As Double
Dim zVal As Double

       
Set exApp = CreateObject("Excel.Application")
If Not exApp Is Nothing Then
    exApp.Visible = True
    If Not exApp Is Nothing Then
        exApp.Workbooks.Add
        Set Sheet = exApp.ActiveSheet
        If Not Sheet Is Nothing Then
            Sheet.Cells(1, 2).Value = "X"
            Sheet.Cells(1, 3).Value = "Y"
            Sheet.Cells(1, 4).Value = "Z"
        End If
    End If
End If

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
Set swRefPt = swFeat.GetSpecificFeature2

refPtArr = swRefPt.GetRefPoint()

For i = 0 To UBound(refPtArr)
    Set swRefPt = refPtArr(i)
    xVal = refPtArr(i).X
    yVal = refPtArr(i).Y
    zVal = refPtArr(i).Z
    Sheet.Cells(2 + i, 2).Value = xVal
    Sheet.Cells(2 + i, 3).Value = yVal
    Sheet.Cells(2 + i, 4).Value = zVal
Next i
   
End Sub

Outcomes