9 Replies Latest reply on Aug 27, 2014 9:04 AM by Roland Schwarz

    How can I find the drawings for parts of an assembly using the API without opening SW?

    Courtney Milligan

      I have a program to replace/rename the references of any SW file. It uses ReplaceReference (ISwDMDocument) and GetAllExternalReferences4 (SwDocumentMgr). Neither of these requires SW to be open which greatly enhances the speed at which the program runs, and increases its stability greatly when processing large batches.

      The program works fine when the user start with a drawing. It drills down through the references. The problem begins when the you start with a part or assembly. The GetAllExternalReferences4 will tell me all the reference under the assembly, but I also need to find those references drawings. The part sub component and its drawing both need to be renamed, not just the part.


      I saw somethings with the swPackAndGo that looked interesting, but I am pretty sure you have to open the object in SW to use it. I want to use something more like the pack and go integrated in the Windows shell, that shows up on the right click menu in Explorer.


      Does anyone have any ideas?

        • Re: How can I find the drawings for parts of an assembly using the API without opening SW?
          Simon Turner

          A part file does not contain any reference to the drawing.

          You must find all drawing files and check their references to see if they reference the part file.

          It is up to you to define which folders to search in. Usually, just the folder containing the part file is sufficient.

            • Re: How can I find the drawings for parts of an assembly using the API without opening SW?
              Courtney Milligan

              I am aware the part or assembly do not contain reference information to the drawing, The Drawing stores the references to the part or assembly. I am looking for an API that basically does the same function as the pack and go (which searches for any items in the current directory that reference the item it was run against), but must happen in the same manner as the one that appears in the right click menu in the Windows Explorer. It must happen without opening Solidworks.


              In thinking about it last night though I think it is probably better to write my own function to search the directory for all .slddrw and use the GetAllExternalReferences4. If I find a function provided by SW that performs the pack and go functionality, it will be easier than making my own (which is why I asked), but it will take much longer to run. If I write my own, I only need to query the directory once and write the filename and references to an array/collection. If I use a SW function, it will query the contents of the directory and find that data's dependents every time it is called, which would happen for each sub-component of an assembly. For large assemblies it might query that directory hundreds of times.

            • Re: How can I find the drawings for parts of an assembly using the API without opening SW?
              Courtney Milligan

              Ronald, Interesting application, but I don't need to search the world, just the folder the parent file is in. Could be useful in the future though.


              Ivana, I ran into that bug developing a similar application, very annoying. I reported it to SW, but doubt they will ever fix it. I seem to recall there was more to it than it reports the wrong value, but I cannot remember enough. I recall it being with the SwDMDocument16.Save  or SaveAs commands. That it was not actually saving the changes. Thanks for the reminder though, I had forgotten about it and will be watching for it.


              Turns out it was really easy to just write my own (VB.Net). I have posted portions below for reference. the array DrwRefs will be filled with the drawing filename and the second column will container its references. I can then search it for matches to the dependents found inside the file the user actually opened. It might be more efficient to use a collection here instead of an array, but is not something I have learned yet.


                      FolderContents = System.IO.Directory.GetFiles(swParentFolder, "*.slddrw")

                      ReDim Preserve DrwRefs(FolderContents.Length, 20)      'Assumes a maximum of 20 dependents for each drawing

                      For Each slddrw In FolderContents

                          swChildren = swGetRefs(slddrw)          'Function that gets the references of any SW file.

                          For Each Ref In swChildren

                              DrwRefs(i, j) = Ref

                              j = j + 1


                          i = i + 1



                 Function swGetRefs(sDocFileName As String) As Object

                      Const sLicenseKey As String = "key as obtained from SW"

                      Dim swClassFact As SwDMClassFactory
                      Dim swDocMgr As SwDMApplication3
                      Dim swDoc As SwDMDocument16
                      Dim swSearchOpt As SwDMSearchOption
                      Dim nDocType As Long
                      Dim nRetVal As Long
                      Dim vBrokenRefs As Object
                      Dim vIsVirtuals As Object
                      Dim vTimeStamps As Object
                      Dim vDependArr As Object

                      vBrokenRefs = Nothing
                      vIsVirtuals = Nothing
                      vTimeStamps = Nothing

                      'On Error GoTo GetRefFiles_Error

                      '==========Determine what type of file=================
                      Select Case Right(LCase(sDocFileName), 7)
                          Case ".slddrw"
                              nDocType = SwDmDocumentType.swDmDocumentDrawing
                          Case ".sldprt"
                              nDocType = SwDmDocumentType.swDmDocumentPart
                          Case ".sldasm"
                              nDocType = SwDmDocumentType.swDmDocumentAssembly
                          Case Else
                              ' Not a drawing, assembly or part file,
                              nDocType = SwDmDocumentType.swDmDocumentUnknown
                              ' so do not open
                              swGetRefs = "Error"
                              Exit Function
                      End Select
                      '==============Find references==========================
                      swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory")
                      swDocMgr = swClassFact.GetApplication(sLicenseKey)
                      swSearchOpt = swDocMgr.GetSearchOptionObject
                      swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)

                      If nRetVal = 1 Then
                          MsgBox("File Missing", vbOKOnly)
                          swGetRefs = vbNull
                      End If
                      vDependArr = swDoc.GetAllExternalReferences4(swSearchOpt, vBrokenRefs, vIsVirtuals, vTimeStamps)
                      swGetRefs = vDependArr
                      Exit Function
                      'MsgBox "Error " & err.Number & " (" & err.Description & ") in procedure GetRefFiles of Module Module1"
                  End Function