<?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 Re: How to close DWG file while it is open in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326527#M29948</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Your code executes in the current document's execution context. When that document closes, its execution context is destroyed, along with your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the revisions &lt;FONT color="#FF0000"&gt;in red&lt;/FONT&gt; in your code below.&lt;/P&gt;&lt;BR /&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;I am currently working on an custom command called "UpdateDB." This command will parse the dwg file, collect item data, and then retreives a hash value. The hash value will be used to see if the DWG file has been changed. When I try to use "CloseAndDiscard()" method to close the drawing, I get an error stating "Drawing is busy." Is there a way around this? I posted my code below so you can see what i am doing.&lt;/P&gt;&lt;PRE&gt;namespace AcadLibrary
{
    public class Class1
    {
        [CommandMethod("UpdateDB"&lt;FONT color="#FF0000"&gt;, CommandFlags.Session&lt;/FONT&gt;)]
        public void UpdateDB()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            string drawingName = Path.GetFileName(doc.Name);
            string path = Path.GetFullPath(doc.Name);

            byte[] hashValue;


            List&amp;lt;string&amp;gt; items = new List&amp;lt;string&amp;gt;();

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Autodesk.AutoCAD.DatabaseServices.Database db = HostApplicationServices.WorkingDatabase;
&lt;FONT color="#FF0000"&gt;            using(doc.LockDocument())&lt;/FONT&gt;            &lt;BR /&gt;            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult selRes = ed.SelectAll();
                if (selRes.Status != PromptStatus.OK)
                {
                    ed.WriteMessage(
                                "\nerror in getting the selectAll");
                    return;
                }
                ObjectId[] ids = selRes.Value.GetObjectIds();
                SelectionSet selObjs = selRes.Value;

                
                foreach (SelectedObject selObj in selObjs)
                {
                    if (selObj != null)
                    {
                        Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity;
                        if(ent != null)
                        {
                            Autodesk.Fabrication.Item item = Autodesk.Fabrication.Job.GetFabricationItemFromACADHandle(ent.Handle.ToString());
                            if (item != null)
                            {
                                //file.WriteLine(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                                items.Add(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                            }
                        }
                    }
                }
            }
            DocumentCollectionExtension.CloseAll(docs);
            if (doc.IsDisposed)
            {
                FileStream readFile = null;
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    FileStream file = File.OpenRead(path);
                    hashValue = md5.ComputeHash(readFile);
                    
                }
                using (StreamWriter file = new StreamWriter("C:\\temp\\temp.txt"))
                {
                    foreach (string item in items)
                    {
                        file.WriteLine(item + " | " + hashValue.ToString());
                    }
                }
            }
        }
    }
}&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Aug 2017 17:25:14 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-08-24T17:25:14Z</dc:date>
    <item>
      <title>How to close DWG file while it is open</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326469#M29946</link>
      <description>&lt;P&gt;I am currently working on an custom command called "UpdateDB." This command will parse the dwg file, collect item data, and then retreives a hash value. The hash value will be used to see if the DWG file has been changed. When I try to use "CloseAndDiscard()" method to close the drawing, I get an error stating "Drawing is busy." Is there a way around this? I posted my code below so you can see what i am doing.&lt;/P&gt;&lt;PRE&gt;namespace AcadLibrary
{
    public class Class1
    {
        [CommandMethod("UpdateDB")]
        public void UpdateDB()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            string drawingName = Path.GetFileName(doc.Name);
            string path = Path.GetFullPath(doc.Name);

            byte[] hashValue;


            List&amp;lt;string&amp;gt; items = new List&amp;lt;string&amp;gt;();

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Autodesk.AutoCAD.DatabaseServices.Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult selRes = ed.SelectAll();
                if (selRes.Status != PromptStatus.OK)
                {
                    ed.WriteMessage(
                                "\nerror in getting the selectAll");
                    return;
                }
                ObjectId[] ids = selRes.Value.GetObjectIds();
                SelectionSet selObjs = selRes.Value;

                
                foreach (SelectedObject selObj in selObjs)
                {
                    if (selObj != null)
                    {
                        Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity;
                        if(ent != null)
                        {
                            Autodesk.Fabrication.Item item = Autodesk.Fabrication.Job.GetFabricationItemFromACADHandle(ent.Handle.ToString());
                            if (item != null)
                            {
                                //file.WriteLine(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                                items.Add(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                            }
                        }
                    }
                }
            }
            DocumentCollectionExtension.CloseAll(docs);
            if (doc.IsDisposed)
            {
                FileStream readFile = null;
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    FileStream file = File.OpenRead(path);
                    hashValue = md5.ComputeHash(readFile);
                    
                }
                using (StreamWriter file = new StreamWriter("C:\\temp\\temp.txt"))
                {
                    foreach (string item in items)
                    {
                        file.WriteLine(item + " | " + hashValue.ToString());
                    }
                }
            }
        }
    }
}&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2017 17:04:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326469#M29946</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-08-24T17:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to close DWG file while it is open</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326478#M29947</link>
      <description>&lt;P&gt;NOTE: In the code I am using DocumentCollectionExtension.CloseAll() method and receiving the same error when using CloseAndDiscard()&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 17:06:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326478#M29947</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-08-24T17:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to close DWG file while it is open</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326527#M29948</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Your code executes in the current document's execution context. When that document closes, its execution context is destroyed, along with your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the revisions &lt;FONT color="#FF0000"&gt;in red&lt;/FONT&gt; in your code below.&lt;/P&gt;&lt;BR /&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;I am currently working on an custom command called "UpdateDB." This command will parse the dwg file, collect item data, and then retreives a hash value. The hash value will be used to see if the DWG file has been changed. When I try to use "CloseAndDiscard()" method to close the drawing, I get an error stating "Drawing is busy." Is there a way around this? I posted my code below so you can see what i am doing.&lt;/P&gt;&lt;PRE&gt;namespace AcadLibrary
{
    public class Class1
    {
        [CommandMethod("UpdateDB"&lt;FONT color="#FF0000"&gt;, CommandFlags.Session&lt;/FONT&gt;)]
        public void UpdateDB()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            string drawingName = Path.GetFileName(doc.Name);
            string path = Path.GetFullPath(doc.Name);

            byte[] hashValue;


            List&amp;lt;string&amp;gt; items = new List&amp;lt;string&amp;gt;();

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Autodesk.AutoCAD.DatabaseServices.Database db = HostApplicationServices.WorkingDatabase;
&lt;FONT color="#FF0000"&gt;            using(doc.LockDocument())&lt;/FONT&gt;            &lt;BR /&gt;            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult selRes = ed.SelectAll();
                if (selRes.Status != PromptStatus.OK)
                {
                    ed.WriteMessage(
                                "\nerror in getting the selectAll");
                    return;
                }
                ObjectId[] ids = selRes.Value.GetObjectIds();
                SelectionSet selObjs = selRes.Value;

                
                foreach (SelectedObject selObj in selObjs)
                {
                    if (selObj != null)
                    {
                        Entity ent = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as Entity;
                        if(ent != null)
                        {
                            Autodesk.Fabrication.Item item = Autodesk.Fabrication.Job.GetFabricationItemFromACADHandle(ent.Handle.ToString());
                            if (item != null)
                            {
                                //file.WriteLine(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                                items.Add(item.Name.ToString() + " | " + item.Weight + " | " + item.Specification.Name + " | " + item.Material.Name + " | " + item.UniqueId + " | " + drawingName);
                            }
                        }
                    }
                }
            }
            DocumentCollectionExtension.CloseAll(docs);
            if (doc.IsDisposed)
            {
                FileStream readFile = null;
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    FileStream file = File.OpenRead(path);
                    hashValue = md5.ComputeHash(readFile);
                    
                }
                using (StreamWriter file = new StreamWriter("C:\\temp\\temp.txt"))
                {
                    foreach (string item in items)
                    {
                        file.WriteLine(item + " | " + hashValue.ToString());
                    }
                }
            }
        }
    }
}&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 17:25:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-close-dwg-file-while-it-is-open/m-p/7326527#M29948</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-08-24T17:25:14Z</dc:date>
    </item>
  </channel>
</rss>

