Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Ralf_Krieg
in reply to: brandon.lee97

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