Force Inventor to close all files which are not used in any visibly open file

Force Inventor to close all files which are not used in any visibly open file

j.pavlicek
Collaborator Collaborator
724 Views
4 Replies
Message 1 of 5

Force Inventor to close all files which are not used in any visibly open file

j.pavlicek
Collaborator
Collaborator

Hello, from time to time I run into issue when Inventor keeps some files open (but not visibly) even when they are not needed anymore in visibly opened files or the file is removed in meanwhile. Only what I found is to restart the application (do I have to tell you about impact to my workflow?).

 

This happens typically when

  1. Making a part in an assembly (Create) and then cancel it.
    This can be easily reproduced:
    Open an assembly, create in-place part named AAA, then exit part environment and delete the part from the assembly without saving it. Now try to create in-place part named AAA again...
  2. Replacing parts in an assembly (previously used parts are "cached by Inventor).

Is possible to force Inventor to clean opened files and keep only files really referenced by opened files?

 

Thank you.



Inventor 2022, Windows 10 Pro
Sorry for bad English.
0 Likes
Accepted solutions (1)
725 Views
4 Replies
Replies (4)
Message 2 of 5

daniel.puchta
Enthusiast
Enthusiast

Hello j.pavlicek, this is interesting behaviour. I quckly wrote this VBA macro. I have overcome the issue of For each document in ThisApplication.Documents error by forcing Inventor to reload Documents object when Document was closed inside of For Each document cycle.

I quickly tested the code and it seems that it works. Can you confirm it? (note that it is a VBA macro, not iLogic Rule)

    Public oAlreadyRemapped As Object
    Sub Main()

        Dim FileExist As Boolean
        Dim FileNames As String
        Set oAlreadyRemapped = CreateObject("System.Collections.ArrayList")
        oAlreadyRemapped.Clear
        Dim oDocument As Inventor.Document
        Dim oReferenced_document As Inventor.Document
        Dim CloseDocument As Boolean
        CloseDocument = True
        Dim InventorApplication As Object
        Set InventorApplication = ThisApplication
        For Each oDocument In ThisApplication.Documents.VisibleDocuments
            FileExist = False
            For Each CheckedFile In oAlreadyRemapped
                If ChekcedFile = oDocument.FullFileName Then
                    FileExist = True
                    Exit For
                End If
            Next
            
            If FileExist = False Then
                oAlreadyRemapped.Add (oDocument.FullFileName)
            End If
'           oAlreadyRemapped.Add(oDocument.FullFileName)
            CheckAllDocuments oDocument
        Next


        For Each FileName In oAlreadyRemapped
            FileNames = FileNames & FileName & vbNewLine
        Next
        
        'i = MessageBox.Show(FileNames, "Dialog Můj iLogic", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
 
        For Each oDocument In ThisApplication.Documents
            CloseAllUnusedDocuments oDocument 'Called recrsively due to the need of reload Documents Object after closing document
        Next
        

    End Sub

    Sub CheckAllDocuments(ByVal oSubDocument As Inventor.Document)
        Dim FileExist As Boolean
        FileExist = False
        For Each oReferenced_document In oSubDocument.ReferencedDocuments
'           oAlreadyRemapped.Add(oReferenced_document.FullFileName)
            FileExist = False
            For Each CheckedFile In oAlreadyRemapped
                If ChekcedFile = oReferenced_document.FullFileName Then
                    FileExist = True
                    Exit For
                End If
            Next
            
            If FileExist = False Then
                oAlreadyRemapped.Add (oReferenced_document.FullFileName)
            End If
            If (oReferenced_document.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject) Or (oReferenced_document.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject) Then
                CheckAllDocuments oReferenced_document
            End If
        Next
    End Sub
    
    Sub CloseAllUnusedDocuments(ByVal oInventorApp As Object)
            Dim CloseDocument As Boolean
            For Each oDocument In ThisApplication.Documents
                CloseDocument = True
                For Each CheckedFile In oAlreadyRemapped
                    If CheckedFile = oDocument.FullFileName Then
                        CloseDocument = False
                        Exit For
                    End If
                Next
                
                If CloseDocument = True Then
                    oDocument.Close
                    Exit Sub
                End If
            Next
        
    End Sub


 

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @j.pavlicek.  I believe there is a one-liner for that purpose.  The 'CloseAll' method.

ThisApplication.Documents.CloseAll(True)

This will close all 'unreferenced' documents when set to True.

Here is the pop-up helper info for that method, and its option.

WCrihfield_0-1641313824382.png

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 5

j.pavlicek
Collaborator
Collaborator

@WCrihfield Nice 

ThisApplication.Documents.CloseAll(True)

seems to be working perfectly. I'm surprised there isn't a button for that in GUI by default.

 

@daniel.puchta Although I'll stick to one-line solution above, I appreciate your effort. Thank you.



Inventor 2022, Windows 10 Pro
Sorry for bad English.
0 Likes
Message 5 of 5

daniel.puchta
Enthusiast
Enthusiast

Hey 😄 this is not fair 😞 

But one-line solution is always better 😄

0 Likes