Close part with model states

Close part with model states

JoãoASilva
Advocate Advocate
684 Views
6 Replies
Message 1 of 7

Close part with model states

JoãoASilva
Advocate
Advocate

Hi!

 

How do you go about closing a document, thru iLogic, that has a model state created?

 

I have this piece of code:

oDoc = ThisDoc.FactoryDocument 'ThisApplication.ActiveEditDocument
oPath =  ThisDoc.Path

If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

	Dim oDocFile As Document
		
	For Each oDocFile In oDoc.AllReferencedDocuments
		
			ThisApplication.Documents.Open(oDocFile.FullFileName, True)
						
			oDocFile.Close
	Next
End If

 

Even changing the 1st line from 

ThisApplication.ActiveEditDocument

to

ThisDoc.FactoryDocument

 still leaves the part open.

 

How do you go about doing this?

João Silva

Mechanical Engineer

 

0 Likes
Accepted solutions (2)
685 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @JoãoASilva.  If the document is currently being referenced by an assembly, drawing, or some other document that is currently open, then all will not be able to truly close it all the way.  You may be able to 'visibly' close it, but that reference will remain open (or at least 'initiated') in the background, until you close the assembly (or other open document) that is currently referencing it.  If we 'visibly' open a Document, then we should use the 'Document.Close' method to close it, but if it is referenced by something, this will only visibly close it, leaving the reference in memory.  If we opened the Document 'invisibly' by code, then we can use the Document.ReleaseReference method, which will work just fine if it is not being referenced by anything else, but that may not work if it is being referenced by something else.

Edit:  We can also use ThisApplication.Documents.CloseAll(True) to close all 'unreferenced' documents only, to clear those out of Inventor's memory.  However, if you specify False, it will close them all, and ignore any unsaved data, so be careful with that one.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Also, in your code, you are iterating through referenced documents, using the 'oDocFile' variable to represent each one.  Then within the loop, where you are opening them, that Open method is a Function, which will return a document object, which you are not capturing to a variable.  And in that Open method, you are specifying 'FullFileName' which is not always effective when ModelStates (and some others) are involved.  You need to use 'FullDocumentName' there instead, because that includes the 'FullFileName', plus sometimes it includes a specification for which ModelState member (or which iPart member, etc) it should have active when it opens.  Each ModelState can represent a different Document within the same File on disk.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 7

JoãoASilva
Advocate
Advocate

I'm visibly opening every file so my colleagues can see that it is processing everything.

Then I want to visibly close it (remove the tab from the bottom bar).

 

Changing the 'FullFileName' to 'FullDocumentName' doesn't close the document, but it does show the model state name when I check with this message box:

		ThisApplication.Documents.Open(oDocFile.FullDocumentName, True)
				
			MsgBox(oDocFile.FullDocumentName.ToString)	
			
		oDocFile.Close

 

How do I go about capturing the document to a variable? 🤔

João Silva

Mechanical Engineer

 

0 Likes
Message 5 of 7

Frederick_Law
Mentor
Mentor
Accepted solution

 

AnotherDoc = ThisApplication.Documents.Open(oDocFile.FullDocumentName, True)
AnotherDoc.Close

 

Message 6 of 7

WCrihfield
Mentor
Mentor

Hi @JoãoASilva.  Do you mean that the document's tab is staying open after the 'Close' line of code?  Or do you just mean that the total number of open documents shown in the lower right corner of the screen does not go down after closing them?  If the document tab is not going away after the Close line, then something odd is definitely going on.  However, the total number of open documents will not go down, because of what I mentioned earlier.  Since that document is being referenced by the assembly, it it most likely already at least initiated, if not already open, and your code for opening it visibly, then closing it, will not change that status.  It will still be open by the assembly after it has been visibly closed, because it is still being referenced by the assembly.  If you opened that referenced document, then closed the assembly that was referencing it, then closed that document, then the number/count representing that document should go away.

 

As an example, I just typed up this example rule.  It lets me 'Pick' an assembly component, then gets its FullDocumentName, then visibly opens it, then shows me its FullDocumentName in a MsgBox, then closes the document again.  During that process, the number of open documents never changes, because that part was already open in the background (no document tab) by the assembly, and remains open in the background by the assembly after it has been visibly closed (document tab from opening it visibly goes away).  This example also shows using a variable to capture the Document that the 'Open' method returns.  In your case, since you already had the 'oDocFile' variable that had already been defined as a Document, that variable could be used again to capture the value returned by that line of code.  This is because traditionally, you would not use that line of code to open a document that you already have a Document type reference for, but one that you only have a FullFileName or FullDocumentName (String) for.  If all you had was a String, then that line of code is the only way to obtain a reference to the Document object, instead of just the String.

Dim oPickedOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a Component.")
If oPickedOcc Is Nothing Then Return
Dim sFDN As String = oPickedOcc.ReferencedDocumentDescriptor.FullDocumentName
Dim oPickedOccDoc As Inventor.Document = ThisApplication.Documents.Open(sFDN, True)
MsgBox(sFDN, vbInformation, "iLogic")
oPickedOccDoc.Close

When I have an assembly visibly open and one of the components in it has multiple ModelStates, and that component is not currently visibly open, I can 'Pick' that component using this rule, visibly open it (document tab showing and that part showing on screen), then close it, which closes that document tab, and leaves me viewing the assembly again.  The total open documents number does not change.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 7

JoãoASilva
Advocate
Advocate

I meant that the tab stayed open even after calling the 'Close'.

 

What you said previously "[...] that Open method is a Function, which will return a document object, which you are not capturing to a variable.", and translated to code by @Frederick_Law , is just what I did and it works:

oDoc = ThisDoc.FactoryDocument 'ThisApplication.ActiveEditDocument
oPath =  ThisDoc.Path

If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

	Dim oDocFile As Document
	Dim oDocFileOcc As Document
		
	For Each oDocFile In oDoc.AllReferencedDocuments
		
		oDocFileOcc = ThisApplication.Documents.Open(oDocFile.FullDocumentName, True)
							
		oDocFileOcc.Close
	Next
End If

Thank you!

João Silva

Mechanical Engineer

 

0 Likes