CAD通用遍历及操作(c++)
对于符号表记录,在各种类型中对应不同的内容,在实例中将体现出来,在此实例中,我们将使用一个模板函数进行对于各个符号表进行读取数据并将他们的句柄挂载至树形结构图,该函数实现如下:
template<
typename IteratorType,
typename Value,
typename Container
>
void CMxDatabaseDlg::CommonInitFunc(
Container * pContainer,
std::function<void(Value*, HTREEITEM)> func,
HTREEITEM pParentNode)
{
IteratorType * pIterator = nullptr;
McDbObjectPointer<Value> spValue;
McDbObjectId mValueId;
HTREEITEM pNode = nullptr;
HTREEITEM pNextNode = nullptr;
CString sFormatTemp;
//将当前的表挂载至控件
if (pParentNode)
pNode = m_DatabaseInfo.InsertItem(pContainer->isA()->name(), pParentNode);
else
pNode = m_DatabaseInfo.InsertItem(pContainer->isA()->name());
//创建一个遍历器,准备遍历块表
pContainer->newIterator(pIterator);
std::auto_ptr<IteratorType> spIterator(pIterator);//该迭代器的生命周期绑定到智能指针
if (pIterator)
{
//遍历所有记录
for (; !pIterator->done(); pIterator->step())
{
//获取ID
pIterator->getRecordId(mValueId);
//打开表
spValue.open(mValueId, McDb::kForRead);
//获取符号表名
CString sTableName;
{
//...
spValue->getNameEx(sTableName);
//2方式
sTableName = MrxDbgUtils::getSymbolTableRecordName(mValueId);
}
//获取句柄
TCHAR szHandle[256] = {};
{
McDbHandle hTableRechandle;
spValue->getAcDbHandle(hTableRechandle);
hTableRechandle.getIntoAsciiBuffer(szHandle);
}
//格式化字符串
sFormatTemp.Format(FORMAT, szHandle, sTableName);
//将层表挂载到当前数据库下
pNextNode = m_DatabaseInfo.InsertItem(sFormatTemp, pNode);
//上述代码是将符号记录表通用信息加载至控件,使用以下函数附加操作
func(spValue.object(), pNextNode);
}
}
}
template<>
inline void CMxDatabaseDlg::CommonInitFunc<
McDbBlockTableRecordIterator,
McDbEntity,
McDbBlockTableRecord
>(
McDbBlockTableRecord * pTable,
std::function<void(McDbEntity*, HTREEITEM)> func,
HTREEITEM pParentNode)
{
McDbBlockTableRecordIterator * pIterator = nullptr;
McDbObjectPointer<McDbEntity> spValue;
McDbObjectId mValueId;
CString sFormatTemp;
//创建一个遍历器,准备遍历块表
pTable->newIterator(pIterator);
std::auto_ptr<McDbBlockTableRecordIterator> spIterator(pIterator);//该迭代器的生命周期绑定到智能指针
if (pIterator)
{
//遍历所有记录
for (; !pIterator->done(); pIterator->step())
{
//获取ID
pIterator->getEntityId(mValueId);
//打开表
spValue.open(mValueId, McDb::kForRead);
//获取句柄
TCHAR szHandle[256] = {};
{
McDbHandle hTableRechandle;
spValue->getAcDbHandle(hTableRechandle);
hTableRechandle.getIntoAsciiBuffer(szHandle);
}
//格式化字符串
sFormatTemp.Format(FORMAT, szHandle, spValue->isA()->name());
//将层表挂载到当前数据库下
m_DatabaseInfo.InsertItem(sFormatTemp, pParentNode);
}
}
}
浙公网安备 33010602011771号