Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change Model State Name From Assembly

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
a81383
273 Views, 2 Replies

Change Model State Name From Assembly

a81383
Enthusiast
Enthusiast

Hello,

 

Is it possible to change a model state name within a component from the assembly level? Currently I'm capably of opening the component and changing the name directly within the excel sheet. But I'm just wondering if there is an easier way? Or if this can be done without opening the document?

 

Thank you!

 

(I will work on putting together some code to paste here)

0 Likes

Change Model State Name From Assembly

Hello,

 

Is it possible to change a model state name within a component from the assembly level? Currently I'm capably of opening the component and changing the name directly within the excel sheet. But I'm just wondering if there is an easier way? Or if this can be done without opening the document?

 

Thank you!

 

(I will work on putting together some code to paste here)

2 REPLIES 2
Message 2 of 3
WCrihfield
in reply to: a81383

WCrihfield
Mentor
Mentor
Accepted solution

Hi @a81383.  If you have an assembly open, and you want to change the name of a ModelState within a component, then the document that the component represents will already be either initiated or open already, in the background (invisibly), so you may not need to visibly open it, depending on a couple things.  If the ModelState that you want to change the name of is currently being referenced by a component anywhere in the assembly (at any level), you may not be able to change its name while the assembly is open.  For instance, if you have any component, anywhere in the assembly's structure (possible even down in sub assemblies), and that component is referencing that same model file, and is set to that specific ModelState, that may prevent you from changing its name, as long as the assembly remains open.  Or do you simply mean that you want to change which ModelState that a specific component is currently pointing to, to a different ModelState that is already available within that referenced model file (instead of renaming it)?

Below is an example iLogic rule you could try out.  You will need to edit this code before you can try it out though, because right now it is just getting the first component, instead of a specifically named one.  Then you will need to edit the ModelState names I am using as examples to the ones you want to work with.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oOcc As ComponentOccurrence = oOccs.Item(1)
'Dim oOcc As ComponentOccurrence = oOccs.ItemByName("Part1:1") 'works in top level only
Dim oOccMSName As String = oOcc.ActiveModelState
Dim oOccDoc As Document = oOcc.Definition.Document
If oOcc.Definition.IsModelStateMember Then
	oOccDoc = oOcc.Definition.FactoryDocument
End If
Dim oMSs As ModelStates = oOccDoc.ComponentDefinition.ModelStates
Dim oMSNameToFind As String = "ModelStateNameToFind"
Dim oMSNameToChangeItTo As String = "ModelStateNameToChangeItTo"
Dim oMS As ModelState = Nothing
Try
	oMS = oMSs.Item(oMSNameToFind)
Catch
	Logger.Error("Could not find ModelState named:  " & oMSNameToFind)
	Exit Sub 'could not find the ModelState, so exit code
End Try
Try
	oMS.Name = oMSNameToChangeItTo
Catch
	Logger.Error("Error changing ModelState Name from:" & vbCrLf & _
	oMSNameToFind & " to " & oMSNameToChangeItTo)
End Try

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

Hi @a81383.  If you have an assembly open, and you want to change the name of a ModelState within a component, then the document that the component represents will already be either initiated or open already, in the background (invisibly), so you may not need to visibly open it, depending on a couple things.  If the ModelState that you want to change the name of is currently being referenced by a component anywhere in the assembly (at any level), you may not be able to change its name while the assembly is open.  For instance, if you have any component, anywhere in the assembly's structure (possible even down in sub assemblies), and that component is referencing that same model file, and is set to that specific ModelState, that may prevent you from changing its name, as long as the assembly remains open.  Or do you simply mean that you want to change which ModelState that a specific component is currently pointing to, to a different ModelState that is already available within that referenced model file (instead of renaming it)?

Below is an example iLogic rule you could try out.  You will need to edit this code before you can try it out though, because right now it is just getting the first component, instead of a specifically named one.  Then you will need to edit the ModelState names I am using as examples to the ones you want to work with.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oOcc As ComponentOccurrence = oOccs.Item(1)
'Dim oOcc As ComponentOccurrence = oOccs.ItemByName("Part1:1") 'works in top level only
Dim oOccMSName As String = oOcc.ActiveModelState
Dim oOccDoc As Document = oOcc.Definition.Document
If oOcc.Definition.IsModelStateMember Then
	oOccDoc = oOcc.Definition.FactoryDocument
End If
Dim oMSs As ModelStates = oOccDoc.ComponentDefinition.ModelStates
Dim oMSNameToFind As String = "ModelStateNameToFind"
Dim oMSNameToChangeItTo As String = "ModelStateNameToChangeItTo"
Dim oMS As ModelState = Nothing
Try
	oMS = oMSs.Item(oMSNameToFind)
Catch
	Logger.Error("Could not find ModelState named:  " & oMSNameToFind)
	Exit Sub 'could not find the ModelState, so exit code
End Try
Try
	oMS.Name = oMSNameToChangeItTo
Catch
	Logger.Error("Error changing ModelState Name from:" & vbCrLf & _
	oMSNameToFind & " to " & oMSNameToChangeItTo)
End Try

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 3 of 3
a81383
in reply to: WCrihfield

a81383
Enthusiast
Enthusiast

Hi @WCrihfield

 

I haven't had a lot of time to fully dive into this code. But from what I'm seeing so far it does contain what I'm looking for. 

 

Thank you very much for your detailed reply!

 

0 Likes

Hi @WCrihfield

 

I haven't had a lot of time to fully dive into this code. But from what I'm seeing so far it does contain what I'm looking for. 

 

Thank you very much for your detailed reply!

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report