<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Cannot Open and Close DWG in Same Command in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184930#M58926</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to copy and rename my current drawing then open the copied drawing, and I need to close the original drawing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[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);
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 08 Oct 2011 21:47:06 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2011-10-08T21:47:06Z</dc:date>
    <item>
      <title>Cannot Open and Close DWG in Same Command</title>
      <link>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184930#M58926</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to copy and rename my current drawing then open the copied drawing, and I need to close the original drawing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[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);
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2011 21:47:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184930#M58926</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-08T21:47:06Z</dc:date>
    </item>
    <item>
      <title>Betreff: Cannot Open and Close DWG in Same Command</title>
      <link>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184934#M58927</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you use the "&lt;EM&gt;CommandFlags.Session&lt;/EM&gt;" 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would also not load the doc into the editor, just create a &lt;EM&gt;&lt;FONT color="#666699"&gt;New Database&lt;/FONT&gt;&lt;/EM&gt; 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH, - alfred -&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2011 21:54:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184934#M58927</guid>
      <dc:creator>Alfred.NESWADBA</dc:creator>
      <dc:date>2011-10-08T21:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot Open and Close DWG in Same Command</title>
      <link>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184974#M58928</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scott, see my response at theSwamp&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Kerry&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2011 01:49:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184974#M58928</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2011-10-09T01:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot Open and Close DWG in Same Command</title>
      <link>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184984#M58929</link>
      <description>&lt;P&gt;Awesome, just what I meant.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2011 03:11:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/cannot-open-and-close-dwg-in-same-command/m-p/3184984#M58929</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-09T03:11:43Z</dc:date>
    </item>
  </channel>
</rss>

