Advertisement
makos

Untitled

Jan 7th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub main()
  4. Dim swApp As SldWorks.SldWorks
  5. Dim pathLen As Integer
  6. Dim folder, file, fName, path, errorPath As String
  7. Dim fileError As Long
  8. Dim fileWarning As Long
  9. Dim swModel As SldWorks.ModelDoc2
  10. Dim swFeat As SldWorks.Feature
  11. Dim swBomFeat As SldWorks.BomFeature
  12. Dim swBomTable As SldWorks.BomTableAnnotation
  13.  
  14. ' MsgBox hi
  15. ' TODO: Check if TASK_FOLDER has trailing \, and add it if it doesn't.
  16. If StrComp(Right($$$TASK_FOLDER$$$, 0), "\") <> 0 Then
  17. folder = $$$TASK_FOLDER$$$ + "\"
  18. Else
  19. folder = $$$TASK_FOLDER$$$
  20. End If
  21. ' MsgBox folder
  22.  
  23. ' folder = $$$TASK_FOLDER$$$
  24. errorPath = folder + "log.txt"
  25. 'ErrorOut "Start log", errorPath
  26.  
  27. Set swApp = Application.SldWorks
  28. swApp.SetCurrentWorkingDirectory folder
  29. Set swModel = swApp.ActiveDoc
  30.  
  31. file = Dir(folder + "*.slddrw")
  32.  
  33. While file <> ""
  34. Set swModel = swApp.OpenDoc6(file, swDocDRAWING, swOpenDocOptions_Silent, "", fileError, fileWarning)
  35.  
  36. If fileError <> 0 Or fileWarning <> 0 Then
  37. ErrorOut "fileError: " + CStr(fileError), errorPath
  38. ErrorOut "fileWarning: " + CStr(fileWarning), errorPath
  39. Exit Sub
  40. End If
  41.  
  42. If swModel Is Nothing Then
  43. ErrorOut "swModel is Nothing", errorPath
  44. Exit Sub
  45. End If
  46.  
  47. Set swFeat = swModel.FirstFeature
  48.  
  49. If swFeat Is Nothing Then
  50. ErrorOut "swFeat is Nothing", errorPath
  51. Exit Sub
  52. End If
  53.  
  54. Do While Not swFeat Is Nothing
  55. If (swFeat.GetTypeName = "BomFeat") Then
  56. Set swBomFeat = swFeat.GetSpecificFeature2
  57. 'TODO: Iterate over the array to export multiple BoMs from a file
  58. Set swBomTable = swBomFeat.GetTableAnnotations(0)
  59.  
  60. path = swModel.GetPathName
  61. pathLen = Len(path)
  62. fName = Left(path, pathLen - 7) & ".xlsx"
  63.  
  64. 'ErrorOut "Saving as Excel...", errorPath
  65. swBomTable.SaveAsExcel fName, True, False
  66. End If
  67. Set swFeat = swFeat.GetNextFeature
  68. Loop
  69. swApp.QuitDoc swModel.GetTitle
  70. file = Dir()
  71. Wend
  72. swApp.ExitApp
  73. Set swApp = Nothing
  74. End Sub
  75.  
  76. Function ErrorOut(errorString As String, errorFilePath As String)
  77. Open errorFilePath For Append As #5
  78. Print #5, errorString
  79. Close #5
  80. End Function
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement