Changing subassembly position representations from top-level assembly multi-value user parameters

Changing subassembly position representations from top-level assembly multi-value user parameters

kwilson_design
Collaborator Collaborator
1,389 Views
3 Replies
Message 1 of 4

Changing subassembly position representations from top-level assembly multi-value user parameters

kwilson_design
Collaborator
Collaborator

I'm trying to create a Form that has multi-value user parameters, where the user can select from and it would select the corresponding position representation of a subassembly that has the same name. 

 

kwilson_design_0-1727113128501.png

 

kwilson_design_1-1727112682570.png

 

When I try the following code, I get a an error on line 5. I was hoping to be able to access the component position rep from here.

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

If  Arm_Trial_Dimension = "5" Then 
	oAsmCompDef.RepresentationsManager.ActivePositionalRepresentation.Name = "Arm Trial Position 5"
End If

 Is this because I have not specified the subassembly name? I would like to avoid using explicit component names, if possible, and rather let it change the position rep of any component within the top-level if it finds a position rep with that name. This is because we have several subassemblies with different file names/part numbers but they all share the same type of position rep and need to behave the same if one is replaced for the other. Hope that makes sense.

 

I wished we could just right click and capture the position rep from here like you can for so many other aspects but this only shows constraints...

kwilson_design_2-1727113029759.png

 

I had this working with model states, but unfortunately model states are not reliable in 2022. So now I'm hoping to achieve the same idea with using positional reps and no model states in hopes I can get more reliable results.

 

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
1,390 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @kwilson_design.  I honestly rarely ever use positional representations, but have used them in a few places.  Generally was not trying to control them by code at the time though.  There is no built-in 'link' tool for linking parent assembly positional representations with their child component's positional representations.  If you want to set the positional representation of components in an assembly by code, then you must do it though the ComponentOccurrence.ActivePositionalRepresentation property, which is Read/Write, with a String type value.  It might be possible to create your own add-in that will monitor representation related events (RepresentationEvents) such as the RepresentationEvents.OnActivatePositionalRepresentation Event, and reacts to them with custom event handlers that will then attempt to go through all child components and set them to a positional representation with the same name, if one is available to those components.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

kwilson_design
Collaborator
Collaborator

Hi @WCrihfield . Hmm this is sounding discouraging.

 

I have edited it to include a string but running into Error on Line 4 : 'ActiveDocument' is not declared. It may be inaccessible due to its protection level.

 

Dim oArmTrlDist() As String = {"4.5", "4.75", "5", "5.25", "5.5", "5.75", "6", "6.25", "6.5", "6.75" }

If Arm_Trial_Dimension = 5 Then 
	oArmTrlDist = ActiveDocument.ComponentOccurrence.ActivePositionalRepresentation(2)
End If

 

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Hi @kwilson_design.  Not sure what your overall code may look like, but I did not understand your use of 'ActiveDocument' in that last code example either.  I will post a code example below for you to try out.  It will look at what PositionalRepresentation is currently active in your current assembly, then it will run a custom Sub routine which will attempt to set each top level component within that assembly to a PositionalRepresentation of the same name, if one with that name is available within the document that the component is referencing.  This code would need to be ran every time you changed which PositionalRepresentation  is active in your assembly.  However, you can not set a component to a PositionalRepresentation that the document the component references does not have defined within it already.

 

Since there is no event listed within the 'This Document' tab of the 'Event Triggers' dialog of an assembly for activating / changing positional representations, you would either have to create your own custom event handler, as mentioned in my last post, or use one of the other events that are listed in that dialog.  However, some of those other events, such as 'Any User Parameter Change' would likely get triggered far more often than you want it to.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim sAPosRepName As String = oADef.RepresentationsManager.ActivePositionalRepresentation.Name
	SetPositionalReps(sAPosRepName, oOccs)
	oADoc.Update2(True)
End Sub

Sub SetPositionalReps(sPosRepName As String, oOccs As ComponentOccurrences)
	Dim oFMgr As FileManager = ThisApplication.FileManager
	For Each oOcc As ComponentOccurrence In oOccs
		Dim sFDN As String = oOcc.ReferencedDocumentDescriptor.FullDocumentName
		Dim sFFN As String = oFMgr.GetFullFileName(sFDN)
		Dim oPosRepNames() As String = oFMgr.GetPositionalRepresentations(sFFN)
		If oPosRepNames.Contains(sPosRepName) Then
			Try
				oOcc.ActivePositionalRepresentation = sPosRepName
			Catch
				Logger.Error("Error setting 'ActivePositionalRepresentation'" & _
				"of Component named '" & oOcc.Name & "' to " & sPosRepName)
			End Try
		End If
	Next oOcc
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes