ObjectARX 创建AcDbLeader引线附着块对象实例

 

 三点引线附着块参照实例

        // 创建AcDbLeader引线
        ads_point p1,p2,p3;
        if (RTNORM != acedGetPoint(NULL,_T("\n请选择引线第一点: "),p1))
            return;
        if (RTNORM != acedGetPoint(p1,_T("\n请选择引线第二点: "),p2))
            return;
        if (RTNORM != acedGetPoint(p2,_T("\n请选择引线第三点: "),p3))
            return;
        // ucs点转换到wcs点
        acdbUcs2Wcs(p1,p1,false);
        acdbUcs2Wcs(p2,p2,false);
        acdbUcs2Wcs(p3,p3,false);
        // ads点转为AcGe点
        AcGePoint3d pt1=asPnt3d(p1);
        AcGePoint3d pt2=asPnt3d(p2);
        AcGePoint3d pt3=asPnt3d(p3);
        //  设置第三点为第二点的水平点
        pt3.y=pt2.y;

        AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
        // 使用智能指针打开块表
        AcDbBlockTablePointer pBlkTbl(pDb,AcDb::kForRead);
        Acad::ErrorStatus es =pBlkTbl.openStatus();
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }
        // 获取指定块名的块表记录ID
        AcDbObjectId blkDefId;
        es=pBlkTbl->getAt(_T("123"),blkDefId);
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }
        // 使用智能指针创建块参照对象
        AcDbObjectPointer<AcDbBlockReference> pBlkRef;
        es= pBlkRef.create();
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }        
        pBlkRef->setBlockTableRecord(blkDefId);
        pBlkRef->setPosition(pt3);
        pBlkRef->setDatabaseDefaults();
        // 使用智能指针打开pDb的当前空间
        AcDbBlockTableRecordPointer pBlkRcd(pDb->currentSpaceId(),AcDb::kForWrite);
        es =pBlkRcd.openStatus();
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }
        AcDbObjectId blkRefId;
        pBlkRcd->appendAcDbEntity(blkRefId,pBlkRef);
        // 引线的附着对象一定要及时关闭,否则attachAnnotation附着失败
        pBlkRef->close();
        // 创建引线对象
        AcDbObjectPointer<AcDbLeader> pLeader;
        es= pLeader.create();
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }
        pLeader->appendVertex(pt1);
        pLeader->appendVertex(pt2);
        pLeader->appendVertex(pt3);
        pLeader->setDatabaseDefaults();
        // 要先将引线对象添加到数据库后再执行attachAnnotation附着对象,否则附着失败
        pBlkRcd->appendAcDbEntity(pLeader);
        // 条件1,附着实体ID,AcDbLeader引线必须是添加到数据库的对象否则出现,eNotInDatabase
        // 条件2,附着实体ID对象也必须是数据库的关闭对象,因为需要对附着对象添加反应器之类操作
        es = pLeader->attachAnnotation(blkRefId);
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }
        es = pLeader->evaluateLeader();
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\n程序执行失败,错误码=%s"),acadErrorStatusText(es));
            return;
        }

附着多行文字,参考链接 https://adndevblog.typepad.com/autocad/2012/07/create-a-leader-attached-mtext.html

posted @ 2020-07-19 17:11  edata  阅读(1260)  评论(0编辑  收藏  举报