'Stack overflow' error caused by AppendEntity and AddNewlyCreatedDBObject usage AutoCAD .NET APIs

0

I run my code in Visual Studio 2019 and AutoCAD 2016. I wrote several simple functions that worked ok. And now I tried to write a plugin that creates Block reference for each MTEXT at the same coordinates. When I call my new command I got all the expected dialog alerts from my command, but after it AutoCAD closes itself and I see the following error in Visual Studio's ouput: The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core. The program '[21236] acad.exe' has exited with code -1073741571 (0xc00000fd) 'Stack overflow'.

My command's source code:

[CommandMethod("blockTest")]
static public void blockTest()
{
    string attbName = "HEIGHT";

    // Get the current docusment and database, and start a transaction
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;
    Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        BlockTable bt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
        BlockTableRecord btr = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

        btr.UpgradeOpen();

        foreach (ObjectId objId in btr)
        {
            if (objId.ObjectClass.DxfName == "MTEXT")
            {
                MText mtext = acTrans.GetObject(objId, OpenMode.ForRead, true) as MText;
                Point3d mtextCoord = mtext.Location;

                //create br for mtext
                br br = new br(new Point3d(mtextCoord.X, mtextCoord.Y, mtextCoord.Z), btr.Id);

                //update HEIGHT attribute in block reference to be same as mtext text
                foreach (ObjectId arId in br.AttributeCollection)
                {
                    AttributeReference ar = acTrans.GetObject(arId, OpenMode.ForRead) as AttributeReference;
                    if (null == ar)
                    {
                        Application.ShowAlertDialog("AttributeReference getting failed!");
                        return;
                    }
                    if (ar.Tag.ToUpper() == attbName)
                    {
                        ar.UpgradeOpen();
                        ar.TextString = mtext.Contents.ToString();
                        ar.DowngradeOpen();
                    }
                }
                br.SetDatabaseDefaults();
                Application.ShowAlertDialog("before AppendEntity!");
                btr.AppendEntity(br);
                acTrans.AddNewlyCreatedDBObject(br, true);
                Application.ShowAlertDialog("after AppendEntity!");
            }
        }

        acTrans.Commit();
    }
}

I ran the code on sample AutoCAD file that contains only 2 MTEXT. If I comment out the lines that use AppendEntity and AddNewlyCreatedDBObject APIs AutoCAD doesn't close itself. I don't understand how can I get a stack overflow. Please help.

.net
autocad-plugin
asked on Stack Overflow May 1, 2020 by user893269

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0