Migrate files VB.NET

Migrate files VB.NET

chrisw01a
Collaborator Collaborator
2,096 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,097 Views
38 Replies
Replies (38)
Message 21 of 39

chrisw01a
Collaborator
Collaborator

Kriegler,
I am just getting back to this project. I have very limited programming skills and I'm already stuck. Are you available?

I am trying to get my VB.net code to open a single Inventor file so I can save it. I already have the filename "selectedFile" because I am using it with Apprentice but I am stuck already getting an error "Reference to a non-shared member requires an object reference." when trying to open the file with Inventor.

From scratch in VB.net, How do I open an Inventor file?

0 Likes
Message 22 of 39

Frederick_Law
Mentor
Mentor

The file is opened in line 284.

0 Likes
Message 23 of 39

chrisw01a
Collaborator
Collaborator

It's opened with apprentice. I need to open it with Inventor so I can save it. Line 292 is where I am starting today.

 

Thanks

0 Likes
Message 24 of 39

Frederick_Law
Mentor
Mentor

You want to open the same file twice?

0 Likes
Message 25 of 39

chrisw01a
Collaborator
Collaborator

Not necessarily but I need to learn how to open the file in Inventor so I can experiment with my program. Right now I'm doing everything with Apprentice but I may need to eliminate that and go with only Inventor or both. I'm not sure yet.

 

Would it help if I post a copy of the VB.net program? It's a lot to look through if you don't have context of what all we are doing with it...

0 Likes
Message 27 of 39

Frederick_Law
Mentor
Mentor

You can make addin and run inside Inventor.

0 Likes
Message 28 of 39

chrisw01a
Collaborator
Collaborator
I will read these. Thanks
0 Likes
Message 29 of 39

chrisw01a
Collaborator
Collaborator

Since we already have an executable published from VB.net that we use for copying and renaming files (which is the whole purpose of this), I don't want to use an addin because we already have the VB.net program doing most of what we require but now we are adding the capability to migrate files at the same time so we don't have to do it as a separate operation. The problem is that our program is based on Apprentice and it cannot migrate files. It cannot even manipulate the files until they are migrated so this is a huge pain for us.

 

Attached is the entire project. We are using Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.2.0

0 Likes
Message 30 of 39

kriegler
Contributor
Contributor

Line 292 does not work, you have to declare the Inventor Application Object.

  Public Shared Function GetInventor() As Inventor.Application
        Dim locInventor As Inventor.Application = Nothing
        Try
            locInventor = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
        Catch ex As System.Runtime.CompilerServices.RuntimeWrappedException
            MessageBox.Show("Zeile: Catch ex As System.Runtime.CompilerServices.RuntimeWrappedException, Fehler in Prozedur GetInventor" & vbNewLine & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            locInventor = Nothing
        Catch ex As System.Runtime.InteropServices.COMException
            MessageBox.Show("Zeile: Catch ex As System.Runtime.InteropServices.COMException, Fehler in Prozedur GetInventor" & vbNewLine & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            locInventor = Nothing
        Catch ex As Exception
            MessageBox.Show("Zeile: Catch ex As Exception, Fehler in Prozedur GetInventor" & vbNewLine & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            locInventor = Nothing
            'Debug.WriteLine("Error: " & ex.ToString)
        End Try

        Return locInventor
    End Function

'replace Line 292 with
dim locDocument as inventor.Document=nothing
GetInventor.SilentOperation = True
try
        If GetInventor IsNot Nothing Then
            locDocument=GetInventor.Documents.Open(yourFile, false)
        Else
            MessageBox.Show("Please start Inventor", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If
catch ex as exception
Finally
GetInventor.SilentOperation = false
End Try
If locDocument isnot nothing then
locDocument.save
locDocument.close
locDocument.ReleaseReference
Endif

 Annotation:
Needs Migrating is not suitable for all document types.

It's not a good idea to mix Apprentice and Inventor together. Do not open the Document with Apprentice the same time.

Do not use ArraList, it is better to yuse Generic like dim lstList as list(of string)

0 Likes
Message 31 of 39

kriegler
Contributor
Contributor

First change ! Option Strict: on !!!!

AllReferencedDocuments is not  a good idea; it does not return all referenced files (e.g. suppressed documents)

ModelStates in particular represent a big problem

 

0 Likes
Message 32 of 39

chrisw01a
Collaborator
Collaborator

Ok. What do you make of this message I am getting?

 

chrisw01a_0-1694519357892.png

 

Thank you

 

0 Likes
Message 33 of 39

WCrihfield
Mentor
Mentor

Hi @chrisw01a.  I believe the ReleaseReference method can only be used on a document that has no other resources referencing it.  For instance of that document is still being referenced by an open assembly, or by an open drawing, you will not be able to release the reference to it yet.  And if you have already Closed the document, you may simply not still have a reference to it to run the ReleaseReference method on.  The Close method is best for documents that have been opened visibly.  And if the document you Closed is still being referenced by something, it will only be visibly closed, but will remain open (or initialized) in the background.  If you open an otherwise unreferenced document invisibly, that is then a good candidate for using ReleaseReference method on.  Then, after using ReleaseReference on some documents, it is usually best to use ThisApplication.Documents.CloseAll(True), where True means only close all the unreferenced documents, as an additional clean-up step.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 34 of 39

chrisw01a
Collaborator
Collaborator

Turning on Option Strict blows up quite a few lines in my program. I realize this entire thing should probably be wiped out and start from scratch but I don't really have time at the moment.

 

So yes, ModelStates is another issue for us now. We are wanting to use them but this limitation in our program has kept us from doing so. What should I use instead?

 

Thank you again!

0 Likes
Message 35 of 39

chrisw01a
Collaborator
Collaborator
So I have this basically working if I comment out "locDocument.ReleaseReference".

Is there a way to force a new instance if Inventor to run in the background to perform the open and save? The current code is hijacking my open Inventor which I don't like because we usually have 3 to 5 instances of Inventor running with various projects open.
0 Likes
Message 36 of 39

kriegler
Contributor
Contributor

without Otion Strict on your program will not run, turn it on and fix any error messages; Otherwise the whole thing makes no sense and program crashes are inevitable

0 Likes
Message 37 of 39

kriegler
Contributor
Contributor

Accessing inventor from outside with multiple instances will not work, there is no solution and it is very dangerous for your filemanagement

0 Likes
Message 38 of 39

kriegler
Contributor
Contributor

AllReferencedFiles

0 Likes
Message 39 of 39

chrisw01a
Collaborator
Collaborator

This looks like a good way to do it but our parent document is usually a .idw. It also can be a .iam or .ipt depending what we are copying. Can this block of code be modified to handle that?

 

Thanks again for your help.

 


@kriegler wrote:

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


 

0 Likes