File rename popup

File rename popup

Welbilt_Designer_3
Enthusiast Enthusiast
667 Views
4 Replies
Message 1 of 5

File rename popup

Welbilt_Designer_3
Enthusiast
Enthusiast

This is actually an extension of another post Prompted name change when placing parts in an assembly hat has been resolved, now I would like to expand on.

 

I have been asked to see if I can get Inventor to automatically provide a new name extension # when placing the component in the assembly.

 

The component name structure is setup with a prefix "S-C-" with the component # "1" ending with an identifier "Metal", this is an example of a "Metal Screed Corner" The name structure cannot be changed our to our automatic BOM generator, we only change the component #. 

 

I would need a bit of code that can look at the files in the destination folder, compare them and update the component # to the next available.

 

The code I have now looks like this and is linked to a form with a rename button and is set to activate when the component is placed into an assembly.

Code Snippet

'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename 
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

'check occurance of "S-C-1-Metal" need to look for the #'Change the # to the next available #

'set the file name string to use in the input box (using the next available # in place if +1)
oFileDlg.FileName = "S-C-" & +1 & "-Metal"

'check occurance of "S-C-1-Metal" need to look for the #'Change the # to the next available #

'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName

'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

I have the filename set to insert a "1" into the filename but would like it to change  to a "2" if "1" is in use and so on.

I have a couple of extra lines describing what I would like to add.

 

Any ideas?

Accepted solutions (1)
668 Views
4 Replies
Replies (4)
Message 2 of 5

Welbilt_Designer_3
Enthusiast
Enthusiast

Sorry the code snippet didn't clearly show that I was asking for, I have offset the notes outlining what I need.

 

Code Snippet

'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename 
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

'set the file name string to use in the input box
oFileDlg.FileName = "S-C-" & +1 & "-Metal"

                 'Check for existing "S-C-1-Metal" (looking for the #)
                 'Change the # to the next available #
                 'Set the new filename

'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName

'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

 

0 Likes
Message 3 of 5

rossano_praderi
Collaborator
Collaborator

Hi,

the follow function will return the last available position + 1.

 

Function GetLastN(path As String, filter As String) As String
        Dim objRegExp As New System.Text.RegularExpressions.Regex("\d+")
        Dim mList As List(Of Int32) = New List(Of Int32)(System.IO.Directory.GetFiles(path, filter) _
                .Select(Function(a) Int32.Parse(objRegExp.Match(System.IO.Path.GetFileNameWithoutExtension(a)).Value.Trim())).ToList())
        mList.Sort()
        Return (mList.Last + 1).ToString()
End Function

 

 

Usage example

LastNumber = GetLastN(ThisDoc.WorkspacePath(), "S-C-*.ipt")

' FileName = "S-C-" & LastNumber & "-Metal"

 

 

I hope this will help you.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 4 of 5

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Welbilt_Designer_3,

 

Using DirectoryInfo of InitialDirectory, file name checked in same folder. If file name exists, file name will be incremented by one.

 

Again incremented file name rechecked in the same folder.

 

Imports System.IO

'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the assembly part name to = filename 
oDoc.DisplayName = ""

'set Part type
oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

'Check for existing "S-C-1-Metal" (looking for the #)
                 'Change the # to the next available #
                 'Set the new filename

Dim fileName As String = "S-C-" & 1 & "-Metal.ipt"
Dim fInfo As FileInfo()
Dim dirInfo As New DirectoryInfo(oFileDlg.InitialDirectory)
fInfo = dirInfo.GetFiles()

Dim i As Integer = 1
Dim file As FileInfo
Reiterate:
For Each file In fInfo
    If file.Name = fileName Then
            i = i + 1
            fileName = "S-C-" & i & "-Metal.ipt"
            Goto Reiterate
        End If
    Next

'set the file name string to use in the input box
oFileDlg.FileName = fileName                  

'work with an error created by the user backing out of the save 
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()


'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
MyFile = oFileDlg.FileName

'save the file 
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As
End If

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 5

Welbilt_Designer_3
Enthusiast
Enthusiast

Thanks Chandra Shekar G,

 

That worked exactly as I had hoped, I especially like that you put the code segment into the rule that I was using. Being Monday I would likely have missed something and spent an hour tracking it down.

0 Likes