An exception with a message 'Drawing is busy' in CloseAndDiscard()

An exception with a message 'Drawing is busy' in CloseAndDiscard()

Anonymous
Not applicable
3,782 Views
6 Replies
Message 1 of 7

An exception with a message 'Drawing is busy' in CloseAndDiscard()

Anonymous
Not applicable

Hello,

 

I have a problem in the Advance Steel 2018 (it partially uses Autocad API) which is described below:

there occurs an exception with a message 'Drawing is busy' when I'm trying to use the Document.CloseAndDiscard() method to close the non-active (with Document.IsActive == false)  document. Also I tried to assign a property Application.DocumentManager.MdiActiveDocument = documentToClose, so after that the property documentToClose.IsActive becomes true but the exception still occurs. 

 

Thank you for your help!

0 Likes
Accepted solutions (2)
3,783 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

This forum is typically for VBA, but it looks like you are using vb.net.The .NET forum is here.

 

It could be that you are running the command in the document context. If you don't specify CommandFlags.Session in your [CommandMethod] attribute, you will be running the command in the document context and AutoCAD automatically locks the document for you. If you run the command in the Session context, you have to explicitly lock the document for editing. After you unlock it, you can run CloseAndDiscard().

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor

Firstly, for historical reason this forum is for AutoCAD VBA/COM API topics. Since your question is about AutoCAD .NET API, you should post in .NET forum.

 

Anyway, if the .NET API code is to open/close drawing, usually, the code should run in Application context (with CommandFlags.Session flag set in CommandMethod attribute). There is a recent question on similar topic in .NET forum that may be of help:

 

https://forums.autodesk.com/t5/net/can-t-close-mdidocument/td-p/8034916

 

You may want to post much more code to show how the code is started, which document is to close.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

Anonymous
Not applicable

Hello!

I left the same post on .NET forum with a detailed description of the problem and code, but nobody answers.

Therefore since here you already responded the post I'll leave you a code that shows what I want to do.

What I want to do it's to close all the documents with the same name as the active one, except for the currently active one.

 

using Autodesk.AdvanceSteel.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using AdvanceSteelApplication = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using AdvanceSteelDocument = Autodesk.AutoCAD.ApplicationServices.Document;

[CommandMethod("MainGroup", "CloseAllButThis", "CloseAllButThis", CommandFlags.Session)]
public void CloseAllButThis()
{
     AdvanceSteelDocument activeDocument = AdvanceSteelApplication.DocumentManager.MdiActiveDocument;
     foreach (AdvanceSteelDocument doc in AdvanceSteelApplication.DocumentManager)
     {
           if (doc.Name == activeDocument.Name && !(doc.UnmanagedObject.Equals(activeDocument.UnmanagedObject)))
           {
                // AdvanceSteelApplication.DocumentManager.MdiActiveDocument = doc; // it doesn't help
                        
                doc.CloseAndDiscard(); // An exception occurs here with a 'Drawing is busy' message
           }
      }
}
0 Likes
Message 5 of 7

norman.yuan
Mentor
Mentor

The reason of not getting responds might be that very few people who visit these forums use Advanced Steel and do programming with it. I do not use it, thus, hesitate to reply on something I am not sure I know.

 

However, since I do know Advance Steel is simply an AutoCAD vertical, similar to other ones (Map3D, Electrical, Plant3D...), and your code only deal with regular AutoCAD classes (Application, Document), so your issue/code should be testable.

 

I am not sure why do have multiple drawing with the same path/name open. if Document.Name is the same, which means the SAME drawing is opened in the same AutoCAD session multiple times, and only the first opened one could be read/write-able, and the rests are all opened as read-only.

 

Anyway, I have no problem the close all opened drawings except for the active drawing (MdiActiveDocument), be the other drawing the same drawing opened as read-only or not, with following code. Note, since you want to key current active drawing and close all other drawings (not active), so, the command method does not need to have CommandFlags.Session (i.e. with or without that flag set, the code all works). 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CloseOpenedDwgs.Commands))]

namespace CloseOpenedDwgs
{
    public class Commands
    {
        /// <summary>
        /// This command close/discard all opened drawings in AutoCAD except 
        /// current active drawing
        /// </summary>
        [CommandMethod("CloseDwgs")]
        public static void RunCommand()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;

            foreach (Document d in CadApp.DocumentManager)
            {
                if (d.UnmanagedObject!=dwg.UnmanagedObject)
                {
                    d.CloseAndDiscard();
                }
            }
        }
    }
}

The screencase below shows how the code works:

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor
Accepted solution

The reason of not getting responds might be that very few people who visit these forums use Advanced Steel and do programming with it. I do not use it, thus, hesitate to reply on something I am not sure I know.

 

However, since I do know Advance Steel is simply an AutoCAD vertical, similar to other ones (Map3D, Electrical, Plant3D...), and your code only deal with regular AutoCAD classes (Application, Document), so your issue/code should be testable.

 

I am not sure why do have multiple drawing with the same path/name open. if Document.Name is the same, which means the SAME drawing is opened in the same AutoCAD session multiple times, and only the first opened one could be read/write-able, and the rests are all opened as read-only.

 

Anyway, I have no problem the close all opened drawings except for the active drawing (MdiActiveDocument), be the other drawing the same drawing opened as read-only or not, with following code. Note, since you want to key current active drawing and close all other drawings (not active), so, the command method does not need to have CommandFlags.Session (i.e. with or without that flag set, the code all works). 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CloseOpenedDwgs.Commands))]

namespace CloseOpenedDwgs
{
    public class Commands
    {
        /// <summary>
        /// This command close/discard all opened drawings in AutoCAD except 
        /// current active drawing
        /// </summary>
        [CommandMethod("CloseDwgs")]
        public static void RunCommand()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;

            foreach (Document d in CadApp.DocumentManager)
            {
                if (d.UnmanagedObject!=dwg.UnmanagedObject)
                {
                    d.CloseAndDiscard();
                }
            }
        }
    }
}

The screencase below shows how the code works:

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Note: the answer, which the OP accepted as "solution" I posted here yesterday disappeared. This missing post things has happened at least twice to my posts, which come with attached Autodesk screen cast. I guess it is a bug in the new forum software. I could not find any link to report this, thus put note here. In order to make this thread complete, I re-post my reply as "accepted solution" here again.

 

<Re-Post>

The reason of not getting responds might be that very few people who visit these forums use Advanced Steel and do programming with it. I do not use it, thus, hesitate to reply on something I am not sure I know.

 

However, since I do know Advance Steel is simply an AutoCAD vertical, similar to other ones (Map3D, Electrical, Plant3D...), and your code only deal with regular AutoCAD classes (Application, Document), so your issue/code should be testable.

 

I am not sure why do have multiple drawing with the same path/name open. if Document.Name is the same, which means the SAME drawing is opened in the same AutoCAD session multiple times, and only the first opened one could be read/write-able, and the rests are all opened as read-only.

 

Anyway, I have no problem the close all opened drawings except for the active drawing (MdiActiveDocument), be the other drawing the same drawing opened as read-only or not, with following code. Note, since you want to key current active drawing and close all other drawings (not active), so, the command method does not need to have CommandFlags.Session (i.e. with or without that flag set, the code all works). 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CloseOpenedDwgs.Commands))]

namespace CloseOpenedDwgs
{
    public class Commands
    {
        /// <summary>
        /// This command close/discard all opened drawings in AutoCAD except 
        /// current active drawing
        /// </summary>
        [CommandMethod("CloseDwgs")]
        public static void RunCommand()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;

            foreach (Document d in CadApp.DocumentManager)
            {
                if (d.UnmanagedObject!=dwg.UnmanagedObject)
                {
                    d.CloseAndDiscard();
                }
            }
        }
    }
}

The following screencast shows how the code works:

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature