.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem creating multiple (100+) drawings using .NET

4 REPLIES 4
Reply
Message 1 of 5
bej
Contributor
500 Views, 4 Replies

Problem creating multiple (100+) drawings using .NET

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

4 REPLIES 4
Message 2 of 5
Hallex
in reply to: bej

Please repost your question on C++ branch:

http://forums.autodesk.com/t5/Autodesk-ObjectARX/bd-p/34

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 5
bej
Contributor
in reply to: Hallex

This is not ObjectARX code, but CLI. I'm only using the .NET API's.

Message 4 of 5
bej
Contributor
in reply to: bej

Just to clarify. I'm coding in C++/CLI, but I'm using only the AutoCAD .NET API's. 

Message 5 of 5
philippe.leefsma
in reply to: bej

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.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost