MACRO LIBRARY

Is component from design library

Free public access. No account or subscription required.

Files

Is component from design library

Code

Module:

'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
'www.cadsharp.com
    
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."
        Else
            Debug.Print "Component does not belong to Design Library."
        End If
    Else
        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
    Else
        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 = _
        swApp.GetUserPreferenceStringValue(swFileLocationsDesignLibrary)
    
    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