8 Replies Latest reply on Mar 21, 2018 6:49 PM by Wiktor Wojdyna

    Is it possible to determine where a part is used without using the Document Manager API?

    Nick Sinno

      Im currently working on a macro to set a number of custom properties to various parts and assemblies from their top-level assembly. My sticking point is that, when cycling through components of the top level assembly, it brings up all components of sub-assemblies, too. This is fine except that I need to get some information from the sub-assemblies as well to insert into their components.

       

      My research is all pointing to the Document Manager's ApI (which I do not have access to.) and the "Whereused" function. Is there any other way to get this information? Or do I have to selectively run through sub-assemblies and use recursion?

       

      Thank you

        • Re: Is it possible to determine where a part is used without using the Document Manager API?
          Josh Brady

          There is nothing that you can get with the Document Manager that you can't get from SolidWorks.  If you have the top level assembly open you can access information from any subassebly or component.

          • Re: Is it possible to determine where a part is used without using the Document Manager API?
            Keith Rice

            As Josh alluded to, the value of the Document Manager isn't that it does things that the SolidWorks API can't do, but rather that it does these things much faster and with much less code.

             

            Your first paragraph is unclear to me, honestly, so I can't offer any specific recommendations. Nevertheless, I would avoid trying to get Where Used references via the SolidWorks API if possible since it is quite complicated and time consuming to do.

             

            Keith

            SolidWorks API Training

            • Re: Is it possible to determine where a part is used without using the Document Manager API?
              Nick Sinno

              Josh and Keith,

               

              I am attempting to use the IComponent2::GetParent Method. What I want to achieve is to open any random part, and be able to tell what assembly it is used in.

               

              <code>

                  If swModel.GetType = swDocASSEMBLY Then

                      Set swAssy = swModel

                      Set swselmgr = swModel.SelectionManager

                      Set swconfmgr = swModel.ConfigurationManager

                      Set swconf = swconfmgr.ActiveConfiguration

                      Set swComp = swconf.GetRootComponent3(True)

                    

                      For i = 0 To UBound(swAssy.GetComponents(False))

               

               

                          On Error Resume Next

                          If Left(swAssy.GetComponents(False)(i).GetPathName, InStr(swAssy.GetComponents(False)(i).GetPathName, "-")) = _ Left(swAssy.GetPathName, InStr(swAssy.GetPathName, "-")) Then

                              Filename = swAssy.GetComponents(False)(i).GetPathName

                        

                              Set Part = swApp.ActivateDoc3(Filename, True, 1, lErrors)

                              Set swpartdoc = swApp.ActiveDoc

                            

                              Set swparentcomp = swconf.GetParent

                              If Nothing Is swparentcomp Then

                                  Set swparentcomp = swconf.GetRootComponent

                              End If

                              Debug.Print swparentcomp.Name2

                              Debug.Print Part.GetTitle

              </code>

               

              This is the section of code I am currently working with, setting swconf to get its parent. This is taken right from the GetParent VBA example and modified to fit where i needed it.

                • Re: Is it possible to determine where a part is used without using the Document Manager API?
                  Keith Rice

                  What I want to achieve is to open any random part, and be able to tell what assembly it is used in.

                   

                  For part X: for every folder in which you think an assembly resides that references part X, you'd have to open up each assembly and run IModelDocExtension::ListExternalFileReferences on each one, noting whether part X is listed.

                   

                  To be honest I'm not totally sure what the advantage of your current approach would be unless you only care to search in one single assembly for all possible Where Used references. Even then, you don't need to use IComponent2::GetParents. Just get an array of all components in the assembly using IAssemblyDoc::GetComponents and then run IModelDocExtension::ListExternalFileReferences on the assembly components only.

                   

                  On another note:

                   

                  With very few exceptions, you should never, ever use "On Error Resume Next". I would recommend you banish that phrase from your VBA vocabulary because it will only result in bad software. Use "On Error GoTo <line>" instead to go to a place in the program where you can properly handle the issue.

                   

                  Keith

                  SolidWorks API Tutorials

                    • Re: Is it possible to determine where a part is used without using the Document Manager API?
                      Nick Sinno

                      Keith Rice wrote:

                      unless you only care to search in one single assembly for all possible Where Used references.

                       

                      Effectively, yes, thats exactly what I am concerned with in this case. Using IModelDocExtension::ListExternalFileReferences would allow me to open the files and retrieve any custom properties they contain?

                       

                      As for the Resume Next, I do try to steer away from it, I really only use it during development when I cant figure out whats causing an error (and thus, dont know what to do with it!) Its typically gone once the program is "finished"

                        • Re: Is it possible to determine where a part is used without using the Document Manager API?
                          Keith Rice

                          No, that won't let you get custom properties. But once you have the Where Used list (say, a list of file paths), you can open up those models one by one and modify their custom properties.

                           

                          OK, glad you know about the pitfalls of On Error Resume Next.

                           

                          If one of my answers answered your original question, would you please mark it as correct? I would appreciate it.

                           

                          Keith

                          SolidWorks API Tutorials

                            • Re: Is it possible to determine where a part is used without using the Document Manager API?
                              Nick Sinno

                              Does IModelDocExtension::ListExternalFileReferences consider assemblies a reference? I ran the example code from the help article, and it returned zero references on a part from an assembly.

                               

                              I have finally managed to get my code working with the getParent call. I was apparently missing a few error checks....

                               

                                  If swModel.GetType = swDocASSEMBLY Then

                                      Set swAssy = swModel

                                      Set swselmgr = swModel.SelectionManager

                                      Set swconfmgr = swModel.ConfigurationManager

                                      Set swconf = swconfmgr.ActiveConfiguration

                                     

                                      For i = 0 To UBound(swAssy.GetComponents(False))

                                          If CInt(Right(swAssy.GetComponents(False)(i).Name2, Len(swAssy.GetComponents(False)(i).Name2) - InStrRev(swAssy.GetComponents(False)(i).Name2, "-"))) > 1 Then

                                              GoTo skippage

                                          End If

                                         

                                          If Left(swAssy.GetComponents(False)(i).GetPathName, InStr(swAssy.GetComponents(False)(i).GetPathName, "-")) = Left(swAssy.GetPathName, InStr(swAssy.GetPathName, "-")) Then

                                              Filename = swAssy.GetComponents(False)(i).GetPathName

                                              Set Part = swApp.ActivateDoc3(Filename, True, 1, lErrors)

                                              If Part.GetType <> swDocASSEMBLY Then

                                                  Set swpartdoc = swApp.ActiveDoc

                                              End If

                                              Set swComp = swAssy.GetComponents(False)(i)

                                             

                                              Set swparentcomp = swComp.GetParent

                                              If Nothing Is swparentcomp Then

                                                  Set swparentcomp = swconf.GetRootComponent

                                              End If