Migrate files VB.NET

Migrate files VB.NET

chrisw01a
Collaborator Collaborator
2,033 Views
38 Replies
Message 1 of 39

Migrate files VB.NET

chrisw01a
Collaborator
Collaborator

Hello Group,

I have a program that we use to copy and rename files. I need the program to automatically migrate any files that need migrated prior to manipulating them. I already have code to display a message box containing a list of the files that need migrated so I feel like most of the work is done. I just do not have the knowledge of where to start this. Can someone get me going in the right direction?

 

Here is the gist of what I have:

 

'Collection to store the filenames that need migrated to inform the user
Dim needsMigrated As New System.Collections.ArrayList

'Open the selected Files
openedDoc = apprentice.Open(selectedFile)

'Make sure the version is current
If openedDoc.NeedsMigrating = True Then
addUnmigratedFile(openedDoc.FullFileName)
End If

If needsMigrated.Count <> 0 Then
Call showUnmigrated()
End If

Private Sub addUnmigratedFile(ByVal unmigratedFile As String)

'Add the file to the needsMigrated collection to inform the user
needsMigrated.Add(unmigratedFile)

End Sub

Private Sub showUnmigrated()

Dim theFiles As String = needsMigrated.Item(0) & vbCrLf

For x As Integer = 1 To needsMigrated.Count - 1
theFiles += needsMigrated.Item(x) & vbCrLf
Next

MsgBox("The file(s) need updated to the current version:" & vbCrLf & theFiles, MsgBoxStyle.Exclamation)

Call resetForm()

End Sub

 

 

Thank you!

Chris

0 Likes
2,034 Views
38 Replies
Replies (38)
Message 2 of 39

JelteDeJong
Mentor
Mentor

Migrating files is as simple as saving the file with the Inventor version you want to migrate to. So you can just open the file with the (apprentice) inventor that you want to migrate to. and save the file.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 39

bradeneuropeArthur
Mentor
Mentor

Or use the application option to do it on the fly.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 39

chrisw01a
Collaborator
Collaborator
Will look into it. Thanks
0 Likes
Message 5 of 39

chrisw01a
Collaborator
Collaborator
Where is this option? Thanks
0 Likes
Message 6 of 39

WCrihfield
Mentor
Mentor

WCrihfield_0-1679579760492.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 39

chrisw01a
Collaborator
Collaborator
That doesn't work for this situation that I can tell. Thanks
0 Likes
Message 8 of 39

chrisw01a
Collaborator
Collaborator

It says the file must be migrated before using the apprentice.FileSaveAs.

 

chrisw01a_0-1679585217099.png

 

0 Likes
Message 9 of 39

chrisw01a
Collaborator
Collaborator

JelteDeJong,
I am struggling here. I thought since the document is already opened, I could just add it to the "FileSaveAs" and then use FileSaveAs.ExecuteSave(). However, it throws an exception when I try to create the "FileSaveAs". This makes no sense because I am using the exact same code in a different private sub to do the save as function and it works just fine...

chrisw01a_0-1680118582878.png

 

 

'Open the selected Files
openedDoc = apprentice.Open(selectedFile)

' Get the FileSaveAs object. (THIS LINE STOPS THE PROGRAM)
Dim fileSaveAsMigrate As Inventor.FileSaveAs = apprentice.FileSaveAs

'Make sure the version is current
If openedDoc.NeedsMigrating = True Then
        ' Add the Parent Document to the fileSaveAs object
	fileSaveAsMigrate.AddFileToSave(openedDoc, openedDoc.FullFileName)
End If

If needsMigrated.Count <> 0 Then
	fileSaveAsMigrate.ExecuteSave()
End If

 

0 Likes
Message 10 of 39

JelteDeJong
Mentor
Mentor

I have some bad news:

JelteDeJong_0-1680120164896.png

https://modthemachine.typepad.com/my_weblog/2010/03/iproperties-without-inventor-apprentice.html

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 11 of 39

chrisw01a
Collaborator
Collaborator
Can't we do it through inventor directly with the VB code?
0 Likes
Message 12 of 39

JelteDeJong
Mentor
Mentor

You can migrate files just not with the apprentice. You will need a full-blown Inventor to do it.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 13 of 39

chrisw01a
Collaborator
Collaborator
Since I am already building a list of all the files that need migrated, can't we call Inventor to open each one and save it, then close it?
0 Likes
Message 14 of 39

WCrihfield
Mentor
Mentor

This is probably a dumb question, but why not just use the Autodesk Task Scheduler to migrate the files some time that is more convenient?

To Migrate Files (Task Scheduler) 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 15 of 39

chrisw01a
Collaborator
Collaborator
Certainly not a dumb question. In the past, we have done that. HERE are the issues:
1. We have destroyed a ton of our standard models because task scheduler created some sort of disconnect between our master sketch and the parts / assemblies tied to it. We used to just do our entire standard directory until that happened a couple of times. Now I don't trust it.
2. A lot of the time we are copying old jobs to a new job and that is what my FileCopy program does. It copies to a new project and changes the name of all the referenced files to match the new job number. So we would have to task schedule on an individual basis. It is faster to just manually copy the files to the new folder, open all files, save all files, then run my FileCopy program to rename the files in place.
Message 16 of 39

chrisw01a
Collaborator
Collaborator
Do you know of a way to access Inventor through my iLogic code to open and save each file? Thank You.
0 Likes
Message 17 of 39

kriegler
Contributor
Contributor

You can try this solution written in VB.net the best way is to do it in an AddIn or iLogic

 Private m_ListOfMigrateFiles As List(Of String)

    ''' <summary>
    ''' Checks the given Assembly document
    ''' </summary>
    ''' <param name="document"></param>
    Public Sub CheckMigration(ByVal document As Inventor.Document)

        m_ListOfMigrateFiles = New List(Of String)

        If document.NeedsMigrating Then
            m_ListOfMigrateFiles.Add(document.FileName)
        End If
        Recurse(document.File.ReferencedFileDescriptors)

        Dim bSilent As Boolean = InventorApplication.SilentOperation
        InventorApplication.SilentOperation = True

        If m_ListOfMigrateFiles.Any Then
            For Each sFile As String In m_ListOfMigrateFiles
                Dim tempDoc As Inventor.Document = Nothing
                Try
                    tempDoc = InventorApplication.Documents.Open(sFile, False)
                    tempDoc.Save()
                Catch ex As Exception
                    If tempDoc IsNot Nothing Then
                        tempDoc.Close()
                    End If
                End Try

            Next
        End If

        InventorApplication.SilentOperation = bSilent
        Debug.WriteLine(m_ListOfMigrateFiles.Count.ToString)
        Debug.WriteLine("Finish")
    End Sub



    ''' <summary>
    ''' Recursive method for referenced Files
    ''' </summary>
    ''' <param name="refs"></param>
    Private Sub Recurse(ByVal refs As Inventor.FileDescriptorsEnumerator)

        ' Loop through the references in the document
        For Each r As Inventor.FileDescriptor In refs
            Dim file As Inventor.File = r.ReferencedFile

            Select Case r.ReferencedFileType
                Case Inventor.FileTypeEnum.kAssemblyFileType, Inventor.FileTypeEnum.kPartFileType

                    If NeedsMigrationEx(file) Then
                        If Not m_ListOfMigrateFiles.Contains(r.FullFileName) Then
                            m_ListOfMigrateFiles.Add(r.FullFileName)
                        End If
                    End If
                Case Else
                    Continue For
            End Select

            If r.ReferencedFileType = Inventor.FileTypeEnum.kAssemblyFileType Then
                ' Recurse through the sub assembly files
                Recurse(file.ReferencedFileDescriptors)
            End If

        Next
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="objFile"></param>
    ''' <returns></returns>
    Private Function NeedsMigrationEx(ByVal objFile As Inventor.File) As Boolean

        Dim locObjFileDisplayVersion As String = objFile.SoftwareVersionSaved.DisplayVersion.Substring(0, 4)
        Dim locAppDisplayversion As String = InventorApplication._inventor.SoftwareVersion.DisplayVersion.Substring(0, 4)

        If locObjFileDisplayVersion <> locAppDisplayversion Then
            'Debug.WriteLine(locObjFileDisplayVersion & ", " & objFile.FullFileName)
            Return True
        Else
            Return False
        End If
    End Function

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards

Johann

0 Likes
Message 18 of 39

kriegler
Contributor
Contributor

..a slightly more elegant solution

   Public Sub CheckMigration2()

        Dim locAktivDoc As Inventor.Document = InventorApplication._inventor.ActiveDocument
        If locAktivDoc.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
            MessageBox.Show("An Assembly Document must be open")
            Return
        End If

        Dim locAssemblyDocument As Inventor.AssemblyDocument = CType(locAktivDoc, Inventor.AssemblyDocument)

        Dim _fileManager As Inventor.FileManager = InventorApplication._inventor.FileManager
        Dim locAppDisplayversion As String = InventorApplication._inventor.SoftwareVersion.DisplayVersion.Substring(0, 4)

        Dim retList As IEnumerable(Of Inventor.File) = locAssemblyDocument.File.AllReferencedFiles.Cast(Of Inventor.File).
                                                                Where(Function(x) _fileManager.IsInventorComponent(x.FullFileName) AndAlso
                                                                locAppDisplayversion <> x.SoftwareVersionSaved.DisplayVersion.Substring(0, 4))

        Dim intCounter As Integer = 0
        Dim intCount As Integer = 1
        Dim bSilent As Boolean = InventorApplication._inventor.SilentOperation
        InventorApplication._inventor.SilentOperation = True

        If retList.Any Then
            For Each retFile As Inventor.File In retList
                Dim tempDoc As Inventor.Document = Nothing
                Try
                    tempDoc = InventorApplication._inventor.Documents.Open(retFile.FullFileName, False)
                    tempDoc.Save()
                    intCounter += 1
                Catch ex As Exception

                Finally
                    If tempDoc IsNot Nothing Then
                        tempDoc.Close()
                    End If
                End Try

            Next
            intCount += retList.Count
        End If

        If locAssemblyDocument.NeedsMigrating Then
            locAssemblyDocument.Save()
            intCounter += 1
        End If

        InventorApplication._inventor.SilentOperation = bSilent
        If intCounter > 0 Then
            Debug.WriteLine(intCounter.ToString & " out of " & retList.Count.ToString & " files were migrated")
        Else
            Debug.WriteLine("no files had to be migrated")
        End If
        Debug.WriteLine("Finish")
    End Sub

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards

Johann

Message 19 of 39

kriegler
Contributor
Contributor

please replace this line

'Debug.WriteLine(intCounter.ToString & " out of " & retList.count.ToString & " files were migrated")

Debug.WriteLine(intCounter.ToString & " out of " & intCount.ToString & " files were migrated")
0 Likes
Message 20 of 39

chrisw01a
Collaborator
Collaborator
kriegler,
This looks very promising. When I have some time, I'll check it out. Very much appreciated for taking the time!!
0 Likes