Pages

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).

Autodesk iLogic: Highlight and Update Features During Changes

Issue:
You have some iLogic code that prompts the user for a number of different inputs. But currently the features only update once the entire rule is run. You'd like for the features to update as the user steps through the input, so they have visual input as to what they've changed. And if each feature could be highlighted as it is selected by the iLogic code, so that the user knows which feature is being changed, that would be the cat's meow!
Solution:
To pull this off you need to use the following two lines after each input is gathered:


'If your rule has changed any parameter values, this will apply the new values to the Inventor model. If you don't use this, the values are not applied until the rule has finished running.
RuleParametersOutput()

'If this is included in a rule,  iLogic will do an update for the document. This update is the same as the Update button in the user interface.
iLogicVb.UpdateWhenDone = True


To highlight the feature, simply change its color before the inputbox is issued, and then change it back immediately afterward. See the example code below.










Here is the example code:


'Start of iLogic Code--------------------------------------------------
'used to trigger rule for testing or running the rule
Trigger = iTrigger0

'ignores errors and continues on with the code
On Error Resume Next

'set the feature color to red and then prompts the user for input
'then pushes the parameter change to the part and updates before moving on
Feature.Color("Hole1") = "Red"
Dia1= InputBox("Enter Diameter of " & "Hole1", "iLogic Hole Diameter", Dia1)
Feature.Color("Hole1") = "As Part"
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True

'set the feature color to red and then prompts the user for input
'then pushes the parameter change to the part and updates before moving on
Feature.Color("Hole2") = "Red"
Dia2= InputBox("Enter Diameter of " & "Hole2", "iLogic Hole Diameter", Dia2)
Feature.Color("Hole2") = "As Part"
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True

'set the feature color to red and then prompts the user for input
'then pushes the parameter change to the part and updates before moving on
Feature.Color("Hole3") = "Red"
Dia3= InputBox("Enter Diameter of " & "Hole3", "iLogic Hole Diameter", Dia3)
Feature.Color("Hole3") = "As Part"
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True

'set the feature color to red and then prompts the user for input
'then pushes the parameter change to the part and updates before moving on
Feature.Color("Hole4") = "Red"
Dia4= InputBox("Enter Diameter of " & "Hole4", "iLogic Hole Diameter", Dia4)
Feature.Color("Hole4") = "As Part"
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True

'prompts the user for input
'then pushes the parameter change to the part and updates before moving on
Length= InputBox("Enter Length", "iLogic Length", Length)
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True

'zoom all
ThisApplication.ActiveView.Fit
'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). 

Thursday, February 24, 2011

iLogic Code for Parts Lists, Title Blocks and Saving out a PDF

Here is a snippet of iLogic code that does a short list of common tasks for drawing files:
  • Sorts the parts list by Part Number
  • Exports the Parts List to an XLS file
  • Sets the Drawn By and Date fields of a titleblock (via the drawing's iProperties)
  • Saves a copy of the drawing as a PDF file, with some of the PDF options set 
This code also contains some error checks to alert the user when the PDF or XLS already exist and can't be replaced (such as when another user has these files open).

You can just paste the code into a new rule in your drawing file to see it in action. I use this as an external rule, and bring it into existing drawings when they are edited or updated, and also have it included in our drawing template so that all new drawings include it. Having it as an external rule, allows me to add more functions as needed, and to update the code (and all of the files it's included in) quickly.

I use this code daily to manage our drawing files and output a PDF and XLS file for our document management and ERP system and have found it to work well. I've set it up to be triggered on the Save event, so that as the user saves the drawing file, the PDF and XLS are updated or output automatically. Feel free to tear this apart and customize it to fit your needs and/or set it up to run when your drawing is closed rather than saved, etc.

On a related note, I just finished a new chapter dealing exclusively with iLogic for the next edition of Mastering Autodesk Inventor book (due out in June of 2011 if all goes well).



'start of iLogic code----------------------------------------------------------------------------------
'sort parts list
on error resume next
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList1 As PartsList
oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
'If oPartsList1 Is Nothing Then Resume Next

oPartsList1.Sort("PART NUMBER")
'oPartsList1.Renumber
'oPartsList1.SaveItemOverridesToBOM

'------------------------------------------------------------------------------------------------------------
'Export Parts List
path_and_name = ThisDoc.PathAndFileName(False) ' without extension
Dim oDoc As Inventor.DrawingDocument
oDoc = ThisDoc.Document

Dim oSheet As Inventor.Sheet
'oSheet = oDoc.Sheets(1) ' first sheet
oSheet = oDoc.Sheets("Sheet:1") ' sheet by name

' say there is a Partslist on the sheet.
Dim oPartslist As PartsList
oPartslist = oSheet.PartsLists(1)

On error goto handleXLSLock
'Publish document.
' export the Partslist to Excel.
oPartslist.Export(path_and_name & ".xls",PartsListFileFormatEnum.kMicrosoftExcel

'--------------------------------------------------------------------------------------------------------------------
'set Drawn by name
iProperties.Value("Summary", "Author" ) = ThisApplication.GeneralOptions.UserName
'set date
iProperties.Value("Project", "Creation Date" ) = Now
InventorVb.DocumentUpdate()

'--------------------------------------------------------------------------------------------------------------------
'Save PDF with options
path_and_namePDF = ThisDoc.PathAndFileName(False) ' without extension
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
'oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If

'Set the destination file name
oDataMedium.FileName = path_and_namePDF & ".pdf"

On error goto handlePDFLock
'Publish document.
Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

'--------------------------------------------------------------------------------------------------------------------

exit sub

handlePDFLock:
MessageBox.Show("PDF could not be saved, most likely someone else has it open", "No PDF for you " & ThisApplication.GeneralOptions.UserName & "!")
Resume Next

handleXLSLock:
MessageBox.Show("No XLS", "iLogic")
Resume Next

'end of iLogic code-----------------------------------------------------------------------------



**** EDIT ****
8-24-2011

If you need to sort a parts list by multiple columns you can use something like this:


Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList1 As PartsList
oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort("DESCRIPTION", 1, "QTY", 1)

This results in the parts list sorting first by the DESCRIPTION column and then the QTY column in ascending order. You can use 0 for the sort Boolean to sort by descending order. For example:

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList1 As PartsList
oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort("DESCRIPTION", 0, "QTY", 0)


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).