转
由于要将DWG文件中N多的文字进行翻译, 想到了先将所有的文字全部导出到一文本文件中(已实现,代码如下)
//导出DWG文件中的文字串
oid CTransTextDlg::OnBtExportText()
{
// TOD Add your control notification handler code here
AcDbDatabase* pDb;
AcDbBlockTable *pBlockTable;
pDb = acdbHostApplicationSerices()->workingDatabase();
pDb->getSymbolTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForRead);
pBlockTable->close();
AcDbBlockTableRecordIterator *pBlockIterator;
pBlockTableRecord->newIterator(pBlockIterator);
//打开文件用于输出
CString szFilter=_T(“Data Files (*.dat;*.txt)|*.dat; *.txt|All Files (*.*)|*.*||”);
CString szDefExt(_T(“.DAT”));
CAcUiFileDialog dlg(FALSE, szDefExt, NULL,
OFN_HIDEREADONLY | OFN_OERWRITEPROMPT, szFilter);
dlg.DoModal();
CString strFileName;
strFileName = dlg.GetFileName();
ofstream out(strFileName);
if(!out)
{
MessageBox(“指定导出文件无法打开!”);
return;
}
//设置计数器
LONG lCounts = 0;
for (; !pBlockIterator->done(); pBlockIterator->step())
{
AcDbEntity *pEntity;
pBlockIterator->getEntity(pEntity, AcDb::kForRead);
const char *pCname = pEntity->isA()->name();
if(!strcmp(pCname, “AcDbText”)) //是单行文字类
{
//获取文字内容
lCounts++;
const char *pText = ((AcDbText*)pEntity)->textString();
out<objectId()<<",t"<<pText<<endl;
}
else if(!strcmp(pCname, “AcDbMText”)) //是多行文字类
{
lCounts++;
const char *pMText = ((AcDbMText*)pEntity)->contents();
out<objectId()<<",t"<<pMText<<endl;
if(pMText)
pMText = NULL;
}
pEntity->close();
}
out.close();
delete pBlockIterator;
pBlockTableRecord->close();
}
翻译后欲重新导入原DWG文件中, 如下:
oid CTransTextDlg::OnBtSubsTexts()
{
// TOD Add your control notification handler code here
//打开文件用于输出
AcDbDatabase* pDb;
AcDbBlockTable *pBlockTable;
pDb = acdbHostApplicationSerices()->workingDatabase();
pDb->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForRead);
pBlockTable->close();
AcDbBlockTableRecordIterator *pBlockIterator;
pBlockTableRecord->newIterator(pBlockIterator);
CString szFilter=_T(“Data Files (*.dat;*.txt)|*.dat; *.txt|All Files (*.*)|*.*||”);
CString szDefExt(_T(“.DAT”));
CAcUiFileDialog dlg(TRUE, szDefExt, NULL,
OFN_HIDEREADONLY | OFN_OERWRITEPROMPT, szFilter);
dlg.DoModal();
CString strFileName;
strFileName = dlg.GetFileName();
ifstream in(strFileName);
if(!in)
{
MessageBox(“指定导入文件无法打开!”);
return;
}
//设置计数器
LONG lCounts = 0;
LONG lID;
for (; !pBlockIterator->done(); pBlockIterator->step())
{
AcDbEntity *pEntity;
Acad::ErrorStatus es;
es = pBlockIterator->getEntity(pEntity, AcDb::kForRead);
switch(es)
{
case Acad::eOk:
acutPrintf(“Acad::eOkn”);
break;
case Acad::ePermanentlyErased:
acutPrintf(“Acad::ePermanentlyErasedn”);
break;
case Acad::eAtMaxReaders:
acutPrintf(“Acad::eAtMaxReadersn”);
break;
case Acad::eWasOpenForNotify:
acutPrintf(“Acad::eWasOpenForNotifyn”);
break;
case Acad::eWasNotifying:
acutPrintf(“Acad::eWasNotifyingn”);
break;
case Acad::eWasOpenForUnd
acutPrintf(“Acad::eWasOpenForUndon”);
break;
case Acad::eWasOpenForWrite:
acutPrintf(“Acad::eWasOpenForWriten”);
break;
case Acad::eWasOpenForRead:
acutPrintf(“Acad::eWasOpenForReadn”);
break;
default:
acutPrintf(“Unknown error type.n”);
break;
}
const char *pCname = pEntity->isA()->name();
if(!strcmp(pCname, “AcDbText”)) //是单行文字类
{
//获取文字内容
acutPrintf(“Step — %ldn”, 4+lCounts);
lCounts++;
char inStr[256];
in>>lID>>inStr;
((AcDbText*)pEntity)->setTextString(inStr);
}
else if(!strcmp(pCname, “AcDbMText”)) //是多行文字类
{
lCounts++;
char inStr[1024];
in>>lID>>inStr;
((AcDbMText*)pEntity)->setContents(inStr);
}
pEntity->close();
}
in.close();
delete pBlockIterator;
pBlockTableRecord->close();
}