AcDbObjectId idBtr = AcDbObjectId::kNull;
AcDbBlockTable *pBlockTable = NULL;
AcDbBlockTableRecord* pBlockRecord = NULL;
const TCHAR *pName = _T("MyBlock");
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForWrite);
if (!pBlockTable->has(pName)) // для краткости привожу вариант только с созданием нового описания блока
{
pBlockRecord = new AcDbBlockTableRecord;
pBlockRecord->setName(pName);
pBlockRecord->setOrigin(AcGePoint3d::kOrigin);
if (pBlockTable->add(idBtr, pBlockRecord) != Acad::eOk)
{
delete pBlockRecord;
pBlockTable->close();
return;
}
}
AcDbPolyline *pLine = new AcDbPolyline();
AcDbObjectId entId;
pLine->addVertexAt(0, AcGePoint2d(0, 1));
pLine->addVertexAt(0, AcGePoint2d(0, -1));
pLine->setColorIndex(0);
pLine->setLineWeight(AcDb::LineWeight::kLnWtByBlock);
pBlockRecord->appendAcDbEntity(entId, pLine);
pLine->close();
AcDbAttributeDefinition *pAttdef = new AcDbAttributeDefinition;
pAttdef->setTag(_T("NUM")); // если сразу не установить тэг, то в дальнейшем выскакивает ошибка при работе с pText
pAttdef->convertIntoMTextAttributeDefinition();
AcDbMText* pText = pAttdef->getMTextAttributeDefinition();
pText->setBackgroundFill(true);
pText->setBackgroundScaleFactor(1.1);
pText->setUseBackgroundColor(true);
pAttdef->setMTextAttributeDefinition(pText);
pAttdef->setTextStyle(styleId());
pAttdef->setHeight(2.0);
pAttdef->setColorIndex(0);
pAttdef->setPosition(AcGePoint3d(0, 2, 0));
pAttdef->setVerticalMode(AcDb::kTextBottom);
pAttdef->setHorizontalMode(AcDb::kTextMid);
pAttdef->setAlignmentPoint(AcGePoint3d(0, 2, 0));
pBlockRecord->appendAcDbEntity(entId, pAttdef);
pAttdef->close();
pBlockRecord->close();
// Вставка BlockReference
AcGeVector3d normal(0.0, 0.0, 1.0);
AcGePlane plane(AcGePoint3d::kOrigin, normal);
AcDbBlockReference *pBlkRef = new AcDbBlockReference;
pBlkRef->setBlockTableRecord(idBtr);
pBlkRef->setPosition(m_Pos);
pBlkRef->setRotation(m_Ang.angleOnPlane(plane));
pBlkRef->setNormal(normal);
AcGeVector3d normal(0.0, 0.0, 1.0);
AcGePlane plane(AcGePoint3d::kOrigin, normal);
AcDbBlockReference *pBlkRef = new AcDbBlockReference;
pBlkRef->setBlockTableRecord(idBtr);
pBlkRef->setPosition(m_Pos);
AcDbObjectId newEntId;
postToDb(pBlkRef, newEntId);
pBlkRef->setNormal(normal);
pBlkRef->setRotation(m_Ang.angleOnPlane(plane));
AcDbEntity *pEnt = NULL;
AcDbBlockTableRecord *pBlockDef;
acdbOpenObject(pBlockDef, idBtr, AcDb::kForRead);
AcDbBlockTableRecordIterator *pIterator;
pBlockDef->newIterator(pIterator);
for (pIterator->start(); !pIterator->done(); pIterator->step())
{
pIterator->getEntity(pEnt, AcDb::kForRead);
pAttdef = AcDbAttributeDefinition::cast(pEnt);
if (pAttdef != NULL)
{
AcDbAttribute *pAtt = new AcDbAttribute();
pAtt->setPropertiesFrom(pAttdef);
pAtt->setInvisible(pAttdef->isInvisible());
AcGePoint3d basePoint = pAttdef->position();
basePoint += pBlkRef->position().asVector();
pAtt->setPosition(basePoint);
pAtt->setHeight(pAttdef->height());
pAtt->setRotation(pBlkRef->rotation());
pAtt->setTag(_T("NUM"));
pAtt->setFieldLength(25);
const TCHAR *pStr = pAttdef->tagConst();
pAtt->setTag(pStr);
pAtt->setFieldLength(pAttdef->fieldLength());
pAtt->setTextString(m_Name);
if (pAttdef->isMTextAttributeDefinition())
{
pAtt->convertIntoMTextAttribute();
AcDbMText* pText = pAtt->getMTextAttribute();
pText->setBackgroundFill(pAttdef->getMTextAttributeDefinition()->backgroundFillOn());
double sf;
pAttdef->getMTextAttributeDefinition()->getBackgroundScaleFactor(sf);
pText->setBackgroundScaleFactor(sf);
pText->setUseBackgroundColor(pAttdef->getMTextAttributeDefinition()->useBackgroundColorOn());
pAtt->setMTextAttribute(pText);
pAtt->updateMTextAttribute();
}
AcDbObjectId attId;
pBlkRef->appendAttribute(attId, pAtt);
pAtt->close();
}
pEnt->close();
}
delete pIterator;
pBlockDef->close();
pBlkRef->close();