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

Crash - Open, edit and save a drawing.

4 REPLIES 4
Reply
Message 1 of 5
Ertqwa
398 Views, 4 Replies

Crash - Open, edit and save a drawing.

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.

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Ertqwa

"Unhandled Access Violation Reading 0xffffffff " tells me you are accessing a null pointer. Did you debug and look at the values of all pointers?
Message 3 of 5
Ertqwa
in reply to: Anonymous

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'?

Message 4 of 5
Anonymous
in reply to: Ertqwa

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

Message 5 of 5
Ertqwa
in reply to: Anonymous

Ah ok. I'll try that.

 

Thank you.

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report