Greetings to everyone…
We use SolidWorks CAM in my company and I wanted to ask if it would be possible to get guidance on how to develop a program in VBA.
I would like to rename the “Mill Part Setup” to “First Step.”
I would like to rename the “Toolpath” to a name chosen from a list.
Possible duplicate:
See this question: Solidworks CAM API development
Hello, everyone.
I have finally managed to create a code that works on SolidWorks CAM Standard and not Professional.
I am attaching the code for anyone who needs it.
Private Sub UserForm_Initialize()
Dim cwApp As CAMWORKSLib.cwApp
Dim cDoc As Object
Dim cwOpSetups As CAMWORKSLib.CWDispatchCollection
Dim cwSetup As CAMWORKSLib.CWBaseSetup
Dim cwMach As Object
Dim cwMachines As Object
Dim docType As Long
Dim i As Long
Set cwApp = New CAMWORKSLib.cwApp
Set cDoc = cwApp.IGetActiveDoc
If cDoc Is Nothing Then
MsgBox "Please open a part or assembly file in SolidWorks.", vbExclamation
Unload Me
Exit Sub
End If
docType = cDoc.GetDocType
If docType = CW_DOCUMENT_PART Then
Set cwMach = cDoc.IGetMachine
Set cwOpSetups = cwMach.IGetEnumSetups
ElseIf docType = CW_DOCUMENT_ASSEMBLY Then
Set cwMachines = cDoc.IGetEnumMachines
Set cwMach = cwMachines.Item(0)
Set cwOpSetups = cwMach.IGetEnumSetups
Else
MsgBox "Unsupported document type.", vbExclamation
Unload Me
Exit Sub
End If
For i = 0 To cwOpSetups.Count - 1
Set cwSetup = cwOpSetups.Item(i)
lstOldNames.AddItem cwSetup.SetupName
lstNewNames.AddItem EnglishOrdinal(i + 1) & " Phase"
Next i
' Add 4 extra entries
Dim j As Long
For j = cwOpSetups.Count To cwOpSetups.Count + 3
lstNewNames.AddItem EnglishOrdinal(j + 1) & " Phase (New)"
Next j
End Sub
Private Sub btnClose_Click()
Unload Me
End Sub
Private Function EnglishOrdinal(n As Integer) As String
Select Case n
Case 1: EnglishOrdinal = “First”
Case 2: EnglishOrdinal = “Second”
Case 3: EnglishOrdinal = “Third”
Case 4: EnglishOrdinal = “Fourth”
Case 5: EnglishOrdinal = “Fifth”
Case 6: EnglishOrdinal = “Sixth”
Case 7: EnglishOrdinal = “Seventh”
Case 8: EnglishOrdinal = “Eighth”
Case 9: EnglishOrdinal = “Ninth”
Case 10: EnglishOrdinal = “Tenth”
Case Else: EnglishOrdinal = n & “th”
End Select
End Function
Private Sub btnApply_Click()
Dim cwApp As CAMWORKSLib.cwApp
Dim cDoc As Object
Dim cwOpSetups As CAMWORKSLib.CWDispatchCollection
Dim cwSetup As CAMWORKSLib.CWBaseSetup
Dim cwMach As Object
Dim cwMachines As Object
Dim docType As Long
Dim i As Long
' Validate selections
If lstOldNames.ListIndex = -1 Or lstNewNames.ListIndex = -1 Then
MsgBox "Please select both an old name and a new name.", vbExclamation
Exit Sub
End If
Dim oldName As String
Dim newName As String
Dim oldIndex As Integer
oldName = lstOldNames.Value
newName = lstNewNames.Value
oldIndex = lstOldNames.ListIndex
' Initialize CAMWorks
Set cwApp = New CAMWORKSLib.cwApp
Set cDoc = cwApp.IGetActiveDoc
If cDoc Is Nothing Then
MsgBox "No active document found.", vbExclamation
Exit Sub
End If
docType = cDoc.GetDocType
If docType = CW_DOCUMENT_PART Then
Set cwMach = cDoc.IGetMachine
Set cwOpSetups = cwMach.IGetEnumSetups
ElseIf docType = CW_DOCUMENT_ASSEMBLY Then
Set cwMachines = cDoc.IGetEnumMachines
Set cwMach = cwMachines.Item(0)
Set cwOpSetups = cwMach.IGetEnumSetups
Else
MsgBox "Unsupported document type.", vbExclamation
Exit Sub
End If
' Check if new name already exists
For i = 0 To cwOpSetups.Count - 1
Set cwSetup = cwOpSetups.Item(i)
If cwSetup.SetupName = newName Then
MsgBox "The name """ & newName & """ already exists. Rename cancelled.", vbCritical
Exit Sub
End If
Next i
' Find and rename the selected setup
For i = 0 To cwOpSetups.Count - 1
Set cwSetup = cwOpSetups.Item(i)
If cwSetup.SetupName = oldName Then
cwSetup.SetupName = newName
' Update ListBox entry
lstOldNames.List(oldIndex) = newName
' Keep selection synchronized
lstOldNames.ListIndex = oldIndex
lstNewNames.ListIndex = oldIndex
Exit Sub
End If
Next i
MsgBox "Setup named """ & oldName & """ not found.", vbExclamation
End Sub