Files
Renaming entities
Code
'Change the name of named entities
'Preconditions: Part is opened with some named entities
'Postconditions: Entities are renamed
'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 swEnt As SldWorks.Entity
Dim vNamedEntity As Variant
Dim i As Integer
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel
vNamedEntity = swPart.GetNamedEntities
If IsEmpty(vNamedEntity) = False Then
For i = 0 To UBound(vNamedEntity)
Set swEnt = vNamedEntity(i)
Debug.Print swPart.GetEntityName(swEnt)
swPart.DeleteEntityName swEnt
Debug.Print swPart.SetEntityName(swEnt, "NewValue")
Next i
Else
Debug.Print "No named entities."
End If
End Sub
'Notes: 'IPartDoc::SetEntityName does not change the name of entities, rather it _
gives names to entities that did not previously have names. This is to prevent _
accidentally re-naming entities that are referenced elsewhere. So to rename an _
entity you must use this method in conjunction with IPartDoc::DeleteEntityName.
'Two entities cannot share the same name. The second replaces the first, which is _
made empty.