diff --git a/src/IFoxCAD.Cad/Runtime/DBTrans.cs b/src/IFoxCAD.Cad/Runtime/DBTrans.cs
index 90aa274ea6ffacfb44716b7b282197b74a9f4628..07131cd2eab67dd7793b0c6f01a3f61f9241dc89 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 0000000000000000000000000000000000000000..86509825dda41a614ccb31c4e487f2eebdfdbc07
--- /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 911fd531f912189fe03a8de3484714e5f40fd51e..7d359845c2a302bc38a04323c9dc36a5f069bca9 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));