User Edit sub-assembly, Modify Design View Rep with API (not remove associativity)

User Edit sub-assembly, Modify Design View Rep with API (not remove associativity)

Josh_Hunt
Advocate Advocate
1,117 Views
2 Replies
Message 1 of 3

User Edit sub-assembly, Modify Design View Rep with API (not remove associativity)

Josh_Hunt
Advocate
Advocate

My scenario is similar to https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/ilogic-part-visibility-in-subassembly-d...

 

As show in the image below

  1. Open "Top.iam" (NOTE: Observe "Sub:1" design view is "Hatch" and Associative = True)
  2. Edit "Sub:1"
  3. Run rule "Toggle Hatch" rule
  4. Design View of "Sub:1" changes to 'Master' because API call defaults to "Remove Associativity".

Josh_Hunt_1-1632166048458.png

 

If the user does the exact same steps but toggles the visibility manually then they are given this prompt. I want the API to Modify the Design View Representation.

 

Josh_Hunt_0-1632166032123.png

data set attached

Josh Hunt
0 Likes
Accepted solutions (1)
1,118 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

 

If I am explaining this correctly the issue here is the selecting of the occurrence is a ComponentOccurrenceProxy in the context of the assembly  rather than a ComponentOccurrence in the  sub assembly. So simply making it a native object will edit the part in the context of the sub assembly. 

 

 

Sub Main()

	' Doc Parameters
	If ThisApplication Is Nothing Then Exit Sub
	
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	asmCompDef_ = ThisDoc.Document.ComponentDefinition
	
	Dim compOcc As ComponentOccurrence
	
	While True
		'Add object to work with the occurrence in the context of its parent and not as a proxy of the main assembly
		compOcc = ThisServer.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Component for hatch toggle").NativeObject
		
		If compOcc Is Nothing
			ThisServer.CommandManager.StopActiveCommand
			Exit While
			
		Else If compOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject
			MsgBox("Selected Occurrence is not a part.")
			
		Else
			setIso(compOcc)
			
		End If
		
		If compOcc Is Nothing OrElse Not compOcc.Name.Toupper = "CHBR" Then Exit While
			
	End While

	''Update the top assembly to update the graphics otherwise exitng the sub assembly.
	oAsmDoc.Update 
End Sub

Sub setIso(occ As ComponentOccurrence)
	For Each sBody In occ.SurfaceBodies
		If sBody.Name.Tolower = "hatch"
			sBody.Visible = Not sBody.Visible 
		End If
	Next sBody
End Sub

 This topic came up recently in a post and although I read it too  late to help figure this one out (hence the below code I constructed) it makes explaining it much easier. 

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/difference-between-componentoccurrence-...

 

Here is also another method to pass the selected occurrence to the sub assembly loop. The passing is done by filtering through the occurrence name. if you tried to make the two objects equal you will get an error message about ComponentOccurrenceProxy not equal to the ComponentOccurrence

 

Sub Main()
	
	' set a reference to the assembly document.
	Dim oAsmDoc As AssemblyDocument
	oAsmDoc = ThisApplication.ActiveDocument
	
	'set a reference to the assembly component definintion.
	Dim oAsmCompDef As AssemblyComponentDefinition
	oAsmCompDef = oAsmDoc.ComponentDefinition
	
	'define the Component Occurrence collection
	Dim oCompOcc As Inventor.ComponentOccurrence 
	
	
	'set top level view rep to Default
	oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Edit").activate

	For Each oCompOcc In oAsmCompDef.Occurrences
		If oCompOcc.Name = "Sub:1" Then
			oCompOcc.SetDesignViewRepresentation("Edit", , True) 'True sets the view rep to be associative
		End If
	Next

	'modify actual subassembly document
	If oCompOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		
		'set a reference to the subassembly component definintion
		compOccDef = oCompOcc.Definition
		'Select Part
		PickPart(oAsmDoc, compOccDef)	
		
	End If
		
End Sub


Sub PickPart(oAsmDoc,compOccDef)

	If ThisApplication Is Nothing Then Exit Sub
	
	asmCompDef_ = ThisDoc.Document.ComponentDefinition
	
	Dim compOcc As ComponentOccurrence
	
	While True
		compOcc = ThisServer.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Component for hatch toggle")
		
		If compOcc Is Nothing
			ThisServer.CommandManager.StopActiveCommand
			Exit While
			
		Else If compOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject
			MsgBox("Selected Occurrence is not a part.")
			
		Else
			
			'[Option: Work backwards from selected item if needed
			'set a reference to the subassembly component definintion
			'compOccDef = compOcc.ParentOccurrence.Definition
			'For Each PickOcc In compOccDef.Occurrences
			']
			
			'Loop throught the subassembly occurrences
			Dim oSubCompOcc As Inventor.ComponentOccurrence
			For Each oSubCompOcc In compOccDef.Occurrences
				'Check If picked occurrence (proxy) matches subassembly occurrence
				If compOcc.Name = oSubCompOcc.Name Then
					setIso(oSubCompOcc)
				End If

			Next
			
		End If
		
		If compOcc Is Nothing OrElse Not compOcc.Name.ToUpper = "CHBR" Then Exit While
			
	End While
	
	'Update the top assembly to update the graphics otherwise exitng the sub assembly.
	oAsmDoc.Update

End Sub



Sub setIso(occ As ComponentOccurrence)
	For Each sBody In occ.SurfaceBodies
		If sBody.Name.Tolower = "hatch"
			sBody.Visible = Not sBody.Visible 
		End If
	Next sBody
	
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

Josh_Hunt
Advocate
Advocate

@A.Acheson Thank you very much. The NativeObject and TopAsmDoc.update are just what I needed.

 

I understand proxies well. However, it was not clear to me that the pick command was getting a proxy but that makes sense now.

 

That rule needs to be used when either TOP or SUB is the active document so this works well.

If TypeOf compOcc Is ComponentOccurrenceProxy Then compOcc = compOcc.nativeobject
Josh Hunt