Detecting if a component occurrence is a virtual component

Detecting if a component occurrence is a virtual component

mrattray
Advisor Advisor
2,349 Views
2 Replies
Message 1 of 3

Detecting if a component occurrence is a virtual component

mrattray
Advisor
Advisor

I have an easy one for you guys,

I need a way to tell if a component occurence is a virtual component.

I'm using a For Each loop to cycle through all of the components in an assembly, and if the component is itself an assembly to set the LOD to "Custom". I'm using the definition document descriptor object for this. It all woks fine until the loop comes across a virtual component which doesn't have a definition document, so I need a way to check if the component is virtual and needs to be skipped.

 

Here's the code:

 

Option Explicit

Imports Inventor.LevelOfDetailEnum

Sub Main

iLogicVb.UpdateWhenDone = True

Dim doc As AssemblyDocument
Dim oLOD As LevelOfDetailRepresentation
Dim oAsmCompDef As ComponentDefinition
Dim oComp As ComponentOccurrence
Dim oComps As ComponentOccurrences

doc = ThisDoc.Document
If doc.ComponentDefinition.RepresentationsManager.ActiveLevelOfDetailRepresentation.LevelOfDetail <> kCustomLevelOfDetail Then 
	oAsmCompDef = doc.ComponentDefinition
	oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Custom")
	oLOD.Activate(True)
End If
oComps = doc.ComponentDefinition.Occurrences
On Error Goto handle
For Each oComp In oComps
	If oComp.Suppressed = False Then
		If oComp.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
			If oComp.ActiveLevelOfDetailRepresentation <> "Custom" Then 
				oComp.SetLevelOfDetailRepresentation("Custom", True)
			End If
			'MsgBox("Name: " & oComp.Name & "  LOD: " & oComp.ActiveLevelOfDetailRepresentation)
		'Else
			'MsgBox("doc type: " & oComp.ReferencedDocumentDescriptor.ReferencedDocumentType)
			'Exit For
		End If
	End If
Next

Exit Sub

handle:
MsgBox("LOD releated error in: " & oComp.Name, vbOKOnly, "LOD error")

End Sub

 

Mike (not Matt) Rattray

0 Likes
Accepted solutions (1)
2,350 Views
2 Replies
Replies (2)
Message 2 of 3

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi mrattray, 

 

This is just what I had handy in my custom snippets collection.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com


 

 

' set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
'check for and skip virtual components
'(in case a virtual component trips things up)
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
'Show occurrence name In the message box body
MessageBox.Show(oOccurrence.Name, "iLogic")
Else
End If
Next

 

EESignature

Message 3 of 3

mrattray
Advisor
Advisor

Thanks Curtis, that was exactly what I was looking for!

 

For future thread searchers, here's the modified version:

 Option Explicit

Imports Inventor.LevelOfDetailEnum

Sub Main

iLogicVb.UpdateWhenDone = True

Dim doc As AssemblyDocument
Dim oLOD As LevelOfDetailRepresentation
Dim oAsmCompDef As ComponentDefinition
Dim oComp As ComponentOccurrence
Dim oComps As ComponentOccurrences

doc = ThisDoc.Document
If doc.ComponentDefinition.RepresentationsManager.ActiveLevelOfDetailRepresentation.LevelOfDetail <> kCustomLevelOfDetail Then 
	oAsmCompDef = doc.ComponentDefinition
	oLOD = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Custom")
	oLOD.Activate(True)
End If
oComps = doc.ComponentDefinition.Occurrences
On Error Goto handle
For Each oComp In oComps
	If oComp.Suppressed = False Then
		If Not TypeOf oComp.Definition Is VirtualComponentDefinition Then
			If oComp.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
				If oComp.ActiveLevelOfDetailRepresentation <> "Custom" Then 
					oComp.SetLevelOfDetailRepresentation("Custom", True)
				End If
			End If
		End If
	End If
Next

Exit Sub

handle:
MsgBox("LOD releated error in: " & oComp.Name, vbOKOnly, "LOD error")

End Sub

 

Mike (not Matt) Rattray