.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Autocad 2014 / VB.NET Com exception caught

10 REPLIES 10
Reply
Message 1 of 11
Anonymous
1202 Views, 10 Replies

Autocad 2014 / VB.NET Com exception caught

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)

10 REPLIES 10
Message 2 of 11
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 3 of 11
Anonymous
in reply to: norman.yuan

Thanx norman

but when document close i need to do checkin and save.
i knew process of document close is already started but my checkin code must have to close the document before checkin.

norman there is any other event which can close document without docbeginclose event fire.
Message 4 of 11
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 5 of 11
Anonymous
in reply to: norman.yuan

Thank you very much for reply

norman i cant checkin my document without close my document , its my business process. when user try to close my document i need to checkin.
and document.closewillstart event in not fire i dont know why?

i add below event handler in documentActivated event
AddHandler doc.CloseWillStart, AddressOf docclosewillstart

but cant get below event when user close document from autocad application

Public Shared Sub docclosewillstart(ByVal sender As Object, ByVal docBegClsEvtArgs As DocumentBeginCloseEventArgs)

End Sub

Message 6 of 11
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 7 of 11
Anonymous
in reply to: norman.yuan

Thanx!

i dont know why my event not hooked. i added my handler in document created event also but cant get. Documentclose event work properly. i dont know my norman plz help!
Message 8 of 11
Anonymous
in reply to: Anonymous

norman clearly i need something like that ;

i want documentbeforeclose event or similar like that and want to close my document in documentbeforeclose event.

plz help norman!
Message 9 of 11
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 10 of 11
Anonymous
in reply to: 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!

Message 11 of 11
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost