UnInclude WorkSurfaces in all DrawingViews on all Sheets

<?xml encoding=”UTF-8″>By Adam Nagy

This is a continuation of the article UnInclude WorkSurfaces in a DrawingView

The only difference is that now we are iterating through all the sheets and all the drawing views on them, and also take into account the fact that the referenced document could be an assembly or a part.

VBA code:

Sub CollectSurfacesInPart( _
doc As PartDocument, occ As ComponentOccurrence, _
coll As ObjectCollection)
Dim ws As WorkSurface
Dim pcd As PartComponentDefinition
Set pcd = doc.ComponentDefinition
For Each ws In pcd.WorkSurfaces
Dim wsp As WorkSurfaceProxy
If Not occ Is Nothing Then
Call occ.CreateGeometryProxy(ws, wsp)
Call coll.Add(wsp)
Else
Call coll.Add(ws)
End If
Next
End Sub
Sub CollectSurfacesInAssembly( _
occs As ComponentOccurrences, coll As ObjectCollection)
Dim occ As ComponentOccurrence
For Each occ In occs
If occ.SubOccurrences.Count > 0 Then
Call CollectSurfacesInAssembly(occ.SubOccurrences, coll)
End If
If TypeOf occ.Definition Is PartComponentDefinition Then
Call CollectSurfacesInPart(occ.Definition.Document, occ, coll)
End If
Next
End Sub
Sub IncludeAllSurfacesNot()
Dim dwg As DrawingDocument
Set dwg = ThisApplication.ActiveDocument
' Iterate through all the sheets
Dim sh As Sheet
For Each sh In dwg.Sheets
' Iterate through all the views
Dim dv As DrawingView
For Each dv In sh.DrawingViews
Dim doc As Document
Set doc = dv.ReferencedDocumentDescriptor.ReferencedDocument
Dim tro As TransientObjects
Set tro = ThisApplication.TransientObjects
Dim coll As ObjectCollection
Set coll = tro.CreateObjectCollection
If TypeOf doc Is AssemblyDocument Then
Call CollectSurfacesInAssembly( _
doc.ComponentDefinition.Occurrences, coll)
ElseIf TypeOf doc Is PartDocument Then
Call CollectSurfacesInPart(doc, Nothing, coll)
End If
Dim ws As WorkSurface
For Each ws In coll
Call dv.SetIncludeStatus(ws, False)
Next
Next
Next
End Sub

iLogic Rule:

Sub Main()
Dim dwg As DrawingDocument
dwg = ThisApplication.ActiveDocument
' Iterate through all the sheets
Dim sh As Sheet
For Each sh In dwg.Sheets
' Iterate through all the views
Dim dv As DrawingView
For Each dv In sh.DrawingViews
Dim doc As Document
doc = dv.ReferencedDocumentDescriptor.ReferencedDocument
Dim tro As TransientObjects
tro = ThisApplication.TransientObjects
Dim coll As ObjectCollection
coll = tro.CreateObjectCollection
If TypeOf doc Is AssemblyDocument Then
Call CollectSurfacesInAssembly( _
doc.ComponentDefinition.Occurrences, coll)
ElseIf TypeOf doc Is PartDocument Then
Call CollectSurfacesInPart(doc, Nothing, coll)
End If
Dim ws As WorkSurface
For Each ws In coll
Call dv.SetIncludeStatus(ws, False)
Next
Next
Next
End Sub
Sub CollectSurfacesInPart( _
doc As PartDocument, occ As ComponentOccurrence, _
coll As ObjectCollection)
Dim ws As WorkSurface
Dim pcd As PartComponentDefinition
pcd = doc.ComponentDefinition
For Each ws In pcd.WorkSurfaces
Dim wsp As WorkSurfaceProxy
If Not occ Is Nothing Then
Call occ.CreateGeometryProxy(ws, wsp)
Call coll.Add(wsp)
Else
Call coll.Add(ws)
End If
Next
End Sub
Sub CollectSurfacesInAssembly( _
occs As ComponentOccurrences, coll As ObjectCollection)
Dim occ As ComponentOccurrence
For Each occ In occs
If occ.SubOccurrences.Count > 0 Then
Call CollectSurfacesInAssembly(occ.SubOccurrences, coll)
End If
If TypeOf occ.Definition Is PartComponentDefinition Then
Call CollectSurfacesInPart(occ.Definition.Document, occ, coll)
End If
Next
End Sub

Here you can see a sample model where the above code would switch off all the work surfaces on all the sheets, whether the view has a part or assembly in it: 

Uninclude

 


Comments

6 responses to “UnInclude WorkSurfaces in all DrawingViews on all Sheets”

  1. This code is a holy grail for me. I’ve tried to run both the VBA and the ilogic in my current drawing. Unfortunately I get errors in both versions. I’m using Inventor 2015 professional 64-bit, build 223, SP2 update 1.
    When running the ilogic the error is: Error in rule:
    Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
    (sorry not that informative)
    When running the VBA the error is:
    Run-time error ‘-2147467259 (80004005)’:
    Method ‘SetIncludeStatus’ of object “DrawingView’ failed
    and the debug points me to:
    Call dv.SetIncludeStatus(ws, False) (this is the highlighted line)
    Next
    Next
    Next
    End Sub
    Any help you could give me to get this working would be very appreciated.

  2. Hi Mike,
    Could you please log this question on the Inventor Customisation forum and also provide a non-confidential document that could be used to reproduce the issue?
    That would make things easier.
    Thank you,
    Adam

  3. Adam,
    Which forum specifically?
    I’m trying to make a quick and simple example, but its proving too simple and the codes are working just fine and doing what it should.
    Mike

  4. This forum: http://forums.autodesk.com/t5/inventor-customization/bd-p/120
    You can try to simplify the assembly until the problem goes away or debug into the code to see which DrawingView and WorkSurface are causing the problem – that might give a clue.

  5. After working with this for a few hours, the errors I’m getting are due to having flat patterns in my drawing. If you remove the flats, you get no errors.
    With this knowledge, do you think there is a work around?

  6. Hi MIke,
    So the issue is with a Drawing View that is showing a flat pattern and also has work surfaces?
    Can you reproduce it with any flat pattern view?
    Could you please log it on the forum and provide a minimal sample part?
    Cheers,
    Adam

Leave a Reply to MikeCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading