Hi,
I have an application that creates multiple new DWG files, fill them with some geometry, and closes the files again, before repeating.
The problem I've encountered is that AutoCAD will crash somewhat randomly with a Fatal Error if I repeat the NEW FILE/CLOSE FILE process several hundred times. The application works fine as long as not too many files are created.
Below is the source code of a test example I've created that will produce this problem. In this test example I simply create and close DWG files without adding any data to them:
dwgHandler.h
------
ref class dwgHandler
{
private:
DocumentCollection ^ docMgr;
Document ^ doc;
Database ^ db;
Editor ^ ed;
Transaction ^tr;
BlockTable ^bt;
BlockTableRecord ^ms;
RegAppTable ^rat;
LayerTable ^lt;
public:
dwgHandler(void);
~dwgHandler(void);
void newDrawing(String ^templateFile);
void closeAndSaveDrawing(String ^dwgfile);
void beginTransaction();
void endTransaction();
};
dwgHandler.cpp
-----
dwgHandler::dwgHandler(void)
{
}
dwgHandler::~dwgHandler(void)
{
}
void dwgHandler::newDrawing(String ^templateFile)
{
try
{
// new file
docMgr = Autodesk::AutoCAD::ApplicationServices::Core::Application::DocumentManager;
doc = DocumentCollectionExtension::Add(docMgr, templateFile);
docMgr->MdiActiveDocument = doc;
doc->LockDocument();
// begin transaction
beginTransaction();
}
catch (System::Exception ^ ex)
{
MessageBox::Show("Unable to open new drawing: " + ex);
return;
}
}
void dwgHandler::beginTransaction()
{
try
{
db = doc->Database;
ed = doc->Editor;
tr = db->TransactionManager->StartTransaction();
bt =(BlockTable^)tr->GetObject(db->BlockTableId, OpenMode::ForRead);
ms =(BlockTableRecord^)tr->GetObject(bt[BlockTableRecord::ModelSpace], OpenMode::ForWrite);
rat = (RegAppTable^)tr->GetObject(db->RegAppTableId, OpenMode::ForWrite, false);
lt = (LayerTable^)tr->GetObject(db->LayerTableId,OpenMode::ForWrite);
}
catch (System::Exception ^ ex)
{
MessageBox::Show("Unable to begin transaction: " + ex);
return;
}
}
void dwgHandler::closeAndSaveDrawing(String ^dwgfile)
{
try
{
// endTransaction
endTransaction();
// Save and close the active drawing
doc->Database->SaveAs(dwgfile, true, DwgVersion::Current,doc->Database->SecurityParameters);
DocumentExtension::CloseAndDiscard(doc);
}
}
catch (System::Exception ^ ex)
{
MessageBox::Show("Unable to save drawing. Please save manually.", "ImportPDMS", MessageBoxButtons::OK,
}
}
void dwgHandler::endTransaction()
{
try
{
tr->Commit();
}
catch (System::Exception ^ ex)
{
MessageBox::Show("failed to end transaction: " + ex);
}
}
testHandler.cpp
-----
void testHandler()
{
String ^outputFile;
for(int i=1; i<500; i++)
{
// new dwghandler
dwgHandler ^dwgh=gcnew dwgHandler();
// new drawing
dwgh->newDrawing("acadiso.dwt");
// save drawing
outputFile="c:\\test\\testfile" + i.ToString() + ".dwg";
dwgh->closeAndSaveDrawing(outputFile);
// delete dwghandler
delete dwgh;
}
}
When running testHandler() AutoCAD will crash with a Fatal Error randomly, but usually not before 300+ files have been created.
Is there anything I'm doing wrong in this example?
Regards,
Bjørn Egil Jenssen
Please repost your question on C++ branch:
http://forums.autodesk.com/t5/Autodesk-ObjectARX/bd-p/34
One suggestion would be to create an exe application that will act as a loader, restarting AutoCAD after a series of drawings to prevent it from crashing.
I hope it helps.
Regards,
Philippe.