明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1356|回复: 2

[基础] 关于创建Database的问题

[复制链接]
发表于 2021-6-17 14:12:03 | 显示全部楼层 |阅读模式
本帖最后由 guohq 于 2021-6-20 21:53 编辑

创建Database时可以传入两个参数new Database(buildDefaultDrawing,noDocument)
其中第二个参数表示是否与当前文档产生关联(帮助的原文 specifying whether or not to associate this database to the current document)


如果没有关联,还好理解,就是无任何关系。那么有关联时,新建的Database对象与当前文档之间有什么关系呢?能体现在什么地方?
发表于 2021-11-19 17:01:01 | 显示全部楼层
本帖最后由 qq1254582201 于 2021-11-19 17:02 编辑

https://adndevblog.typepad.com/a ... ng-readdwgfile.html
这应该是你想要的答案!!
  1. eNoInputFiler exception when using ReadDwgFile
  2. By Adam Nagy
  3. When I run the following code I get an eNoInputFiler exception when calling ReadDwgFile. What could be the problem?
  4. using System;
  5. using System.IO;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.Runtime;
  8. using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
  9. using Autodesk.AutoCAD.ApplicationServices;
  10. [assembly: CommandClass(typeof(MyAddIn.Commands))]
  11. namespace MyAddIn
  12. {
  13.   public class Commands
  14.   {
  15.     [CommandMethod("MyCmd", CommandFlags.Session)]
  16.     public static void MyCmd()
  17.     {
  18.       Document doc = acApp.DocumentManager.MdiActiveDocument;
  19.       using (Database db = new Database(false, false))
  20.       {
  21.         db.ReadDwgFile(
  22.           @"C:\testdwg.dwg", FileShare.Read, false, null);
  23.         // do something with it
  24.       }
  25.     }
  26.   }
  27. }
  28. Solution
  29. The second parameter of the Database constructor (noDocument) controls if the Database should be associated with the current document or not.
  30. Probably the main reason to decide to associate the Database with a document would be to participate in Undo: see ObjectARX Reference > Document-Independent Databases
  31. In this case you would need to do document locking (and since you are in Session context because of CommandFlags.Session, you would need to do it yourself, explicitly, using Document.LockDocument())
  32. However if you do not associate the Database with the current document, then no locking is needed.
  33. So the solution is to either lock the Document when you want to associate your Database with it (noDocument = false):
  34. [CommandMethod("MyCmd", CommandFlags.Session)]
  35. public static void MyCmd()
  36. {
  37.   Document doc = acApp.DocumentManager.MdiActiveDocument;
  38.   using (doc.LockDocument())
  39.   {
  40.     using (Database db = new Database(false, false))
  41.     {
  42.       db.ReadDwgFile(
  43.         @"C:\testdwg.dwg", FileShare.Read, false, null);
  44.       // do something with it
  45.     }
  46.   }
  47. }
  48. ... or don’t associate the Database with the current document (noDocument = true, much more common)
  49. [CommandMethod("MyCmd", CommandFlags.Session)]
  50. public static void MyCmd()
  51. {
  52.   using (Database db = new Database(false, true))
  53.   {
  54.     db.ReadDwgFile(
  55.       @"C:\testdwg.dwg", FileShare.Read, false, null);
  56.     // do something with it
  57.   }
  58. }



 楼主| 发表于 2021-11-21 23:54:40 | 显示全部楼层
谢谢,学习了!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 04:28 , Processed in 0.170853 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表