Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Help with creating an assembly constraint with offset that uses an expression

JBerns
Advisor

Help with creating an assembly constraint with offset that uses an expression

JBerns
Advisor
Advisor

Community,

 

I have searched Help, Forums, and have asked Gemini to write ilogic code, but have been unsuccessful in creating an assembly constraint that uses an expression for the offset.

 

Here is the code I have tried:

Dim offsetExpression As String = "Return_Rail_Liner_Thickness * -1.0 ul"
oConstraintSub = oAsmCompDef.Constraints.AddFlushConstraint(oAsmCompDef.WorkPlanes("XY Plane"), oproxyPlaneAFLXY, Offset:=offsetExpression)

 

The error message is, "The parameter is incorrect."

 

I know the rest of the line is good because I was originally using an offset value of 0 and the constraint was created.

 

Within the iLogic Editor, I used the relationship capture tool. This was the result:

JBerns_0-1732225231957.png

 

I thought it was bad practice to use parameters as shown in gold above.

 

I tried this - after creating the constraint with an offset of 0 (zero), I tried to edit the constraint:

oConstraintSub.Offset = offsetExpression

but Inventor reports that the Offset is Read-Only.

 

Your assistance would be appreciated to create an assembly constraint using an expression for the offset.

If there is a reference or an example for this, I would welcome the link.

 

Thank you for your time and attention. I look forward to your replies.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Reply
Accepted solutions (3)
406 Views
12 Replies
Replies (12)

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@JBerns 

 

Hey Jerry hope you're doing well! This worked for me using the iLogic functions.

 

Dim PT_100_1 = Components.Add("PT_100:1", "PT_100.ipt", grounded := True)
Dim PT_300_1 = Components.Add("PT_300:1", "PT_300.ipt")

Constraints.AddMate("Mate:1", "PT_100:1", "Face1", "PT_300:1", "Face0")
Constraints.AddFlush("Flush:1", "PT_300:1", "Face1", "PT_100:1", "Face2")

Dim offsetExpression As String = "Return_Rail_Liner_Thickness * -1.0 ul" Constraints.AddFlush("Flush:2", "PT_300:1", "Face2", "PT_100:1", "Face3",offsetExpression)

 

Curtis_Waguespack_0-1732231850487.png

 

0 Likes

JBerns
Advisor
Advisor

@Curtis_Waguespack,

 

Thanks for the quick reply.

I noticed your line of code with the offset has more parameters as compared to mine.

My code was working with 0 for the offset argument, but failed when I tried to use a non-zero argument.

I will explore these differences tomorrow at work.

Thanks again for the example.

 

Regards,

Jerry 

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

Michael.Navara
Advisor
Advisor
Accepted solution

HI @JBerns 

The reason why the parameter is incorrect is that the Offset property is of type Parameter. You need to set its Expression property. See the code below

 

Dim asm As AssemblyDocument = ThisDoc.Document
Dim occ1 As ComponentOccurrence = asm.ComponentDefinition.Occurrences(1)
Dim occ2 As ComponentOccurrence = asm.ComponentDefinition.Occurrences(2)

Dim occ1WorkPlane As WorkPlane = occ1.Definition.WorkPlanes("Work Plane1")
Dim occ2WorkPlane As WorkPlane = occ2.Definition.WorkPlanes("Work Plane1")

Dim occ1WorkPlaneProxy As WorkPlaneProxy 
occ1.CreateGeometryProxy(occ1WorkPlane, occ1WorkPlaneProxy)
Dim occ2WorkPlaneProxy As WorkPlaneProxy
occ2.CreateGeometryProxy(occ2WorkPlane, occ2WorkPlaneProxy)

Dim offsetExpression1 As String = "10 mm"
Dim flushConstr As FlushConstraint = asm.ComponentDefinition.Constraints.AddFlushConstraint(occ1WorkPlaneProxy, occ2WorkPlaneProxy, offsetExpression1)

Dim offsetExpression2 As String = "20 mm"
flushConstr.Offset.Expression = offsetExpression2

 

 

 

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@JBerns wrote:

@Curtis_Waguespack,

 

Thanks for the quick reply.

I noticed your line of code with the offset has more parameters as compared to mine.

 


I think the difference you're noticing is due to the use of the API function "AddFlushConstraint" vs the iLogic function "AddFlush", that do pretty much the same thing.

 

The iLogic function is what you get when you use Capture Current State.

 

Inventor API function to create a flush constraint

 

oAsmCompDef.Constraints.AddFlushConstraint(oAsmCompDef.WorkPlanes("XY Plane"), oproxyPlaneAFLXY, Offset:=offsetExpression)

 

 

iLogic function to create a flush constraint

 

Constraints.AddFlush("Flush:2", "PT_300:1", "Face2", "PT_100:1", "Face3")

 

 

The iLogic function will accept a string as the offset value and use it as an expression, without the need to do anything else. The API function requires an extra step as Michael.Navara's example shows.

 

But it might help to understand the difference, by understanding how to work with both functions and their resulting objects, together.

 

To do that we could do something like the example below where we add the constraint using the iLogic function, but capture it with a variable, for use later.

 

It is created as an iLogic IManagedConstraint object. As can be seen in the tooltip by hovering over the ilogic AddFlush function.

 

Curtis_Waguespack_4-1732263673333.png

 

This allows you to add the constraint and then access it again later to set the offset.

 

I might use something like this if I were adding components to an assembly, constraining them in place, and then using some calculation or condition later in the rule to change the location of one of them.

 

To do that we just need to sort of take the mask off of the iManagedConstraint object to find the underlaying API constraint object, so that we can do API stuff

 

Curtis_Waguespack_5-1732263723667.png

 

You might recall  this image from Autodesk University. 

Curtis_Waguespack_3-1732263130068.png

The short version is that we can get the underlaying API object from pretty much any iLogic object.

 

Dim PT_100_1 = Components.Add("PT_100:1", "PT_100.ipt", grounded := True)
Dim PT_300_1 = Components.Add("PT_300:1", "PT_300.ipt")

Constraints.AddMate("Mate:1", "PT_100:1", "Face1", "PT_300:1", "Face0")
Constraints.AddFlush("Flush:1", "PT_300:1", "Face1", "PT_100:1", "Face2")
MyiLogicFlush = Constraints.AddFlush("Flush:2", "PT_300:1", "Face2", "PT_100:1", "Face3")

'some other code here, to do stuff, with things
'some other code here, to do stuff, with things
'some other code here, to do stuff, with things

'get the API constraint object from the iLogic constraint object Dim oFlush As FlushConstraint = MyiLogicFlush.Constraint
'define the offset expression Dim offsetExpression As String = "Return_Rail_Liner_Thickness * -1.0 ul"

'set the expression oFlush.Offset.Expression = offsetExpression

 

JBerns
Advisor
Advisor
Curtis,
I noticed the difference between our code.
I was constraining work planes of two parts with an offset=0.
When I wanted to use a string expression instead of 0, that is when the error appeared.
-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

JBerns
Advisor
Advisor
Michael,
On line 6, just had to change 'occ1' to 'occ2'.
Works great!
-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes

JBerns
Advisor
Advisor

@Curtis_Waguespack / @Michael.Navara,

Apparently you have to post on the forum in order to find the mistake in your own code.

I was constraining two components in a subassembly.

The subassembly was being created on the fly from a template.

What I did not notice, until NOW, was that the 'Return_Rail_Liner_Thickness' parameter was not defined in the template.

I had used similar code using expressions to constrain top-level components - where the parameter WAS defined.

Can't constrain in a subassembly if parameter is missing. That explains why offset=0 worked, but an expression with the parameter name failed.

Thank you all for helping me find this embarrassing mistake.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional

Curtis_Waguespack
Consultant
Consultant

@JBerns, I just thought of something...

 

... make sure that 'Return_Rail_Liner_Thickness' parameter is defined in the template! :face_with_tears_of_joy: :rolling_on_the_floor_laughing: :face_savoring_food:

 

The thing I do all the time is try to use part of a file name, and I get everything working, and then I do some testing using and unsaved file, hit the error because the name is nothing, and spend way too much time trying to figure it out.... almost every time :upside_down_face:

 

Curtis_Waguespack_0-1732289139513.png

 

0 Likes

JBerns
Advisor
Advisor
Curtis,
I have added code to ensure the parameter is present in the template.
I enjoyed the TGIF image.
Regards,
Jerry
-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional

Curtis_Waguespack
Consultant
Consultant

@JBerns wrote:
Curtis,
I have added code to ensure the parameter is present in the template.


I was just trying to be goofy, by saying the thing that you discovered as if it was my epiphany  ( a poor attempt at dry humor on a forum) . :upside_down_face:

Michael.Navara
Advisor
Advisor

JMGunnar
Collaborator
Collaborator

Simply way too add experssion too constraints 



Constraints
.AddMate("Mate:1", "Part2:1", "Face0", "Part2:2", "Face1", oOffset & "Offset = Width")

 Best Regards Johan G

@JBerns 

 

0 Likes