自定义块以及属性的获取
自定义一个块,包括一条直线和一个圆,代码如下:
void createSelfBlock()
{
AcDbBlockTableRecord *pBlkTblRcd = new AcDbBlockTableRecord();
pBlkTblRcd->setName(_T("JSW")); //设置块名
AcGePoint3d basePoint(0,0,0);
pBlkTblRcd->setOrigin(basePoint);
//添加记录块到块表中
AcDbBlockTable *pBlkTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForWrite);
AcDbObjectId blockId;
pBlkTbl->add(blockId, pBlkTblRcd);
//创建实体图形
//circle
AcGePoint3d center(100,100,0);
AcDbCircle *pCircle = new AcDbCircle();
pCircle->setCenter(center);
pCircle->setRadius(50);
//line
AcGePoint3d startPoint(10, 10, 0);
AcGePoint3d endPoint(10, -10, 0);
AcDbLine *pLine = new AcDbLine(center, endPoint);
pLine->setColorIndex(1);
//设置属性
AcDbAttributeDefinition *pAttrDef = new AcDbAttributeDefinition();
pAttrDef->setPosition(center);
pAttrDef->setHeight(30);
pAttrDef->setPrompt(_T("输入圆形点的值"));
pAttrDef->setTextString(_T("abc"));
pAttrDef->setTag(_T("自定义的块标签"));
pAttrDef->setHorizontalMode(AcDb::kTextLeft);
pAttrDef->setVerticalMode(AcDb::kTextBase);
pAttrDef->setPreset(Adesk::kFalse);
pAttrDef->setInvisible(Adesk::kFalse);
pAttrDef->setConstant(Adesk::kFalse);
AcDbObjectId entityId;
pBlkTblRcd->appendAcDbEntity(entityId, pCircle);
pBlkTblRcd->appendAcDbEntity(entityId, pLine);
pBlkTblRcd->appendAcDbEntity(entityId, pAttrDef);
pCircle->close();
pLine->close();
pAttrDef->close();
pBlkTbl->close();
pBlkTblRcd->close();
}
获取块属性
void getAttributeOfBlock()
{
AcDbBlockTable *pBlkTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForRead);
//检测是否存在输入块
TCHAR blkName[40];
if (acedGetString(Adesk::kFalse, TEXT("\n输入的图形块名:"), blkName) != RTNORM)
{
pBlkTbl->close();
return;
}
CString strBlkDef;
strBlkDef.Format(TEXT("%s"), blkName);
if(!pBlkTbl->has(strBlkDef))
{
acutPrintf(TEXT("\n没有找到所在块"));
pBlkTbl->close();
return;
}
AcDbObjectId RcdId;
//获取记录块表的id
pBlkTbl->getAt(strBlkDef,RcdId,AcDb::kForRead);
pBlkTbl->close();
AcDbObjectPointer<AcDbBlockTableRecord> pBlkRefRcd(RcdId, AcDb::kForRead);
if (pBlkRefRcd.openStatus() != Acad::eOk)
{
return;
}
AcDbObjectIdArray blockIds;
pBlkRefRcd->getBlockReferenceIds(blockIds);
AcDbObjectPointer<AcDbBlockReference> pBlkRef(blockIds[0], AcDb::kForRead);
if (pBlkRef.openStatus() != Acad::eOk)
{
return;
}
AcDbObjectIterator* pIter = pBlkRef->attributeIterator();
if (!pIter)
{
return;
}
for (pIter->start(); !pIter->done(); pIter->step())
{
AcDbObjectId attId = pIter->objectId();
AcDbAttribute* pAtt = NULL;
if (pBlkRef->openAttribute(pAtt, attId, AcDb::kForRead) != Acad::eOk)
continue;
//acutPrintf(pAtt->tagConst(), pAtt->textStringConst(),pAtt->textString());
acutPrintf(pAtt->tagConst());
acutPrintf(pAtt->textStringConst());
pAtt->close();
}
delete pIter;
}