Showing posts with label Excel Export. Show all posts
Showing posts with label Excel Export. Show all posts

Thursday, June 30, 2011

iLogic: Export Parts List with Options

Issue:
You want to use some iLogic code to automatically export your parts list to an XLS file and set the options and formatting.






Solution: 
Setting options can be helpful when you use an XLS file to hand off BOM data to a MRP system. Typically when doing this kind of thing having the formatting consistent is important. Using iLogic to set these options will ensure human error does not cause a problem down stream in the process.

Another way you use the exported parts lists is for costing. If you generate take offs or quotes for your sales staff, or use Inventor for small projects where you'd like to quickly create estimates, you can setup an XLS template file containing pricing information and then use it to import the parts list data.

Here I'll share some sample code that exports a parts list and uses the export options to specify a custom XLS file template, choose which columns to export, choose which cells to populate and set other options. I've set up the XLS template to do some calculations on the data automatically as an extra touch.

Here is the drawing file I'll use for this example:


To understand the options let's first look at the options available when you right-click on a parts list table and choose Export. Here you can see the options found after clicking the Options button when exporting the parts list as an Excel file:



Here are the results of exporting a typical parts list with no options set. I use a basic exported XLS file such as this to hand data to our MRP system for all of our Inventor drawings.



Here are the results of setting some of the options, such as the Start Cell set to A3, the parts list Title, selecting only 3 columns, and setting the column widths to auto fit:



Here is an XLS file I've customized to use as an export template. This file provides a destination for the Quantity, Part Number, and Description column data. It also contains a look up list of cataloged part numbers and  prices. The goal is to export the Parts List from the Inventor drawing and automatically insert it into this template. This allows a formula in the Unit Cost column to match the part number from the Part Number column with the PN column and then insert the corresponding value from the Price column. The Extended Cost column will then multiply the Unit Cost column with the QTY column:
Click to Enlarge
 Here are the results of the export using the template.

Click to Enlarge
The vertical look up formula used in the Unit Cost cells is:
=(VLOOKUP(B5,$G$5:$H$13,2,FALSE))
Here's a link to a video detailing Excel's vlookup function, in case you've never used it before.

And the formula used in the Extended Cost cells is simply:
=D5*A5

 Here is the the example iLogic code. You could set this up to run on the After Save Document event trigger so that the rule is triggered automatically each time the file is saved, or you could run it manually.



'-------------Start of ilogic ------------------------------------------------
'get the path and name of the drawing file
path_and_name = ThisDoc.PathAndFileName(False) ' without extension

'define oDoc
oDoc = ThisDoc.Document

'specify the drawing sheet
oSheet = oDoc.Sheets("Sheet:1") ' sheet by name
'oSheet = oDoc.Sheets(1) ' first sheet

 ' say there is a Partslist on the sheet.
oPartslist = oSheet.PartsLists(1)
     
' create a new NameValueMap object
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

'specify an existing template file
'to use For formatting colors, fonts, etc
oOptions.Value("Template") = "C:\Temp\PartListExport.xls"
 
'specify the columns to export         
oOptions.Value("ExportedColumns") = "QTY;PART NUMBER;DESCRIPTION"
 
'specify the start cell
oOptions.Value("StartingCell") = "A3"
 
'specify the XLS tab name
'here the file name is used
oOptions.Value("TableName") = ThisDoc.FileName(False) 'without extension

'choose to include the parts list title row
'in this example "Ye Old List of Parts" is written to the StartingCell
oOptions.Value("IncludeTitle") = True          

'choose to autofit the column width in the xls file
oOptions.Value("AutoFitColumnWidth") = True
       
' export the Partslist to Excel with options
oPartslist.Export(path_and_name & ".xls", _
PartsListFileFormatEnum.kMicrosoftExcel, oOptions)  

'-------------End of ilogic ------------------------------------------------

Edits:

I ran into a situation when using the export template, where the XLS file was being appended rather than replaced. You can use this snippet to delete the existing file before saving out the new one if you run into this:
 
'check for existing XLS file and delete it if found
if Dir(path_and_name & ".xls") <> "" then
Kill (path_and_name & ".xls")
else
end if

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