|
static void test()
{
// Create selection filter (all AcDbText entities in drawing)
auto* pFilter = acutBuildList(
RTDXF0, _T("TEXT"), 0);
if (pFilter == nullptr)
return;
ads_name sset;
auto retVal = acedSSGet(ACRX_T("_A"), nullptr, nullptr, pFilter, sset);
if (acutRelRb(pFilter) != RTNORM)
return;
pFilter = nullptr;
if (retVal != RTNORM)
return;
int count;
if (retVal != RTNORM || acedSSLength(sset, &count) != RTNORM)
{
acedSSFree(sset);
return;
}
// Get AcDbObjectId's from ads_name
AcDbObjectIdArray objIds;
objIds.setLogicalLength(count);
for (auto i = 0; i < count; i++)
{
ads_name ent;
retVal = acedSSName(sset, i, ent);
if (retVal != RTNORM)
return;
AcDbObjectId objId;
const auto es = acdbGetObjectId(objId, ent);
if (es != Acad::eOk)
return;
objIds[i] = objId;
}
// Put all text values in "allTexts" collection
// and create "uniqueTxts" collection of unique text values
std::vector<AcString> allTexts;
allTexts.reserve(count);
std::set<AcString> uniqueTxts;
for (const auto& objId : objIds)
{
AcDbText* pTxt = nullptr;
auto es = acdbOpenObject(pTxt, objId, AcDb::kForRead);
if (es != Acad::eOk)
return;
AcString curTxt;
es = pTxt->textString(curTxt);
if (es != Acad::eOk)
{
pTxt->close();
return;
}
allTexts.emplace_back(curTxt);
uniqueTxts.insert(curTxt);
pTxt->close();
}
// Create AcDbTable
auto* pTable = new AcDbTable();
// Get the table style
AcDbDictionary* pDict = nullptr;
AcDbObjectId styleId;
auto* pDb = acdbHostApplicationServices()->workingDatabase();
pDb->getTableStyleDictionary(pDict, AcDb::kForRead);
auto es = pDict->getAt(_T("Standard"), styleId);
pDict->close();
if (es != Acad::eOk)
return;
// Set the table style Id
pTable->setTableStyle(styleId);
//20 row, 3 columns
pTable->setSize(50, 3);
//Flow Direction From Bottom To Top
pTable->setFlowDirection(AcDb::kTtoB);
// Generate the table layout based on the table style
pTable->generateLayout();
pTable->setPosition(AcGePoint3d::kOrigin);
// Iterate uniqueTxts collection, and for each value, search allTexts collection
// and find matches, and finaly print result to command prompt.
auto counter = 1;
for (const auto& text : uniqueTxts)
{
std::vector<AcString> matches;
std::copy_if(
allTexts.begin(), allTexts.end(), std::back_inserter(matches), [&](const AcString v)
{
return v == text;
});
AcString ctr;
ctr.format(_T("%d"), counter);
AcString nr;
nr.format(_T("%d"), static_cast<int>(matches.size()));
pTable->setTextString(counter, 0, ctr.constPtr());
pTable->setTextString(counter, 1, text.constPtr());
pTable->setTextString(counter, 2, nr.constPtr());
counter++;
}
// Show AcDbTable
const auto modelId = acdbSymUtil()->blockModelSpaceId(pDb);
AcDbBlockTableRecord* pBlockTableRecord;
es = acdbOpenAcDbObject(
reinterpret_cast<AcDbObject*&>(pBlockTableRecord),
modelId, AcDb::kForWrite);
if (es == Acad::eOk)
{
// Add to Db
AcDbObjectId objectId;
pBlockTableRecord->appendAcDbEntity(objectId, pTable);
pBlockTableRecord->close();
pTable->close();
}
else
{
delete pTable;
}
} |
|