- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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:
- [CommandMethod("DynablockVisibilityStates")]
- public void DynablockVisibilityStates()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptResult pr = ed.GetString("\nEnter block name: ");
- if (pr.Status != PromptStatus.OK)
- return;
- using (Transaction Tx = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = Tx.GetObject(
- db.BlockTableId,
- OpenMode.ForRead)
- as BlockTable;
- if (!bt.Has(pr.StringResult))
- {
- ed.WriteMessage("\nBlock doesn't exist :(");
- return;
- }
- BlockTableRecord btr = Tx.GetObject(
- bt[pr.StringResult],
- OpenMode.ForRead)
- as BlockTableRecord;
- if (!btr.IsDynamicBlock)
- {
- ed.WriteMessage("\nNot a dynamic block :(");
- return;
- }
- if (btr.ExtensionDictionary == null)
- {
- ed.WriteMessage("\nNo ExtensionDictionary :(");
- return;
- }
- DBDictionary dico = Tx.GetObject(
- btr.ExtensionDictionary,
- OpenMode.ForRead)
- as DBDictionary;
- if (!dico.Contains("ACAD_ENHANCEDBLOCK"))
- {
- ed.WriteMessage(
- "\nACAD_ENHANCEDBLOCK Entry not found :(");
- return;
- }
- ObjectId graphId = dico.GetAt("ACAD_ENHANCEDBLOCK");
- System.Collections.Generic.List<object> parameterIds =
- acdbEntGetObjects(graphId, 360);
- foreach (object parameterId in parameterIds)
- {
- ObjectId id = (ObjectId)parameterId;
- if (id.ObjectClass.Name ==
- "AcDbBlockVisibilityParameter")
- {
- System.Collections.Generic.List<TypedValue>
- visibilityParam = acdbEntGetTypedVals(id);
- System.Collections.Generic.
- List<TypedValue>.Enumerator enumerator =
- visibilityParam.GetEnumerator();
- while (enumerator.MoveNext())
- {
- if (enumerator.Current.TypeCode == 303)
- {
- string group =
- (string)enumerator.Current.Value;
- enumerator.MoveNext();
- int nbEntitiesInGroup =
- (int)enumerator.Current.Value;
- ed.WriteMessage(
- "\n . Visibility Group: " + group +
- " Nb Entities in group: " +
- nbEntitiesInGroup);
- for (int i = 0; i < nbEntitiesInGroup; ++i)
- {
- enumerator.MoveNext();
- ObjectId entityId =
- (ObjectId)enumerator.Current.Value;
- Entity entity = Tx.GetObject(
- entityId,
- OpenMode.ForRead)
- as Entity;
- ed.WriteMessage("\n - " +
- entity.ToString() + " " +
- entityId.ToString());
- }
- }
- }
- break;
- }
- }
- Tx.Commit();
- }
- }
- public struct ads_name
- {
- IntPtr a;
- IntPtr b;
- };
- [DllImport("acdb18.dll",
- CallingConvention = CallingConvention.Cdecl,
- EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z")]
- public static extern int acdbGetAdsName(
- ref ads_name name, ObjectId objId);
- [DllImport("acad.exe",
- CharSet = CharSet.Ansi,
- CallingConvention = CallingConvention.Cdecl,
- EntryPoint = "acdbEntGet")]
- public static extern System.IntPtr acdbEntGet(ref ads_name ename);
- private System.Collections.Generic.List<object>
- acdbEntGetObjects(ObjectId id, short dxfcode)
- {
- System.Collections.Generic.List<object> result =
- new System.Collections.Generic.List<object>();
- ads_name name = new ads_name();
- int res = acdbGetAdsName(ref name, id);
- ResultBuffer rb = new ResultBuffer();
- Autodesk.AutoCAD.Runtime.Interop.AttachUnmanagedObject(
- rb, acdbEntGet(ref name), true);
- ResultBufferEnumerator iter = rb.GetEnumerator();
- while (iter.MoveNext())
- {
- TypedValue typedValue = (TypedValue)iter.Current;
- if (typedValue.TypeCode == dxfcode)
- {
- result.Add(typedValue.Value);
- }
- }
- return result;
- }
- private System.Collections.Generic.List<TypedValue>
- acdbEntGetTypedVals(ObjectId id)
- {
- System.Collections.Generic.List<TypedValue> result =
- new System.Collections.Generic.List<TypedValue>();
- ads_name name = new ads_name();
- int res = acdbGetAdsName(ref name, id);
- ResultBuffer rb = new ResultBuffer();
- Autodesk.AutoCAD.Runtime.Interop.AttachUnmanagedObject(
- rb, acdbEntGet(ref name), true);
- ResultBufferEnumerator iter = rb.GetEnumerator();
- while (iter.MoveNext())
- {
- result.Add((TypedValue)iter.Current);
- }
- return result;
- }
|
评分
-
查看全部评分
|