Automatic View Representations for all parts in assembly

Automatic View Representations for all parts in assembly

BrandonLee_Innovex
Contributor Contributor
2,398 Views
4 Replies
Message 1 of 5

Automatic View Representations for all parts in assembly

BrandonLee_Innovex
Contributor
Contributor

I am trying to create a rule to automatically create a view representation for all parts in the main assembly and all parts within the subassembly as well. I Found a code that allows me to create a view rep for the parts but does not seem to work for the subassembly parts. Note that it creates the view rep for each part in the subassembly but does not turn the visibility of that part on.

 

See the code I have been using below

'create a design view representation for each unique part in the assembly 

'define current document
Dim openDoc As Document
openDoc = ThisDoc.Document

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

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection
Dim oCompOcc As Inventor.ComponentOccurrence 

'define view rep 
Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep names
Dim NameList As New ArrayList()

'Look at the view reps in the assembly
For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'set the list of names to the array list
NameList.add(oViewRep.Name)
Next

'check for a Default view rep and create it if not found
If Not NameList.Contains("Default") Then
	'create Default view rep 
	oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default") 
	oViewRep.ShowAll
	oViewRep.Activate
End If

'zoom all
ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

'look at all of the unique parts in the assembly
For Each docFile In openDoc.AllReferencedDocuments
	If docFile.DocumentType = 12290 Then '12290 is the part document enumurator
	'locate the last backslash position in the full file name
	Dim FNamePos As Long
	FNamePos = InStrRev(docFile.FullFileName, "\", -1) 
	'remove path from part file name 	
	Dim docFName As String
	docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)         
	'remove extension from part file name 
	ShortName = Left(docFName,  Len(docFName) - 4)   
		'check to see if the arraylist contains the desired view rep
		If Not NameList.Contains(ShortName) Then
		'create new View Rep 
		oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
		oViewRep.Activate
		oViewRep.Locked = False	
		Else If NameList.Contains(ShortName) Then
		'reference existing View Rep 
		oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName) 
		oViewRep.Activate
		oViewRep.Locked = False
		End If
		'look at all of the occurences
		For Each oCompOcc in oCompDef.Occurrences
		'locate the colon position in the occurence name
		oCompOccPos = InStrRev(oCompOcc.Name, ":") 
		'set occurence name to everything left of the colon
		oOccName = Left(oCompOcc.Name, oCompOccPos -1) 
			'set visible if name matches first occurence
	      	If oCompOcc.Name = ShortName & ":1"  Then
			oCompOcc.Visible = True
			ThisApplication.ActiveView.Update()
			Else
			oCompOcc.Visible = False
			ThisApplication.ActiveView.Update()
			End If
		Next
	End If
	'lock view rep
	oViewRep.Locked = True	
Next

'set Default View Rep active
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").activate

 

0 Likes
Accepted solutions (1)
2,399 Views
4 Replies
Replies (4)
Message 2 of 5

bradeneuropeArthur
Mentor
Mentor
'define current document
Dim openDoc As AssemblyDocument
openDoc = ThisAssembly.Document

And the 

oCompOcc.Name = ShortName & ":1"

 is not the same because the displayname is different then the part name!

 

put a massage box between and you will see:

msgbox (oCompOcc.Name = ShortName & ":1")

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 5

Ralf_Krieg
Advisor
Advisor

Hello

 

You loop through all oCompOcc.Occurrences. This collection contains only the occurrences of the first level. You can loop through the oCompOcc.Occurrences.AllLeafOccurrences. This Enumerator contains all leaf occurrences. I assume that if there's more than one occurrence "XYZ:1" in different subassemblys, the viewrep should contain only one occurence.

I encapsulated the whole script in a transaction, so it's easier to undo.

try this:

'create a design view representation for each unique part in the assembly 

'define current document
Dim openDoc As Document
openDoc = ThisDoc.Document

Dim oTrans As Transaction=ThisApplication.TransactionManager.StartTransaction(openDoc,"Create View Rep's")

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

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection
Dim oCompOcc As Inventor.ComponentOccurrence 

'define view rep 
Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep names
Dim NameList As New ArrayList()

'Look at the view reps in the assembly
For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'set the list of names to the array list
NameList.Add(oViewRep.Name)
Next

'check for a Default view rep and create it if not found
If Not NameList.Contains("Default") Then
	'create Default view rep 
	oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default") 
	oViewRep.ShowAll
	oViewRep.Activate
End If

'zoom all
ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

'look at all of the unique parts in the assembly
For Each docFile In openDoc.AllReferencedDocuments
	If oCompDef.Occurrences.AllReferencedOccurrences(docFile).Count > 0 Then 'avoid to create a view rep for base part of derived parts
		If docFile.DocumentType = 12290 Then '12290 is the part document enumurator
			'locate the last backslash position in the full file name
			Dim FNamePos As Long
			FNamePos = InStrRev(docFile.FullFileName, "\", -1) 
			'remove path from part file name 	
			Dim docFName As String
			docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)         
			'remove extension from part file name 
			ShortName = Left(docFName,  Len(docFName) - 4)   
				'check to see if the arraylist contains the desired view rep
				If Not NameList.Contains(ShortName) Then
					'create new View Rep 
					oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
					oViewRep.Activate
					oViewRep.Locked = False	
				Else If NameList.Contains(ShortName) Then
					'reference existing View Rep 
					oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName) 
					oViewRep.Activate
					oViewRep.Locked = False
				End If
				Dim bSkip As Boolean=False
				'look at all of the occurences
				For Each oCompOcc In oCompDef.Occurrences.AllLeafOccurrences 
					'locate the colon position in the occurence name
					oCompOccPos = InStrRev(oCompOcc.Name, ":") 
					'set occurence name to everything left of the colon
					oOccName = Left(oCompOcc.Name, oCompOccPos -1) 
					'set visible if name matches first occurence
			      	If oCompOcc.Name = ShortName & ":1"  And bSkip=False Then
						oCompOcc.Visible = True
						ThisApplication.ActiveView.Update()
						bSkip=True 'even if there is more than one occurrence named "xyz:1" in different subassys, we want only one occurence in our view rep
					Else
						oCompOcc.Visible = False
						ThisApplication.ActiveView.Update()
					End If
				Next
		End If
		'lock view rep
		oViewRep.Locked = True	
	End If
Next

'set Default View Rep active
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").activate

oTrans.End

Catch ex As Exception
	MsgBox(ex.Message)
	oTrans.Abort
End Try

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 4 of 5

BrandonLee_Innovex
Contributor
Contributor

That seems to work better, it is still having trouble working for frame generated parts, but works for assemblies that don't contain any frame parts.

0 Likes
Message 5 of 5

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

And what kind of trouble? Maybe the Reference Skeleton part has not an occurrence number in browser tree and your code crashes in this case? Then skip occurences without a number. Or that for the Reference Skeleton part a view rep is created? Then we skip all parts with BOMstructure Phantom?

I have also edited the name compare a bit.

 

'create a design view representation for each unique part in the assembly 

'define current document
Dim openDoc As Document
openDoc = ThisDoc.Document

Dim oTrans As Transaction=ThisApplication.TransactionManager.StartTransaction(openDoc,"Create View Rep's")

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

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition = openDoc.ComponentDefinition

'define the first level components collection
Dim oCompOcc As Inventor.ComponentOccurrence 

'define view rep 
Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep names
Dim NameList As New ArrayList()

'Look at the view reps in the assembly
For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'set the list of names to the array list
NameList.Add(oViewRep.Name)
Next

'check for a Default view rep and create it if not found
If Not NameList.Contains("Default") Then
	'create Default view rep 
	oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default") 
	oViewRep.ShowAll
	oViewRep.Activate
End If

'zoom all
ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

'look at all of the unique parts in the assembly
For Each docFile In openDoc.AllReferencedDocuments
	If oCompDef.Occurrences.AllReferencedOccurrences(docFile).Count > 0 Then 'avoid to create a view rep for base part of derived parts
		If Not docFile.componentdefinition.bomstructure = BOMStructureEnum.kPhantomBOMStructure Then
			If docFile.DocumentType = 12290 Then '12290 is the part document enumurator
				'locate the last backslash position in the full file name
				Dim FNamePos As Long
				FNamePos = InStrRev(docFile.FullFileName, "\", -1) 
				'remove path from part file name 	
				Dim docFName As String
				docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)         
				'remove extension from part file name 
				ShortName = Left(docFName,  Len(docFName) - 4)   
				'check to see if the arraylist contains the desired view rep
				If Not NameList.Contains(ShortName) Then
					'create new View Rep 
					oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
					oViewRep.Activate
					oViewRep.Locked = False	
				Else If NameList.Contains(ShortName) Then
					'reference existing View Rep 
					oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName) 
					oViewRep.Activate
					oViewRep.Locked = False
				End If
				Dim bSkip As Boolean=False
				'look at all of the occurences
				For Each oCompOcc In oCompDef.Occurrences.AllLeafOccurrences 
					'locate the colon position in the occurence name
					oCompOccPos = InStrRev(oCompOcc.Name, ":")
					If oCompOccPos<2 Then oCompOccPos=Len(oCompOcc.Name)
					'set occurence name to everything left of the colon
					oOccName = Left(oCompOcc.Name, oCompOccPos -1) 
					'set visible if name matches first occurence
			      	'If oCompOcc.Name = ShortName & ":1" And bSkip = False Then
					If System.IO.Path.GetFileNameWithoutExtension(oCompOcc.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName) = ShortName  And bSkip=False Then
						oCompOcc.Visible = True
						ThisApplication.ActiveView.Update()
						bSkip=True 'even if there is more than one occurrence named "xyz:1" in different subassys, we want only one occurence in our view rep
					Else
						oCompOcc.Visible = False
						ThisApplication.ActiveView.Update()
					End If
				Next
			End If
		End If
		'lock view rep
		oViewRep.Locked = True	
	End If
Next

'set Default View Rep active
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").Activate

oTrans.End

Catch ex As Exception
	MsgBox(ex.StackTrace  )'Message)
	oTrans.Abort
End Try

 

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes