Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Option Explicit
- Dim docEvents As clsDocEvents
- Const csvFilename As String = "auto_filename.csv"
- Const NMB_FORMAT As String = "0000" 'padding for the number, e.g. 001, 002, instead of 1, 2
- Sub AutoSetPartTitle()
- Dim swApp As SldWorks.SldWorks
- Dim swModel As ModelDoc2
- Dim templatePath As String
- Dim csvPath As String
- Dim fso As Object
- Dim FileContent As String
- Dim FileLines() As String
- Dim validLines() As String
- Dim csvData() As Variant
- Dim i As Long, j As Long
- Dim selectedIndex As Long
- Dim newNumber As Long
- Dim newTitle As String
- Dim updatedCSV As String
- Dim colCount As Long
- Dim validCount As Long
- Dim originalSavePath As String
- Set swApp = Application.SldWorks
- Set swModel = swApp.ActiveDoc
- If swModel Is Nothing Then Exit Sub
- ' Get Document Templates path
- ' templatePath = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
- ' csvPath = Left(templatePath, InStrRev(templatePath, "\")) & csvFilename
- csvPath = "Y:\~Solidworks Templates\" & csvFilename
- ' Read entire CSV file into a string
- Set fso = CreateObject("Scripting.FileSystemObject")
- If Not fso.FileExists(csvPath) Then
- MsgBox "CSV file not found: " & csvPath, vbCritical
- Exit Sub
- End If
- With fso.OpenTextFile(csvPath, 1)
- FileContent = .ReadAll
- .Close
- End With
- ' Split into lines
- FileLines = Split(FileContent, vbCrLf)
- FileContent = ""
- ' First, count valid (non-blank) lines
- validCount = 0 'non-zero based
- For i = 0 To UBound(FileLines)
- If Len(Trim(FileLines(i))) > 0 Then
- ReDim Preserve validLines(0 To validCount)
- validLines(validCount) = FileLines(i)
- validCount = validCount + 1
- End If
- Next i
- Erase FileLines
- ' Exit if no valid data
- If validCount = 0 Then
- MsgBox "CSV file is empty or contains only blank lines.", vbCritical
- Exit Sub
- End If
- ' Determine column count from first valid line
- colCount = UBound(Split(validLines(0), ",")) 'zero based
- ' Populate csvData with only valid rows
- ReDim csvData(0 To validCount - 1, 0 To colCount)
- For i = 0 To validCount - 1
- Dim fields() As String
- fields = Split(validLines(i), ",")
- For j = 0 To colCount
- If j <= UBound(fields) Then
- csvData(i, j) = Trim(fields(j))
- Else
- csvData(i, j) = ""
- End If
- Next j
- Next i
- Erase fields
- Erase validLines
- ' Show form (skip header row)
- frmPrefixSelector.lstPrefixes.Clear
- For i = 1 To UBound(csvData, 1)
- If Trim(csvData(i, 0)) <> "" Then
- frmPrefixSelector.lstPrefixes.AddItem csvData(i, 0) ' Prefix column
- End If
- Next i
- frmPrefixSelector.Show
- selectedIndex = frmPrefixSelector.lstPrefixes.ListIndex
- If selectedIndex < 0 Then Exit Sub
- ' Adjust index to match csvData (skip header)
- selectedIndex = selectedIndex + 1
- ' Generate new title
- newNumber = CLng(csvData(selectedIndex, 1)) + 1
- newTitle = csvData(selectedIndex, 0) & Format(newNumber, NMB_FORMAT)
- swModel.SetTitle2 newTitle
- ' Update CSV array
- csvData(selectedIndex, 1) = CStr(newNumber)
- ' Write updated CSV including header
- With fso.CreateTextFile(csvPath, True)
- For i = 0 To UBound(csvData, 1)
- If Trim(csvData(i, 0)) <> "" Then
- updatedCSV = ""
- For j = 0 To colCount
- updatedCSV = updatedCSV & csvData(i, j)
- If j < colCount Then updatedCSV = updatedCSV & ","
- Next j
- .WriteLine updatedCSV
- End If
- Next i
- .Close
- End With
- Dim targetPath As String
- targetPath = csvData(selectedIndex, 2) & "\" & swModel.GetTitle & GetFileExtension(swModel)
- originalSavePath = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swFileLocationsDefaultSave)
- swApp.SetUserPreferenceStringValue swUserPreferenceStringValue_e.swFileLocationsDefaultSave, csvData(selectedIndex, 2)
- ' Hook into document events
- Set docEvents = New clsDocEvents
- docEvents.Init swModel, swApp, originalSavePath
- 'swApp.SetUserPreferenceStringValue swUserPreferenceStringValue_e.swFileLocationsDefaultSave, csvData(selectedIndex, 2)
- MsgBox "Title set to: " & newTitle, vbInformation
- End Sub
- Function GetFileExtension(doc As ModelDoc2) As String
- Dim docType As Long
- docType = doc.GetType
- Select Case docType
- Case swDocPART
- GetFileExtension = ".sldprt"
- Case swDocASSEMBLY
- GetFileExtension = ".sldasm"
- Case swDocDRAWING
- GetFileExtension = ".slddrw"
- Case Else
- GetFileExtension = ""
- End Select
- End Function
Advertisement
Add Comment
Please, Sign In to add comment