MACRO LIBRARY

Setting and getting vertices by name

Free public access. No account or subscription required.

Files

Setting and getting vertices by name

Code

'Demonstrates usage of IPartDoc::SetEntityName.
    
'Preconditions: Part is open and at least one body is present.
'Postconditions: All vertices are named using IPartDoc::SetEntityName then seleccted _
    using IPartDoc::GetEntityByName.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim vBodies As Variant
Dim swBody As SldWorks.Body2
Dim vVertices As Variant
Dim swVertex As SldWorks.Vertex
Dim swEnt As SldWorks.Entity
    
Dim i As Integer
Dim z As Integer
Dim vertexcounter As Integer
Dim errors As Long
Dim warnings As Long
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    
    vertexcounter = 1
    
    vBodies = swPart.GetBodies2(swAllBodies, False)
    For i = 0 To UBound(vBodies)
        Set swBody = vBodies(i)
        vVertices = swBody.GetVertices
        For z = 0 To UBound(vVertices)
            Set swVertex = vVertices(z)
            Set swEnt = swVertex
            'Debug.Print swPart.SetEntityName(swEnt, "Vertex-" & vertexcounter)
            vertexcounter = vertexcounter + 1
        Next z
    Next i
    
    'select all vertices
    swModel.ClearSelection2 True
    vertexcounter = 1
    Do
        Set swEnt = swPart.GetEntityByName("Vertex-" & vertexcounter, swSelVERTICES)
        If Not swEnt Is Nothing Then swEnt.Select4 True, swSelData
        vertexcounter = vertexcounter + 1
    Loop Until swEnt Is Nothing
End Sub
    
'Notes: IPartDoc::SetEntityName returns true if the entity was not previously named. _
    Otherwise it returns false and the previous entity name was kept. Use _
    IPartDoc::DeleteEntityName to remove an existing entity name before renaming it.
    
    'IPartDoc::GetEntityByName supports only faces, edges, and vertices.