ObjectARX插入属性块简单例子
//插入属性块简单例子2020-10-11 by www.cnblogs.com/edata //指定块名 CString blkName=_T("123"); //指定块插入点 AcGePoint3d insertPoint(100,100,0); //ARX智能指针打开块表 AcDbBlockTablePointer pBlkTbl(curDoc()->database(),AcDb::kForRead); if (Acad::eOk != pBlkTbl.openStatus()) { acutPrintf(_T("\n打开块表失败,错误码= %s"),acadErrorStatusText(pBlkTbl.openStatus())); return; } //判断是否有块定义 if (!pBlkTbl->has(blkName)) { acutPrintf(_T("\n块定义名< %s >不存在!"),blkName); return; } //获取块定义objectId AcDbObjectId blkId; pBlkTbl->getAt(blkName,blkId); if (blkId.isNull()) { acutPrintf(_T("\n获取块定义ID失败!")); return; } //ARX智能指针创建块参照 AcDbObjectPointer<AcDbBlockReference> pBlkRef; if (Acad::eOk != pBlkRef.create()) { acutPrintf(_T("\n块参照创建失败,可能内存已满!")); return; } pBlkRef->setDatabaseDefaults(); pBlkRef->setPosition(insertPoint); pBlkRef->setBlockTableRecord(blkId); //ARX智能指针打开当前空间的块表记录 AcDbBlockTableRecordPointer pBlkRcd(curDoc()->database()->currentSpaceId(),AcDb::kForWrite); if(Acad::eOk != pBlkRcd.openStatus()) { acutPrintf(_T("\n打开当前空间的块表记录失败,错误码= %s"),acadErrorStatusText(pBlkRcd.openStatus())); return; } //添加块参照到数据库 pBlkRcd->appendAcDbEntity(pBlkRef); //缩放旋转块 AcGeMatrix3d mat; mat.setToScaling(3,insertPoint); pBlkRef->transformBy(mat); mat.setToRotation(3.1415*0.25,AcGeVector3d::kZAxis,insertPoint); pBlkRef->transformBy(mat); //ARX智能指针打开块定义块表记录 AcDbBlockTableRecordPointer pBlkDef(blkId,AcDb::kForRead); if (Acad::eOk != pBlkDef.openStatus()) { acutPrintf(_T("\n打开块定义块表记录失败,错误码= %s"),acadErrorStatusText(pBlkDef.openStatus())); return; } //判断块定义是否包含属性定义 if (pBlkDef->hasAttributeDefinitions()) { //获取迭代器 AcDbBlockTableRecordIterator *pItr=NULL; pBlkDef->newIterator(pItr); if (pItr==NULL) { acutPrintf(_T("\n获取迭代器失败!")); return; } //获取块参照的Transform变换矩阵 AcGeMatrix3d blkXform=pBlkRef->blockTransform(); AcDbObjectId entId; for (pItr->start(); !pItr->done(); pItr->step()) { pItr->getEntityId(entId); if (entId.isNull()) { continue; } //ARX智能指针打开属性定义AcDbAttributeDefinition AcDbObjectPointer<AcDbAttributeDefinition> pAttDef(entId,AcDb::kForRead); if (Acad::eOk != pAttDef.openStatus()) { continue; } //ARX智能指针创建一个属性参照AcDbAttribute AcDbObjectPointer<AcDbAttribute> pAtt; if (Acad::eOk != pAtt.create()) { continue; } //设置从块中获取属性定义相关数据 pAtt->setAttributeFromBlock(pAttDef,blkXform); //设置属性值,此处省略区分属性标记对应属性值,全部设置为一个值 pAtt->setTextString(_T("40")); // 向块参照追加属性对象 pBlkRef->appendAttribute(pAtt); } delete pItr; pItr=NULL; }