Blog posts

Bio

by Luke Davenport

Problem:

You have some iLogic code that exports a file of some kind (STEP file, PDF, DXF etc.) or performs a ‘Save As’ operation. And you want the exported file name to increment with a new version number suffix if an existing file export is found in the export folder.

 

Solution:

Try the simple code below to check for existing files and add a ‘V1’, ‘V2’, ‘V3’ after the filename as required.

 

Explanation:

Every company has slightly different requirements for exporting files from Inventor. And therefore there’s no such thing as a ‘one size fits all’ export format. This little bit of code may get you started though.

 

Let’s use an example of the tasty turbine model in the picture above (downloaded from Grabcad – thanks to Edward Otter). The file is called ‘Turbine.ipt’. Here’s what the rule will do:

 

1)      Find the name of the current Inventor file (e.g. Turbine.ipt)

2)      Check to see if a file called ‘Turbine_V0.stp’ exists in the same folder as Turbine.ipt.

  1. If it doesn’t – export ‘Turbine_V0.stp’.
  2. If it does – check to see if ‘Turbine_V1.stp’ exists, and keep checking higher numbers until an unused filename is found – e.g. ‘Turbine_V6.stp’ – then export this file.

3)      Ask the user if they would like to open the export folder to check the results, and open the folder if they click ‘Yes’

4)      Job done.

Note that you can easily modify this code depending on which type of file you are exporting. The same principle will work with any file extension that is exportable from Inventor.

 

I hope you find this useful – ‘til next time!

 

' Start of iLogic code *******************************************
Stepfilename = ThisDoc.PathAndFileName(False)
Counter = 0
FileExists = True
 
'check to see if the file to be exported already exists
Do While FileExists    
    ' Define name of exported file - note a .stp file extension 
    ' Is currently being used. In this example I am exporting a Step file
    CurrentFile = Stepfilename & "_V" & Counter & ".stp"
    
    If Dir(CurrentFile) <> "" Then ' The file does exist
        Counter += 1
        FileExists = True
    Else
        SaveAs = MessageBox.Show("Export file as '" & CurrentFile & _
        "'?", "Cadline iLogic", _
        MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
        MessageBoxDefaultButton.Button1)
        If SaveAs = vbNo Then
            Return
        Else ' User says continue
            FileExists = False
        End If
    End If
Loop
 
' Put your export or 'save as' code in here ***********************
 
' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById _
("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
 
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument _
, oContext, oOptions) Then
    oOptions.Value("ApplicationProtocolType") = 3
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
    
    ' Set export name of STEP file
    oData.FileName = CurrentFile
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext _
, oOptions, oData)
End If
 
' End of export code ***********************************************
 
' Ask user if they want to open the export folder
OpenFolder = MessageBox.Show("Export successful! " & _
"- open containing folder now?", "Cadline iLogic", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)
 
If OpenFolder = vbYes Then
    Process.Start("explorer.exe", ThisDoc.Path)
Else ' User says continue
    'Return
End If
 
' End of iLogic code **************************************************