I know that this subject has already been discussed many times and that there are surely ready-made applications.
I want to create an Excel BOM that would update automatically when it is opened in which without the assembly being opened, in order to integrate it into a project management table.
@Yannick: It is possible to do this with Solidworks closed using "SolidWorks.Interop.swdocumentmgr.dll" (or even with Solidworks not installed using "swdocumentmgr.dll") Use the GetComponents function to get the BOM.
On the other hand, now that document manager only works in x64, we would have to make an external module to Excel. And call it from an Excel macro with the command "Call Shell()". Or have an application that writes the bill of materials directly to an Excel file.
Here is a draft that will write the BOM in the same directory as the assembly: (Copy the code into a new VB.NET console application , or use the attached file)
Imports System.IO
Imports SwDocumentMgr
'Imports SolidWorks.Interop.swdocumentmgr'
Module Module1
Private swDocMgr As SwDMApplication
Private Bom As New Dictionary(Of String, Integer)
Private TsvPath As String
Sub Main()
Dim LicenseFile As String = Path.Combine(My.Application.Info.DirectoryPath, "LicenseKey.txt")
Dim Lines As List(Of String) = File.ReadAllLines(LicenseFile).ToList
Dim LicenseKey As String = Lines(0)
If LicenseKey.Length < 50 Then
Console.Write("Entrer le numéro de license document manager dans ce fichier TXT")
Process.Start(LicenseFile)
Console.ReadLine()
System.Environment.Exit(-1)
End If
Dim Args As List(Of String) = System.Environment.GetCommandLineArgs.ToList
Dim AssyPath As String
If Args.Count = 2 Then
AssyPath = Args(1)
Else
Console.Write("Entrer le chemin de l'assemblage: ")
AssyPath = Console.ReadLine()
End If
Dim swCf As New SwDMClassFactory()
swDocMgr = swCf.GetApplication(Lines(0))
GetComps(AssyPath)
TsvPath = AssyPath.Replace(".SLDASM", ".TSV")
If File.Exists(TsvPath) Then File.Delete(TsvPath)
WriteLine("PN" & vbTab & "QT.")
For Each item In Bom.Keys
WriteLine(item & vbTab & Bom(item))
Next
End Sub
Private Sub GetComps(ByVal FilePath As String)
Dim DocType As SwDmDocumentType = SwDmDocumentType.swDmDocumentAssembly
Select Case Path.GetExtension(FilePath).ToLower
Case ".sldprt"
DocType = SwDmDocumentType.swDmDocumentPart
Case ".sldasm"
DocType = SwDmDocumentType.swDmDocumentAssembly
Case ".slddrw"
DocType = SwDmDocumentType.swDmDocumentDrawing
End Select
Dim res As Long
Dim swDoc As SwDMDocument13
swDoc = swDocMgr.GetDocument(FilePath, DocType, True, res)
Dim swConfig As SwDMConfiguration12
Dim swConfigMgr As SwDMConfigurationMgr = swDoc.ConfigurationManager
swConfig = swConfigMgr.GetConfigurationByName(swConfigMgr.GetActiveConfigurationName())
Dim comps As Object = swConfig.GetComponents()
Dim arrComps As Array = comps
If arrComps IsNot Nothing AndAlso arrComps.Length > 0 Then
For Each swComp As SwDMComponent9 In arrComps
If swComp.ExcludeFromBOM = swDmExcludeFromBOMResult.swDmExcludeFromBOM_TRUE _
OrElse swComp.IsSuppressed Then
Continue For
End If
If swComp.DocumentType = SwDmDocumentType.swDmDocumentAssembly Then
GetComps(swComp.PathName)
Else
Dim FileName As String = Path.GetFileNameWithoutExtension(swComp.PathName)
If Not Bom.ContainsKey(FileName) Then
Bom.Add(FileName, 1)
Else
Bom(FileName) += 1
End If
End If
Next
End If
swDoc.CloseDoc()
End Sub
Private Sub WriteLine(ByVal line As String)
Dim swt As StreamWriter
If Not File.Exists(TsvPath) Then
swt = File.CreateText(TsvPath)
Else
swt = File.AppendText(TsvPath)
End If
swt.WriteLine(line)
swt.Flush()
swt.Close()
End Sub
End Module
- To the question "In your opinion, it is possible to extract a BOM with Solidworks closed.": everything is said by JeromeP with the bonus of the example that goes well.
- To the question "How would you proceed? Using a macro on Excel? ": by an independent program that would fetch the nomenclature to either write it in a new Excel file or fill in an existing Excel file.
Hello Jonathan, The tool above uses only SolidWorks. You can use it in Excel with the function:
Private Sub CommandButton1_Click()
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\MesProgrammes\BomToXls.exe"
strArgument = "C:\MesFichiers\Assem1.SLDASM"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub