Hi all,
I have a very basic sketch of a triangle. And my parameters are
Height = 5
Length = (driven Dimension)
Angel = 50 - 75
Max_L = 3
and I want iLogic to find the length that is under the max_L and the angel is between 50 to 75 degree.
so Here is my code:
Angel = 50
If Length > Max_L Then
While Length > Max_L
Angel = Angel + 1
If Angel = 75
Exit While
End If
End While
End If
iLogicVb.UpdateWhenDone = True
InventorVb.DocumentUpdate()
for some reason the while statement doesnt stop even the length goes uder the max_L and it only stops when the angel reaches to 75.
Solved! Go to Solution.
Solved by cwhetten. Go to Solution.
Hi and welcome to the forum!
Try this instead:
Parameter.UpdateAfterChange = True Parameter("Angle") = 50 i = 0 If Parameter("Length") < Max_L Then While (Parameter("Length") < Max_L And Parameter("Angle") < 75) Parameter("Angle") = Parameter("Angle") + 1 i = i + 1 If i = 50 Then MessageBox.Show("Iterations exceeded expected value. Exiting loop.", "Warning") Exit While End If End While End If iLogicVb.UpdateWhenDone = True
There were a couple of problems. One was that you had While Length > Max_L, but I believe your stated intention was to check if it was less than Max_L, so I changed it to While Length < Max_L.
Secondly, Inventor was not actually changing the value of Length while the iLogic rule was running. The rule was changing the value of Angle, but the model was not updating after each iteration, and so Length wasn't changing from its initial value. To fix this, I added the first line of the code you see above. That line forces the model to update immediately after each parameter change. But, it requires that you use the explicit parameter function to get and set the value of a parameter. That is why you see in the code I posted Parameter("Angle"), instead of just Angle.
Instead of an If statement to check that the angle doesn't exceed 75, I just included it as a condition for the While loop, using the And operator. As long as both of those conditions are met, the loop will continue; as soon as one condition is not met, the loop will exit.
I also added a couple of lines using the temporary variable called i, including an If statement that will exit the loop after a set number of iterations and a message box that will notify when the loop has been exited in this way. I did this because one of my tests resulted in an infinite loop, and the only way to end it was to kill Inventor. With this extra bit of code, it was possible to make sure that the infinite loop didn't occur. This isn't necessary, but it's worth considering in case something goes wrong.
I have attached the part (file is version 2012) that I created to test this, in case you want to see it in action. I have also attached a text version of the rule, in case you have troubles copying and pasting the code from the message board.
Cameron Whetten
Inventor 2012
Please click "Accept as Solution" if this response answers your question.
Thank you very much for your help. that parameter update code is sick. I didnt know there was a code exisit like that. I can find it under the snippets.
Is there any online resource for all the codes and snippets can be used for iLogic ?
And what is the diffrence between Leght or Parameter("Lenght") ?
There is a nearly comprehensive list of iLogic functions at the following link:
http://wikihelp.autodesk.com/Inventor/enu/2012/Help/0073-Autodesk73/0673-iLogic673/0676-Function676
It's a bit cumbersome in the web format. I find it's much more useful (and searchable) in PDF format. To get it in PDF format, you can click on the Print button at the top of the page and choose the option to Save as PDF:
I have attached the PDF here for your convenience. (I had to zip it because the message board kept refusing to attach the PDF, saying the file was corrupted. I just saved it from the Wikihelp page, so whatever...)
As for the difference between calling a parameter by name and using the explicit Parameter( ) function, the Parameter( ) function immediately gets or sets the value of a parameter. If you don't use the Parameter( ) function, sometimes it doesn't get or set the value until later in the execution of the rule. I haven't been able to figure out the timing. But sometimes you need the parameter value to be set before the rest of the rule runs, so it's important to use the Parameter( ) function in this case.
Another difference is this: If a parameter is used in a rule, that rule will automatically execute if the value of that parameter is changed. However, this doesn't work if the parameter is used inside the explicit Parameter( ) function. Other than this disadvantage, I have found it best to use the Parameter( ) function instead of just the name of the parameter.
Hopefully that makes some kind of sense.
Cameron Whetten
Inventor 2012
Thanks for the great post.
I am wondering if we can change
While (Parameter("Length") < Max_L And Parameter("Angle") < 75)
to While (Parameter ("Angle") < 75)
because
Parameter("Length") < Max_L
was in first "If" row already.
Baishihu
Can't find what you're looking for? Ask the community or share your knowledge.