您好,AutoCAD C#大师, 我在这里有这段代码,用于打开图形,更改某些图层并关闭,但是在完成图形并关闭之后,在与我创建的dwg相同的文件夹中有一个名为 DocName.dwl 的文件无法关闭autocad无法删除。
有没有可以放开锁的地方,我没有用?
[CommandMethod("Test", CommandFlags.UsePickSet | CommandFlags.Redraw | CommandFlags.Session | CommandFlags.Modal)]
public void Test()
{
var DocList = AskUserToSelectDocs();
foreach (string FileName in DocList.Files)
{
Application.DocumentManager.Open(FileName, false);
Document zcDoc = Application.DocumentManager.MdiActiveDocument;
Database zcDB = zcDoc.Database;
using (DocumentLock acLckDoc = zcDoc.LockDocument())
{
using (Transaction ZcTran = zcDoc.TransactionManager.StartTransaction())
{
BlockTable zcBLT = (BlockTable)ZcTran.GetObject(zcDB.BlockTableId, OpenMode.ForWrite);
BlockTableRecord zcBLTR = (BlockTableRecord)ZcTran.GetObject(zcBLT[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
zcBLTR.UpgradeOpen();
var editor = zcDoc.Editor;
var SelectionSet = editor.SelectAll().Value;
foreach (ObjectId Objeto in SelectionSet.GetObjectIds())
{
Entity ent = ZcTran.GetObject(Objeto, OpenMode.ForWrite) as Entity;
if (ent is Viewport)
{
var VP = ent as Viewport;
VP.Layer = "Defpoints";
}
}
editor.Regen();
ZcTran.Commit();
}
}
zcDB.SaveAs(zcDB.Filename, zcDB.OriginalFileVersion);
zcDoc.CloseAndSave(zcDoc.Name);
zcDoc.Dispose();
}
}
编辑:现在完整的代码
谢谢!
答案 0 :(得分:0)
对于这样的事情,无需打开文档。我建议只将数据库加载到内存中,进行操作,然后保存。所有这些都将在“ DocumentManager”之外。非常适合提高性能,尤其是随着要处理的文件数量的增加。
当前打开的文档并不重要,只要它不是正在处理的文档之一即可(您可以检查)。
尝试一下:
public static void Test()
{
var DocList = AskUserToSelectDocs();
//save working db reference
Database originalDB = HostApplicationServices.WorkingDatabase;
foreach (string FileName in DocList.Files)
{
//a little trick here:
//construct the database in memory, and read in the target file.
//now your database is your working database, not the active doc!!
using (Database database = new Database(false, true))
{
database.ReadDwgFile(FileName, System.IO.FileShare.ReadWrite, true, string.Empty);
HostApplicationServices.WorkingDatabase = database;//important!
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
//do your stuff
}
//reset WorkingDB to original db
HostApplicationServices.WorkingDatabase = originalDB;
database.SaveAs(FileName, DwgVersion.Current);
}
}
}