Monday, February 28, 2011

iLogic Audio Alerts: Set up Autodesk Inventor to give you a scolding!

Issue:
You're not satisfied with polite message boxes to remind you (and/or your fellow Inventor users) to complete common tasks such as filling out iProperties. Instead you'd like to use an iLogic rule to play an audio alert.

Solution:
You can create a rule in your Inventor file, and then use CreateObject to call the Windows media player, and specify a sound file to play. Then you can use a conditional If, Then statement to check to see if the iProperty is empty. And finally you can set up an Event Trigger to run the rule to fire after the file is saved.

Below is a screen shot of the message box and a video of the rule in action, using a wave file of a popular cartoon character exclaiming "ah crap!", which is usually the thought I have when I realize I forgot to fill out the iProperties... again!


Here's a small video of this in action with sound (turn down your speakers if needed).


Here is the sample code.


'start of iLogic Code-----------------------------
‘declare variables
‘get the user name from the Inventor Options
myName= ThisApplication.GeneralOptions.UserName

‘get the iProperty Description Field
Description = iProperties.Value("Project", "Description")

‘create a message for the user
mymessage = (myname & ",  you forgot to fill out the description again.")

‘set the path of the sound file to use
sound_alert = "C:\Users\Curtis\Music\ah_crap.wav"

‘check the iProperty Description Field to see if it is empty
If Description = "" Then
'play a sound
player = CreateObject("MediaPlayer.MediaPlayer.1")
player.FileName = sound_alert

'display a message box
MessageBox.Show(mymessage, "iLogic Reminder")
End If
'end of iLogic Code-----------------------------







Ok, so that was fun!
But why stop there? Here is a variation on this theme using the Windows text to speech utility.  This utility, will read aloud the message from your iLogic code, using a computer generated voice. It's fun to mess around with, but can also be useful. And it is typically installed on most standard versions of Windows.
http://en.wikipedia.org/wiki/Microsoft_text-to-speech_voices

I saw an example of this posted by Rob Cohee, but in his example his computer was a bit more civil than mine!




Here is the sample code.

'start of iLogic Code-----------------------------
‘declare variables
‘get the iProperty Revision Number Field
Revision_Number = iProperties.Value("Project", "Revision Number")

‘create a message for the user
mymessage = ("Really? You're just going to leave the Revision Number blank? You Looser.")

If Revision_Number = "" Then
      '___Use windows voice command____________
      Dim objSPVoice,colVoices
      objSPVoice = CreateObject("SAPI.SpVoice")
      objSPVoice.Speak (mymessage)
      'display a message box
      MessageBox.Show(mymessage, "iLogic Reminder")
End If
'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).