21 Replies Latest reply on Apr 15, 2017 2:12 AM by Deepak Gupta

    macro to hide all visible surfaces in assembly

    Steven Marcus

      I'm trying to piece together a marco which goes through the entire assembly and hides all surface bodies.

       

      Here's what I have

       

      [Code]

      Dim swApp As SldWorks.SldWorks

       

          Dim swModel As ModelDoc2

          Dim swPart As IPartDoc

          Dim Bodies As Variant

          Dim swBody As IBody2

       

       

       

          Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

        

       

          Bodies = swPart.GetBodies2(swBodyType_e.swSheetBody, True)

        

          For Each swBody In Bodies

              swBody.HideBody (True)

          Next

        

        

        

        

          End Sub

       

      [/Code]

        • Re: macro to hide all visible surfaces in assembly
          Christian Chu

          bodies, swpart and swbody haven't been set in the macro - how you run this?

          You might want to check this out

          • Re: macro to hide all visible surfaces in assembly
            Deepak Gupta

            Why you want to traverse all bodies. Why not simply get the surface body folder and hide in one shot (per part)

            • Re: macro to hide all visible surfaces in assembly
              Steven Marcus

              The following code I got to work which works in a part only, not a complete assembly.

               

              Dim swApp As Object

              Dim model As ModelDoc2

              Dim part As PartDoc

              Dim BodyArr As Variant

              Dim swBody As Body2

               

              Sub main()

               

              Set swApp = Application.SldWorks

               

              Set model = swApp.ActiveDoc

              Set part = model

               

              BodyArr = part.GetBodies2(swSheetBody, False)

               

              Dim Cnt As Integer

              For Cnt = 0 To UBound(BodyArr)

                  Set swBody = BodyArr(Cnt)

                  If Not swBody Is Nothing Then

                      swBody.HideBody (True)

                     

                  End If

              Next Cnt

               

              End Sub

                • Re: macro to hide all visible surfaces in assembly
                  Deepak Gupta

                  Steven, try these codes. Have only tested one level deep. These are little slow but works. I'm also trying the surface folder route.

                   

                  Option Explicit
                  Dim swApp                       As SldWorks.SldWorks
                  Dim swModel                    As ModelDoc2
                  Dim swAssembly             As AssemblyDoc
                  Dim swComponent          As Component2
                  Dim BodyArr                    As Variant
                  Dim swBody                    As Body2
                  Dim vComponents          As Variant
                  Dim i                               As Integer
                  Dim j                               As Integer
                  
                  Sub main()
                  
                  Set swApp = Application.SldWorks
                  Set swAssembly = swApp.ActiveDoc
                  TransverseComponents swAssembly
                  MsgBox "Mission Accomplished!!"
                  
                  End Sub
                  Sub TransverseComponents(swAssy As AssemblyDoc)
                  
                      vComponents = swAssy.GetComponents(True)
                      For i = 0 To UBound(vComponents)
                          Set swComponent = vComponents(i)
                          Set swModel = swComponent.GetModelDoc2
                         
                          If swModel.GetType = swDocASSEMBLY Then
                              Set swAssembly = swModel
                              TransverseComponents swAssembly
                          End If
                          
                          BodyArr = swComponent.GetBodies3(swSheetBody, False)
                          
                          For j = 0 To UBound(BodyArr)
                            Set swBody = BodyArr(j)
                              If Not swBody Is Nothing Then
                                  swBody.HideBody (True)
                              End If
                          Next j
                      Next i
                      
                  End Sub
                  
                • Re: macro to hide all visible surfaces in assembly
                  Steven Marcus

                  This code works however its not clean since I took code that selected all bodies done by SW and put in my one line of code to hide the visible surface bodies.

                   

                  ------ CODE --------

                   

                  '----------------------------------------

                   

                   

                  '

                   

                   

                  ' Preconditions: Part or assembly is open.

                   

                   

                  '

                   

                   

                  ' Postconditions: All solid and surface bodies are selected.

                   

                   

                  '

                   

                   

                  '----------------------------------------

                   

                   

                  Option Explicit

                   

                   

                  Public Enum swDocumentTypes_e

                   

                   

                      swDocNONE = 0       '  Used to be TYPE_NONE

                   

                   

                      swDocPART = 1       '  Used to be TYPE_PART

                   

                   

                      swDocASSEMBLY = 2   '  Used to be TYPE_ASSEMBLY

                   

                   

                      swDocDRAWING = 3    '  Used to be TYPE_DRAWING

                   

                   

                  End Enum

                   

                   

                  Public Enum swBodyType_e

                   

                   

                      swSolidBody = 0

                   

                   

                      swSheetBody = 1

                   

                   

                      swWireBody = 2

                   

                   

                      swMinimumBody = 3

                   

                   

                      swGeneralBody = 4

                   

                   

                      swEmptyBody = 5

                   

                   

                  End Enum

                   

                   

                  Sub SelectBodies( _

                      swApp As SldWorks.SldWorks, _

                      swModel As SldWorks.ModelDoc2, _

                      vBody As Variant, _

                      sPadStr As String _

                  )

                   

                   

                      Dim swModExt                    As SldWorks.ModelDocExtension

                   

                   

                      Dim swBody                      As SldWorks.Body2

                   

                   

                      Dim sBodySelStr                 As String

                   

                   

                      Dim sBodyTypeSelStr             As String

                   

                   

                      Dim i                           As Long

                   

                   

                      Dim bRet                        As Boolean

                   

                   

                     

                      If IsEmpty(vBody) Then Exit Sub

                   

                   

                      Set swModExt = swModel.Extension

                   

                   

                     

                      For i = 0 To UBound(vBody)

                   

                   

                          Set swBody = vBody(i)

                   

                   

                         

                          'sBodySelStr = swBody.GetSelectionId

                   

                   

                         

                          'Debug.Print "  " & sPadStr & sBodySelStr

                   

                   

                         

                          Select Case swBody.GetType

                   

                   

                              Case swSolidBody

                   

                   

                                  sBodyTypeSelStr = "SOLIDBODY"

                   

                   

                                 

                              Case swSheetBody

                   

                   

                                  sBodyTypeSelStr = "SURFACEBODY"

                                  swBody.HideBody (True)

                                 

                              Case Else

                   

                   

                                  Debug.Assert False

                   

                   

                          End Select

                   

                   

                         

                          'bRet = swModExt.SelectByID2(sBodySelStr, sBodyTypeSelStr, 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert bRet

                   

                   

                      Next i

                   

                   

                  End Sub

                   

                   

                  Sub ProcessComponent( _

                      swApp As SldWorks.SldWorks, _

                      swModel As SldWorks.ModelDoc2, _

                      swComp As SldWorks.Component2, _

                      nLevel As Long _

                  )

                   

                   

                      Dim vChildComp                  As Variant

                   

                   

                      Dim swChildComp                 As SldWorks.Component2

                   

                   

                      Dim swCompConfig                As SldWorks.Configuration

                   

                   

                      Dim sPadStr                     As String

                   

                   

                      Dim vBody                       As Variant

                   

                   

                      Dim i                           As Long

                   

                   

                     

                      For i = 0 To nLevel - 1

                   

                   

                          sPadStr = sPadStr + "  "

                   

                   

                      Next i

                   

                   

                      Debug.Print sPadStr & swComp.Name2 & " <" & swComp.ReferencedConfiguration & ">"

                   

                   

                      ' Solid bodies

                   

                   

                      vBody = swComp.GetBodies2(swSolidBody)

                   

                   

                      'SelectBodies swApp, swModel, vBody, sPadStr

                   

                   

                     

                      ' Surface bodies

                   

                   

                      vBody = swComp.GetBodies2(swSheetBody)

                   

                   

                      SelectBodies swApp, swModel, vBody, sPadStr

                     

                   

                   

                     

                      vChildComp = swComp.GetChildren

                   

                   

                      For i = 0 To UBound(vChildComp)

                   

                   

                          Set swChildComp = vChildComp(i)

                   

                   

                         

                          ProcessComponent swApp, swModel, swChildComp, nLevel + 1

                   

                   

                      Next i

                   

                   

                  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.GetRootComponent

                   

                   

                      ProcessComponent swApp, swModel, swRootComp, 1

                   

                   

                  End Sub

                   

                   

                  Sub main()

                   

                   

                      Dim swApp                       As SldWorks.SldWorks

                   

                   

                      Dim swModel                     As SldWorks.ModelDoc2

                   

                   

                      Dim swPart                      As SldWorks.PartDoc

                   

                   

                      Dim vBody                       As Variant

                   

                   

                      Dim i                           As Long

                   

                   

                      Dim bRet                        As Boolean

                   

                   

                     

                      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

                   

                   

                              vBody = swPart.GetBodies2(swSolidBody, True)

                   

                   

                              'SelectBodies swApp, swModel, vBody, ""

                   

                   

                             

                              ' Surface bodies

                   

                   

                              vBody = swPart.GetBodies2(swSheetBody, True)

                   

                   

                              SelectBodies swApp, swModel, vBody, ""

                   

                   

                             

                         

                          Case swDocASSEMBLY

                   

                   

                              ProcessAssembly swApp, swModel

                   

                   

                             

                          Case Else

                   

                   

                              Exit Sub

                   

                   

                      End Select

                   

                   

                  End Sub

                   

                   

                  '----------------------------------------

                  • Re: macro to hide all visible surfaces in assembly
                    Amen Allah Jlili

                    Hi Steven

                     

                    Here's a macro that I wrote. It would hide all sheet metal bodies from an assembly. You can use as long as you can keep the header notes

                     

                     

                    ' ******************************************************************************
                    ' MACRO BY JLILI AMEN - CAHDERO.COM SOLIDWORKS API TRAINING & SERVICES
                    ' PRE-CONDITIONS: ASSEMBLY OPEN
                    ' POST-CONDTIONS: ALL SHEET METAL BODIES ARE HIDDEN
                    ' ******************************************************************************
                    Dim swApp As SldWorks.SldWorks
                    
                        Dim swModel As SldWorks.ModelDoc2
                        Dim swPart As SldWorks.PartDoc
                        Dim Bodies As Variant
                        Dim swBody
                        Dim Components As Variant
                     Sub main()
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        If swModel Is Nothing Then
                        MsgBox "No Active Doc!"
                        Exit Sub
                        'Debug.Print swModel.GetType
                        ElseIf swModel.GetType <> SwConst.swDocumentTypes_e.swDocASSEMBLY Then
                        MsgBox "File Is Not An Assembly!"
                        Exit Sub
                        End If
                        Dim swAssembly As AssemblyDoc
                        Set swAssembly = swModel
                        Components = swAssembly.GetComponents(False)
                        Dim Component
                        For Each Component In Components
                        If UBound(Component.GetChildren) = -1 Then
                        'Component is a part
                        Dim Types As Variant
                        Bodies = Component.GetBodies3(swBodyType_e.swAllBodies, Types)
                        Debug.Print ("Component name: " & Component.Name2)
                        If Not IsEmpty(Bodies) Then
                        For Each swBody In Bodies
                        If swBody.IsSheetMetal Then
                            swBody.HideBody True
                            End If
                        Next
                        End If
                        End If
                        Next Component
                        swModel.ForceRebuild3 False
                    End Sub
                    

                     

                     

                    Best,
                    Amen
                    CADHero.com