找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2318|回复: 1

[分享] Reading the Block Table of a Dynamic Block

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-11 21:38:31 | 显示全部楼层 |阅读模式

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

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

×
Reading the Block Table of a Dynamic BlockBy Augusto Goncalves
For a dynamic block we can create a Block Properties Table, which defines and controls values for parameters and properties. This is available when BEDIT’ing a dynamic block, as shown below.
0.jpg
From the API perspective, this data is stored under a DBDictionary called ACAD_ENHANCEDBLOCK. To access it, we need to use some ‘Internal’ APIs, which means that there is no documentation and it may change.
The following code sample show how to access it for a given selected BlockReference object. Minimal error check were done for simplicity.
[CommandMethod("readBlockTable"
static public void CmdReadBlockTable()
{
  Editor ed = Application.DocumentManager.
    MdiActiveDocument.Editor;
  // select a block reference
  PromptEntityOptions peo = new PromptEntityOptions(
    "Select a dynamic block reference: ");
  peo.SetRejectMessage("Only block reference");
  peo.AddAllowedClass(typeof(BlockReference), false);
  PromptEntityResult per = ed.GetEntity(peo);
  if (per.Status != PromptStatus.OK) return;
  ObjectId blockRefId = per.ObjectId;
  // get the database and start a transaction
  Database db = Application.DocumentManager.
    MdiActiveDocument.Database;
  using (Transaction trans = db.
    TransactionManager.StartTransaction())
  {
    // open the dynamic block reference
    BlockReference blockRef = trans.GetObject(
      blockRefId, OpenMode.ForRead) as BlockReference;
    if (!blockRef.IsDynamicBlock) return;
    // get the dynamic block table definition
    BlockTableRecord blockDef = trans.GetObject(
      blockRef.DynamicBlockTableRecord,
      OpenMode.ForRead) as BlockTableRecord;
    // open the extension dictionary
    if (blockDef.ExtensionDictionary.IsNull) return;
    DBDictionary extDic = trans.GetObject(
      blockDef.ExtensionDictionary, OpenMode.ForRead)
      as DBDictionary;
    // open the ENHANCEDBLOCK dictionary
    Autodesk.AutoCAD.Internal.DatabaseServices.EvalGraph graph =
      trans.GetObject(extDic.GetAt("ACAD_ENHANCEDBLOCK"),
      OpenMode.ForRead) as EvalGraph;
    int[] nodeIds = graph.GetAllNodes();
    foreach (uint nodeId in nodeIds)
    {
      // open the node ID
      DBObject node = graph.GetNode(nodeId,
        OpenMode.ForRead, trans);
      // check is if the correct type
      if (!(node is BlockPropertiesTable)) continue;
      // convert the object
      BlockPropertiesTable table =
        node as BlockPropertiesTable;
      // ok, we have the data, let's show it...
      // get the number of columns
      int columns = table.Columns.Count;
      int currentRow = 0;
      foreach (
        BlockPropertiesTableRow row
        in table.Rows)
      {
        ed.WriteMessage("\n[{0}]:\t", currentRow);
        for (
          int currentColumn = 0;
          currentColumn < columns;
          currentColumn++)
        {
          // get the colum value for the
          // current row. May be more multiple
          TypedValue[] columnValue =
            row[currentColumn].AsArray();
          foreach (TypedValue tpVal in columnValue)
          {
            ed.WriteMessage("{0}; ", tpVal.Value);
          }
          ed.WriteMessage("|");
        }
        currentRow++;
      }
    }
  }
}
1.jpg



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

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-6-11 21:45:49 | 显示全部楼层
1. How to get the name of an anonymous block?


blockreference = (BlockReference)transition.GetObject(objectid, OpenMode.ForRead);
if (blockreference.IsDynamicBlock)
blockname = ((BlockTableRecord)transition.GetObject(blockreference.DynamicBlockTableRecord, OpenMode.ForRead)).Name;
else
blockname = ((BlockTableRecord)transition.GetObject(blockreference.BlockTableRecord, OpenMode.ForRead)).Name;


( blockreference.DynamicBlockTableRecord return nothing/null )

2. How to list all references of a block definition by the name if the block are dynamic?


BlockTable blocktable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead); if (blocktable.Has(blockname))
{
BlockTableRecord blocktablerecord = (BlockTableRecord)transaction.GetObject(blocktable[blockname], OpenMode.ForRead, false, false);

ObjectIdCollection allblockreferences =
blocktablerecord.GetBlockReferenceIds(true, true);

foreach (ObjectId auxreferenceid in
allblockreferences)
blocksreferencesbyname.Add(auxReferenceId); //add to a list of objects ids

if (blocktablerecord.IsDynamicBlock)

{
//ObjectIdCollection blockanonymousreferences = blocktablerecord.GetAnonymousBlockIds();


//foreach (ObjectId anonyreferenceid in blockanonymousreferences)
//{
// blocktablerecord = (BlockTableRecord)transaction.GetObject(
anonyreferenceid, OpenMode.ForRead, false, false);


// blockreferences = blocktablerecord.GetBlockReferenceIds(true, true);

// foreach (ObjectId auxreferenceid in blockreferences)
// if (!BlocksReferencesByName.Contains(auxreferenceid))
// BlocksReferencesByName.Add(
auxreferenceid);
//}
}
}

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-6 03:40 , Processed in 0.265087 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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