Message 1 of 3
How to close DWG file while it is open
Not applicable
08-24-2017
10:04 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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());
}
}
}
}
}
}