C# How to create AutoCAD entities

Ok, found it…

In my method as a few posts above I dispose the transaction. If I rewrite the method like this:

public static IList<string> CreateDBPoints(Autodesk.AutoCAD.DynamoNodes.Document document, List<Point> points)
{
    IList<string> returnValue = new List<string>();

    using (dynAppServ.DocumentContext ctx = new dynAppServ.DocumentContext(document.AcDocument))
    {

        acDbServ.Transaction tr = ctx.Transaction;

        using (acDbServ.BlockTableRecord currentSpace = (acDbServ.BlockTableRecord)tr.GetObject(ctx.Database.CurrentSpaceId, acDbServ.OpenMode.ForWrite))
        {
            int pointNumber = 0;
            foreach (Point point in points)
            {
                acDbServ.DBPoint node = new acDbServ.DBPoint(new Point3d(point.X, point.Y, point.Z));
                node.SetDatabaseDefaults();
                node.Layer = "0";
                returnValue.Add((++pointNumber).ToString());
                currentSpace.AppendEntity(node);
                tr.AddNewlyCreatedDBObject(node, true);
            }
        }
        tr.Commit();
    }
    return returnValue;
}

Then everything works like a charm. No Fatal Error.

Maybe the DocumentContext.Transaction is a top transaction or so, and disposing that is killing Dynamo. I’m not sure. I can dispose the DocumentContext, the Modelspace, everything, but NOT the Transaction.

Hope it will help others who stumble over this problem :slight_smile:

3 Likes