Turn work features off in patterned components/sub assemblies

Turn work features off in patterned components/sub assemblies

Anonymous
Not applicable
659 Views
4 Replies
Message 1 of 5

Turn work features off in patterned components/sub assemblies

Anonymous
Not applicable

Does anyone have a snippet of code they could share for switching work features off in an assembly that contains patterned components?

 

The other piece of help I am looking for is to switch off sketches in a part within an assembly or/and sub assembly.

 

I have some code that traverses through the assembly using AllReferencedDocuments method but it does not switch of sketches in parts..

 

many thanks in advance to anyone who has the time to help out...

0 Likes
Accepted solutions (2)
660 Views
4 Replies
Replies (4)
Message 2 of 5

HideoYamada
Advisor
Advisor
Accepted solution

Hello bclark,

 

Does this code help you?

Option Explicit

Sub test()
    Dim aDoc As AssemblyDocument
    Set aDoc = ThisApplication.ActiveEditDocument
    Dim occPattern As OccurrencePattern
    
    For Each occPattern In aDoc.ComponentDefinition.OccurrencePatterns
        Dim occPatternElm As OccurrencePatternElement
        For Each occPatternElm In occPattern.OccurrencePatternElements
            Dim occ As ComponentOccurrence
            For Each occ In occPatternElm.Occurrences
                Debug.Print occ.Name
                '
                ' Something to do to the occurrence.
                '
            Next occ
        Next occPatternElm
    Next occPattern
End Sub

If you change visuality of the work features in the sub part document (not the occurrence), one change effects to all the other elements. (Such as the size or length changing in the part model effects to all elements.)

 

Because of some reasons, if you want to change visualities of the elements individually, each occurrence in the elements must be treated shown in the code above.

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
Message 3 of 5

Anonymous
Not applicable
Accepted solution

HI 

 

Thanks for your help. It is appreciated. I've almost finished the code now. In the process of testing it. I 've had issues with virtual components in the assembly causing the code to exit, but I have fixed that. will post back once it is completed.. Thanks again!

0 Likes
Message 4 of 5

Anonymous
Not applicable

So, I have this working now. open an assembly, run the rule and all work features and sketches are turned off

 

What I would like to do, is ammend it so that it deals with weldments also.. I've read that I need to use WeldmentComponentDefinition to access the assembly, but have had no luck this far

 

Can anyone kindly help? - Code below

 

Sub Main ()
   
'catch and skip errors
On Error Resume Next
   
Dim oAssyDoc As AssemblyDocument
  oAssyDoc = ThisApplication.ActiveDocument
 
 ' switch of features and sketches in top level assy 
    For Each oWorkPlane In oAssyDoc.ComponentDefinition.WorkPlanes
    oWorkPlane.Visible = False
    Next
    'set work axis visibility
    For Each oWorkAxis In oAssyDoc.ComponentDefinition.WorkAxes
    oWorkAxis.Visible = False
    Next
    'set work point visibility
    For Each oWorkPoint In oAssyDoc.ComponentDefinition.WorkPoints
    oWorkPoint.Visible = False
    Next    
    'set sketch visibility
    For Each oSketch In oAssyDoc.ComponentDefinition.Sketches
    oSketch.Visible = False
    Next
    'set 3d sketch visibility
    For Each threeDSketch In oAssyDoc.Sketches3D
    threeDSketch.Visible = False
    Next

  Dim oAssyDef As AssemblyComponentDefinition
  oAssyDef = oAssyDoc.ComponentDefinition
  
  'process all subassemblies
  Dim oOcc As ComponentOccurrence
  For Each oOcc In oAssyDef.Occurrences
    If oOcc.Definition.Type = kAssemblyComponentDefinitionObject Then
      Dim oDef As AssemblyComponentDefinition
      oDef = oOcc.Definition

          
      'turn off sketches in subassemblies
      Dim oSkProxy As PlanarSketchProxy
      For Each oSk In oDef.Sketches
               oSkProxy.Visible = False    
      Next
      'turn off workplanes in subassemblies
      Dim oWpProxy As WorkPlaneProxy
      For Each oWp In oDef.WorkPlanes
                oWpProxy.Visible = False
      Next
      'turn off workpoints in subassemblies  
      Dim oWtProxy As WorkPointProxy
      For Each oWt In oDef.WorkPoints
                oWtProxy.Visible = False
      Next            
     'turn off workaxis in subassemblies  
      Dim oWaProxy As WorkAxisProxy
      For Each oWa In oDef.WorkAxes
                oWaProxy.Visible = False
      Next            
    'turn off 3d sketches in subassemblies  
      Dim o3DProxy As Sketch3DProxy
      For Each o3D In oDef.Sketches3D
                o3DProxy.Visible = False

    Next
    End If
  Next 'oOcc
    Call TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences, 1) 
End Sub 
  
Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, _ 
                             Level As Integer) 
    ' Iterate through all of the occurrence in this collection.  This 
    ' represents the occurrences at the top level of an assembly. 
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In Occurrences 
        oDef = oOcc.Definition
        
    'Check for virtual components
    If TypeOf oDef Is VirtualComponentDefinition Then    
        Else 
    
    Dim oSkProxy As PlanarSketchProxy    
      For Each oSk In oDef.Sketches
        Call oOcc.CreateGeometryProxy(oSk, oSkProxy)
        oSkProxy.Visible = False
      Next
      
       For Each oWp In oDef.WorkPlanes
        Call oOcc.CreateGeometryProxy(oWp, oWpProxy)
        oWpProxy.Visible = False
      Next
          
        For Each oWt In oDef.WorkPoints
        Call oOcc.CreateGeometryProxy(oWt, oWtProxy)
        oWtProxy.Visible = False
      Next
      
          For Each oWa In oDef.WorkAxes
        Call oOcc.CreateGeometryProxy(oWa, oWaProxy)
        oWaProxy.Visible = False
      Next
      
        Dim o3DProxy As Sketch3DProxy
          For Each o3D In oDef.Sketches3D
        Call oOcc.CreateGeometryProxy(o3D, o3DProxy)
        o3DProxy.Visible = False
      Next


        ' Check to see if this occurrence represents a subassembly 
        ' and recursively call this function to traverse through it. 
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
        End If
            
    End If
    Next 


End Sub

 

0 Likes
Message 5 of 5

Anonymous
Not applicable

sorry for confusion. corrected code below

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 21941 StartFragment: 314 EndFragment: 21909 StartSelection: 314 EndSelection: 314

   Sub Main ()
   
'catch and skip errors
On Error Resume Next
   
Dim oAssyDoc As AssemblyDocument
  oAssyDoc = ThisApplication.ActiveDocument
 
 ' switch of features and sketches in top level assy 
    For Each oWorkPlane In oAssyDoc.ComponentDefinition.WorkPlanes
    oWorkPlane.Visible = False
    Next
    'set work axis visibility
    For Each oWorkAxis In oAssyDoc.ComponentDefinition.WorkAxes
    oWorkAxis.Visible = False
    Next
    'set work point visibility
    For Each oWorkPoint In oAssyDoc.ComponentDefinition.WorkPoints
    oWorkPoint.Visible = False
    Next    
    'set sketch visibility
    For Each oSketch In oAssyDoc.ComponentDefinition.Sketches
    oSketch.Visible = False
    Next


  Dim oAssyDef As AssemblyComponentDefinition
  oAssyDef = oAssyDoc.ComponentDefinition
  
    Call TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences, 1) 
End Sub  
   
 
  Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, _ 
                             Level As Integer) 
    ' Iterate through all of the occurrence in this collection.  This 
    ' represents the occurrences at the top level of an assembly. 
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In Occurrences 
        oDef = oOcc.Definition
        
    'Check for virtual components
    If TypeOf oDef Is VirtualComponentDefinition Then    
        Else 
    
    Dim oSkProxy As PlanarSketchProxy    
      For Each oSk In oDef.Sketches
        Call oOcc.CreateGeometryProxy(oSk, oSkProxy)
        oSkProxy.Visible = False
      Next
      
       For Each oWp In oDef.WorkPlanes
        Call oOcc.CreateGeometryProxy(oWp, oWpProxy)
        oWpProxy.Visible = False
      Next
          
        For Each oWt In oDef.WorkPoints
        Call oOcc.CreateGeometryProxy(oWt, oWtProxy)
        oWtProxy.Visible = False
      Next
      
          For Each oWa In oDef.WorkAxes
        Call oOcc.CreateGeometryProxy(oWa, oWaProxy)
        oWaProxy.Visible = False
      Next
      

        ' Check to see if this occurrence represents a subassembly 
        ' and recursively call this function to traverse through it. 
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
        End If
            
    End If
    Next 

InventorVb.DocumentUpdate()
End Sub

 

0 Likes