hello
i close my document(drawing) from autocad and i got docbeginclose event then i need to do doc.CloseAndDiscard(). how can i do that?
Public Shared Sub docBeginDocClose(ByVal sender As Object, ByVal docBegClsEvtArgs As DocumentBeginCloseEventArgs)
' need to do closeAnddiscard() here
doc.closeAndDiscard()
End Sub
but my above code line(doc.closeAndDiscard()) throws following exception :
System.Runtime.InteropServices.COMException (0x80004005)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Autodesk.AutoCAD.ApplicationServices.DocumentExtension.CloseInternal(Document doc, Boolean discard, String fileName)
at DocsvaultAutocadPlugin.class1.docBeginDocClose(Object sender, DocumentBeginCloseEventArgs docBegClsEvtArgs) in E:\Docsvault SB 7autocad\DocsvaultAutocadPlugin\Class1.vb:line 147
at CEditorReactorImpl.FireBeginDocClose(CEditorReactorImpl* , AcDbDatabase* pDwg)
When DocumentBeginClose event occurs, the document closing process is already started, as its name clearly hinted. When you want to call a method that is to close the document again? It is logically wrong when some operation already started, and you want to do the same operation inside the operation.
However, my guess is that in the event handler, you want to (check some condition or not) to force the document beign closed without saving changes. If this is your goal, even you can call CloseAndDiscard() inside DocumentBegineClose() event hadler, you still cannot achieve what you want to: by the time DocumentBeginClose event is raised, the document change have already been saved (that is, when user closes a document and being asked "Save changes?", if user's answer is "Yes", AutoCAD first save the changes, then started the closing operation, which raises the DocumentBeginClose event. In this case, even you use the Veto() method of the DocumentBeginCloseEventArgs, it is already to late to prevent document change being saved.
So, if your purpose is to prevent user to save changes to a document, you may have to rethink the logic of your bsiness process. For example, storing the document in read-only location, opening the document as read-only, or handling DocumentLockModeChanged event to veto "Save", SaveAs", "QSAVE" and/or any other related commands...
Norman Yuan
It might be helpful to describe in detail what you are trying to achieve in the business process.
If you need to do something before the document closing starts, you could handle Document.CloseWillStart event or DocumentCollection.DocumentLockModeChanged event, so taht you can catch the chance before the closing begins.
Norman Yuan
Well, if DocumentWillClose event handler does not work, it only means that the event handler is not correctly hooked to target document. At least you can try to hook the event handler in a command method to current MdiActiveDocument and verify the code iin the event handler works.
Also, when using DocumentActivated event to hook other event handlers, you need to be careful, because DocumentActivated event could be raised many times even the MdiActiveDocument is not changed (i.e. when message box/dialog box pups up and then closed, the DocumentActivated event fires. If you did not handle this event correctly, you may add multiple DocumentWillClose event handlers to the same document. Using Documentcreated event might be better choice.
Norman Yuan
OK, I was probably not right by thinking when/how CloseWillStart event fires. Here is the ObjectARX SDK document on this:
<QUOTE>
Wraps the AcEditorReactor2.docCloseWillStart() ObjectARX function.
This notification is fired at the same time as the BeginDocumentClose event
</QUOTE>
So, when it says "at the same time", does it mean 2 events at the same time, or only one event fires (then which one)? I did quite test and seems that we can only hook an event handler to BeginDocumentClose, not CloseWillStart.
Then I went to check the similar situation on Application.QuitWillStart and BeginQuit. In this case, QuitWillStart is actually fires after BeginQuit. So, we have to be careful of not inteprete things by its literal woding (like thinking "will" is happening before "Begin").
Back to your issue, however, it is still hard to understand what you need to do: why you want to closing a document that is ALREADY starting the closing process. Since the closing is already started no matter what you do, the document will be closed (unless you veto it (in the beginDocumentClose event handler. So, when document starts its closing, you can do something, but NOT try to close it while it is closing.
It seems what you really need is a chance before the intention of closing becoming the action of closing. So, BeginDocumentClose is the last chance you can stop the regular closing process, i.e. either veto it, or let it go complete. If you want do to something when the regular cloing is stopped/vetoed, such as your own closing proecess, you may want to handle DocumentCollection.DocumentLockModeChanged/DocumentLockModeChaneVetoed events.
Again, not knowing your real business requirement, it is difficult to suggest more.
Norman Yuan
Thanx norman,
listen i want to do CloseAndSave() my document when user close my document from autocad.
when user close the document, i got the documentclose event so i need to do closeAndSave() here in docuementClose event.
if i m not done close the document then my business process says you are trying to import more than one document for the checkin.
so i need to do checkin before close the document.
i exactly done same process in microsoft Excel in beforecloseevent. In excel i close my workbook and then save and checkin.
same things i need to do here
i hope u can understand me
plz help norman!
Again, you DO NOT WANT TO CLOSE document (calling CloseAndSave()) when the document is already closing. I simply do not understand why you want to close the document while it is closing? Do you not trust AutoCAD can close document?
Or maybe, your true need is to make sure the document is saved/checked in when user close the document. If so, you DO NOT CALL CloseAndSave(). That is, you do not let AutoCAD do closing action again during the already started closing process; you only want to let AutoCAD save the document or check in to somewhere. Is this what you want? If so, I have mentioned 2 ways in previous replies:
1. You handle BeginDocumentClose event, and see if the document is saved or not (or checked in or not, if your document management system allows it), if not, you can veto the closing and prompt user.
2. Handle DocumentLockModeChanged to see if "Close" command is issued. If yes, check the saving/checking in status of the document and veto the command if necessary. You can then handle DocumentLockModeChangeVetoed event to see that if the vetoed command is "Close", you can then run your own saving/checking in/closing code, in which you can call CloseAndSave() at some point.
It is much like override the existing "Close" command with your own version of "Close" command. The point is that you DO NOT CLOSE document while it is closing, you only stop the existing closing process if some condition is not met, and start your own closing process if necessary.
You can even simply redefine the "Close" command so that the AutoCAD built-in "Close" command is not used. For example, you can have this lisp:
(command "UNDEFINE" "CLOSE")
(command "UNDEFINE" "CLOSEALL")
(defun C:Close ()
;; do something, such as calling your .NET command that closing
;; the document as you desired
)
(defun C:CLOSEALL()
;; ....
)
This way, you do not even have to handle document/documentclooection events. Just make sure your C:Close command always loaded.
Norman Yuan