Hey everyone,
I've been testing something that some users asked me to do.
Project :
Be able to change the Bom source of every referenced file from a TopLevel assembly with the click of a button.
Client is using Solidworks 2016 SP4.
APIs Used : Solidworks 2019 SP1 (It's fine as long as you use fonctions available in the 2016 version)
Solution 1 :
I achieved this project but without the most optimal solution. Basically I get all the referenced configurations and I open them 1 by 1, change the BomPartNoSource to what was desired and I save it.
Sub ChangerNomenclatureMeuble_NomConfiguration()
ChangerNomenclature_Boucle(IO.Path.GetFileNameWithoutExtension(swDoc.GetPathName), swDoc.GetPathName, swDoc.ConfigurationManager.ActiveConfiguration.Name, swBOMPartNumberSource_e.swBOMPartNumber_ConfigurationName)
SwApp.SendMsgToUser2("Les nomenclatures sont changées!", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
End Sub
Private Sub ChangerNomenclature_Boucle(TopLevelName As String, FileName As String, Config As String, BomSource As swBOMPartNumberSource_e)
Dim currDoc As sldworks.ModelDoc2
If FileName <> "" Then currDoc = OpenFile(FileName)
Dim ConfigurationOpened As Boolean = currDoc.ConfigurationManager.ActiveConfiguration.Name = Config OrElse currDoc.ShowConfiguration2(Config)
Dim CurrentConfig As sldworks.Configuration = currDoc.GetConfigurationByName(Config)
CurrentConfig.BOMPartNoSource = BomSource
currDoc.Save2(True)
For Each Piece As FileConfig In GetAllPartsOfAssembly(TopLevelName, currDoc, Config)
ChangerNomenclature_Boucle(TopLevelName, Piece.File, Piece.Config, BomSource)
SwApp.CloseDoc(Piece.File)
Next
End Sub
Private Function GetAllPartsOfAssembly(TopLevelName As String, currDoc As sldworks.ModelDoc2, Config As String) As List(Of FileConfig)
Dim ReturnList As New List(Of FileConfig)
Dim CurrentConfig As sldworks.Configuration = currDoc.GetConfigurationByName(Config)
Dim Component As sldworks.Component2 = CurrentConfig.GetRootComponent3(True)
Dim Enfants As Object = Component.GetChildren()
If Enfants Is Nothing Then Return ReturnList
For i = 0 To UBound(Enfants)
Dim ChildComp As sldworks.Component2 = Enfants(i)
If ChildComp.Name.ToUpper.Contains(TopLevelName.ToUpper) Then ReturnList.Add(New FileConfig(ChildComp.GetPathName(), ChildComp.ReferencedConfiguration))
Next
Return ReturnList
End Function
Solution 2 :
Use the Document Manager APIs to do everything without opening a single file.
Sub ChangerNomenclatureMeuble_NomConfiguration()
ChangerNomenclature_Boucle_DocManager(IO.Path.GetFileNameWithoutExtension(swDoc.GetPathName), swDoc.GetPathName, swDoc.ConfigurationManager.ActiveConfiguration.Name, swDmBOMPartNumberSource.swDmBOMPartNumber_ConfigurationName)
SwApp.SendMsgToUser2("Les nomenclatures sont changées!", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk)
End Sub
Private Sub ChangerNomenclature_Boucle_DocManager(TopLevelName As String, FileName As String, Config As String, BomSource As swDmBOMPartNumberSource)
Const sLicenseKey2016 As String = "LicenceKeyExample"
FreeMemory()
Dim DocType As swDocumentTypes_e = swDocumentTypes_e.swDocPART
If FileName.ToUpper.EndsWith(".SLDASM") Then DocType = swDocumentTypes_e.swDocASSEMBLY
Dim swClassFact As SwDMClassFactory = CreateObject("SwDocumentMgr.SwDMClassFactory")
Dim dmApp As SwDMApplication4 = swClassFact.GetApplication(sLicenseKey2016)
Dim nRetVal As Long
Dim swDocument As SwDMDocument20 = dmApp.GetDocument(FileName, DocType, True, nRetVal)
Dim swCfgMgr As SwDMConfigurationMgr = swDocument.ConfigurationManager
Dim swConfig As SwDMConfiguration11 = swCfgMgr.GetConfigurationByName(Config)
'Not Implemented yet
swConfig.BOMPartNoSource = BomSource
swDocument.Save()
For Each Piece As FileConfig In GetAllPartsOfAssembly_DocManager(TopLevelName, dmApp, swDocument, Config)
ChangerNomenclature_Boucle_DocManager(TopLevelName, Piece.File, Piece.Config, BomSource)
SwApp.CloseDoc(Piece.File)
Next
End Sub
Private Function GetAllPartsOfAssembly_DocManager(TopLevelName As String, dmApp As SwDMApplication4, dmDoc As SwDMDocument18, Config As String) As List(Of FileConfig)
Dim ReturnList As New List(Of FileConfig)
Dim SearchOptions As SwDMExternalReferenceOption2 = dmApp.GetExternalReferenceOptionObject2()
SearchOptions.Configuration = Config
SearchOptions.NeedSuppress = False
SearchOptions.SearchOption = dmApp.GetSearchOptionObject()
Dim BrokenRefVar As Object
Dim isVirtual As Object
Dim TimeStamp As Object
Dim ImportedPaths As Object
Dim dmReferences As Object = dmDoc.GetExternalFeatureReferences2(SearchOptions)
If dmReferences Is Nothing Then Return ReturnList
For i = 0 To UBound(dmReferences)
Dim ChildComp As sldworks.Component2 = dmReferences(i)
If ChildComp.Name.ToUpper.Contains(TopLevelName.ToUpper) Then ReturnList.Add(New FileConfig(ChildComp.GetPathName(), ChildComp.ReferencedConfiguration))
Next
Return ReturnList
End Function
Solution #2 gives an error when trying to set the Value of BomPartNoSource (NotImplementedYet).
GetExternalReferences2 Method
Ce message a été modifié par : Michael Corriveau-Cote
Ce message a été modifié par : Michael Corriveau-Cote
SolidworksApi/macros