.NET

Reply
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 1 of 13 (1,218 Views)
Accepted Solution

Autocad busy

1218 Views, 12 Replies
11-27-2011 05:10 AM

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!

*Expert Elite*
Alfred.NESWADBA
Posts: 8,982
Registered: ‎06-29-2007
Message 2 of 13 (1,215 Views)

Re: Autocad busy

11-27-2011 05:51 AM in reply to: stuartnathan

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
-------------------------------------------------------------------------
Active Contributor
HomeBoyLV
Posts: 47
Registered: ‎06-04-2004
Message 3 of 13 (1,205 Views)

Re: Autocad busy

11-27-2011 12:43 PM in reply to: stuartnathan

Take a look here, it may solve your error.

Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 4 of 13 (1,181 Views)

Re: Autocad busy

11-28-2011 02:19 AM 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?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,982
Registered: ‎06-29-2007
Message 5 of 13 (1,175 Views)

Re: Autocad busy

11-28-2011 02:51 AM in reply to: stuartnathan

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
-------------------------------------------------------------------------
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 6 of 13 (1,170 Views)

Re: Autocad busy

11-28-2011 03:01 AM in reply to: Alfred.NESWADBA

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

*Expert Elite*
Alfred.NESWADBA
Posts: 8,982
Registered: ‎06-29-2007
Message 7 of 13 (1,168 Views)

Re: Autocad busy

11-28-2011 03:08 AM in reply to: stuartnathan

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
-------------------------------------------------------------------------
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 8 of 13 (1,163 Views)

Re: Autocad busy

11-28-2011 03:46 AM in reply to: Alfred.NESWADBA

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?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,982
Registered: ‎06-29-2007
Message 9 of 13 (1,158 Views)

Re: Autocad busy

11-28-2011 03:59 AM in reply to: stuartnathan

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
-------------------------------------------------------------------------
Valued Contributor
stuartnathan
Posts: 70
Registered: ‎02-17-2005
Message 10 of 13 (1,150 Views)

Re: Autocad busy

11-28-2011 04:22 AM in reply to: Alfred.NESWADBA

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?



Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.