Issue
In the User Interface I can right click on a sub assembly in the browser and select "Get Model Sketches" to include a sketch from the sub-assembly. I need to achieve the same thing programmatically. Is there a way to do this?
Solution
To include / exclude entities for a Drawing View the "SetVisibility" method can be used. The ability to include a sketch from an assembly became available in the 2009 release. In prior versions only part sketches can be included using SetVisibility.
When dealing with a sketch contained in a sub-level assembly, you need to create a proxy for the sketch in the top-level assembly context, because the "SetVisibility" method can only handle objects that exist in the assembly that is represented in the view.
This VBA and VB.NET example iterates through the occurrences of the top-level assembly which is referenced by the view and for every assembly occurrence, it creates a SketchProxy object and includes it. The method can be extended to include sketches in deeper level sub-occurrences.
Public Sub SetAssemblySketchesVisibility()
Dim oDrawingDoc As DrawingDocument
If ThisApplication.ActiveDocument.DocumentType <>
kDrawingDocumentObject Then
MsgBox("need to make a drawing the active document")
End
End If
oDrawingDoc = ThisApplication.ActiveDocument
Dim oView As DrawingView
If oDrawingDoc.ActiveSheet.DrawingViews.Count < 1 Then
MsgBox("Add a view to the drawing")
End
End If
oView = oDrawingDoc.ActiveSheet.DrawingViews(1)
Dim oDoc As Document
oDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
If oDoc.DocumentType <> kAssemblyDocumentObject Then
MsgBox("View's Referenced doc needs to be an assembly")
End
End If
Dim oAssy As AssemblyDocument
oAssy = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oSubOcc As ComponentOccurrence
For Each oSubOcc In oAssy.ComponentDefinition.Occurrences
If oSubOcc.DefinitionDocumentType =
kAssemblyDocumentObject Then
Dim oSubAssyDef As AssemblyComponentDefinition
oSubAssyDef = oSubOcc.Definition
Dim oSketch As PlanarSketch
Debug.Print(oSubAssyDef.Sketches.Count)
For Each oSketch In oSubAssyDef.Sketches
Dim oSketchProxy As PlanarSketchProxy
Call oSubOcc.CreateGeometryProxy(oSketch,
oSketchProxy)
'Set Visibility of the Proxy Object DOES NOT
‘support AssemblySketches with release prior
‘to 2009
Call oView.SetVisibility(oSketchProxy, True)
Next
End If
Next
End Sub
This VB.NET example is from a button on a form in a stand alone exe that access Inventor from out of process.
Imports Inventor
Public Class Form1
Dim m_inventorApp As Inventor.Application = Nothing
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Try ' Try to get an active instance of Inventor
Try
m_inventorApp =
System.Runtime.InteropServices.Marshal.GetActiveObject
("Inventor.Application")
Catch ' If not active, create a new Inventor session
Dim inventorAppType As Type =
System.Type.GetTypeFromProgID("Inventor.Application")
m_inventorApp = System.Activator.CreateInstance
(inventorAppType)
'Must be set visible explicitly
m_inventorApp.Visible = True
End Try
Catch
System.Windows.Forms.MessageBox.Show(
"Error: couldn't create Inventor instance")
End Try
SetAssemblySketchesVisibility()
End Sub
Public Sub SetAssemblySketchesVisibility()
Dim oDrawingDoc As DrawingDocument
If m_inventorApp.ActiveDocument.DocumentType <>
DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("need to make a drawing the active document")
End
End If
oDrawingDoc = m_inventorApp.ActiveDocument
Dim oView As DrawingView
If oDrawingDoc.ActiveSheet.DrawingViews.Count < 1 Then
MsgBox("Add a view to the drawing")
End
End If
oView = oDrawingDoc.ActiveSheet.DrawingViews(1)
Dim oDoc As Document
oDoc =
oView.ReferencedDocumentDescriptor.ReferencedDocument
If oDoc.DocumentType <>
DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("View's Referenced doc needs to be an assembly")
End
End If
Dim oAssy As AssemblyDocument
oAssy =
oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oSubOcc As ComponentOccurrence
For Each oSubOcc In oAssy.ComponentDefinition.Occurrences
If oSubOcc.DefinitionDocumentType =
DocumentTypeEnum.kAssemblyDocumentObject Then
Dim oSubAssyDef As AssemblyComponentDefinition
oSubAssyDef = oSubOcc.Definition
Dim oSketch As PlanarSketch
Debug.Print(oSubAssyDef.Sketches.Count)
For Each oSketch In oSubAssyDef.Sketches
Dim oSketchProxy As PlanarSketchProxy =
Nothing
Call oSubOcc.CreateGeometryProxy(oSketch,
oSketchProxy)
'Set Visibility of the Proxy Object DOES NOT
‘support AssemblySketches with release prior
‘to 2009
Call oView.SetVisibility(oSketchProxy, True)
Next
End If
Next
End Sub
End Class