Getting surface model vertex coordinates

Programming and macros
edcoggins
Posts: 13
Joined: Thu Apr 29, 2021 11:46 am
Answers: 0

Getting surface model vertex coordinates

Unread post by edcoggins »

Greetings. I am trying to get the 3D coordinates from a selection set of vertices in a surface model.

I found an example code in the API Help (create and delete selection sets) that returns the type of entity found in the selection set which is 3.

How can the 3D coordinates of the vertices be obtained?

Thanks in advance.
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: Getting surface model vertex coordinates

Unread post by josh »

set selected object = a Vertex object and use GetPoint
edcoggins
Posts: 13
Joined: Thu Apr 29, 2021 11:46 am
Answers: 0

Re: Getting surface model vertex coordinates

Unread post by edcoggins »

Josh; Thanks for the tip!

Regards,
Ed
mario malic
Posts: 9
Joined: Thu Apr 13, 2023 4:19 am
Answers: 1
Location: Croatia
x 8
x 2

Re: Getting surface model vertex coordinates

Unread post by mario malic »

As mentioned above, it is possible to do it with a vertex object. It is an old thread, but posting for a complete solution. There are some extra unneccessarry objects since I used this macro for other purpose, but the vertex coords are found in ptVar variant.

Code: Select all

Dim swApp As SldWorks.SldWorks

Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim faceObj As SldWorks.Face2
Dim vertexObj As SldWorks.Vertex

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelExt = swModel.Extension
Set swSelMgr = swModel.SelectionManager

' Declare and initialise needed variables
Dim bool As Boolean
Dim numSelectedObjs As Integer
Dim selMark As Long
Dim selObjectType() As Long
Dim selObject() As Object
Dim ptCtr As Integer: ptCtr = 1
Dim ptObjArr As Variant
Dim ptVar() As Variant
Dim i, j As Integer
selMark = -1

' Check selection and reallocate needed objects
numSelectedObjs = swSelMgr.GetSelectedObjectCount2(selMark)
If (numSelectedObjs = 0) Then
    MsgBox "No vertices selected"
    End
End If
ReDim Preserve selObjectType(numSelectedObjs - 1)
ReDim Preserve selObject(numSelectedObjs - 1)

' Traverse selection and obtain vertex coordinates
For i = 1 To numSelectedObjs
    selObjectType(i - 1) = swSelMgr.GetSelectedObjectType3(i, selMark)
    If (selObjectType(i - 1) = SwConst.swSelVERTICES) Then 'Identify selection
        Set vertexObj = swSelMgr.GetSelectedObject6(i, selMark)
        Set selObject(i - 1) = vertexObj
        ptObjArr = swSelMgr.GetSelectionPoint2(i, selMark)
        ReDim Preserve ptVar(ptCtr - 1)
        ptVar(ptCtr - 1) = ptObjArr
        ptCtr = ptCtr + 1
    'ElseIf (selObjectType(i - 1) = SwConst.swSelFACES) Then
    'ElseIf (selObjectType(i - 1) = SwConst.swSelEDGES) Then
    End If
Next
End Sub
Looking for opportunities related to SolidWorks API usage.
Post Reply