MACRO LIBRARY

Get material of selected component

Free public access. No account or subscription required.

Files

Get material of selected component

Code

Module: mMain

'Demonstrates how to get the name of the material of a selected component.
    
'Preconditions: Assembly is open and component is selected.
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swComp As SldWorks.Component2
    Dim swCompModel As SldWorks.ModelDoc2
    Dim swCompPart As SldWorks.PartDoc
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then Exit Sub
    If swModel.GetType <> swDocASSEMBLY Then Exit Sub
    
    Set swSelMgr = swModel.SelectionManager
    Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
    Set swCompModel = swComp.GetModelDoc2
    
    If swCompModel Is Nothing Then
        MsgBox "Cannot access component model, perhaps due to it being suppressed or lightweight."
        Exit Sub
    End If
    
    Set swCompPart = swCompModel
    MsgBox swCompPart.GetMaterialPropertyName2(swCompModel.ConfigurationManager.ActiveConfiguration.Name, Empty)
End Sub