如何修改嵌套块内的标注文字的颜色
求助:需要将某图层下的图块及图块内的实体改为随层的颜色,但是标注不行,即使改色了,也需要去块内编辑进去才能变色,有什么处理方法?
void GrayEntity::Gray(AcDbDatabase* pDB)
{
if (nullptr == pDB)
return;
//color by layer
AcCmColor byLayer;
byLayer.setColorIndex(256);
AcCmColor grayColor;
grayColor.setColorIndex(8);
//color by block
AcCmColor byBlock;
byBlock.setColorIndex(0);
const TCHAR* layerName = _T("T-建筑-底图");
//解锁该图层
AcDbLayerTableRecordPointer pLayerTableRecord(layerName, pDB, AcDb::kForWrite);
if (pLayerTableRecord.openStatus() != Acad::eOk)
return;
bool islocked = pLayerTableRecord->isLocked();
if (islocked)
{
pLayerTableRecord->setIsLocked(false);
pLayerTableRecord->setIsOff(pLayerTableRecord->isOff());
}
//设置所有标注样式
if (true)
{
AcDbDimStyleTablePointer pDimTable(pDB, AcDb::kForWrite);
if (Acad::eOk != pDimTable.openStatus())
return;
AcDbDimStyleTableIterator* pIterator = NULL;
pDimTable->newIterator(pIterator);
for (; !pIterator->done(); pIterator->step())
{
AcDbDimStyleTableRecord* pRecord = NULL;
if (Acad::eOk != pIterator->getRecord(pRecord, AcDb::kForWrite))
continue;
//尺寸线颜色
pRecord->setDimclrd(byLayer);
//尺寸界线颜色
pRecord->setDimclre(byLayer);
//尺寸文字颜色
pRecord->setDimclrt(byLayer);
pRecord->close();
}
delete pIterator;
}
std::set<AcDbObjectId> idSet;
if (nullptr != pDB)
{
AcDbBlockTableRecordPointer pRE(ACDB_MODEL_SPACE, pDB, AcDb::kForWrite);
if (pRE.openStatus() != Acad::eOk)
return;
AcDbBlockTableRecordIterator* iter = nullptr;
Acad::ErrorStatus es = pRE->newIterator(iter);
for (iter->start(); !iter->done(); iter->step())
{
AcDbEntity* pEntity = nullptr;
es = iter->getEntity(pEntity, AcDb::kForWrite);
if (es != Acad::eOk)
continue;
if (pEntity->layerId() == pLayerTableRecord->objectId() && pEntity->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference* pRef = AcDbBlockReference::cast(pEntity);
if (nullptr == pRef)
continue;
pRef->setColor(byLayer);
AcDbObjectId BRID = pRef->blockTableRecord();
idSet.insert(BRID);
}
pEntity->recordGraphicsModified();
pEntity->close();
}
delete iter;
iter = nullptr;
}
if (idSet.empty())
{
return;
}
//查找嵌套块
std::set<AcDbObjectId> allIDSet(idSet);
std::set<AcDbObjectId> nextIDSet;
std::set<AcDbObjectId> dimIDSet;
while (!idSet.empty())
{
for (auto ii = idSet.begin(); ii != idSet.end(); ++ii)
{
AcDbObjectId id = *ii;
AcDbBlockTableRecordPointer pRecord(id, AcDb::kForWrite);
if (Acad::eOk != pRecord.openStatus())
continue;
//获取块内实体
AcDbBlockTableRecordIterator* iiter = nullptr;
Acad::ErrorStatus es = pRecord->newIterator(iiter);
if (es != Acad::eOk)
continue;
for (iiter->start(); !iiter->done(); iiter->step())
{
AcDbEntity* pEntity = nullptr;
es = iiter->getEntity(pEntity, AcDb::kForWrite);
if (es != Acad::eOk)
continue;
//如果是嵌套块
if (pEntity->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference* pRef = AcDbBlockReference::cast(pEntity);
AcDbObjectId BRID = pRef->blockTableRecord();
if (allIDSet.count(BRID) == 0)
{
nextIDSet.insert(pRef->blockTableRecord());
allIDSet.insert(pRef->blockTableRecord());
}
//处理属性块
AcDbObjectIterator* pIter = pRef->attributeIterator();
if (pIter != nullptr)
{
for (pIter->start(); !pIter->done(); pIter->step())
{
AcDbObjectId attID = pIter->objectId();
AcDbObjectPointer<AcDbAttribute> pAttr(attID, AcDb::kForWrite);
if (pAttr.openStatus() == Acad::eOk)
pAttr->setColor(byLayer);
}
delete pIter;
pIter = nullptr;
}
}
else if (pEntity->isKindOf(AcDbDimension::desc()))
{
AcDbDimension* pDIM = AcDbDimension::cast(pEntity);
if (nullptr != pDIM)
{
pDIM->setDimclrt(byLayer);
dimIDSet.insert(pDIM->objectId());
}
}
es = pEntity->setColor(byLayer);
pEntity->recordGraphicsModified();
pEntity->close();
}
delete iiter;
}
idSet = nextIDSet;
nextIDSet.clear();
}
for (auto iter = dimIDSet.begin(); iter != dimIDSet.end(); ++iter)
{
AcDbObjectPointer<AcDbDimension> pDIM(*iter, AcDb::kForWrite);
if (pDIM.openStatus() == Acad::eOk)
{
pDIM->setDimclrt(byLayer);
pDIM->setColor(byLayer);
pDIM->recordGraphicsModified();
}
}
if (islocked)
{
pLayerTableRecord->setIsLocked(islocked);
pLayerTableRecord->setIsOff(pLayerTableRecord->isOff());
}
pLayerTableRecord.close();
} 标注颜色应该是标注样式决定的,
而且改了样式之后要刷新一下,刷新函数如下:
https://www.cnblogs.com/JJBox/p/11354224.html
但是我记得没错的话,其实我也遍历过标注内部进行改颜色... 你有种再说一遍 发表于 2024-11-18 20:31
标注颜色应该是标注样式决定的,
而且改了样式之后要刷新一下,刷新函数如下:
https://www.cnblogs.com/JJB ...
pDIM->recomputeDimBlock();
这个函数起作用了,谢大佬 思路,用vla方式历遍块内所有对象x,根据条件修改块内x,如果有块中块,则再调用函数自身处理,也就是递归的方式。
页:
[1]