Cancel File Close/Delete On Check In

Cancel File Close/Delete On Check In

Matthew_Policelli
Advocate Advocate
713 Views
6 Replies
Message 1 of 7

Cancel File Close/Delete On Check In

Matthew_Policelli
Advocate
Advocate

We have an iLogic currently that asks users if they want a PDF and then if yes makes a PDF of the .dwg file. It is triggered by the Close Document event.

 

I would like to add a check in that iLogic that the date on the revision table is not more than a week old, which would suggest the possibility that the user forgot to add a new revision to the rev table.

 

Is there a way that I can then cancel the Close Document event if the user gives the correct dialog response? The reason I'd like to cancel the event is because often we are creating the PDF when checking in to Vault, using the Close and Delete on Check In option. So if I can't cancel the close document event, the user would have to get the file from vault again in addition to opening the .dwg file again.

 

Bonus points if I can cancel the vault check in as well, but I'm not sure if the check in happens before the document close event is initiated so I won't get my hopes up.

0 Likes
714 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @Matthew_Policelli.  I do not know about the Vault related stuff, because I do not currently have it, but I do know there is a way to cancel a drawing close event.  It is a bit complicated to achieve though.  Even though there is an event for this in the Event Triggers dialog, you will not be able to use it that way.  You will need to create your own event handler code, without using the Event Handlers dialog, then have that code running in the background, listening for that document close event to happen.  Then when the event is triggered, this custom code can catch it before it finishes, check some stuff, then abort/cancel the task before it finishes.  This sort of thing is usually best suited for something like an Inventor add-in, but may be done with iLogic for quick, short term usage.  Below are a couple of links to pages within the online help area for the Sub routine's definition line that must be used in these types of custom codes.

DocumentEvents.OnClose Event 

ApplicationEvents.OnCloseDocument Event 

If you are not familiar with creating these types of codes, we may be able to help you further.  I had an article about how to write these types of codes using iLogic rules, but unfortunately all those personally authored 'Knowledge' articles are being retired/destroyed after March 8th, so the link below will not last long.

https://knowledge.autodesk.com/community/article/348361 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

Matthew_Policelli
Advocate
Advocate

I tried making this little test code to see if I could cancel file close based on the result of a messagebox:

 

Sub Main
	Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
	AddHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
End Sub

Public Sub ApplicationEvents_OnCloseDocument(DocumentObject As Inventor._Document, FullDocumentName As String, BeforeOrAfter As Inventor.EventTimingEnum, Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
	If DocumentObject Is ThisAssembly.Document  AndAlso MessageBox.Show("Do you want to keep open?","test",MessageBoxButtons.YesNo) = DialogResult.Yes Then	'AndAlso BeforeOrAfter = kBefore
		Debug.Print("OnCloseDocument event vetoed")
		HandlingCode = kEventCanceled
	Else
		Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
		RemoveHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
	End If
End Sub

I put it in the After Open Document event trigger of a test file. Before I added the else remove handler (lines 10-12), it would give me multiple messageboxes asking if I wanted to cancel the close event. After I added the removehandler code and closed and reopened, it doesn't open the messagebox at all. It seems what I am needing is to be able to add the event handler once but not add it if it's already there. And then remove it when its actually time to close the document. But I'm not exactly sure how to do that?

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

Hi @Matthew_Policelli.  You are using the one from the ApplicationEvents perspective, which is really nice for many scenarios where you may be dealing with multiple documents closing, and want to do different things for different documents.  However, the DocumentEvents version is used from the perspective of a specific document (DocumentEvents.OnClose).  I don't know if that one would be any more appropriate for your situation, but it is generally a little easier to use, because it is already document specific, instead of reacting to any other document closing, and therefore does not offer a variable which captures the document object.  But there is no need to check which document you are working with when using that one, and simply/naturally it eliminates itself after the document closes.

 

If your custom code within the method is planning on potentially preventing the document from closing, then the first 'check' (first If statement) should be to check if 'BeforeOrAfter = kBefore'.  Then, if using the ApplicationEvents route, you would need to confirm that the document is the one you want to work with, but that would not be necessary with the DocumentEvents version.  Your question should be on its own line, after that point.  The 'Else' portion of your code is allowing the event handler to be destroyed, before it has had a change to act upon your document, if that document was not the first one it encounters...effectively preventing you from seeing any reaction.  That is not a good way to format that block of code.  I would just eliminate the 'Else' keyword, and only destroy the event handler after it has closed the document, such as within the 'kAfter' side of your main 'If' statement, once you have let the document close.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

Matthew_Policelli
Advocate
Advocate

I've revised my code to use document events, but the timing is always after close, and there is no before close trigger. Here is my test code to try to add the event trigger and give the option to abort closing. Is there something wrong with it that it doesn't trigger before close?

Sub Main
	Dim _docEvents As DocumentEvents = ThisApplication.ActiveDocument.DocumentEvents
	AddHandler _docEvents.OnClose, AddressOf DocumentEvents_OnClose
End Sub

Public Sub DocumentEvents_OnClose(BeforeOrAfter As Inventor.EventTimingEnum, Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
Logger.Info(BeforeOrAfter.ToString)
If BeforeOrAfter = kBefore Then
	If MessageBox.Show("Do you want to keep open?","test",MessageBoxButtons.YesNo) = DialogResult.Yes Then	'AndAlso 
		Debug.Print("OnCloseDocument event vetoed")
		HandlingCode = kEventCanceled
	End If
End If

End Sub  

 

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

I did a little quick testing of my own today, with some similar code, and am getting the same behavior...no kBefore reporting/responce.  Here is the simple code I was using:

Class ThisRule
	Dim oTargetDoc As Document
	Dim oDocEvents As DocumentEvents
	Sub Main
		oTargetDoc = ThisDoc.Document
		oDocEvents = oTargetDoc.DocumentEvents
		AddHandler oDocEvents.OnClose, AddressOf oDocEvents_OnClose
		Logger.Info("DocumentEvents.OnClose Event Handler Created")
	End Sub
	
	Sub oDocEvents_OnClose(oTiming As EventTimingEnum, oContext As NameValueMap, _
	ByRef oHandlingCode As HandlingCodeEnum)
		If oTiming = EventTimingEnum.kBefore Then
			Logger.Info("DocumentEvents.OnClose Event Handler Triggered - kBefore")
			If oContext.Count > 0 Then
				Dim oHealthStatus As HealthStatusEnum = oContext.Value("HealthStatusEnum")
				Logger.Info("HealthStatus Before:  " & oHealthStatus.ToString)
			End If
		End If
		If oTiming = EventTimingEnum.kAfter Then
			Logger.Info("DocumentEvents.OnClose Event Handler Triggered - kAfter")
			If oContext.Count > 0 Then
				Dim oHealthStatus As HealthStatusEnum = oContext.Value("HealthStatusEnum")
				Logger.Info("HealthStatus After:  " & oHealthStatus.ToString)
			End If
			RemoveHandler oDocEvents.OnClose, AddressOf oDocEvents_OnClose
			Logger.Info("Removed DocumentEvents.OnClose Event Handler")
		End If
	End Sub
End Class

...and here is what I get in the iLogic Logger:

INFO| 5: >>---------------------------
INFO|DocumentEvents.OnClose Event Handler Created
INFO|DocumentEvents.OnClose Event Handler Triggered - kAfter
INFO|HealthStatus After: kInErrorHealth
INFO|Removed DocumentEvents.OnClose Event Handler

 

Even if all I did was open a simple part, with no apparent problems, then close it again several seconds later, without doing anything to it.  I also tried creating a new sketch in the part, then deleting it, just to 'dirty' the part, then close it, and say No to saving it.  Same report.  The official online help documentation, as well as the built-in help system documentation for this API Event say "Notification is sent before and after the document is closed."  Perhaps their documentation was either copied over from some other event, and never corrected, or they have changed something and have not bothered updating the documentation about that change.  Maybe someone at Autodesk would know more about this situation.  I was pretty sure that I had gotten responces from both sides of that event before, but not right now.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

I also tried changing up the code a bit to use an 'ElseIf' in the middle of the kBefore & kAfter checks, instead of two separate If...Then statements, but got the same results.  I believe the 'ElseIf' format is the normal way anyways, but I wanted to test it both ways, just to be sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes