Cannot Open and Close DWG in Same Command

Cannot Open and Close DWG in Same Command

Anonymous
Not applicable
556 Views
3 Replies
Message 1 of 4

Cannot Open and Close DWG in Same Command

Anonymous
Not applicable

Hi,

 

I need to copy and rename my current drawing then open the copied drawing, and I need to close the original drawing.

 

However when I try to both open the renamed drawing and close the original drawing (in the code below), the original drawing will not close.

 

If I don't open the renamed drawing, the following code works and I can close the original drawing. Can anyone see what I am doing wrong?

 

[CommandMethod("TestClose", CommandFlags.Session)]
        public void TestCloseDocument()
        {
            string fullName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name;
            string projectDirectory = System.IO.Path.GetDirectoryName(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string drawingNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string ext = System.IO.Path.GetExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            // create directory if it doesn't exists
            string newTempDir = string.Concat(projectDirectory, "\\temp");
            if (!Directory.Exists(newTempDir))
            {
                Directory.CreateDirectory(newTempDir);
            }

            string fullNewDrawingName = string.Concat(newTempDir, "\\", drawingNameWithOutExt, "_1", ext);
            if (!File.Exists(fullNewDrawingName))
                File.Copy(fullName, fullNewDrawingName);
 
            try
            {
                foreach (Autodesk.AutoCAD.ApplicationServices.Document doc in Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager)
                {
                    if (doc.Name == fullName)
                    {
                        doc.CloseAndDiscard();
                        break;
                    }
                }
                // 
            }
            catch (SystemException ex)
            {
                string message = ex.ToString();
            }
                     Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullNewDrawingName, false);
        }

 

0 Likes
557 Views
3 Replies
Replies (3)
Message 2 of 4

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

if you use the "CommandFlags.Session" the opened document will become current. As long as your command (that is started within the other drawing) is not finished, AutoCAD has the approach "make thedoc current in the editor" in the queue, and in my opinion that is the reason of why you can't close the doc.

 

I would also not load the doc into the editor, just create a New Database and load the DWG-file into that object, make your modifications and save it back. Opening a DWG in the editor always uses more time.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 4

kerry_w_brown
Advisor
Advisor

 

Scott, see my response at theSwamp

 

Regards

Kerry


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 4 of 4

Anonymous
Not applicable

Awesome, just what I meant.

0 Likes