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

Autocad busy

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
stuartnathan
4044 Views, 12 Replies

Autocad busy

I am writing a VB application that loads Autocad, opens a drawing, reads drawing and then closes it.

I open Autocad using CreateObject.

I open drawing using AcadApp.Documents.Open(FileName, True)

I then wait for the AcadApp.EndOpen event to fire

I then read drawing

I then try to close drawing using AcadApp.ActiveDocument.Close(False)

but it fails with Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))

 

Help!

12 REPLIES 12
Message 2 of 13

Hi,

 

imho you don't need to wait for EndOpen-event (as long you are not working with multithreading).

The code after the line "...Documents.Open..." will continue with the next code-line after the drawing is loaded into editor (and not before). So I see no necessarity for handling that with an event.

 

And the event may be the source of your problem, because when you try to start the "...ActiveDocument.Close...." while you are in the event-handler, don't forget that this event-handler is (indirectly) part of your document-object ==> you can't close the doc while you are "in the document" - or with other words - the document is locked because a document-based-event is active.

 

Try to rewrite the code without this event-handler and that may be the solution. If not ... let use see a minimum code snippet that makes it for us possible to reproduce the problem.

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 13
HomeBoyLV
in reply to: stuartnathan

Take a look here, it may solve your error.

Message 4 of 13
stuartnathan
in reply to: stuartnathan

Thanks to you both, and so I decided to write the code as an inprocess class.

The code is this:

   Friend WithEvents AcadDoc As Document
   Friend WithEvents DocCol As DocumentCollection
   <CommandMethod("OMREADTITLES")> Public Sub OMREADTITLES()
      fls = Directory.GetFiles(cFolder)
      For Each n As String In fls
         If n.ToUpper.EndsWith("DWG") Then
            AcadDoc = DocCol.Open(n)
            GetTitleSheets()
         End If
      Next
      For Each doc As Document In Application.DocumentManager
         doc.CloseAndDiscard()
      Next
   End Sub

 GetTitleSheets is a routine that reads title blocks and extracts their attributes. All works well.

BUT

CloseAndDiscard returns error - Drawing Busy

 

I don't understand why. Each drawing is opened as Read Only. I don't alter the database (at least I don't think so)

Any ideas?

Message 5 of 13

Hi,

 

if you don't use the events remove the "WithEvents" phrase in the declaration.

 

>> BUT [...] CloseAndDiscard returns error - Drawing Busy

That is not a save way: to close all documents, also that one that has an open command ... your command!

 

However you can try to change the calling-attributes in that way:

<CommandMethod("OMREADTITLES", Autodesk.AutoCAD.Runtime.CommandFlags.Session)> _ 

 

Just as an additional input: I would open one drawing, do the modifications needed and close it then. Don't open all documents parallel. It's not only a question of memory and performance, it's also a questions of how save will your documents be if the file 150 crashes AutoCAD because of any defect ==> so all your previously opened files are unsave in this case.

 

And one more input: why opening the drawings in the editor? Do you do some Zoom-modifications .... or any functions that need the editor? Because opening just the database will be much more efficient than loading the drawings into the editor.

For that look to "New Autodesk.AutoCAD.DatabaseServices.Database(....)" and "...Database.ReadDwgFile(..."

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 13

Thanks for this - food for thought! Never thought about just using the database.

The withevents is used as a also trap events.

 

I did originally try to close the dwg in the main loop - same error.

But logically, my command will be active so how to close.

 

So I tried this:-

   <CommandMethod("OMREADTITLES", Autodesk.AutoCAD.Runtime.CommandFlags.Session)> Public Sub OMREADTITLES()
      fls = Directory.GetFiles(cFolder)
      For Each n As String In fls
         If n.ToUpper.EndsWith("DWG") Then
            AcadDoc = DocCol.Open(n)
            GetTitleSheets()
            AcadDoc.CloseAndDiscard()
         End If
      Next
   End Sub

 Still get the Drawing Busy

Message 7 of 13

Hi,

 

This statement defines your current doc:

AcadDoc = DocCol.Open(n)

Save your current drawing e.g. "Drawing1" previously in a varaible and before closing the "AcadDoc" make your "Drawing1" active.

 

Plus ,,, have you tried changing the CommandMethod-Attributes?

 

>> The withevents is used as a also trap events.

As long as your routine "GetTitleSheets" does all modifications you can disable the eventhandling .... just for test! Do you get the exceptions then also or not.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 13

Thanks very much - solved!

      For Each n As String In fls
         If n.ToUpper.EndsWith("DWG") Then
            Dim db As New Database(False, True)
            db.ReadDwgFile(n, FileOpenMode.OpenForReadAndAllShare, False, "")
            GetTitleSheets(db)
         End If
      Next

 No need to close drawings etc. I have got this right?

Message 9 of 13

Hi,

 

well there are some parts to be done after opening a database:

db.CloseInput(True)

db.Dispose()

 

plus don't forget to save 😉

 

Have a nice day, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 10 of 13

OK and thanks again.

Actually I don't save, but I do write data to an external file.

 

BUT - now I have a new question.

 

OK. The code works great inside AutoCAD. I also can attach VS to the Acad process and check every line. All seems fine.

 

However I actually have an app that loads Autocad using CreateObject etc. I will not be showing it when debugged.

Now I really want to execute the code from my app which I do by sending message to acad.

 

App.ActiveDocument.SendCommand(cnEsc & cnEsc & "OMREADTITLES ")

AutoCad receives message and executes BUT I get a file sharing error.

 

If I stop VS, and execute the command inside Acad, it does work properly.

Any ideas?

Message 11 of 13

Hi,

 

>> I get a file sharing error.

Have you got the message more detailed? Does the message show a filename that it struggles with?

Could that be that you get this message when opening a DWG-file more than once and forgot to .CloseInput or .Dispose the DB-object?

 

And just to understand your situation: You have 2 VS-projects, one that starts AutoCAD with "...createObject" and the other as DLL that is _NETLOADed into AutoCAD?

 

>> which I do by sending message to acad

Why that?

If you created an AutOCAD process with CreateObject, you have the AcadApplication-object (COM) and so you have the AcadDocument-object (COM) and one step more you can use the SendCommand without using Windows-messaging (I would see SendCommand as more save).

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 12 of 13

I have a number of projects in the VS solution.

One is a Library of various functions, classes etc. which is used by other projects.

One is a library of my own controls

One is the exe file

One is a class which wraps around loading AutoCad's different versions. It uses Com to load a hidden version of AutoCad. It also provides a way for the exe file to know of changes inside AutoCad.

and

One is an independant class that loads as an inprocess class. It provides various functions such as reading title sheets, as well as block and layer manipulation. It effectively adds my Commands to AutoCad.

Following your advice, the function that reads title sheets does this using the database and not the editor. It now (thankyou) works faultlessly when called inside AutoCad.

 

But if I invoke it from my exe. I get this error:-

 

Autodesk.AutoCAD.Runtime.Exception: eFileSharingViolation    at Autodesk.AutoCAD.DatabaseServices.Database.ReadDwgFile(String fileName, FileOpenMode mode, Boolean allowCPConversion, String password)    at om2011net.clsAcad.OMREADTITLES() in D:\Visual Studio 2008\Projects\Acadnet2011\clsAcad.vb:line 374    at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)    at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)    at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)    at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

 

Furthermore, nothing was written to an external file, so it hasn't even got into the routine.

 

 

Message 13 of 13

Hi,

 

this part of the exception-message:

eFileSharingViolation    at Autodesk.AutoCAD.DatabaseServices.Database.ReadDwg

gives you a lot of info and I would guess that you open a file that is already open (or at least that was opened but not closed afterwards). I think that you should verify the workflow in your app .... where and when does your app open a database (of have already anyone open), could it be that the list of files you work through contains double filesnames ... or whatever ==> for analyzing that you must have your code, I can't do that for you, sorry!

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

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