Tuesday, March 15, 2011

Using a Yes No Message Box in Your iLogic Rule

Issue:
You have a rule that requires you to ask the user a Yes/No question in order to proceed. The action required is determined by the user's answer. If the user answers No, you want to disregard previous user input and set the parameter back to its original value before the rule was engaged.


Solution:
Here is one solution, although there are certainly others.

You can use the MessageBoxButtons.YesNo option appended to a message box in order to provide the user with a yes and no button to select from. And you can create a temporary variable to hold the original length for later use in the rule, if the user's answer requires that value to be reset.






In the example code provided below the user is asked to enter a Length.



If the Length is larger than the Width, the user is prompted with a Yes or No question.





If Yes is selected, the user is prompted to set the Width.





If No is selected, the Length is reset to its original value and it is displayed in a message box for the user to see.



Here is the example iLogic code:
In order to test this, you can create a simple block with three parameters, named Length, Width and iTrigger0.
Then just create a rule and paste this code into it.



'start of iLogic code

'trigger rule for testing
trigger = iTrigger0

'gather Length value
tempLength = Length

'gather input from user. Use current parameter as input default
Length = InputBox("Enter Length", "iLogic Length", Length)

'check length
If Length > Width Then

'query user

question = MessageBox.Show("Do you want to adjust the Width?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)

          'set condition based on answer
            If question = vbyes then
         
          'gather input from user, uses current parameter as input default
            Width = InputBox("Enter Width", "iLogic Width", Width)

          Else if question = vbno then
         
          'set parameter back to its original value
            Length tempLength
            'confirm action
            MessageBox.Show("Length = " & Length, "iLogic Message")
          End if
Else
End if

iLogicVb.UpdateWhenDone = True
  
'end of iLogic code


Look for more iLogic examples on this blog or in the chapter dedicated to iLogic Basics in the next edition of Mastering Autodesk Inventor book (due out in June of 2011 if all goes well).