When we want to open a DWG file that h is already open in another session of AutoCAD, any call to AcDbDatabase::readDwgFile() fails with the error eFileAccessErr.
Basically we cannot open a DWG files in read-only state.
The problem is that readDwgFile() reads not the whole DWG file into memory. readDwgFile() opens the file and reads only some header information. Every time when we access an entity/object/symboltable… in the drawing file, AutoCAD reads another part of the DWG file. When an other instance of AutoCAD saves during your read process the drawing, it can happen that your application/AutoCAD gets an invalid file pointer.
The workaround is to copy the DWG file to a temporary location if you get eFileAccessErr on readDwgFile() and to try again to open the temporary DWG file. The following code does this.
NOTE: Don’t forget that you are working on a temporary copy of the drawing. Any changes you are doing on the drawing will be lost because you have to delete the temp file.
void loadDwgReadOnly() { // TODO: add your code here BOOL isReadOnly = FALSE; ACHAR dwgFileName[MAX_PATH]; resbuf *rb = acutNewRb(RTSTR); if (RTNORM != acedGetFileD(L"Select File to read", NULL, L"dwg", 0, rb)) { acutRelRb(rb); return; } _tcscpy(dwgFileName, rb->resval.rstring); acutRelRb(rb); Acad::ErrorStatus es; AcDbDatabase db(Adesk::kFalse); if ((Acad::eOk != (es = db.readDwgFile(dwgFileName)))) { if (Acad::eFileAccessErr == es) { // // Copy the file and try again // ACHAR tempPath[MAX_PATH], tempName[MAX_PATH]; // Get a temporary file name GetTempPath(MAX_PATH, tempPath); GetTempFileNameW(tempPath, L"dwg", 0, tempName); if (!CopyFile(dwgFileName, tempName, FALSE)) return; if ((Acad::eOk != (es = db.readDwgFile(tempName)))) return; &#
160; isReadOnly = TRUE; _tcscpy(dwgFileName, tempName); } } // Now you can query information from the readed dwg file // **** // **** // **** // Delete the temp file if (isReadOnly) { DeleteFile(dwgFileName); } }

Leave a Reply