Retrieve the files of the creation tree from the nomenclature?

Hello

I am currently thinking about a system to check the nomenclature of a machine. The machine tree is structured in subsets, grouped in "function" folders in the creation tree.

Ideally, I would like to be able to export the machine's bill of materials to Excel , respecting the groupings in assembly folders. I haven't found a way to do it directly, maybe it would be possible to do it via VBA? What do you think?

Thanks in advance!

Hello. The macro below will export the tree structure including the folders such as:

TopLevelAsm1 > Asm3-1 > Part4-1
TopLevelAsm1 > Folder1 > Asm2-1
TopLevelAsm1 > Folder1 > Asm2-1 > Part1-1

Option Explicit
Dim FilePath As String
Sub main()
    FilePath = "C:\Temp\myBOM.TSV"
    If Dir(FilePath) <> "" Then Kill FilePath
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    TraverseAssyFeatures swModel, Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1, InStrRev(swModel.GetPathName, ".") - InStrRev(swModel.GetPathName, "\") - 1)
End Sub

Sub TraverseAssyFeatures(ByVal swModel As SldWorks.ModelDoc2, ByRef Rep As String)
    Dim swComp As SldWorks.Component2
    Dim swCompModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swEntity As SldWorks.Entity
    Set swFeat = swModel.FirstFeature
    While Not swFeat Is Nothing
        Set swEntity = swFeat
        If swEntity.GetType = swSelectType_e.swSelFTRFOLDER Then
            If InStr(swFeat.Name, "EndTag") = 0 Then
                Rep = Rep & " > " & swFeat.Name
            Else
                Rep = Left(Rep, InStrRev(Rep, " > ") - 1)
            End If
        End If

        If swEntity.GetType = swSelectType_e.swSelCOMPONENTS Then
            WriteIntoFile Rep & " > " & swFeat.Name

            If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
                Set swComp = swFeat.GetSpecificFeature2
                swComp.SetSuppression2 swComponentSuppressionState_e.swComponentFullyResolved
                Set swCompModel = swComp.GetModelDoc2
                swCompModel.ShowConfiguration2 swComp.ReferencedConfiguration
                TraverseAssyFeatures swCompModel, Rep & " > " & swFeat.Name
            End If
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
End Sub

Sub WriteIntoFile(logSTR As String)
    Dim My_fileNum As Integer
    My_fileNum = FreeFile
    Open FilePath For Append As #My_fileNum
    Print #My_fileNum, logSTR
    Close #My_fileNum
End Sub

 

4 Likes

Hello

Thank you very much!

I wanted to test this, but I have an error 91 on the following line:

                swCompModel.ShowConfiguration2 swComp.ReferencedConfiguration

object variable or block variable with not defined

The file is created but there is only one line in it

Do you have any idea of the origin of the problem? Thanks in advance!

This line is not necessarily necessary if the parts have only one config (or are saved in the config used in the assembly). So you can remove it.

However, it shouldn't cause an error. Verifies that the component is valid with:

            If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
                Set swComp = swFeat.GetSpecificFeature2
                If swComp is Nothing then Debug.print "Problème comp avec: " & swFeat.Name
                Set swCompModel = swComp.GetModelDoc2
                If swCompModel is Nothing then Debug.print "Problème modèle avec: " & swFeat.Name

                If Not swCompModel is Nothing then
                    'Debug.Print "Config Name: " & swComp.ReferencedConfiguration
                    'swCompModel.ShowConfiguration2 swComp.ReferencedConfiguration
                    TraverseAssyFeatures swCompModel, Rep & " > " & swFeat.Name
                End If
            End If

If you can send me the assembly with the part that is causing the problem, I'll look into it.

1 Like

I have the same error (91) on the line 

               Set swCompModel = swComp.GetModelDoc2

 

By shunting the lines of this block my file is generated.

This block is used to process subassemblies. If the assembly does not have subassemblies, you can leave it disabled, or use:

            If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
                Set swComp = swFeat.GetSpecificFeature2
                If swComp is Nothing then Debug.print "Problème comp avec: " & swFeat.Name
                If Not swComp is Nothing then
                Set swCompModel = swComp.GetModelDoc2
                If swCompModel is Nothing then Debug.print "Problème modèle avec: " & swFeat.Name

                If Not swCompModel is Nothing then
                    'Debug.Print "Config Name: " & swComp.ReferencedConfiguration
                    'swCompModel.ShowConfiguration2 swComp.ReferencedConfiguration
                    TraverseAssyFeatures swCompModel, Rep & " > " & swFeat.Name
                End If
                End If
            End If

Otherwise:

What does the "immediate" window say? (Menu View > Immediate)

I can't really help more without having the files.

 

I didn't find the "immediate" window... Is this VBA's execution window?

From what I've seen, the error occurs on the first part of the first subset in the first folder of the tree ("Comp problem with: 23495-4364-2")

The folder and subset appear in the file, but the part does not.

This window:

Again: "I can't really help more without having the files."

Sorry I can't send my assembly as is for privacy reasons. I think I'll do some tests with a test assembly, if I don't get out of it I'll send you this to deepen.

Thank you for your help in any case!

I don't need the design of the parts.

Make a copy of the assembly, open each of the parts, erase the functions, save.

See attached file. By the way, you can check with this assembly that it works.


asm.zip

Hello

The macro given at the beginning of the discussion works very well, you just have to set all the components to solved before launching it otherwise it bugs because the lightweight components are not accessible hence the error message "object variable or block variable with not defined".

Kind regards

2 Likes

Thank you d.roger.

Original code amended to include:

swComp.SetSuppression2 swComponentSuppressionState_e.swComponentFullyResolved

After: Set swComp = ...

1 Like

Thank you d.roger and JeromeP!

Unfortunately I always get an error message when running the macro, either after passing all the parts to solved or with the addition of the line (it crashes on the line in question).

That said, it works at first because I still have a dozen lines in the TSV file.

I'm going to test with parts "from scratch" to see if I have the same problems, I'll keep you posted!

Do you have the same problem with the assembly attached as an attachment 3 messages before?

Yes, the execution is stuck on the following line:

                swComp.SetSuppression2 swComponentSuppressionState_e.swComponentFullyResolved

 

In debug mode , I see that the value of swComponentSuppressionState_e.swComponentFullyResolved is 2. 

I just did a test with blank parts, the macro executes properly.

Do you think that this problem could be related to a configuration of the parts or to the use of the PDM?

Yes, it's probably coming from the PDM, to test do a get latest version on all your files to put them in your local cache...

The macros made to work with the PDM are not built in the same way at all, you have to rely on the Epdm APIs to connect to the vault, get the file in local cache, etc ... It's not quite the same job anymore...

1 Like

The value of 2 for swComponentSuppressionState_e.swComponentFullyResolved is normal.

The problem with SetSuppression2 can also be with the PDM. You can try swComp.SetSuppression2 swComponentSuppressionState_e.swComponentResolved

I did "get latest version" on all the files (and modified the JeromeP line of code), it exports well but it crashes on the 1st subset of the tree.