Hide Table of Contents

Remove Material Properties from Bodies Example (VBA)

This example shows how to remove materials from bodies in a multibody part. This example also works with a part with a single body and an assembly with single and multibody components.

'---------------------------------------------------------------
' Preconditions:
' 1. Open public_documents\samples\tutorial\multibody\multi_inter.sldprt.
' 2. Expand Solid Bodies(2) > right-click Extrude-Thin1 > click 
'    Appearances > Body > any color in Appearances(color) > OK.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Traverses the FeatureManager design tree.
' 2. Removes the color that you applied to Extrude-Thin1.
' 3. Examine the Immediate window and graphics area.
'
' NOTE: Because the part is used elsewhere, do not save changes.
'---------------------------------------------------------------
Option Explicit
Dim bRet As Boolean
Sub ProcessComponent(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swComp As SldWorks.Component2)
    Dim vChildComp As Variant
    Dim swChildComp As SldWorks.Component2
    Dim vBody As Variant
    Dim childComp As Variant  
    Debug.Print swComp.Name2 & " <" & swComp.ReferencedConfiguration & ">"
    ' Solid bodies
    Dim vBodyArr As Variant
    Dim swBody As Body2
    vBodyArr = swComp.GetBodies2(swSolidBody)
    If Not IsEmpty(vBodyArr) Then
        Debug.Print "  Number of bodies: " & UBound(vBodyArr) + 1
        For Each vBody In vBodyArr
            Set swBody = vBody
            Dim vConfigName As Variant
            Debug.Print "    Body name: " & swBody.Name
            vConfigName = swModel.GetConfigurationNames
            bRet = swBody.RemoveMaterialProperty(swThisConfiguration, (vConfigName))
            Debug.Print "      Material removed from body? " & bRet
        Next
    End If
    vChildComp = swComp.GetChildren
    For Each childComp In vChildComp
        Set swChildComp = childComp
        ProcessComponent swApp, swModel, swChildComp
    Next
End Sub
Sub ProcessAssembly(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2)
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Set swConfigMgr = swModel.ConfigurationManager
    Set swConf = swConfigMgr.ActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
    ProcessComponent swApp, swModel, swRootComp
End Sub
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim vBody As Variant
    Dim j As Long
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    swModel.ClearSelection2 True
    Debug.Print "File = " & swModel.GetPathName
    Select Case swModel.GetType
        Case swDocPART
            Set swPart = swModel
            ' Solid bodies
            Dim vBodyArr As Variant
            Dim swBody As Body2
            vBodyArr = swPart.GetBodies2(swSolidBody, True)
            If Not IsEmpty(vBodyArr) Then
                Debug.Print "  Number of bodies: " & UBound(vBodyArr) + 1
                Debug.Print "    Material removed from: "
                j = 1
                For Each vBody In vBodyArr
                    Set swBody = vBody
                    Dim vConfigName As Variant
                    vConfigName = swModel.GetConfigurationNames
                    bRet = swBody.RemoveMaterialProperty(swAllConfiguration, (vConfigName))
                    Debug.Print "      Body " & j & "? " & bRet
                    j = j + 1
                Next
            End If
        Case swDocASSEMBLY
            ProcessAssembly swApp, swModel
        Case Else
            Exit Sub
    End Select
End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Remove Material Properties From Bodies Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2021 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.