创建普通块和无名块
static Acad::ErrorStatus CreateAnonBlock(AcArray<AcDbObjectId> &selectedIds, AcDbObjectId &idAnonBlock){
Acad::ErrorStatus es;
AcDbBlockTable *pBlockTable = NULL;
AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecord *pAnonBlock = NULL;
if (idAnonBlock == AcDbObjectId::kNull)
{
if ((es = pDb->getBlockTable(pBlockTable, AcDb::kForWrite))
!= Acad::eOk)
{
acutPrintf(L"Could not open block table 无法打开块表.\n");
return es;
}
AcDbBlockTableRecord *pAnonBlock = new AcDbBlockTableRecord;
if (!pAnonBlock)
{
acutPrintf(L"Could not create new block table record.无法创建新的块表记录\n");
return Acad::eNullObjectPointer;
}
if ((es = pAnonBlock->setName(采用T("*U"))) != Acad::eOk)
{
acutPrintf(L"Could not name anonymous block.无法命名匿名块\n");
return es;
}
if ((es = pBlockTable->add(idAnonBlock, pAnonBlock)) != Acad::eOk)
{
acutPrintf(L"Could not add anonymous block to block table.无法将匿名块添加到块表\n");
return es;
}
if ((es = pBlockTable->close()) != Acad::eOk)
{
acutPrintf(L"Could not close block table.\n");
return es;
}
if ((es = pAnonBlock->close()) != Acad::eOk)
{
acutPrintf(L"Could not close block table record.\n");
return es;
}
}
AcDbObjectPointer<AcDbBlockTableRecord> pAnonBlockObjectPtr(idAnonBlock, AcDb::kForWrite);
if ((es = pAnonBlockObjectPtr.openStatus()) != Acad::eOk)
{
acutPrintf(L"Could not open anonymous block.\n");
return es;
}
for (int cnt = 0; cnt < selectedIds.length(); cnt++)
{
AcDbObjectId entId = selectedIds.at(cnt);
AcDbObjectPointer<AcDbEntity> pEntity(entId, AcDb::kForWrite);
if (Acad::eOk == pEntity.openStatus())
{
AcDbEntity *pCloneEnt = (AcDbEntity *)pEntity->clone();
if ((es = pAnonBlockObjectPtr->appendAcDbEntity(pCloneEnt)) != Acad::eOk)
{
acutPrintf(L"Could not append entity to anonymous block.\n");
return es;
}
if ((es = pCloneEnt->close()) != Acad::eOk)
{
acutPrintf(L"Could not close entity.\n");
return es;
}
pEntity->erase();
pEntity->close();
}
}
return Acad::eOk;
}
static AcDbObjectId CreateBlock1(const CString &blkName, const AcDbObjectIdArray &idObjectArray, const AcGeScale3d &m采用aScale, const double &m采用angle)
{
Acad::ErrorStatus es;
AcDbBlockTable *pBlkTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForWrite);
AcDbBlockTableRecord *pBlkRcd = new AcDbBlockTableRecord();
es = pBlkRcd->setName(blkName);
AcDbObjectId blkDefId;
pBlkTbl->add(blkDefId, pBlkRcd);
pBlkTbl->close();
AcDbObjectId entId;
for (INT32 i = 0; i < idObjectArray.length(); i++)
{
AcDbEntity *pEntity = NULL;
acdbOpenAcDbEntity(pEntity, idObjectArray.at(i), AcDb::kForWrite);
//TB: You can't append an entity to another block that is already in the modelspace.
//Instead clone it and append the clone:
AcDbEntity *pNewEntity = AcDbEntity::cast(pEntity->clone());
es = pBlkRcd->appendAcDbEntity(entId, pNewEntity);
if (es)
delete pNewEntity;
else
pNewEntity->close();
pEntity->erase();
pEntity->close();
}
pBlkRcd->close();
AcDbBlockReference *pBlkRef = new AcDbBlockReference(AcGePoint3d::kOrigin, blkDefId); //TB was entId
pBlkRef->setScaleFactors(m采用aScale);
//TB: I assume that PostToModelSpace(ent) closes the entity. So you must...
pBlkRef->setRotation(m采用angle); // ...do this 采用before采用 PostToModelSpace().
PostToModelSpace(pBlkRef);
return entId;
}
页:
[1]