Hello Forum,
I have code that opens a drawing, draws a circle, saves and closes the drawing. With some drawings the code works, and with other drawings AutoCAD crashes:
FATAL ERROR: Unhandled Access Violation Reading 0xffffffff Exception at e82ba51eh
Looking at the code, does anything have an idea what could cause the crashes?
[CommandMethod("Test", CommandFlags.Session)] public void Test() { string strPath; try { strPath = "C:\\Drawings\\MyDrawing.dwg"; Document docTarget = CADS.Application.DocumentManager.Open(strPath, false); if (CADS.Application.DocumentManager.MdiActiveDocument != docTarget) { CADS.Application.DocumentManager.MdiActiveDocument = docTarget; } using (DocumentLock dlActive = docTarget.LockDocument()) { dynamic objAcadDocument = docTarget.AcadDocument; objAcadDocument.GetType().InvokeMember("SendCommand", BindingFlags.InvokeMethod, null, objAcadDocument, new object[] { "(command \".circle\" \"0,0\" \"100\") " }); objAcadDocument = null; } docTarget.CloseAndSave(strPath); } catch (System.Exception Ex) { MessageBox.Show("Error testing: " + Ex.Message); } }
System:
AutoCAD 2012
Windows 7 64bit
Thank you.
Ty for your response.
I stepped through the code and no NULL.
Could it be that this code does not always run synchronously? It crashes at 'CloseAndSave'. Maybe it isn't done yet with the 'SendCommand'? Is there way to check this? Or make sure 'SendCommand' is done before calling 'CloseAndSave'?
You code works with AutoCAD 2014. However, on AutoCAD 2012, you have to lock the active document before opening another drawing from the active drawing. It seems later AutoCAD versions handle locking documents better.
The following code was tested to work on AutoCAD 2012:
[CommandMethod("Test", CommandFlags.Session)] public void Test() { string strPath; try { strPath = "C:\\Drawings\\MyDrawing.dwg"; Document docSource = CADS.Application.DocumentManager.MdiActiveDocument; using (DocumentLock dlSource = docSource.LockDocument()) { Document docTarget = CADS.Application.DocumentManager.Open(strPath, false); if (CADS.Application.DocumentManager.MdiActiveDocument != docTarget) { CADS.Application.DocumentManager.MdiActiveDocument = docTarget; } using (DocumentLock dlActive = docTarget.LockDocument()) { dynamic objAcadDocument = docTarget.AcadDocument; //dynamic objAcadDocument = docTarget.GetAcadDocument(); objAcadDocument.GetType().InvokeMember( "SendCommand", BindingFlags.InvokeMethod, null, objAcadDocument, new object[] { "(command \".circle\" \"0,0\" \"100\") " }); objAcadDocument = null; } docTarget.CloseAndSave(strPath); } } catch (System.Exception Ex) { MessageBox.Show("Error testing: " + Ex.Message); } }
Khoa
Can't find what you're looking for? Ask the community or share your knowledge.