乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 107|回复: 2

关于创建Database的问题

[复制链接]

75

主题

306

帖子

10

银币

中流砥柱

Rank: 25

铜币
606
发表于 2021-6-17 14:12:00 | 显示全部楼层 |阅读模式
创建Database时可以传入两个参数new Database(buildDefaultDrawing,noDocument)
其中第二个参数表示是否与当前文档产生关联(帮助的原文 specifying whether or not to associate this database to the current document)
如果没有关联,还好理解,就是无任何关系。那么有关联时,新建的Database对象与当前文档之间有什么关系呢?能体现在什么地方?
回复

使用道具 举报

11

主题

92

帖子

10

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2021-11-19 17:01:00 | 显示全部楼层

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. }

回复

使用道具 举报

75

主题

306

帖子

10

银币

中流砥柱

Rank: 25

铜币
606
发表于 2021-11-21 23:54:00 | 显示全部楼层
谢谢,学习了!!
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-4-12 14:07 , Processed in 2.694502 second(s), 59 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表