<?xml encoding=”UTF-8″>By Adam Nagy
Let’s say we have a drawing view showing an assembly with this structure:
If some of the work surfaces are visible and some are not then the context menu will show this – a filled rectangle next to Include All Surfaces:
If we want to achieve the same programmatically as clicking that menu item, i.e. either making all work surfaces visible or invisible then this is how we can do it.
The DrawingView object has methods SetIncludeStatus and SetVisibility methods to control what is visible in the given view. As the API Help File says these methods require that the objects are in context of the document being shown by the view:
Input object to set the include status of.
Valid objects are 2d and 3d sketches, work features,
surface features, and proxies for all of these.
<strong>The object needs to be supplied in the context
of the document referenced by the drawing view.</strong>
For instance, to set the include status of a 3D sketch
within a part instanced in an assembly
(and the drawing view is of the assembly),
the input should be a Sketch3DProxy object created
in context of the assembly. An error will occur
if no corresponding object exists in the drawing view.
You can find more info on contexts here:
http://adndevblog.typepad.com/manufacturing/2013/07/occurrences-contexts-definitions-proxies.html
Some objects can only be accessed through the document’s definition so that you have to create the proxy for them using ComponentOccurrence.CreateObjectProxy.
In this VBA example we’ll go through all the occurrences and suboccurrences in the assembly document that the selected DrawingView is referencing, create a proxy for each WorkSurface and then set the include status for them, so that they will not be visible:
Sub CollectAllSurfaces( _
occs As ComponentOccurrences, coll As ObjectCollection)
Dim occ As ComponentOccurrence
For Each occ In occs
If occ.SubOccurrences.count > 0 Then
Call CollectAllSurfaces(occ.SubOccurrences, coll)
End If
If TypeOf occ.Definition Is PartComponentDefinition Then
Dim pcd As PartComponentDefinition
Set pcd = occ.Definition
Dim ws As WorkSurface
For Each ws In pcd.WorkSurfaces
Dim wsp As WorkSurfaceProxy
Call occ.CreateGeometryProxy(ws, wsp)
Call coll.Add(wsp)
Next
End If
Next
End Sub
Sub IncludeAllSurfacesNot()
Dim dv As DrawingView
Set dv = ThisApplication.ActiveDocument.SelectSet(1)
Dim doc As AssemblyDocument
Set doc = dv.ReferencedDocumentDescriptor.ReferencedDocument
Dim tro As TransientObjects
Set tro = ThisApplication.TransientObjects
Dim coll As ObjectCollection
Set coll = tro.CreateObjectCollection
Call CollectAllSurfaces(doc.ComponentDefinition.Occurrences, coll)
Dim wsp As WorkSurfaceProxy
For Each wsp In coll
Call dv.SetIncludeStatus(wsp, False)
Next
End Sub
After running the code you’ll see that all work surfaces became invisible:




Leave a Reply