Adding Model State to a Component (Part or SubAssembly) from an Assembly using iLogic

3DAli
Collaborator Collaborator
672 Views
6 Replies
Message 1 of 7

Adding Model State to a Component (Part or SubAssembly) from an Assembly using iLogic

3DAli
Collaborator
Collaborator

Hello,

I am running into this weird issue, where I am trying to run a rule that creates a new model state in a part. when I run the rule form the part it perfectly runs ok, but when I run it from the assembly here is what happens :

 

when you run it from an assembly for the first time it runs , no problem 

but when you run it again form the assembly , it fails "unexpected error" , and can not pass the parameters from the assembly to the part.

 

I could get around this issue partially by passing the parameters to a master layout which then is derived into the part , but still get error running the second code below from the assembly.

can you please give me a  simple example of adding modelstate to a part from an assembly by passing a parameter to the part which works more than once (I mean just the first time 🙂 ) ?

 

Thanks for your support

Ali

 

Dim oDoc As PartDocument
oDoc=ThisDoc.ModelDocument

Dim oPartCompDef As PartComponentDefinition
oPartCompDef = oDoc.ComponentDefinition
	    
Dim oStates As ModelStates 	
oStates = oPartCompDef.ModelStates 	

Dim oModelState As ModelState

'create New set of ModelState
 
 If SnapRing = 1 ul Then 
	oName = "Hub-" & HID & "-" & HThickness & "-" & HDepth & "_S"
Else
	oName = "Hub-" & HID & "-" & HThickness & "-" & HDepth
End If 

'Check for existance

For Each oModelState In oStates
 	If oModelState.Name = oName Then
		MStateExist = True
		MessageBox.Show("Model State Exist")
		Exit For
	Else
		MstateExist = False
	End If
Next 

	Try
		oModelState = oDoc.ComponentDefinition.ModelStates.Add(oName)
		ThisDoc.ActiveModelState = oName
		MessageBox.Show("Model State Created")
		Catch
	End Try
iLogicVb.RunRule("Reset Model State")
Dim oDoc As PartDocument
oDoc=ThisDoc.ModelDocument
oDoc.ComponentDefinition.ModelStates(1).Activate

 

Signature_Small
0 Likes
Accepted solutions (1)
673 Views
6 Replies
Replies (6)
Message 2 of 7

Curtis_Waguespack
Consultant
Consultant

Hi @3DAli 

 

I think this will work for your needs.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oDocAssembly As AssemblyDocument
oDocAssembly = ThisDoc.Document

Dim oDocPart As PartDocument
oDocPart = Component.InventorComponent("Part:1").Definition.Document

Dim oDoc As Document
oDoc = oDocAssembly
For i = 1 To 2
	If i = 2 Then oDoc = oDocPart
	oCompDef = oDoc.ComponentDefinition

	oName = "Hub-" & HID & "-" & HThickness & "-" & HDepth 'set default
	If SnapRing = 1 ul       Then oName = oName & "_S" 'set variation

	Try
		oTest = oDoc.ComponentDefinition.ModelStates.item(oName)
		MessageBox.Show("Model State Exists in " & IO.Path.GetFileName(oDoc.FullFileName))
	Catch
		Dim oModelState As ModelState
		oModelState = oDoc.ComponentDefinition.ModelStates.Add(oName)
		ThisDoc.ActiveModelState = oName
		MessageBox.Show("Model State Created in  " & IO.Path.GetFileName(oDoc.FullFileName))
	End Try

	Try : oDoc.ComponentDefinition.ModelStates(1).Activate : Catch : End Try
Next

InventorVb.DocumentUpdate()

 

 

0 Likes
Message 3 of 7

3DAli
Collaborator
Collaborator

Hi @Curtis_Waguespack 

 

same issue, it runs for the first time when there is no exisitng model state in that "Part:1" , after the first round, if I change some parameters to modify the size of Part:1 and run the code again, it does not do anything! this is weird!

I even tried using API in VBA , to simply cycle through all referenced docs in an assembly and creat a new modelstate in them. if the parts in that assembly dont have any exisitn g model state it works fine, but if they do it fails. (see below) this works total fine if there is no exisitng Model state in the referenced part!

 

Dim oAss As AssemblyDocument
Set oAss = ThisApplication.ActiveDocument
Dim oPart As PartDocument

For Each oPart In oAss.ReferencedDocuments
               
        MsgBox ("My Name is : " & oPart.DisplayName)
        
        oPart.ComponentDefinition.ModelStates.Add ("It's Me")
              
Next

 

 

Signature_Small
0 Likes
Message 4 of 7

3DAli
Collaborator
Collaborator

Screenshot 2023-01-25 105109.jpg

Signature_Small
0 Likes
Message 5 of 7

3DAli
Collaborator
Collaborator

the irony is when I open the part using API, it works totally fine regardless of the part has exisitng Model states or not

Dim oPartDoc As PartDocument
HubPath = "C:\Ali\Part\Hub.ipt"
Set oPartDoc = ThisApplication.Documents.Open(HubPath)
For i = 1 To 5
    oPartDoc.ComponentDefinition.ModelStates.Add ("ms-" & i)
Next
Call oPartDoc.Save
Call oPartDoc.Close

if I can acheive the same using iLogic, its not the end of the world. can you help me translate this into iLogic? 

Signature_Small
0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @3DAli 

 

My apologies, I did not understand the question fully the first time around.

 

Here is a version that will create a new MS when the parameters are changed.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Dim oDocAssembly As AssemblyDocument
oDocAssembly = ThisDoc.Document
oAssemnblyDef = oDocAssembly.ComponentDefinition

Dim oDocPart As PartDocument
oPartDef = Component.InventorComponent("Part:1").Definition
oDocPart = oPartDef.Document

Dim oDoc As Document
oDoc = oDocAssembly
oMSFactory = oDoc

For i = 1 To 2
	If i = 2 Then 
		oDoc = oDocPart
		oMSFactory = oPartDef.FactoryDocument
	End If	

	oName = "Hub-" & HID & "-" & HThickness & "-" & HDepth 'set default
	If SnapRing = 1 ul        Then oName = oName & "_S" 'set variation
	
	Try
		oTest = oMSFactory.ComponentDefinition.ModelStates.item(oName)
		MessageBox.Show("Model State Exists in " & IO.Path.GetFileName(oDoc.FullFileName))
	Catch

		Dim oModelState As ModelState
		oModelState = oMSFactory.ComponentDefinition.ModelStates.add(oName)
		MessageBox.Show("Model State Created in  " & IO.Path.GetFileName(oDoc.FullFileName))
	End Try

	Try : oDoc.ComponentDefinition.ModelStates(1).Activate : Catch : End Try
Next

InventorVb.DocumentUpdate()

 

0 Likes
Message 7 of 7

3DAli
Collaborator
Collaborator

@Curtis_Waguespack 

 

Thank you so much, I see now , you have used Factory Documnet, I need to learn more, I guess by default its on Member Documnet, which obviously doesnt work. one last question: if you see the code below, I am trying to pass a Parameter from the assembly to the part with existing Model State , and it fails the parameter is called "Snap Ring" and it only again works the first time. how would you approach passing a parameter in this scenario?

 

if you can just give me a typical example, would be great: lets say you are setting a parameter insde a part from an assembly by passing the vault from a userdefined parameter in an assembly to a model parameter to the part that has existing model state. obviously its critical to make sure before passing the values down , the model state of the part is set back to {Primary] , then you pass the values, get the geometry updated and then you engage the code above to create the new Model State (which you perfectly showed me how to do)

 

your support is truly appreciated 

 

Cheers

Ali

 

 

 

'Set Hub Size 
Parameter("Sheave Master Concept", "Hub_ID") = Parameter("HubID") 
Parameter("Sheave Master Concept", "Hub_Wall_Thickness")=Parameter("HubWallThickness")
Parameter("Sheave Master Concept", "Hub_Depth") = Parameter("HubDepth")

Component.ActiveModelState("Hub") = ThisServer.LanguageTools.CurrentPrimaryModelStateString

If SnapRing = True Then 
	Parameter("Sheave Master Concept", "SnapRing") = 1 ul
Else 
	Parameter("Sheave Master Concept", "SnapRing") = 0 ul
End If

InventorVb.DocumentUpdate(True)

 

 

Signature_Small
0 Likes