.NET

.NET

Reply
Contributor bej
Contributor
16 Posts
0 Kudos
Registered: ‎11-21-2012
Post 1 of 5

Problem creating multiple (100+) drawings using .NET

313 Views, 4 Replies
08-14-2013 02:47 AM

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::smileyvery-happy:ocumentManager;
    doc = DocumentCollectionExtension::Add(docMgr, templateFile);
    docMgr->MdiActiveDocument = doc;
    doc->LockDocument();

 

    // begin transaction
    beginTransaction();
  }
  catch (System::Exception ^ ex)
  {
    MessageBox::smileyfrustrated:how("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::smileyfrustrated:how("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::smileyfrustrated:how("Unable to save drawing. Please save manually.", "ImportPDMS", MessageBoxButtons::smileysurprised:K, 
}

}

 

void dwgHandler::endTransaction()
{
try
{

  tr->Commit();

}

catch (System::Exception ^ ex)
{
  MessageBox::smileyfrustrated:how("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

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 5

Re: Problem creating multiple (100+) drawings using .NET

08-14-2013 03:52 AM in reply to: bej

Please repost your question on C++ branch:

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

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor bej
Contributor
16 Posts
0 Kudos
Registered: ‎11-21-2012
Post 3 of 5

Re: Problem creating multiple (100+) drawings using .NET

08-14-2013 03:55 AM in reply to: Hallex

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

Contributor bej
Contributor
16 Posts
0 Kudos
Registered: ‎11-21-2012
Post 4 of 5

Re: Problem creating multiple (100+) drawings using .NET

08-14-2013 03:57 AM in reply to: bej

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

ADN Support Specialist
687 Posts
80 Kudos
Registered: ‎06-02-2009
Post 5 of 5

Re: Problem creating multiple (100+) drawings using .NET

08-21-2013 08:37 AM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!