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

Cannot Open and Close DWG in Same Command

3 REPLIES 3
Reply
Message 1 of 4
scott.ferguson
400 Views, 3 Replies

Cannot Open and Close DWG in Same Command

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);
        }

 

3 REPLIES 3
Message 2 of 4

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 4
kdub_nz
in reply to: scott.ferguson

 

Scott, see my response at theSwamp

 

Regards

Kerry


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 4 of 4

Awesome, just what I meant.

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