Parameter Error sending to parts with Model State

Parameter Error sending to parts with Model State

brianA32ZM
Advocate Advocate
525 Views
5 Replies
Message 1 of 6

Parameter Error sending to parts with Model State

brianA32ZM
Advocate
Advocate

Hello, I am attempting to change a parameter of a part the has multiple Model States for an assembly with the following code:

Parameter("Part", "Param") = "New Value"

I get the error message that is show below.  I am running Inventor 2023. Any help is appreciated, thank you. 

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Parameter.set_Expression(String )
at iLogic.ParamDynamicFinder.SetParamValue(Parameter param, Object value, Boolean doUpdate)
at ThisRule.Main() in external rule: Test4:line 557
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Accepted solutions (2)
526 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @brianA32ZM.  That can be a complicated task, when dealing with parameters within components from the main assembly, and some of the components have multiple ModelStates in them.  They are often treated as ReadOnly in situations like that, but there are sometimes ways to work around that situation.  Is this part a top level component in the assembly, or is it down within a sub assembly?  If it has multiple ModelStates, then do you want this parameter change to only effect one of its ModelStates, or all of them?  Are there multiple components in your assembly referencing that same part file on disk, but maybe set to different ModelStates?  That can also complicate things further.  In situations like that, we may be able to make a change to the first instance, but then when the code gets to the next one, the component's referenced document will need to be updated before any changes will be allowed to it, due to multiple versions documents referencing that same file on disk being open in memory at the same time.  They have not yet offered much in the way of updated iLogic snippets for changing the values of parameters in other documents, that will allow us to specify which ModelState (or all) in that other document we want to make the change apply to.  But there are a few iLogic tricks available to us now.  Once we get those few questions answered, that will help with how to move forward.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

CattabianiI
Collaborator
Collaborator

I think it is related to this issue I've already reported few years ago.
Do not like this message because it is not helpful at all. I'm just here to complain.

0 Likes
Message 4 of 6

brianA32ZM
Advocate
Advocate
Accepted solution

Hello @WCrihfield,

 

Thank you, below are the answers to your questions.

I am interested in your solution; my workaround is maybe not the best solution.

  • I set up a new external rule specifically to pass a parameter, ParamPassRule. I used rule arguments so I can use the rule generically and pass the parameter name and value.  
  • From the assembly I run the initial External rule that:
    • Opens the part in the affected Model State
    • Runs the External ParamPassRule on the Part
    • Closes the part
For Each oOcc In oOccs

	'Filter Bracket----------------------------------------------------------------------------
	If oOcc.Name = "Bracket Regulator Filter Right | 01-M"
		'Open Bracket Regulator Filter Right in the "2 3 Bolted" Model State and update the Mounting Hole Spacing to Match the Acuator Bracket Leg Hole Spacing in the Multibody
		' Set a reference to the FileManager object.
		Dim oFileManager As FileManager
		oFileManager = ThisApplication.FileManager
		' Use the full file name and ModelState name to get the full document name.
		Dim strFullDocumentName As String
		strFullDocumentName = oFileManager.GetFullDocumentName(oOcc.ReferencedFileDescriptor.FullFileName, "2 3 Bolted")
		'Open the Part	
		oDoc3 = ThisApplication.Documents.Open(strFullDocumentName, False)
		Dim oParamArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
		'Initial Map add
		oParamArgs.Add("aParameterName", "t")
		oParamArgs.Add("aParameterValue", 33)
		'Run rule on part with arguments
		Call iLogicVb.Automation.RunExternalRuleWithArguments(oDoc3, "ParamPassRule", oParamArgs)
		'Close the Part	
		oDoc3.close
	End If
Next

Is this part a top level component in the assembly, or is it down within a sub assembly? Top Level

If it has multiple ModelStates, then do you want this parameter change to only effect one of its ModelStates, or all of them?  Multiple with a specific Model State to be modified

Are there multiple components in your assembly referencing that same part file on disk, but maybe set to different ModelStates?  Yes, there are multiple instances and they could be set to different Model States.

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @brianA32ZM.  Using an external rule as a Sub routine, and passing data to it using RuleArguments is actually a pretty good idea.  I have done similar tasks that way myself, but have not used that technique for this specific task.  There does not appear to be any official documentation for the objects and techniques I am about to show you, but there is a pretty great Class available to us directly in our iLogic rules named "ModelStateUtils" (used without the quotes).  When you type that into a rule, then type the dot after it, the built-in Intellisense system suggests a list of useful stuff you can use from there, but does not offer much in the way of hints about how they are used, or any tips about the input variables within them.  This code sample below is using 3 of those tools.

 

ModelStateUtils.UpdateComponentDocument(oOcc)
Dim oOccMSFactoryDoc As Document = ModelStateUtils.GetFactoryDocument(oOcc)
ModelStateUtils.AssignFactoryParameter(oOccMSFactoryDoc, oOcc.ActiveModelState, sParamName, sParamValue)
ModelStateUtils.UpdateComponentDocument(oOcc)

 

Just in case it is not obvious, the 'oOcc' variable being used here represents a ComponentOccurrence type object in an assembly.  I am attempting to update the component document both before and after attempting to make any changes to the component document, just in case the main code has already worked with another component that references the same file, but a ModelState (and therefor a different Document) within that file.  I heard about that needed update in the procedure directly from an Autodesk API programmer a year or so back, but not about the specific line of code I am using here.  In Line 3 of this example code, I am just using the ComponentOccurrence.ActiveModelState property to specify the name of the ModelState I want to make the parameter value change in.  The alternate version of that line of code would be the ModelStateUtils.AssignTableCell() method, which has all the same inputs, but I am still not 100% sure what all the differences are between the two methods, due to no documentation available for them, and not enough time to do exhaustive testing, and report about those testing results.  Keep in mind that column names within ModelStateTable are a bit odd, because if the column is for an iProperty, it will include not only the name of the iProperty, it will also include the 'nick name' of the PropertySet that the iProperty resides in, within [ & ] brackets.  But nothing like that for columns for parameters.

 

Edit:  It should also be noted that this code sequence should only be used on components that have multiple ModelStates.  There is the similar ModelStatesUtil.GetModelStates(oDoc) type method available, that is supposed to return a ModelStates collection object, which we could check the Count property of, but it says it only applies to a 'factory' document.  If any document has no ModelStates, or only the one original ModelState, then there is no 'factory' document, just the regular document.  Once there are 2 or more ModelStates present, then the 'factory' will be the document under the control of the file's 'active' ModelState, which is not always the original (Master/Primary) one.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

brianA32ZM
Advocate
Advocate

Thank you @WCrihfield I appreciate the help! I will have to look deeper into ModelStateUtils.  

 

0 Likes