BricsCAD crashes when undoing a drawn table

Hello everybody,
I implemented a .NET-method that draws a legend into a BricsCAD drawing using a Table().
Every block of the drawing gets added as an own table-row.
In future there will be 2 more columns with the blockname and the amount of references.

Using the code below, the table gets drawn correctly!
But: when I press CTRL-Z (undo -> remove the table) BricsCAD crashes without any debug-info.
Bricscad also crashes when i click into several cells of the table.
Anybody got a solution?

public static void DrawLegend()
{
var database = _AcAp.Application.DocumentManager.MdiActiveDocument.Database;

        // Create the table, set its style and default row/column size
        var tb = new Table();
        tb.TableStyle = database.Tablestyle;
        tb.SetRowHeight(3.0);
        tb.SetColumnWidth(70.0);
        tb.Position = new Point3d(0, 0, 0);

        using (var tr = CADHandler.CADDocument.TransactionManager.StartTransaction())
        {
            var bt = (BlockTable)tr.GetObject(database.BlockTableId, OpenMode.ForRead);
            if (bt == null) return;

            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
            foreach (ObjectId blockId in btr)
            {
                DBObject blkRefdbObj = (DBObject)tr.GetObject(blockId, OpenMode.ForRead);
                BlockReference blkRef = blkRefdbObj as BlockReference;
                if (blkRef != null)
                {
                    ObjectId btrBlockId = blkRef.BlockTableRecord;
                    if (btrBlockId != null)
                    {
                        BlockTableRecord btrBlock = (BlockTableRecord)tr.GetObject(btrBlockId, OpenMode.ForRead);

                        // Add a row
                        tb.InsertRows(tb.Rows.Count, rowHeight, 1);
                        var rowIdx = tb.Rows.Count - 1;

                        // column 1: thumbnail of the block
                        var second = tb.Cells[rowIdx, 0];
                        second.Contents.InsertAt(0);
                        second.Contents[0].BlockTableRecordId = btrBlockId;                            
                    }
                }
            }
        }

        using (var tr = CADHandler.CADDocument.TransactionManager.StartTransaction())
        {
            // Now we add the table to the current space
            var sp = (BlockTableRecord)tr.GetObject(database.CurrentSpaceId, OpenMode.ForWrite);
            sp.AppendEntity(tb);

            // And to the transaction, which we then commit
            tr.AddNewlyCreatedDBObject(tb, true);
            tr.Commit();
        }
    }

Comments

  • It's always a good idea to call SetDatabaseDefaults() on newly created NDBR entities before calling complex member functions.

  • Thank you, but calling SetDatabaseDefaults() did not bring a solution...

  • Did you try committing the first transaction?