Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Sub main()
- Dim swApp As SldWorks.SldWorks
- Dim pathLen As Integer
- Dim folder, file, fName, path, errorPath As String
- Dim fileError As Long
- Dim fileWarning As Long
- Dim swModel As SldWorks.ModelDoc2
- Dim swFeat As SldWorks.Feature
- Dim swBomFeat As SldWorks.BomFeature
- Dim swBomTable As SldWorks.BomTableAnnotation
- ' MsgBox hi
- ' TODO: Check if TASK_FOLDER has trailing \, and add it if it doesn't.
- If StrComp(Right($$$TASK_FOLDER$$$, 0), "\") <> 0 Then
- folder = $$$TASK_FOLDER$$$ + "\"
- Else
- folder = $$$TASK_FOLDER$$$
- End If
- ' MsgBox folder
- ' folder = $$$TASK_FOLDER$$$
- errorPath = folder + "log.txt"
- 'ErrorOut "Start log", errorPath
- Set swApp = Application.SldWorks
- swApp.SetCurrentWorkingDirectory folder
- Set swModel = swApp.ActiveDoc
- file = Dir(folder + "*.slddrw")
- While file <> ""
- Set swModel = swApp.OpenDoc6(file, swDocDRAWING, swOpenDocOptions_Silent, "", fileError, fileWarning)
- If fileError <> 0 Or fileWarning <> 0 Then
- ErrorOut "fileError: " + CStr(fileError), errorPath
- ErrorOut "fileWarning: " + CStr(fileWarning), errorPath
- Exit Sub
- End If
- If swModel Is Nothing Then
- ErrorOut "swModel is Nothing", errorPath
- Exit Sub
- End If
- Set swFeat = swModel.FirstFeature
- If swFeat Is Nothing Then
- ErrorOut "swFeat is Nothing", errorPath
- Exit Sub
- End If
- Do While Not swFeat Is Nothing
- If (swFeat.GetTypeName = "BomFeat") Then
- Set swBomFeat = swFeat.GetSpecificFeature2
- 'TODO: Iterate over the array to export multiple BoMs from a file
- Set swBomTable = swBomFeat.GetTableAnnotations(0)
- path = swModel.GetPathName
- pathLen = Len(path)
- fName = Left(path, pathLen - 7) & ".xlsx"
- 'ErrorOut "Saving as Excel...", errorPath
- swBomTable.SaveAsExcel fName, True, False
- End If
- Set swFeat = swFeat.GetNextFeature
- Loop
- swApp.QuitDoc swModel.GetTitle
- file = Dir()
- Wend
- swApp.ExitApp
- Set swApp = Nothing
- End Sub
- Function ErrorOut(errorString As String, errorFilePath As String)
- Open errorFilePath For Append As #5
- Print #5, errorString
- Close #5
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement