找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1457|回复: 0

[分享] 如何把定制的信息保存到DWG中

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-27 23:12:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
How can I store my custom information in a dwg file?          By Marat Mirgaleev
  Q: I need to store some information in a dwg file, which would describe this drawing and let to integrate it with another software system. Can I write such custom information into a drawing and read it later? Can it be done without opening the drawing in AutoCAD?
     
A: You may use so called Named Object Dictionary (NOD) to store custom data in a drawing. NOD is an essential part of an AutoCAD drawing database and it is created automatically when the drawing is created.     

  As always with database operations, your program may open a dwg file invisibly to the user via Database.ReadDwgFile() method, i.e. it does not have to be the drawing in the active AutoCAD window.   

  Here is an example:
  1. // Write some data to the NOD
  2. //============================
  3. [CommandMethod("WNOD")]
  4. public void WriteToNOD()
  5. {
  6.   Database db = new Database();
  7.   try
  8.   {
  9.     // We will write to C:\Temp\Test.dwg. Make sure it exists!
  10.     // Load it into AutoCAD
  11.     db.ReadDwgFile(@"C:\Temp\Test.dwg",
  12.                     System.IO.FileShare.ReadWrite, false, null);
  13.     using( Transaction trans =
  14.                       db.TransactionManager.StartTransaction() )
  15.     {
  16.       // Find the NOD in the database
  17.       DBDictionary nod = (DBDictionary)trans.GetObject(
  18.                   db.NamedObjectsDictionaryId, OpenMode.ForWrite);
  19.       // We use Xrecord class to store data in Dictionaries
  20.       Xrecord myXrecord = new Xrecord();
  21.       myXrecord.Data = new ResultBuffer(
  22.               new TypedValue((int)DxfCode.Int16, 1234),
  23.               new TypedValue((int)DxfCode.Text,
  24.                               "This drawing has been processed"));
  25.       // Create the entry in the Named Object Dictionary
  26.       nod.SetAt("MyData", myXrecord);
  27.       trans.AddNewlyCreatedDBObject(myXrecord, true);
  28.       // Now let's read the data back and print them out
  29.       //  to the Visual Studio's Output window
  30.       ObjectId myDataId = nod.GetAt("MyData");
  31.       Xrecord readBack = (Xrecord)trans.GetObject(
  32.                                     myDataId, OpenMode.ForRead);
  33.       foreach (TypedValue value in readBack.Data)
  34.         System.Diagnostics.Debug.Print(
  35.                   "===== OUR DATA: " + value.TypeCode.ToString()
  36.                   + ". " + value.Value.ToString());
  37.       trans.Commit();
  38.     } // using
  39.     db.SaveAs(@"C:\Temp\Test.dwg", DwgVersion.Current);
  40.   }
  41.   catch( Exception e )
  42.   {
  43.     System.Diagnostics.Debug.Print(e.ToString());
  44.   }
  45.   finally
  46.   {
  47.     db.Dispose();
  48.   }
  49. } // End of WriteToNOD()

You can treat the NOD as a place to keep "document-level" data.   

  If you need to store some data related to particular objects in the drawing, consider using their Extension Dictionaries.   

评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:39 , Processed in 0.171553 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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