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