找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2034|回复: 0

[分享] 存取 dynamic block 中的可见实体

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-28 11:20:53 | 显示全部楼层 |阅读模式

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

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

×
Accessing visible entities in a dynamic blockBy Philippe Leefsma

Here is a question once posted by an ADN member:
How do I access the list of visible entities for a specific visibility state in a dynamic block that contains a visibility parameter?
The following solution is a code sample based on a suggestion proposed by Tony Tanzillo in this forum post.

Solution
Unfortunately, as the dynamic block API is a bit limited, there is no direct way to access this information. This is actually buried in the dynamic block definition's extension dictionary, and the object you need to look for is an instance of an "AcDbBlockVisibilityParameter" (that's the native ObjectARX class and the runtime class name, but it isn’t exposed to the public API, neither in C++ nor .Net).
So a bit of extra work is needed in order to obtain it, here are the steps:
1. Get the BlockTableRecord for the dynamic block definition.
2. Get the extension dictionary for the BlockTableRecord.
3. Get ObjectId of the "ACAD_ENHANCEDBLOCK" entry in the extension dictionary.
4. call acdbEntGet() on ObjectId obtained in step 3, which returns a ResultBuffer containing TypedValues.
5. Iterate over the TypedValues looking for an item whose group code is 360, and get the ObjectId stored in the
TypedValue's Value property, then get value of the 'Name' property of the ObjectId's ObjectClass property,
and see if it equals the string "AcDbBlockVisibilityParameter".
6. Once you've found the AcDbBlockVisibilityParameter object's ObjectId, call acdbEntGet() on it.
7. Iterate over the TypedValues in the ResultBuffer returned by acdbEntGet(), looking for an item whose group code is 303.

8. Get the next element from the ResultBuffer, which is a 94 group code whose value is the number of ObjectIds that follow, and then read that many subsequent items and collect the ObjectIds in each into a list, and you then have finally the ObjectIds of the block entities that are explicitly visible in the visiblity state.
Here is the complete C# code:

  1. [CommandMethod("DynablockVisibilityStates")]
  2. public void DynablockVisibilityStates()
  3. {
  4.     Document doc = Application.DocumentManager.MdiActiveDocument;
  5.     Database db = doc.Database;
  6.     Editor ed = doc.Editor;
  7.     PromptResult pr = ed.GetString("\nEnter block name: ");
  8.     if (pr.Status != PromptStatus.OK)
  9.         return;
  10.     using (Transaction Tx = db.TransactionManager.StartTransaction())
  11.     {
  12.         BlockTable bt = Tx.GetObject(
  13.             db.BlockTableId,
  14.            OpenMode.ForRead)
  15.                 as BlockTable;
  16.         if (!bt.Has(pr.StringResult))
  17.         {
  18.             ed.WriteMessage("\nBlock doesn't exist :(");
  19.             return;
  20.         }
  21.         BlockTableRecord btr = Tx.GetObject(
  22.             bt[pr.StringResult],
  23.             OpenMode.ForRead)
  24.                 as BlockTableRecord;
  25.         if (!btr.IsDynamicBlock)
  26.         {
  27.             ed.WriteMessage("\nNot a dynamic block :(");
  28.             return;
  29.         }
  30.         if (btr.ExtensionDictionary == null)
  31.         {
  32.             ed.WriteMessage("\nNo ExtensionDictionary :(");
  33.             return;
  34.         }
  35.         DBDictionary dico = Tx.GetObject(
  36.             btr.ExtensionDictionary,
  37.             OpenMode.ForRead)
  38.                 as DBDictionary;
  39.         if (!dico.Contains("ACAD_ENHANCEDBLOCK"))
  40.         {
  41.             ed.WriteMessage(
  42.                 "\nACAD_ENHANCEDBLOCK Entry not found :(");
  43.             return;
  44.         }
  45.         ObjectId graphId = dico.GetAt("ACAD_ENHANCEDBLOCK");
  46.         System.Collections.Generic.List<object> parameterIds =
  47.             acdbEntGetObjects(graphId, 360);
  48.         foreach (object parameterId in parameterIds)
  49.         {
  50.             ObjectId id = (ObjectId)parameterId;
  51.             if (id.ObjectClass.Name ==
  52.                 "AcDbBlockVisibilityParameter")
  53.             {
  54.                 System.Collections.Generic.List<TypedValue>
  55.                     visibilityParam = acdbEntGetTypedVals(id);
  56.                 System.Collections.Generic.
  57.                     List<TypedValue>.Enumerator enumerator =
  58.                     visibilityParam.GetEnumerator();
  59.                 while (enumerator.MoveNext())
  60.                 {
  61.                     if (enumerator.Current.TypeCode == 303)
  62.                     {
  63.                         string group =
  64.                             (string)enumerator.Current.Value;
  65.                         enumerator.MoveNext();
  66.                         int nbEntitiesInGroup =
  67.                             (int)enumerator.Current.Value;
  68.                         ed.WriteMessage(
  69.                            "\n . Visibility Group: " + group +
  70.                            " Nb Entities in group: " +
  71.                            nbEntitiesInGroup);
  72.                         for (int i = 0; i < nbEntitiesInGroup; ++i)
  73.                         {
  74.                             enumerator.MoveNext();
  75.                             ObjectId entityId =
  76.                                 (ObjectId)enumerator.Current.Value;
  77.                             Entity entity = Tx.GetObject(
  78.                                 entityId,
  79.                                 OpenMode.ForRead)
  80.                                     as Entity;
  81.                             ed.WriteMessage("\n    - " +
  82.                                 entity.ToString() + " " +
  83.                                 entityId.ToString());
  84.                         }
  85.                     }
  86.                 }
  87.                 break;
  88.             }
  89.         }
  90.         Tx.Commit();
  91.     }
  92. }
  93. public struct ads_name
  94. {
  95.     IntPtr a;
  96.     IntPtr b;
  97. };
  98. [DllImport("acdb18.dll",
  99.     CallingConvention = CallingConvention.Cdecl,
  100.    EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z")]

  101. public static extern int acdbGetAdsName(
  102.     ref ads_name name, ObjectId objId);
  103. [DllImport("acad.exe",
  104.     CharSet = CharSet.Ansi,
  105.     CallingConvention = CallingConvention.Cdecl,
  106.     EntryPoint = "acdbEntGet")]
  107. public static extern System.IntPtr acdbEntGet(ref ads_name ename);
  108. private System.Collections.Generic.List<object>
  109.     acdbEntGetObjects(ObjectId id, short dxfcode)
  110. {
  111.     System.Collections.Generic.List<object> result =
  112.         new System.Collections.Generic.List<object>();
  113.      ads_name name = new ads_name();
  114.     int res = acdbGetAdsName(ref name, id);
  115.     ResultBuffer rb = new ResultBuffer();
  116.     Autodesk.AutoCAD.Runtime.Interop.AttachUnmanagedObject(
  117.         rb, acdbEntGet(ref name), true);
  118.     ResultBufferEnumerator iter = rb.GetEnumerator();
  119.     while (iter.MoveNext())
  120.     {
  121.         TypedValue typedValue = (TypedValue)iter.Current;
  122.         if (typedValue.TypeCode == dxfcode)
  123.         {
  124.             result.Add(typedValue.Value);
  125.         }
  126.     }
  127.     return result;
  128. }
  129. private System.Collections.Generic.List<TypedValue>
  130.     acdbEntGetTypedVals(ObjectId id)
  131. {
  132.     System.Collections.Generic.List<TypedValue> result =
  133.         new System.Collections.Generic.List<TypedValue>();
  134.     ads_name name = new ads_name();
  135.     int res = acdbGetAdsName(ref name, id);
  136.     ResultBuffer rb = new ResultBuffer();
  137.     Autodesk.AutoCAD.Runtime.Interop.AttachUnmanagedObject(
  138.         rb, acdbEntGet(ref name), true);
  139.     ResultBufferEnumerator iter = rb.GetEnumerator();
  140.     while (iter.MoveNext())
  141.     {
  142.         result.Add((TypedValue)iter.Current);
  143.     }
  144.     return result;
  145. }

评分

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

查看全部评分

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

本版积分规则

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

GMT+8, 2024-5-1 05:36 , Processed in 0.379796 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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