How to close DWG file while it is open

How to close DWG file while it is open

Anonymous
Not applicable
792 Views
2 Replies
Message 1 of 3

How to close DWG file while it is open

Anonymous
Not applicable

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.

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<string> items = new List<string>();

            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());
                    }
                }
            }
        }
    }
}
0 Likes
793 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

NOTE: In the code I am using DocumentCollectionExtension.CloseAll() method and receiving the same error when using CloseAndDiscard()

0 Likes
Message 3 of 3

ActivistInvestor
Mentor
Mentor

Your code executes in the current document's execution context. When that document closes, its execution context is destroyed, along with your code.

 

See the revisions in red in your code below.



@Anonymous wrote:

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.

namespace AcadLibrary
{
    public class Class1
    {
        [CommandMethod("UpdateDB", CommandFlags.Session)]
        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<string> items = new List<string>();

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Autodesk.AutoCAD.DatabaseServices.Database db = HostApplicationServices.WorkingDatabase;
            using(doc.LockDocument())            
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()); } } } } } }

 

0 Likes