How to Link Model States in an assembly via iLogic or VBA?

How to Link Model States in an assembly via iLogic or VBA?

3DAli
Collaborator Collaborator
649 Views
4 Replies
Message 1 of 5

How to Link Model States in an assembly via iLogic or VBA?

3DAli
Collaborator
Collaborator

Is there a way to manage model state via iLogic in an assembly file to link all corresponding states between main and sub assemblies? in other words, I want to automate the "Link Model state" command under productivity tool and  based on what I experience, you can not add any column to the temporary excel worksheet using API/iLogic and all the objects you have access to via iLogic are read-only. if anyone has any experience with this, it would be greatly appreciated if you can share.

here is the code I wrote , is this correct? any potential issues with this?

Dim oDoc As AssemblyDocument = ThisDoc.Document

Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition

Dim oStates As ModelStates = oDef.ModelStates

Dim oState As ModelState

oModelStateName = oStates.ActiveModelState.Name

Dim oTable As ModelStateTable= oStates.ModelStateTable

Dim oRow As ModelStateTableRow

CurrentState = oStates.ActiveModelState.Name

i = 1

For Each oRow In oTable.TableRows
    
    'this activates the row to make it the current state
        
        oStates.Item(i).Activate
		'MsgBox(oStates.Item(i).Name)
		
		        'Start Looping
	For Each oOcc As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences
		
		oOcc.ActiveModelState=oStates.Item(i).Name
		
		
	Next

    i = i + 1
Next 'model state

 

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

WCrihfield
Mentor
Mentor

Hi @3DAli.  Dealing with these new ModelStates can certainly be confusing at first, and they likely still don't have all the bugs worked out of everything.  Below is a single line of Inventor API code you can use in an iLogic rule to simulate clicking that productivity bonus tool "Link Model States".

ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyBonusTools_LinkLODsCmd").Execute

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

3DAli
Collaborator
Collaborator

@WCrihfield , Thank you so much for your help. 

 

Cheers

Ali

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Since that command essentially displays a user interactive dialog, and you may want to avoid that, I went ahead and looked into your code process too.  I honestly have not had the need for this myself yet, so I have not tested this yet, but here is some similar code you can try out.  Keep in mind that this is only working with top level components, and not going down into components within sub-assemblies or any deeper at this point.

Dim oADoc As AssemblyDocument = ThisDoc.FactoryDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oMSs As ModelStates = oADef.ModelStates
Dim oOrigActivMS As ModelState = oMSs.ActiveModelState
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
For Each oMS As ModelState In oMSs
	oMS.Activate
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Suppressed Then Continue For 'else unsuppress, change it, then suppress again
		If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
		If TypeOf oOcc.Definition Is WeldsComponentDefinition Then Continue For
		oOcc.ActiveModelState = oMS.Name
	Next 'oOcc
Next 'oMS
oOrigActivMS.Activate
oADoc.Update

I would assume that if you were to attempt to edit the model states table by code, which they clearly have exposed that ability for us to be able for us to do, then you would most likely need to set the MemberEditScope to kEditAllMembers instead, because it includes all model states settings within it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

3DAli
Collaborator
Collaborator

That's definitely a more comprehensive method that incudes potential scenarios. 

 

I hope others find this helpful, thanks so much for taking the time and review the code.

 

Best,

Ali

0 Likes