From edc3b149d71da48368c5d5db9094d3fa792222f0 Mon Sep 17 00:00:00 2001 From: yupeng_dyp Date: Sun, 16 Jun 2024 10:42:06 +0000 Subject: [PATCH] =?UTF-8?q?update=20CADShared/Runtime/DBTrans.cs.=20?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E4=BC=A0=E5=85=A5=20DBTrans=20=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=89=88=E6=9C=AC=E6=98=AF=E9=AB=98=E4=BA=8E?= =?UTF-8?q?=E5=BD=93=E5=89=8DCAD=E7=89=88=E6=9C=AC=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4=20Database.ReadDwgFile()=20?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=20"=E6=9C=AA=E5=AE=9E=E7=8E=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD"=20=E7=9A=84=E9=94=99=E8=AF=AF=EF=BC=8C=E9=82=A3?= =?UTF-8?q?=E4=B9=88=E8=BF=99=E4=B8=AA=E6=97=B6=E5=80=99=E5=B0=B1=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E6=9C=89=20push=EF=BC=8C=E6=9C=80=E5=90=8E=E5=9C=A8?= =?UTF-8?q?=E5=9B=9E=E6=94=B6=20DDTrans=20=E6=97=B6=E5=AF=BC=E8=87=B4=20po?= =?UTF-8?q?p=20=E9=94=99=E8=AF=AF=EF=BC=8C=E7=BB=88=E8=87=B3CAD=E9=80=80?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yupeng_dyp --- CADShared/Runtime/DBTrans.cs | 62 +++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/CADShared/Runtime/DBTrans.cs b/CADShared/Runtime/DBTrans.cs index 38af048..31e24b7 100644 --- a/CADShared/Runtime/DBTrans.cs +++ b/CADShared/Runtime/DBTrans.cs @@ -177,6 +177,14 @@ public DBTrans(Database database, bool commit = true) /// /// 事务栈 /// 打开文件,默认提交事务 + /// 建议用法: + /// try + /// { + /// using var tr = new DBTrans(...); + /// ... + /// } + /// catch (...) { ... } + /// /// /// 要打开的文件名 /// 事务是否提交 @@ -184,22 +192,26 @@ public DBTrans(Database database, bool commit = true) /// 密码 /// 后台打开false;前台打开true(必须设置CommandFlags.Session) public DBTrans(string fileName, - bool commit = true, - FileOpenMode fileOpenMode = FileOpenMode.OpenForReadAndWriteNoShare, - string? password = null, - bool activeOpen = false) + bool commit = true, + FileOpenMode fileOpenMode = FileOpenMode.OpenForReadAndWriteNoShare, + string? password = null, + bool activeOpen = false) { if (string.IsNullOrWhiteSpace(fileName)) + { + IsDisposed = true; // 出错后未 Push 会造成 Pop 抛错, 固设为 true throw new ArgumentNullException(nameof(fileName)); - + } + _fileName = fileName.Replace("/", "\\"); // doc.Name总是"D:\\JX.dwg" - + // 此处若为失败的文件名,那么保存的时候就会丢失名称, // 因此用 _fileName 储存 if (!File.Exists(_fileName)) { if (activeOpen) { + IsDisposed = true; // 出错后未 Push 会造成 Pop 抛错, 固设为 true throw new IOException("错误:事务栈明确为前台开图时,文件不存在"); } else @@ -213,9 +225,9 @@ public DBTrans(string fileName, else { var doc = Acaop.DocumentManager - .Cast() - .FirstOrDefault(doc => !doc.IsDisposed && doc.Name == _fileName); - + .Cast() + .FirstOrDefault(doc => !doc.IsDisposed && doc.Name == _fileName); + if (activeOpen) { if (doc is null) @@ -228,19 +240,20 @@ public DBTrans(string fileName, } catch (Exception e) { + IsDisposed = true; // 出错后未 Push 会造成 Pop 抛错, 固设为 true throw new IOException($"错误:此文件打开错误:{fileName}\n错误信息:{e.Message}"); } } - + // 设置命令标记: CommandFlags.Session // 若没有设置: doc.IsActive 会异常 if (!doc.IsActive) Acaop.DocumentManager.MdiActiveDocument = doc; - + // Open()是跨文档,所以必须要锁文档 // 否则 Editor?.Redraw() 的 tm.QueueForGraphicsFlush() 将报错提示文档锁 _documentLock = doc.LockDocument(); - + Database = doc.Database; Document = doc; Editor = doc.Editor; @@ -250,16 +263,27 @@ public DBTrans(string fileName, if (doc is null) { Database = new Database(false, true); - if (Path.GetExtension(_fileName).ToLower().Contains("dxf")) + try { - Database.DxfIn(_fileName, null); + if (Path.GetExtension(_fileName).ToLower().Contains("dxf")) + { + Database.DxfIn(_fileName, null); + } + else + { + Database.ReadDwgFile(_fileName, fileOpenMode, true, password); + } } - else + catch (Exception e) { - Database.ReadDwgFile(_fileName, fileOpenMode, true, password); + IsDisposed = true; // 出错后未 Push 会造成 Pop 抛错, 固设为 true + Acap.ShowAlertDialog($"错误:此文件打开错误:{fileName}\n格式版本可能高于当前CAD版本\n错误信息:{e.Message}"); + throw; // 交给调用处处理更灵活,避免再次报错时CAD就直接退出 + } + finally + { + Database.CloseInput(true); } - - Database.CloseInput(true); } else { @@ -269,7 +293,7 @@ public DBTrans(string fileName, } } } - + Transaction = Database.TransactionManager.StartTransaction(); _commit = commit; _dBTrans.Push(this); -- Gitee