找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 593|回复: 0

Accessing sub-entities using AssocPersSubentityIdPE in ARX or .Net

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-7 18:07:16 | 显示全部楼层 |阅读模式

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

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

×

问题:
How do I access the sub-entities of an AutoCAD entity in ObjectARX or .Net?

解答:
Below are two samples in C++ and C# that illustrate how to access the AssocPersSubentityIdPE and use it to iterate through the vertices and edges of the selected entity.

  1. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //Use: Retrieves entity vertices & edges using AssocPersSubentityIdPE
  3. //
  4. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. void ArxSubEntityPE()
  6. {
  7.            Acad::ErrorStatus err;

  8.            ads_name name;
  9.            ads_point pt;

  10.            if(acedEntSel(L"\nSelect an Entity: ", name, pt) != RTNORM)
  11.                       return;

  12.            AcDbObjectId id;
  13.            acdbGetObjectId(id, name);

  14.            AcDbObjectPointer  pEntity(id, AcDb::kForRead);

  15.            // Get the Protocol extension associated with the entity
  16.            AcDbAssocPersSubentIdPE* const pAssocPersSubentIdPE =
  17.              AcDbAssocPersSubentIdPE::cast(pEntity->queryX(AcDbAssocPersSubentIdPE::desc()));

  18.            if( pAssocPersSubentIdPE == NULL)
  19.                       return;

  20.            AcArray  vertexIds;
  21.            pAssocPersSubentIdPE->getAllSubentities(pEntity, AcDb::kVertexSubentType, vertexIds);

  22.            acutPrintf(L"\n- Vertex Subentities: ");

  23.            for (int i = 0; i < vertexIds.length(); ++i)
  24.            {
  25.                       AcDbFullSubentPath path(id, vertexIds);

  26.                       AcDbPoint* pPoint = AcDbPoint::cast(pEntity->subentPtr(path));

  27.                       if (pPoint != NULL)
  28.                       {
  29.                                   AcGePoint3d pos = pPoint->position();
  30.                                   acutPrintf(L"\n . Vertex: [%.2f, %.2f, %.2f]", pos.x, pos.y, pos.z);
  31.                                   delete pPoint;
  32.                       }
  33.            }

  34.            AcArray  edgeIds;
  35.            pAssocPersSubentIdPE->getAllSubentities(pEntity, AcDb::kEdgeSubentType, edgeIds);

  36.            acutPrintf(L"\n- Edge Subentities: ");

  37.            for (int i = 0; i < edgeIds.length(); ++i)
  38.            {
  39.                       AcDbFullSubentPath path(id, edgeIds);

  40.                       AcDbEntity* pSubEntity = pEntity->subentPtr(path);

  41.                       if (pSubEntity != NULL)
  42.                       {
  43.                                   acutPrintf(L"\n . %s", pSubEntity->isA()->name());
  44.                                   delete pSubEntity;
  45.                       }
  46.            }
  47. }



  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //Use: Retrieves entity vertices & edges using AssocPersSubentityIdPE
  3. //
  4. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. [CommandMethod("SubEntityPE")]
  6. public void SubEntityPE()
  7. {
  8.     Document doc = Application.DocumentManager.MdiActiveDocument;
  9.     Database db = doc.Database;
  10.     Editor ed = doc.Editor;

  11.     PromptEntityOptions peo = new PromptEntityOptions("\nSelect an Entity: ");

  12.     PromptEntityResult per = ed.GetEntity(peo);

  13.     if (per.Status != PromptStatus.OK)
  14.         return;

  15.     //Using AssocPersSubentityIdPE
  16.     using (Transaction Tx = db.TransactionManager.StartTransaction())
  17.     {
  18.         Entity entity = Tx.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;

  19.         ObjectId[] entId = new ObjectId[] { entity.ObjectId };

  20.         IntPtr pSubentityIdPE = entity.QueryX(AssocPersSubentityIdPE.GetClass(typeof(AssocPersSubentityIdPE)));

  21.         if (pSubentityIdPE == IntPtr.Zero)
  22.             //Entity doesn't support the subentityPE
  23.             return;

  24.         AssocPersSubentityIdPE subentityIdPE = AssocPersSubentityIdPE.Create(pSubentityIdPE, false)
  25.             as AssocPersSubentityIdPE;

  26.         SubentityId[] vertexIds = subentityIdPE.GetAllSubentities(entity, SubentityType.Vertex);

  27.         ed.WriteMessage("\n- Vertex Subentities: ");

  28.         foreach (SubentityId subentId in vertexIds)
  29.         {
  30.             FullSubentityPath path = new FullSubentityPath(entId, subentId);
  31.             DBPoint vertex = entity.GetSubentity(path) as DBPoint;

  32.             if (vertex != null)
  33.             {
  34.                 ed.WriteMessage("\n . Vertex: [{0}, {1}, {2}]", vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
  35.                 vertex.Dispose();
  36.             }
  37.         }


  38.         SubentityId[] edgeIds = subentityIdPE.GetAllSubentities(entity, SubentityType.Edge);

  39.         ed.WriteMessage("\n- Edge Subentities: ");

  40.         foreach (SubentityId subentId in edgeIds)
  41.         {
  42.             FullSubentityPath path = new FullSubentityPath(entId, subentId);
  43.             Entity edgeEntity = entity.GetSubentity(path);

  44.             if (edgeEntity != null)
  45.             {
  46.                 ed.WriteMessage("\n . " + edgeEntity.ToString());
  47.                 edgeEntity.Dispose();
  48.             }
  49.         }
  50.     }
  51. }


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

本版积分规则

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

GMT+8, 2024-4-23 16:42 , Processed in 0.364038 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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