CAXA2023隨機改塊色(VS2019 ObjectArx)

大力水手008發表於2024-03-29
 1 //改色
 2 void cmdChangeColorX(bool bRand=true, CAXA::UInt16 color_Index = 10)
 3 {
 4     CDraft::ErrorStatus es;
 5     CRxDbObjectId objID;
 6     CRxDbEntity* pEntity=NULL;
 7     crx_name en;
 8     crx_point pt;    
 9     //拾取要改色的圖元
10     while (crxedEntSel(_T("\n請選擇要改色的圖元:"), en, pt) == RTNORM)
11     {
12         if (bRand) color_Index = getRandColorIndex();
13         es = crxdbGetObjectId(objID, en);
14         if (es != CDraft::eOk)    return;
15 
16         //objID轉pEnt
17         es = crxdbOpenCRxDbEntity(pEntity, objID, CRxDb::kForWrite);
18         if (es != CDraft::eOk || pEntity == NULL)    return;
19 
20         if (pEntity->isKindOf(CRxDbBlockReference::desc())) //是塊
21         {
22             CRxDbBlockTableRecord* pRecord;
23             CRxDbBlockTableRecordIterator* iter;
24             CRxDbObjectId id;
25             //pEnt轉pBlkref
26             CRxDbBlockReference* pBlkRef = CRxDbBlockReference::cast(pEntity);
27 
28             //pBlkref轉blkTRid
29             CRxDbObjectId blockTableRecordId = pBlkRef->blockTableRecord();
30 
31             //blkTRid轉到pRecord
32             es = crxdbOpenObject(pRecord, blockTableRecordId, CRxDb::kForWrite);
33             if (es != CDraft::eOk || pRecord == NULL)    return;
34 
35             //遍歷要目標塊中每一圖元
36             pRecord->newIterator(iter);
37             for (iter->start(); !iter->done(); iter->step())
38             {
39                 es = iter->getEntityId(id);
40                 if (es != CDraft::eOk || !id.isValid())    continue;
41                 es = crxdbOpenCRxDbEntity(pEntity, id, CRxDb::kForWrite);
42                 if (es != CDraft::eOk)    continue;
43                 //改塊中實體顏色
44                 pEntity->setColorIndex(color_Index);
45             }
46             delete iter;
47             pRecord->close();
48         }
49         else //普通圖元
50         {
51             pEntity->setColorIndex(color_Index);
52         }        
53         crxedCommand(RTSTR, _T("rea"), RTNONE);    //重新整理,使更改立即生效。
54     }
55     pEntity->close();
56 }

相關文章