ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
AJAlfred Jelinek17/10/2016

I found a wonderful Macro that processes and removes the body appearance on all components (*.sldprt) in an open assembly or on an open part.

I have been trying to alter it to have two other macros:

1) To process and remove appearances on all faces of a part or all components (*.sldprt) of an assembly.

2) To process and remove appearances on all features of parts or all components (*.sldprt) of an assembly.

I have figured out how to remove all appearances (part, body, feature, faces) from an open part (Which is too much and need more control as to what appearances I can remove), how to remove appearances added on parts at assembly level, and the following code which removes all appearances for bodies on a part or all components in an assembly.

Any help in altering this code to meet #1 and #2 above would be greatly appreciated as I have hit a wall and could use some advice.... IBody2, GetFaces...ahhhhh

Attached is the test assembly and parts with appearances assigned at all the levels. Macro code for body I want to alter is below.

If anyone would like any of the other appearance removal macros I mentioned above please let me know, I found most of them in the SW API help files and on forums then altered some to suit my needs.

Thanks to @Keith Rice and @Deepak Gupta and many others for all their help in the past, I have learned a lot.

Sincerely,

Al

'---paste code into a module

'---beginning of code

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

'---end of code