Link Model state to view representation

Link Model state to view representation

DarthBane55
Advisor Advisor
802 Views
8 Replies
Message 1 of 9

Link Model state to view representation

DarthBane55
Advisor
Advisor

Hi, this is regarding an assembly.

I have already created a couple of view representations (called "M1" and "M2").  I also created 2 model states also called "M1" and "M2".

My goal would be that when I click on a view rep, the model state with the same name activates automatically.  The view rep changes some components shown, and the model state changes some positions of other components that are shown in both view rep, this is why I need to use both view rep and model state (model state does not deal with the visibility of components).

I tried a few things but getting nowhere, is this even possible?  It would mean it has to activate the rule when I change a view rep.

Thanks!

0 Likes
Accepted solutions (1)
803 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @DarthBane55.  That sounds like a solid request, and one that might be popular, but I do not know of any built-in way to make it happen.  I know that we can copy a DVR (DesignViewRepresentation) to a ModelState, and we can copy a ModelState to a DVR, but that does not create a 'link'.  I know we also have a tool for linking ModelStates of an assembly with the ModelStates of top level components.  I know we can monitor the events of activating a ModelState and activating a DVR, so we may be able to create an add-in to simulate the behavior you want.  If a DVR is activated, then try to find a ModelState with the same name in the same document and activate that too, and the other way around.  It might be worth testing that type of functionality with a rule before jumping into creating an add-in though.

The two events are as follows:

ModelStateEvents.OnActivateModelState

RepresentationEvents.OnActivateDesignView 

Both give you a reference to the Document involved, and the DVR or ModelState involved.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

DarthBane55
Advisor
Advisor

That is awesome!  I will try something with what you gave me.  I don't even need to look for the same name, I can hard code the names in this particular assembly file, but of course, if we can find the name, it is even better!  I also don't need an addin, because if it gets triggered when changing the DVR, then it is automated, I can just add this rule in the assembly template and it will be there for all new assemblies.

I'll report back, thanks again!

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

Just be careful not to create an endless loop if you have the action going both ways (if DVR activated, then activate MS // and if MS activated, then activate DVR).  😉  And if needed, I can post a starter example of how to set up the basic event handler codes, then you can customize it as needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

DarthBane55
Advisor
Advisor

I would really appreciate that... I tried a few things, but not having success really...  This is a bit over my head to be honest ☹️

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor
Accepted solution

OK.  Below are two different iLogic rules.  The first one is primarily just for research, documentation purposes, because it just writes information to the iLogic Log window when those two events happen.  The second example is a very simple working example that listens for a DVR to be activated, then it tries to activate a ModelState with the same name as the DVR (not the other way around right now).

Research example:

Sub Main
	If oRepEvents Is Nothing Then oRepEvents = ThisApplication.RepresentationEvents
	If oMSEvents Is Nothing Then oMSEvents = ThisApplication.ModelStateEvents
	AddHandler oRepEvents.OnActivateDesignView, AddressOf oRepEvents_OnActivateDesignView
	AddHandler oMSEvents.OnActivateModelState, AddressOf oMSEvents_OnActivateModelState
End Sub

Dim oRepEvents As RepresentationEvents
Dim oMSEvents As ModelStateEvents

Sub oRepEvents_OnActivateDesignView(Doc As Document, DVR As DesignViewRepresentation, _
BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	'HandlingCodeEnum is ignored for this event
	If BeforeOrAfter = EventTimingEnum.kBefore Then
		Logger.Info("Before:  DesignViewRepresentation named '" & DVR.Name & "' activated in " & Doc.DisplayName)
		If Context.Count = 0 Then
			Logger.Info("Zero Context")
		Else
			For i = 1 To Context.Count
				Logger.Info(Context.Name(i) & " = " & Context.Item(i).ToString)
			Next
		End If
	ElseIf BeforeOrAfter = EventTimingEnum.kAfter Then
		Logger.Info("After:  DesignViewRepresentation named " & DVR.Name & "' activated in " & Doc.DisplayName)
		If Context.Count = 0 Then
			Logger.Info("Zero Context")
		Else
			For i = 1 To Context.Count
				Logger.Info(Context.Name(i) & " = " & Context.Item(i).ToString)
			Next
		End If
	End If
End Sub

Sub oMSEvents_OnActivateModelState(Doc As Document, MS As ModelState, _
BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	'no context for this event, and HandlingCodeEnum is ignored also
	If BeforeOrAfter = EventTimingEnum.kBefore Then
		Logger.Info("Before:  ModelState named '" & MS.Name & "' activated in " & Doc.DisplayName)
	ElseIf BeforeOrAfter = EventTimingEnum.kAfter Then
		Logger.Info("After:  ModelState named '" & MS.Name & "' activated in " & Doc.DisplayName)
	End If
End Sub

Working example:

Sub Main
	If oRepEvents Is Nothing Then oRepEvents = ThisApplication.RepresentationEvents
	AddHandler oRepEvents.OnActivateDesignView, AddressOf oRepEvents_OnActivateDesignView
End Sub

Dim oRepEvents As RepresentationEvents

Sub oRepEvents_OnActivateDesignView(Doc As Document, DVR As DesignViewRepresentation, _
BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	'HandlingCodeEnum is ignored for this event
	If BeforeOrAfter = EventTimingEnum.kAfter Then
		If Doc.ModelStateName <> DVR.Name Then
			Dim oMSs As ModelStates = Doc.ComponentDefinition.ModelStates
			If oMSs.Count < 2 Then Return
			Dim oMS As ModelState = Nothing
			Try : oMS = oMSs.Item(DVR.Name) : Catch : End Try
			If oMS Is Nothing Then Return
			Try : oMS.Activate : Catch : End Try
		End If
	End If
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)

Message 7 of 9

WCrihfield
Mentor
Mentor

But beware that you should only run a rule like this once per Inventor session, and you will need to close Inventor to get rid of that event handler.  These solutions could be developed further to design in an 'exit plan' for removing the event handlers when some other event happens, or some other value changes.  And once it is launched, you can't retrieve it or change how it works without restarting Inventor first, because it will be held in session memory.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

And here is the opposite simple working example (Activate DVR when ModelState Activated).

But do not run both working examples during the same session of Inventor, or it might cause an endless loop and lock up Inventor.

Sub Main
	If oMSEvents Is Nothing Then oMSEvents = ThisApplication.ModelStateEvents
	AddHandler oMSEvents.OnActivateModelState, AddressOf oMSEvents_OnActivateModelState
End Sub

Dim oMSEvents As ModelStateEvents

Sub oMSEvents_OnActivateModelState(Doc As Document, MS As ModelState, _
BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	'no context for this event, and HandlingCodeEnum is ignored also
	If BeforeOrAfter = EventTimingEnum.kAfter Then
		Dim RepMgr As RepresentationsManager = Doc.ComponentDefinition.RepresentationsManager
		If RepMgr.ActiveDesignViewRepresentation.Name <> MS.Name Then
			Dim DVRs As DesignViewRepresentations = RepMgr.DesignViewRepresentations
			Dim DVR As DesignViewRepresentation = Nothing
			Try : DVR = DVRs.Item(MS.Name) : Catch : End Try
			If DVR Is Nothing Then Return
			Try : DVR.Activate : Catch : End Try
		End If
	End If
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
Message 9 of 9

DarthBane55
Advisor
Advisor

This is exactly what I was after!  The 2nd one works exactly to perfection!  This is the only thing I wanted, nothing else.

Thank you very much!

0 Likes