diff --git a/src/CAD/IFox.CAD.Shared/Runtime/DBTrans.cs b/src/CAD/IFox.CAD.Shared/Runtime/DBTrans.cs
index b7617b5585bc3cd44f826c924ea6f5bcf5d2ea78..6094e3aa7b8affe69c9c2ce1eddb7f7fe43c5744 100644
--- a/src/CAD/IFox.CAD.Shared/Runtime/DBTrans.cs
+++ b/src/CAD/IFox.CAD.Shared/Runtime/DBTrans.cs
@@ -169,6 +169,14 @@ public DBTrans(Database database, bool commit = true)
///
/// 事务栈
/// 打开文件,默认提交事务
+ /// 建议用法:
+ /// try
+ /// {
+ /// using var tr = new DBTrans(...);
+ /// ...
+ /// }
+ /// catch (...) { ... }
+ ///
///
/// 要打开的文件名
/// 事务是否提交
@@ -182,16 +190,20 @@ public DBTrans(string fileName,
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
@@ -207,7 +219,7 @@ public DBTrans(string fileName,
var doc = Acaop.DocumentManager
.Cast()
.FirstOrDefault(doc => !doc.IsDisposed && doc.Name == _fileName);
-
+
if (activeOpen)
{
if (doc is null)
@@ -220,6 +232,7 @@ public DBTrans(string fileName,
}
catch (Exception e)
{
+ IsDisposed = true; // 出错后未 Push 会造成 Pop 抛错, 固设为 true
throw new IOException($"错误:此文件打开错误:{fileName}\n错误信息:{e.Message}");
}
}
@@ -227,11 +240,11 @@ public DBTrans(string fileName,
// 若没有设置: 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;
@@ -241,15 +254,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
{
@@ -259,7 +284,7 @@ public DBTrans(string fileName,
}
}
}
-
+
Transaction = Database.TransactionManager.StartTransaction();
_commit = commit;
_dBTrans.Push(this);