【高阶CAD二次开发】在ARX或.Net中使用AssocPersSubentityIdPE访问子实体

标题如何在ObjectARX或。net中访问AutoCAD实体的子实体?

下面是c++和c#中的两个示例,它们演示了如何访问AssocPersSubentityIdPE并使用它遍历所选实体的顶点和边缘。

void ArxSubEntityPE()

{

        Acad::ErrorStatus err;

 

        ads_name name;

        ads_point pt;

 

        if(acedEntSel(L"\nSelect an Entity: ", name, pt) != RTNORM)

                return;

 

        AcDbObjectId id;

        acdbGetObjectId(id, name);

 

        AcDbObjectPointer <AcDbEntity> pEntity(id, AcDb::kForRead);

 

        // Get the Protocol extension associated with the entity

        AcDbAssocPersSubentIdPE* const pAssocPersSubentIdPE =

                AcDbAssocPersSubentIdPE::cast(

                        pEntity->queryX(AcDbAssocPersSubentIdPE::desc()));

 

        if( pAssocPersSubentIdPE == NULL)

                return;

 

        AcArray <AcDbSubentId> vertexIds;

        pAssocPersSubentIdPE->getAllSubentities(

                pEntity,

                AcDb::kVertexSubentType,

                vertexIds);

 

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

 

        for (int i = 0; i<vertexIds.length(); ++i)

        {

                AcDbFullSubentPath path(id, vertexIds[i]);

 

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

       

                if (pPoint != NULL)

                {

                   AcGePoint3d pos = pPoint->position();

                   acutPrintf(L"\n . Vertex: [%.2f, %.2f, %.2f]",

                       pos.x, pos.y, pos.z);


                        delete pPoint;

                }

        }

 

        AcArray <AcDbSubentId> edgeIds;

        pAssocPersSubentIdPE->getAllSubentities(

                pEntity,

                AcDb::kEdgeSubentType,

                edgeIds);

       

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

 

        for (int i = 0; i<edgeIds.length(); ++i)

        {

                AcDbFullSubentPath path(id, edgeIds[i]);

 

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

       

                if (pSubEntity != NULL)

                {

                        acutPrintf(L"\n . %s (Id = %d)",

                                pSubEntity->isA()->name(),

                                edgeIds[i].index());

 

                        delete pSubEntity;

                }

        }

}

 
[CommandMethod("SubEntityPE")]

public void SubEntityPE()

{

    Document doc = Application.DocumentManager.MdiActiveDocument;

    Database db = doc.Database;

    Editor ed = doc.Editor;

 

    PromptEntityOptions peo = new PromptEntityOptions(

        "\nSelect an Entity: ");

 

    PromptEntityResult per = ed.GetEntity(peo);

 

    if (per.Status != PromptStatus.OK)

        return; 

 

    using (Transaction Tx = db.TransactionManager.StartTransaction())

    {

        Entity entity = Tx.GetObject(per.ObjectId, OpenMode.ForRead)

            as Entity;

 

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

 

        IntPtr pSubentityIdPE = entity.QueryX(

            AssocPersSubentityIdPE.GetClass(

            typeof(AssocPersSubentityIdPE)));

 

        if (pSubentityIdPE == IntPtr.Zero)

            //Entity doesn't support the subentityPE

            return;

 

        AssocPersSubentityIdPE subentityIdPE =

            AssocPersSubentityIdPE.Create(pSubentityIdPE, false)

                as AssocPersSubentityIdPE;

 

        SubentityId[] vertexIds = subentityIdPE.GetAllSubentities(

            entity,

            SubentityType.Vertex);

 

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

 

        foreach (SubentityId subentId in vertexIds)

        {

            FullSubentityPath path = new FullSubentityPath(entId, subentId);

 

            DBPoint vertex = entity.GetSubentity(path) as DBPoint;

 

            if (vertex != null)

            {

                ed.WriteMessage(

                    "\n . Vertex: [{0}, {1}, {2}]",

                    vertex.Position.X,

                    vertex.Position.Y,

                    vertex.Position.Z);

 

                vertex.Dispose();

            }

        }

 

        SubentityId[] edgeIds = subentityIdPE.GetAllSubentities(

            entity,

            SubentityType.Edge);

 

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

 

        foreach (SubentityId subentId in edgeIds)

        {

            FullSubentityPath path = new FullSubentityPath(entId, subentId);

 

            Entity edgeEntity = entity.GetSubentity(path);

 

            if (edgeEntity != null)

            {

                ed.WriteMessage("\n . " + edgeEntity.ToString());

                edgeEntity.Dispose();

            }

        }

 

 

        SubentityId[] faceIds = subentityIdPE.GetAllSubentities(

            entity,

            SubentityType.Face);

 

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

 

        foreach (SubentityId subentId in faceIds)

        {

            FullSubentityPath path = new FullSubentityPath(entId, subentId);

 

            Entity faceEntity = entity.GetSubentity(path);

 

            if (faceEntity != null)

            {

                ed.WriteMessage("\n . " + faceEntity.ToString());

                faceEntity.Dispose();

            }

        }

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三好学生~张旺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值