From 3f999fe020e41bed60df1195dacc88055a0340e5 Mon Sep 17 00:00:00 2001 From: Leon_Office Date: Fri, 11 Feb 2022 10:30:23 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=9ADBTrans(string=20fileName,=20bool=20?= =?UTF-8?q?commit=20=3D=20true)=EF=BC=8C=E5=8E=9F=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BB=85=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=96=87=E4=BB=B6=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=AF=95=E5=89=8D=E5=8F=B0=E6=89=93=E5=BC=80=202=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9void=20Dispose(bool=20disposing)=EF=BC=8C?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=96=87=E6=A1=A3=E4=B8=8D=E6=98=AF=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E7=8A=B6=E6=80=81=E5=87=BA=E7=8E=B0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=203=E3=80=81=E6=B7=BB=E5=8A=A0void=20SaveAs(string=20fileName,?= =?UTF-8?q?=20DwgVersion=20version)=E6=96=B9=E6=B3=95=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=95=B0=E6=8D=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/IFoxCAD.Cad/Runtime/DBTrans.cs | 47 ++++++++++++++++++++++++------ tests/Test/testFileDatabase.cs | 28 ++++++++++++++++++ tests/Test/testid.cs | 2 +- 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 tests/Test/testFileDatabase.cs diff --git a/src/IFoxCAD.Cad/Runtime/DBTrans.cs b/src/IFoxCAD.Cad/Runtime/DBTrans.cs index 90aa274..07131cd 100644 --- a/src/IFoxCAD.Cad/Runtime/DBTrans.cs +++ b/src/IFoxCAD.Cad/Runtime/DBTrans.cs @@ -67,7 +67,7 @@ public static DBTrans Top /// /// 要打开的文档 /// 事务是否提交 - public DBTrans(Document doc = null, bool commit = true, bool doclock = false) + public DBTrans(bool commit = true, bool doclock = false, Document doc = null) { doc ??= Application.DocumentManager.MdiActiveDocument; Document = doc; @@ -93,17 +93,34 @@ public DBTrans(Database database, bool commit = true) /// 事务是否提交 public DBTrans(string fileName, bool commit = true) { - Database = new Database(false, true); - if (Path.GetExtension(fileName).ToLower().Contains("dxf")) + if (string.IsNullOrWhiteSpace(fileName)) + throw new ArgumentNullException(nameof(fileName)); + + var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Cast() + .FirstOrDefault(doc => doc.Database.OriginalFileName == fileName); + if (null != doc) { - Database.DxfIn(fileName, null); + Database = doc.Database; + Document = doc; + Editor = doc.Editor; + Init(commit, true); } else { - Database.ReadDwgFile(fileName, FileShare.Read, true, null); + if (System.IO.File.Exists(fileName)) + { + Database = new Database(false, true); + if (Path.GetExtension(fileName).ToLower().Contains("dxf")) + Database.DxfIn(fileName, null); + else + Database.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, null); + } + else + Database = new Database(true, false); + + Database.CloseInput(true); + Init(commit, false); } - Database.CloseInput(true); - Init(commit, false); } /// /// 初始化事务及事务队列、提交模式 @@ -288,7 +305,18 @@ public ObjectId GetObjectId(string handleString) #endregion #region idispose接口相关函数 - + /// + /// 保存 + /// + /// 文件名 + /// 版本 + public void SaveAs(string fileName, DwgVersion version) + { + if (Document != null) + Document.SendStringToExecute("_qsave\n", false, true, true); + else + Database.SaveAs(fileName, version); + } public void Abort() { Transaction.Abort(); @@ -309,7 +337,6 @@ public void Commit() protected virtual void Dispose(bool disposing) { - Transaction.TransactionManager.QueueForGraphicsFlush(); if (!disposedValue) { if (disposing) @@ -319,6 +346,8 @@ protected virtual void Dispose(bool disposing) dBTrans.Pop(); if (!Transaction.IsDisposed) { + if (Document?.IsActive==true) + Transaction.TransactionManager.QueueForGraphicsFlush(); Transaction.Dispose(); } documentLock?.Dispose(); diff --git a/tests/Test/testFileDatabase.cs b/tests/Test/testFileDatabase.cs new file mode 100644 index 0000000..8650982 --- /dev/null +++ b/tests/Test/testFileDatabase.cs @@ -0,0 +1,28 @@ +/************************************************************** +*作者:Leon +*创建时间:2022/2/11 9:55:32 +**************************************************************/ +namespace Test +{ + public class TestFileDatabase + { + [CommandMethod("Test_FileDatabaseInit")] + public void TestDatabase() + { + try + { + var fileName = @"C:\Users\Administrator\Desktop\合并详图测试BUG.dwg"; + + DBTrans trans = new(fileName); + trans.ModelSpace.AddEntity(new Line(new(0, 0, 0), new(1000, 1000, 0))); + trans.SaveAs(fileName, DwgVersion.AC1021); + trans.Dispose(); + } + catch (System.Exception e) + { + System.Windows.MessageBox.Show(e.Message); + } + + } + } +} \ No newline at end of file diff --git a/tests/Test/testid.cs b/tests/Test/testid.cs index 911fd53..7d35984 100644 --- a/tests/Test/testid.cs +++ b/tests/Test/testid.cs @@ -55,7 +55,7 @@ public void TestId() [CommandMethod("testmycommand")] public void TestMyCommand() { - using (var dbtrans = new DBTrans(Env.Document,true,false)) { + using (var dbtrans = new DBTrans(true,false)) { using (var trans = Env.Database.TransactionManager.StartTransaction()) { var l1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0)); -- Gitee