28 Replies Latest reply on Dec 12, 2018 9:18 PM by Eric Buckner

    Macro to get all sort of names in SW assembly

    Kishore K.

      Hi Folks.

       

      How to get all Part Names, configuration and derived configuration names from the SW assembly by using macro.

       

      Names should be copied to excel if possible

       

      Anyone has such kind of macro or an idea to develop? it will be great helpful for me to develop future.

       

      Kishore

        • Re: Macro to get all sort of names in SW assembly
          Ivana Kolin

          depends on what you want to automate => scan folder => open assembly => get parts? or do you have an assembly opened in SW and you want dump part information to excel. In first case look at documentmanager in second 2017 SOLIDWORKS API Help - Traverse Assembly at Component and Feature Level Example (VBA)

          • Re: Macro to get all sort of names in SW assembly
            Kishore K.

            Thanks for your quick reply.I'll check your shared information

            Below are my needs

             

            I've a open SW assembly from that we need to get the  following four information

            Column 1 : Part name

            Column  2 : Part Description

            Column  3 : Part Configuration name

            Column  4 : Part Derived configuration name

            • Re: Macro to get all sort of names in SW assembly
              Fifi Riri

              Not sure what is the "description" you speak of, but here is a macro that will create a CSV file in "C:\MyFolder" and write the rest of the information you want in it.

               

              Dim swApp As SldWorks.SldWorks

              Dim swAssy As SldWorks.AssemblyDoc

              Dim longstatus As Long, longwarnings As Long

               

              Sub main()

                  Set swApp = Application.SldWorks

                  Dim swConf As SldWorks.Configuration

                  Dim swRootComp As SldWorks.Component2

                  Set swAssy = swApp.ActiveDoc

                  Set swConf = swAssy.GetActiveConfiguration

                  Set swRootComp = swConf.GetRootComponent3(True)

                  WriteCSVFile "Part Name" & "," & "Part Config" & "," & "Alt. Config"

                  TraverseComponent swRootComp, 1

              End Sub

               

              Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

                  Dim vChilds As Variant, vChild As Variant

                  Dim swChildComp As SldWorks.Component2

                  Dim MyString As String

                  vChilds = swComp.GetChildren

                  For Each vChild In vChilds

                      Set swChildComp = vChild

                      Dim FileName As String

                      FileName = swChildComp.GetPathName

                      FileName = Left(FileName, InStr(FileName, ".") - 1)

                      FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))

                      Debug.Print "Part Name    : " & FileName

                      MyString = FileName

                     

                      Dim ActiveConfig As String

                      ActiveConfig = swChildComp.ReferencedConfiguration

                      Debug.Print "Configuration: " & ActiveConfig

                      MyString = MyString & "," & ActiveConfig

                     

                      FileName = swChildComp.GetPathName

                      If UCase(Right(FileName, Len(FileName) - InStr(FileName, "."))) = "SLDPRT" Then

                          Dim swPart As SldWorks.ModelDoc2

                          Set swPart = swApp.OpenDoc6(swChildComp.GetPathName, 1, 0, "", longstatus, longwarnings)

                          Dim ConfigNames() As String

                          Dim ConfigName As Variant

                          ConfigNames = swPart.GetConfigurationNames

                          For Each ConfigName In swPart.GetConfigurationNames

                              If ActiveConfig <> ConfigName Then

                                  Debug.Print "Alt. config  : " & ConfigName

                                  MyString = MyString & "," & ConfigName

                              End If

                          Next

                          swApp.CloseDoc swPart.GetTitle

                      End If

                      Debug.Print

                      WriteCSVFile MyString

                      TraverseComponent swChildComp, nLevel + 1

                  Next

              End Sub

               

              Sub WriteCSVFile(logSTR As String)

                  Dim My_filenumber As Integer

                  My_filenumber = FreeFile

                  Open "C:\MyFolder\MyList.csv" For Append As #My_filenumber

                      Print #My_filenumber, logSTR

                  Close #My_filenumber

              End Sub

                • Re: Macro to get all sort of names in SW assembly
                  Kishore K.

                  Hi Fifi Riri,

                   

                  Thanks for sharing the codes. You saved my time as well.

                   

                  I like to know is there any chance we can get only sheet metal parts in assembly and grab those information into .CSV?

                   

                  I got this code but I d'know how to play with your code.

                   

                  If swModel.GetBendState = swSMBendStateNone Then

                  End If

                   

                  Would you help me ?

                    • Re: Macro to get all sort of names in SW assembly
                      Fifi Riri

                      Enclose the writecsvfile in a if statement like so:

                           If swPart.GetBendState <> swSMBendStateNone Then

                                WriteCSVFile MyString

                           End If

                        • Re: Macro to get all sort of names in SW assembly
                          Kishore K.

                          Thanks Fifi Riri

                           

                          As you said I've tried the code its working fine.

                           

                          But still if we have any way to filter  the sheet metal parts in initial stage of the code  so I can avoid the timings for opening and closing the other parts.

                           

                          Since I'm working this macro in PDM for each parts will consume more time so I would like to know we can filter sheet-metal parts before opening?

                           

                          This there any way we skip the Checkout Popup window in the macro?

                            • Re: Macro to get all sort of names in SW assembly
                              Fifi Riri

                              You need to open the part to see if it's a bend part

                                • Re: Macro to get all sort of names in SW assembly
                                  Kishore K.

                                  https://forum.solidworks.com/message/515943#comment-515943

                                   

                                  Thanks Fifi Riri Please have a look on above link if you find some time.

                                   

                                  Macro looks likes, it is not opening the SW part but still it able to find whether parts are sheet metal.

                                   

                                  I'm very close to finalizing the macro and I agree with your point but please have a look and kindly let me know your feedback.

                                    • Re: Macro to get all sort of names in SW assembly
                                      Fifi Riri

                                      Good catch. I stand corrected.

                                      Here is the macro with some optimization

                                       

                                       

                                      Dim swApp As SldWorks.SldWorks

                                      Dim longstatus As Long, longwarnings As Long

                                       

                                      Sub main()

                                          Set swApp = Application.SldWorks

                                          Dim swAssy As SldWorks.AssemblyDoc

                                          Dim swConf As SldWorks.Configuration

                                          Dim swRootComp As SldWorks.Component2

                                          Set swAssy = swApp.ActiveDoc

                                          Set swConf = swAssy.GetActiveConfiguration

                                          Set swRootComp = swConf.GetRootComponent3(True)

                                          WriteCSVFile "Part Name" & "," & "Part Config" & "," & "Alt. Config"

                                          TraverseComponent swRootComp

                                      End Sub

                                       

                                      Sub TraverseComponent(swComp As SldWorks.Component2)

                                          Dim vChilds As Variant, vChild As Variant

                                          Dim swChildComp As SldWorks.Component2

                                          Dim MyString As String

                                          Dim FileName As String

                                          Dim swPart As SldWorks.ModelDoc

                                          Dim Bodies As Variant

                                          Dim swBody As SldWorks.Body2

                                          Dim ActiveConfig As String

                                          Dim ConfigNames() As String

                                          Dim ConfigName As Variant

                                        

                                          vChilds = swComp.GetChildren

                                          For Each vChild In vChilds

                                              Set swChildComp = vChild

                                              Set swPart = swChildComp.GetModelDoc2

                                              FileName = swPart.GetTitle

                                              FileName = Left(FileName, InStr(FileName, ".") - 1)

                                              Debug.Print "Part Name    : " & FileName

                                       

                                              If swPart.GetType = 1 Then

                                                  Bodies = swPart.GetBodies2(swBodyType_e.swAllBodies, True)

                                                  Set swBody = Bodies(0)

                                                  If swBody.IsSheetMetal = 1 Then

                                                      MyString = FileName

                                                      ActiveConfig = swChildComp.ReferencedConfiguration

                                                      Debug.Print "Configuration: " & ActiveConfig

                                                      MyString = MyString & "," & ActiveConfig

                                       

                                                      ConfigNames = swPart.GetConfigurationNames

                                                      For Each ConfigName In swPart.GetConfigurationNames

                                                          If ActiveConfig <> ConfigName Then

                                                              Debug.Print "Alt. config  : " & ConfigName

                                                              MyString = MyString & "," & ConfigName

                                                          End If

                                                      Next

                                                      Debug.Print

                                                      WriteCSVFile MyString

                                                  End If

                                              Else

                                                  TraverseComponent swChildComp

                                              End If

                                          Next

                                      End Sub

                                       

                                      Sub WriteCSVFile(logSTR As String)

                                          Dim My_filenumber As Integer

                                          My_filenumber = FreeFile

                                          Open "C:\MyList.csv" For Append As #My_filenumber

                                              Print #My_filenumber, logSTR

                                          Close #My_filenumber

                                      End Sub

                                        • Re: Macro to get all sort of names in SW assembly
                                          Eric Buckner

                                          Great bit of code here, thank you for posting it up..

                                           

                                          I've adapted it a bit for pulling assembly and bom info but strangely it works once, providing all the data I want, then the next pass it doesn't work well at all, only commas are returned. Here's a screen shot of the immediate window after running twice consecutively. It's an almost perfect every other run that it fails and I cannot see any reason for the very few consecutive successes.

                                           

                                           

                                          Can you imagine why this is happening? I'm not using your traverse code (yet) so maybe the writecsv section needs to be modofied?

                                           

                                          Here's a sample of the code...

                                           

                                          Option Explicit

                                          Dim swApp As SldWorks.SldWorks

                                          Dim swModel As DrawingDoc

                                          Dim frstView As SldWorks.View

                                          Dim refdoc As SldWorks.ModelDoc2

                                          Dim valoutnum As String

                                          Dim valoutrev As String

                                          Dim valoutdesc As String

                                          Dim valoutnotes As String

                                          Dim valoutmfgn As String

                                          Dim valoutsource As String

                                          Dim valoutitemt As String

                                          Dim WriteString As String

                                           

                                          Sub main()

                                          Set swApp = Application.SldWorks

                                          Set swModel = swApp.ActiveDoc

                                          Set frstView = swModel.GetFirstView

                                          Set frstView = frstView.GetNextView

                                          Set refdoc = frstView.ReferencedDocument

                                           

                                          WriteCSVFile "ANumber" & "," & "ARevision" & "," & "ADescription" & "," & "ANotes" & "," & "AMFG Number" & "," & "AItem Type" & "," & "ASource"

                                           

                                          valoutnum = refdoc.GetCustomInfoValue(valoutnum, "Number")

                                          WriteString = valoutnum

                                          Debug.Print "Evaluated value:          " & WriteString

                                          ......

                                          valoutsource = refdoc.GetCustomInfoValue(valoutsource, "Source")

                                          WriteString = WriteString & "," & valoutsource

                                          Debug.Print "Evaluated value:          " & WriteString

                                           

                                          Debug.Print

                                          WriteCSVFile WriteString

                                          End Sub

                                           

                                          Sub WriteCSVFile(logSTR As String)

                                              Dim My_filenumber As Integer

                                              My_filenumber = FreeFile

                                              Open "C:\MyFolder\MyList.csv" For Append As #My_filenumber

                                                  Print #My_filenumber, logSTR

                                              Close #My_filenumber

                                          End Sub

                            • Re: Macro to get all sort of names in SW assembly
                              David Matula

                              does a bom do this if you ask to use the configuration name as the part # or something like that?

                              a list of all files can be gotten from file find references.

                              • Re: Macro to get all sort of names in SW assembly
                                Francisco Martínez

                                I just wanted to add that you can get this info a different way by

                                 

                                1. Creating a drawing file and inserting a bom with the required fields in the order you want

                                 

                                2. export csv from that bom ( click on bom , save as , csv or excel)

                                 

                                 

                                This is how I am getting my data to our older system right now, it requires 5 or 6 fields and this has worked great for me.

                                • Re: Macro to get all sort of names in SW assembly
                                  Kishore K.

                                   

                                  Hi Fifi Riri,

                                   

                                  Thanks for looking into the post and helped me.

                                   

                                  Today I saw your code I was happy to run but above error makes me to stop I dknow how to solve this error is there anything wrong in the SW files?

                                    • Re: Macro to get all sort of names in SW assembly
                                      Fifi Riri

                                      you don't really need the line FileName = Left(FileName, InStr(FileName, ".") - 1)

                                      you can remove it. it's just that the name of the part in excel will appear with sldprt at the end

                                       

                                      if you want to investigate try to see if the part filename is valid:

                                      insert : Debug.Print "Part Name    : " & FileName

                                      before the line FileName = Left(FileName, InStr(FileName, ".") - 1)

                                      it should give something like: "part1.sldprt"

                                       

                                    • Re: Macro to get all sort of names in SW assembly
                                      Kishore K.

                                      I followed your comments I didn't get any file names but I tried with " MsgBox Debug.Print "Part Name    : " & FileName

                                      then I got file name.

                                       

                                      Again same error on the line FileName = Left(FileName, InStr(FileName, ".") - 1)

                                       

                                       

                                      • Re: Macro to get all sort of names in SW assembly
                                        Kishore K.

                                        Sorry Ivana Kolin and Fifi Riri to make you trouble. There is an error in assembly I think later I tried with small assembly its working fine.

                                         

                                        I've to check the large assembly if I get same error I'll try to upload here to understand more.