[每日一码] 删除一个图层上所有引用该层的实体,并删除图层定义
void eraseLayer(CString layerName){
AcDbDatabase *pDataBase = acdbHostApplicationServices()->workingDatabase();
// get the AcDbObjectId of the layer
AcDbLayerTable *pLayerTable;
if (pDataBase->getSymbolTable(pLayerTable, AcDb::kForRead) != Acad::eOk)
{
return;
}
if (!pLayerTable->has(layerName))
{
pLayerTable->close();
return;
}
AcDbObjectId layerId;
if (pLayerTable->getAt(layerName, layerId) != Acad::eOk)
{
pLayerTable->close();
return;
}
pLayerTable->close();
// iterate through the AcDbBlockTable
AcDbBlockTable *pBlockTable;
if (pDataBase->getSymbolTable(pBlockTable, AcDb::kForRead) != Acad::eOk)
{
return;
}
AcDbBlockTableIterator *pTableIterator;
if (pBlockTable->newIterator(pTableIterator) != Acad::eOk)
{
pBlockTable->close();
return;
}
pBlockTable->close();
while (!pTableIterator->done())
{
AcDbBlockTableRecord *pRecord;
if (pTableIterator->getRecord(pRecord, AcDb::kForRead) == Acad::eOk)
{
// iterate through the AcDbBlockTableRecord
AcDbBlockTableRecordIterator *pRecordIterator;
if (pRecord->newIterator(pRecordIterator) == Acad::eOk)
{
while (!pRecordIterator->done())
{
AcDbEntity *pEntity;
if (pRecordIterator->getEntity(pEntity, AcDb::kForRead) == Acad::eOk)
{
if (pEntity->layerId() == layerId)
{
if (pEntity->upgradeOpen() == Acad::eOk)
{
pEntity->erase();
}
}
pEntity->close();
}
pRecordIterator->step();
}
delete pRecordIterator;
}
pRecord->close();
}
pTableIterator->step();
}
delete pTableIterator;
// check if there is any other AcDbHardPointerId on the layer
// if not, erase the layer
AcDbObjectIdArray purgableIds;
purgableIds.append(layerId);
if (pDataBase->purge(purgableIds) == Acad::eOk)
{
if (purgableIds.length() > 0)
{
AcDbLayerTableRecord *pLayer;
if (acdbOpenObject(pLayer, layerId, AcDb::kForWrite) == Acad::eOk)
{
pLayer->erase();
pLayer->close();
}
}
}
}
页:
[1]