Unexpected error message - "ThisAssembly"

Unexpected error message - "ThisAssembly"

Anonymous
Not applicable
504 Views
5 Replies
Message 1 of 6

Unexpected error message - "ThisAssembly"

Anonymous
Not applicable

I have the following code as the first lines in a rule I wrote last week, as graceful error handling if the rule is called from a non-assembly document:

Dim oAsm As Document = ThisApplication.ActiveDocument
	Dim oCompDef As ComponentDefinition = oAsm.ComponentDefinition
	If Not oAsm.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("You cannot call this rule in a non-assembly document.", , "Error")
		Exit Sub
	End If

When run in the rule I wrote last week, it works perfectly, and I get the MsgBox and it exits the rule. However, I copied and pasted that code into a NEW rule I am working on today, and instead, I get the following error message when the rule is run from a part document:

System.ArgumentException: ThisAssembly: This document "Part1" is not an assembly.
   at Autodesk.iLogic.Core.ForRuleCode.ManagedAssembly.get_Document()
   at Autodesk.iLogic.Core.ForRuleCode.ManagedAssembly.get_ManagedConstraints()
   at ThisRule.SetHost(IRulesHost host)
   at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

This must be occurring at compilation, because I put a "MsgBox("Debug")" in as the VERY first line of code inside the rule, and it doesn't even pop up. What's more, I am not USING "ThisAssembly" anywhere in my code.

Can someone clarify what is happening here?

0 Likes
505 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

After further debugging, this is apparently the line that is causing the issue, but I can't figure out why.

Constraints.AddMate("ConstraintName:" & iter, part1.Name, "Z Axis", part2.Name, "INNER_FACE", e1InferredType := InferredTypeEnum.kInferredLine, e2InferredType := InferredTypeEnum.kInferredLine, solutionType := MateConstraintSolutionTypeEnum.kUndirectedSolutionType)

 

0 Likes
Message 3 of 6

JelteDeJong
Mentor
Mentor

im not sure what is happening. but i always try to avoid lines like:

Dim oAsm As Document = ThisApplication.ActiveDocument

The problem: "ActiveDocument" is not always what you expect it to be. it is possible that you code (in the part) gets triggerd as part of an update from an assembly. in that case the code in you part rule takes the assembly as "ActiveDocument". (In some cases that can be usefull. see this post.) Anyway i would try using

Dim oAsm As Document = ThisDoc.Document

In this way oAsm will always be the document that you expect it to be.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 4 of 6

Anonymous
Not applicable

From what I have read in the forums and the product documentation, "ThisDoc.Document" is actually NOT always what document you expect it to be = If you are in edit mode in a part within an assembly "ThisDoc.Document" is the part under edit, whereas "ThisApplication.ActiveDocument" is the assembly. Either way, it's the "Constraint.AddMate" that is failing.

I tried changing it to:

oConstraints.AddMateConstraint2({part1Occ.Name, "Z Axis"}, {part2Occ.Name, "INNER_FACE"},, e1InferredType := InferredTypeEnum.kInferredLine, e2InferredType := InferredTypeEnum.kInferredLine, solutionType := MateConstraintSolutionTypeEnum.kUndirectedSolutionType).Name = "GrabRailConstraint:" & iter

But it kept giving me the error messages:
Argument not specified for parameter 'Offset' of 'Public Function AddMateConstraint2(EntityOne as Object, Entity2 as Object, Offset As Object, [EntityOneInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference], [EntityTwoInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference])
'e1InferredType' is not a parameter of 'Public Function AddMateConstraint2(EntityOne as Object, Entity2 as Object, Offset As Object, [EntityOneInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference], [EntityTwoInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference])
'e2InferredType' is not a parameter of 'Public Function AddMateConstraint2(EntityOne as Object, Entity2 as Object, Offset As Object, [EntityOneInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference], [EntityTwoInferredType as Inventor.InferredTypeEnum = InferredTypeEnum.kNoInference])

I believe this is because there is no override that ends with the solution type, (only two overrides: one with NO optional inputs specified, and one with ALL optional inputs specified) and I needed to change it to:

oConstraints.AddMateConstraint2({part1Occ.Name, "Z Axis"}, {part2Occ.Name, "INNER_FACE"},, e1InferredType := InferredTypeEnum.kInferredLine, e2InferredType := InferredTypeEnum.kInferredLine, solutionType := MateConstraintSolutionTypeEnum.kUndirectedSolutionType,,).Name = "GrabRailConstraint:" & iter

 

Also, when I am in BOTH the "e2InferredType" and "solutionType" sections, the pop up tooltip shows me as being in the "solutionType" section of the override, despite there being a comma in between separating them ... and all other sections display correctly.

 

Continuing to try to debug this, but this is nonsensical to me.

And now I get the error:
"Named argument expected".

0 Likes
Message 5 of 6

Anonymous
Not applicable

Figured out the issue (don't want "e1:=", since we're just specifying) so saving the rule doesn't produce any errors, but running the rule now gives the error:

System.InvalidCastException: Specified cast is not valid.
   at System.Runtime.InteropServices.Marshal.GetIDispatchForObjectNative(Object o, Boolean onlyInContext)
   at System.Runtime.InteropServices.DispatchWrapper..ctor(Object obj)
   at System.RuntimeType.WrapArgsForInvokeCall(Object[] aArgs, Int32[] aWrapperTypes)
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Inventor.AssemblyConstraints.AddMateConstraint2(Object EntityOne, Object EntityTwo, Object Offset, InferredTypeEnum EntityOneInferredType, InferredTypeEnum EntityTwoInferredType, MateConstraintSolutionTypeEnum SolutionType, Object BiasPointOne, Object BiasPointTwo)
   at ThisRule.Main()
   at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

Which is once again stumping me, since I have included all the necessary parameters - with the exception of not declaring BiasPointOne or BiasPointTwo, but the documentation specifically says "If a bias point is not given, one is calculated that is at the center of the parameter range of the input entity.", so they shouldn't be required to be declared?

0 Likes
Message 6 of 6

Anonymous
Not applicable

Also - this doesn't matter ANYWAY, irregardless of coding style preferences, because it's the "Constraint.AddMate" that is causing the error when run from a part and not the cast to document type. Apparently "Constraint.AddMate" invisibly calls "ThisAssembly" and apparently "ThisAssembly" causes a crash BEFORE CODE EXECUTION when run from a part document, neither of which is explained in the documentation/API, and neither of which makes any sense.

0 Likes