乐筑天下

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

[结贴]插入外部属性块,无法获得属性的值

[复制链接]

1

主题

3

帖子

1

银币

初来乍到

Rank: 1

铜币
7
发表于 2012-10-11 11:16:00 | 显示全部楼层 |阅读模式
开发了个程序,想实现如下功能,但是遇到问题,请各位帮助分析诊断一下,谢谢!
方法:选取外部文件,读取里面的属性块并插入,获取其默认属性值,显示在指定的位置。
问题:块插入正常,但是属性值无法获取,如:一个块中有个属性Tag=“工号”,textString=“0123”,通过程序插入后,能查看和编辑该属性,但是值为"",也即没有将定义好的默认属性值带入插入的图形中去。
代码如下:
  1. private static bool InsertBlock(string strBlockName, string strFilePath)
  2.         {
  3.                       Autodesk..ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;            
  4.                      using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  5.             {
  6.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  7.                 {
  8.                     using (Database db = new Database(false, true))
  9.                     {
  10.                         db.ReadDwgFile(strFilePath, System.IO.FileShare.Read, true, null);
  11.                         using (Transaction trm = db.TransactionManager.StartTransaction())
  12.                         {
  13.                             BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
  14.                             if (!sourceBlkTbl.Has(strBlockName))
  15.                             {
  16.                                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 91");
  17.                                 return false;
  18.                             }
  19.                             ObjectIdCollection blockIds = new ObjectIdCollection();
  20.                             blockIds.Add(sourceBlkTbl[strBlockName]);
  21.                             IdMapping IdMap = new IdMapping();
  22.                             db.WblockCloneObjects(blockIds, doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace, false);
  23.                                                      trm.Commit();
  24.                         }
  25.                     }
  26.                     tr.Commit();
  27.                 }
  28.             }
  29.             return true;
  30.         }
  31.         public static ObjectId InsertBlock(string strFilePath, string strBlockName, Point3d insertPt, Double scale)
  32.         {
  33.                      ObjectId objId = new ObjectId();
  34.             Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  35.             if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
  36.             {
  37.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 118!");
  38.                 return objId;
  39.             }
  40.             if (!System.IO.File.Exists(strFilePath))
  41.             {
  42.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("没有找到外部引用文件!");
  43.                 doc.Editor.WriteMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
  44.                 return objId;
  45.             }
  46.             using (Transaction tr = doc.TransactionManager.StartTransaction())
  47.             {
  48.                 BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
  49.                 if (!bt.Has(strBlockName))
  50.                 {
  51.                     if (!InsertBlock(strBlockName, strFilePath))
  52.                     {
  53.                         Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 137");
  54.                         return objId;
  55.                     }
  56.                 }
  57.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  58.                 using (BlockReference newBlkRef = new BlockReference(insertPt, bt[strBlockName]))
  59.                 {
  60.                     newBlkRef.ScaleFactors = new Scale3d(scale);
  61.                     objId = btr.AppendEntity(newBlkRef);
  62.                     #region 加入默认属性
  63.                     BlockTableRecord btrBlock = tr.GetObject(bt[strBlockName], OpenMode.ForRead) as BlockTableRecord;
  64.                     if (btrBlock.HasAttributeDefinitions)
  65.                     {
  66.                         foreach (ObjectId id in btrBlock)
  67.                         {
  68.                             DBObject ent = tr.GetObject(id, OpenMode.ForRead) as DBObject;
  69.                             if (ent is AttributeDefinition)
  70.                             {
  71.                                 
  72.                                 AttributeDefinition attdef = ent as AttributeDefinition;//取得块的属性定义
  73.                               
  74.                                 AttributeReference attref = new AttributeReference();//新建属性参照
  75.                                 attref.SetPropertiesFrom(attdef);
  76.                                 attref.SetAttributeFromBlock(attdef, Matrix3d.Displacement(btrBlock.Origin.GetVectorTo(insertPt)));//复制属性
  77.                                  newBlkRef.AttributeCollection.AppendAttribute(attref);//附着属性参照
  78.                                 tr.AddNewlyCreatedDBObject(attref, true);
  79.                                 attref = null;
  80.                             }
  81.                         }
  82.                     }
  83.                     #endregion
  84.                     tr.AddNewlyCreatedDBObject(newBlkRef, true);
  85.                 }
  86.                 tr.Commit();
  87.                 //tr.Dispose();
  88.             }
  89. return objId;
  90.         }

回复

使用道具 举报

32

主题

651

帖子

8

银币

中流砥柱

Rank: 25

铜币
779
发表于 2012-10-11 13:22:00 | 显示全部楼层
textString=“0123”是存在于块参考中,而从外部文件搬进来的是块记录
回复

使用道具 举报

1

主题

3

帖子

1

银币

初来乍到

Rank: 1

铜币
7
发表于 2012-10-11 16:19:00 | 显示全部楼层
感谢回复。怎么才能获得textString=“0123”呢?
回复

使用道具 举报

32

主题

651

帖子

8

银币

中流砥柱

Rank: 25

铜币
779
发表于 2012-10-11 20:04:00 | 显示全部楼层
textString=“0123”只是其中一个块参考BlockReference的值,若有多个块参考,则或许另有textString=“0000”;
你若是要把块参考搬进来,则把块参考的ObjectId放进blockIds,如blockIds.Add(sourceBlkTbl[strBlockName]);
回复

使用道具 举报

1

主题

3

帖子

1

银币

初来乍到

Rank: 1

铜币
7
发表于 2012-10-18 10:01:00 | 显示全部楼层
如你所说,在块定义的时候必须要给默认属性值,这样在其它图形中插入该块的时候就能获取到该属性值。
回复

使用道具 举报

0

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
2
发表于 2012-12-6 12:30:00 | 显示全部楼层
不错,很好
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-14 21:10 , Processed in 0.361370 second(s), 64 queries .

© 2020-2025 乐筑天下

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