|
static CString generateTempFilePath()
{
TCHAR tempPath[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
GetTempPath(MAX_PATH, lpTempPathBuffer);
GetTempFileName(lpTempPathBuffer, nullptr, 0, tempPath);
return CString{ tempPath };
}
static bool writeBufferToFile(const CLongBinary& buf, const CString& path)
{
bool flag = false;
LPBYTE pLock = static_cast<LPBYTE>(::GlobalLock(buf.m_hData));
if (pLock != nullptr)
{
CFile fileS;
CFileException e;
if (fileS.Open(path, CFile::modeWrite | CFile::modeCreate, &e))
{
fileS.Write(pLock, buf.m_dwDataLength);
fileS.Flush();
fileS.Close();
flag = true;
}
GlobalUnlock(buf.m_hData);
}
return flag;
}
static bool downloadFile(const ODBCDwgFileInfo& info)
{
try
{
CString statment;
statment.Format(_T("SELECT BlockFile FROM Blocks WHERE Name = %ls"), info.name);
CDatabase db;
if (db.Open(info.dsn))
{
CRecordset rs(&db);
if (rs.Open(CRecordset::forwardOnly, statment, CRecordset::readOnly | CRecordset::executeDirect) == TRUE)
{
while (!rs.IsEOF())
{
const auto numFields = rs.GetODBCFieldCount();
for (auto item = 0; item < numFields; item++)
{
CODBCFieldInfo fieldInfo;
rs.GetODBCFieldInfo(item, fieldInfo);
auto oldType = fieldInfo.m_nSQLType;
switch (fieldInfo.m_nSQLType)
{
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
rs.m_rgODBCFieldInfos[item].m_nSQLType = SQL_LONGVARBINARY;//<< hackola!
break;
default:
break;
}
CDBVariant var;
rs.GetFieldValue(item, var);
if (var.m_dwType == DBVT_BINARY && var.m_pbinary != nullptr)
{
writeBufferToFile(*var.m_pbinary, info.path);
}
}
rs.MoveNext();
}
rs.Close();
}
}
}
catch (...)
{
acutPrintf(_T("\nDOH Your coding sucks!"));
}
return std::filesystem::exists((const TCHAR*)info.path);
}
static void ArxTest_doit2(void)
{
ODBCDwgFileInfo info1;
info1.dsn = _T("ODBC_FIELD");
info1.name = _T("6036");
info1.path = generateTempFilePath();
ODBCDwgFileInfo info2;
info2.dsn = _T("ODBC_FIELD");
info2.name = _T("6236");
info2.path = generateTempFilePath();
if (!downloadFile(info1))
return;
if (!downloadFile(info2))
return;
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
{
Acad::ErrorStatus es = eOk;
std::unique_ptr<AcDbDatabase> dwg1(new AcDbDatabase(false, true));
if (es = dwg1->readDwgFile(info1.path); es != eOk)
return;
std::unique_ptr<AcDbDatabase> dwg2(new AcDbDatabase(false, true));
if (es = dwg2->readDwgFile(info2.path); es != eOk)
return;
AcGePoint3d pt;
AcDbObjectIdArray ids;
AcDbBlockTableRecordPointer pDwg2Mspace(acdbSymUtil()->blockModelSpaceId(dwg2.get()));
if (pDwg2Mspace.openStatus() != eOk)
return;
for (auto [es, pIter] = makeBlockTableRecordIterator(*pDwg2Mspace); !pIter->done(); pIter->step())
{
AcDbObjectId id;
es = pIter->getEntityId(id);
ids.append(id);
}
if (es = dwg2->wblock(dwg1.get(), ids, pt, AcDb::kDrcReplace); es != eOk)
return;
AcDbObjectId id;
es = pDb->insert(id, info1.name, dwg1.get(), false);
}
DeleteFile(info1.path);
DeleteFile(info2.path);
}
|
|