7 Replies Latest reply on Sep 16, 2016 8:39 AM by Cuinn Herrick

    Macro: Is file from the Design Library?

    Cuinn Herrick

      Hi all, I am currently modifying Adam Hoffman's clean configuration macro so that it excludes cleaning design library items. Although I am currently having trouble nutting out the best way to assassin if parts and components come from the design library. Has anyone tried this before? So far I written 2 subs.


      Array of Design Library Addresses Macro:



      Sub DesignLibraryList()  'Get list of users Design Library's (Content Manager Folders)
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Dim vsplit() As String
          Dim DL_REG_KEY As String
          vsplit = Split(swApp.RevisionNumber, ".") 'Get Solidworks Version Year
          DL_REG_KEY = "HKEY_CURRENT_USER\Software\Solidworks\Solidworks " & CStr(1992 + vsplit(0)) & "\ExtReferences\Content Manager Folders"
          'Debug.Print DL_REG_KEY
          On Error Resume Next
          Dim wsh As Object
          Set wsh = CreateObject("WScript.Shell") 'access Windows scripting
          RegKeyRead = wsh.RegRead(DL_REG_KEY) 'read key from registry
          vsplit() = Split(RegKeyRead, ";") 'Seperate strings
          For i = 0 To UBound(vsplit, 1)
          Debug.Print vsplit(i)
          Next i
      End Sub


      File Address Macro:


      Sub CurrentFileLocation()
          Dim swApp                       As SldWorks.SldWorks
          Dim swModel                     As SldWorks.ModelDoc2
          Dim swPart                      As SldWorks.PartDoc
          Dim swAssy                      As SldWorks.AssemblyDoc
          Dim bRet                        As Boolean
          Dim MyPath                      As String
          Dim MyFolder                    As String
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          If swModel.GetType() = swDocumentTypes_e.swDocPART Then
              Set swPart = swModel
              Set swAssy = swModel
          End If
          MyFolder = CurDir$
          MyPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)
          Debug.Print "File = " & swModel.GetPathName
          Debug.Print "Folder = " & MyPath
      End Sub


      Any help would be appreciated.

        • Re: Macro: Is file from the Design Library?
          John Alexander

          Your first sub is querying a registry value of the current SolidWorks session for a semicolon delimited list of design library directories and then parsing it into an array of strings.


          Your second sub is getting a reference to the specific swPart or swAssy (which appears to be unnecessary) and then splitting the current model document's full path into a directory and filename.


          To check that the active document is a design library part, check if any of the design library directories stored in vsplit() are left-oriented sub-strings of the MyFolder value.


          I believe something like this would do the job:


          flag_found = false

          For each directory in vsplit()

               if InStr(1, MyFolder, directory, CompareMethod.text) = 1 then

                    flag_found = true

                    exit for

               end if

          next directory


          You can get rid of the portion of the second sub that checks the document type.

          1. If swModel.GetType() = swDocumentTypes_e.swDocPART Then 
          2.         Set swPart = swModel 
          3.     Else 
          4.         Set swAssy = swModel 
          5.     End If


          • Re: Macro: Is file from the Design Library?
            Cad Admin

            Are you referring to toolbox type components...


            If so use swToolBoxPartType_e to determine part file type.  Use that bases to add or exclude the part file...

            2016 SOLIDWORKS API Help - swToolBoxPartType_e Enumeration

            • Re: Macro: Is file from the Design Library?
              Scott Stuart

              That entire first sub is not necessary. Just add this line to the second sub:

              LibraryPaths = swApp.GetUserPreferenceStringValue(swFileLocationsDesignLibrary)



              It's not even necessary to split the paths into and array and loop through the array. All you need is something like this:

                   If instr(LibraryPaths, MyPath) Then DesignLibraryFile = True

              • Re: Macro: Is file from the Design Library?
                Keith Rice
                'Demonstrates how to determine if a component belongs to the Design Library.
                'Preconditions: Component is selected.
                'Results: See immediate window.
                'Written by Keith Rice
                'CADSharp LLC
                Sub main()
                    Dim swApp As SldWorks.SldWorks
                    Dim swModel As SldWorks.ModelDoc2
                    Dim swSelMgr As SldWorks.SelectionMgr
                    Dim swComp As SldWorks.Component2
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Set swSelMgr = swModel.SelectionManager
                    Set swComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)
                    Dim result As Boolean
                    If IsDesignLibraryComponent(swComp.GetModelDoc2, result) Then
                        If result Then
                            Debug.Print "Component belongs to Design Library."
                            Debug.Print "Component does not belong to Design Library."
                        End If
                        Debug.Print "Failed to determine if component belongs to Design Library."
                    End If
                End Sub
                Private Function IsDesignLibraryComponent(compModel As SldWorks.ModelDoc2, _
                    ByRef result As Boolean) As Boolean
                    If compModel Is Nothing Then
                        IsDesignLibraryComponent = False
                        Exit Function
                        IsDesignLibraryComponent = True
                    End If
                    Dim swApp As SldWorks.SldWorks
                    Dim strDesignLibraryFolderList As String
                    Dim vSplit As Variant
                    Dim strModelFolder As String
                    Dim i As Integer
                    Set swApp = Application.SldWorks
                    strDesignLibraryFolderList = _
                    If compModel.GetPathName = Empty Then
                        result = False
                        Exit Function
                    End If
                    strModelFolder = Left(compModel.GetPathName, InStrRev(compModel.GetPathName, "\"))
                    vSplit = Split(strDesignLibraryFolderList, ";")
                    For i = 0 To UBound(vSplit)
                        If InStr(1, strModelFolder, vSplit(i), vbTextCompare) = 1 Then
                            result = True
                            Exit Function
                        End If
                    Next i
                    result = False
                End Function



                SolidWorks API Training and Services