Author Topic: How to write the OpenCloseTransaction class for AutoCAD 2008?  (Read 1441 times)

0 Members and 1 Guest are viewing this topic.

csharpbird

  • Newt
  • Posts: 64
How to write the OpenCloseTransaction class for AutoCAD 2008?
« on: November 19, 2011, 08:31:12 AM »
    I write an OpenCloseTransaction class for AutoCAD 2008. It works well in most situations.I can create a table with it,but any modification such as moving the table or trying to save the file i get an error eisalready in database.
   The OpenCloseTransaction class's definition is in the attachment.
   And the Test code for creating a table:
Code: [Select]
        [CommandMethod("Test")]
        public static void Test()
        {
            Document doc=Application.DocumentManager.MdiActiveDocument;
            Database db=doc.Database;
            Editor ed=doc.Editor;
            using (OpenCloseTrans trans=db.StartOpenCloseTrans ())
            {               
                BlockTable bt=(BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                List<BlockTableRecord> btrs=new List<BlockTableRecord>();
                foreach (var id in bt)
                {
                    BlockTableRecord btr=(BlockTableRecord)trans.GetObject(id, OpenMode.ForRead);
                    if (btr.HasPreviewIcon)
                    {
                        btrs.Add(btr);
                    }
                }
                Table tb=new Table();
                tb.Position = Point3d.Origin;
                tb.NumColumns = 2;
                tb.NumRows = btrs.Count + 2;
                tb.SetRowHeight(3);
                tb.SetColumnWidth(15);
                tb.TableStyle = db.Tablestyle;
                tb.SetTextString(0, 0, "Blocks");
                tb.SetTextString(1, 0, "Name");
                tb.SetTextString(1, 1, "Preview");
                int i=2;
                foreach (var btr in btrs)
                {
                    tb.SetTextString(i, 0, btr.Name);
                    tb.SetBlockTableRecordId(i, 1, btr.ObjectId, true);
                    i++;
                }
                tb.GenerateLayout();
                BlockTableRecord model=(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                model.AppendEntity(tb);
                trans.AddNewlyCreatedDBObject(tb, true);
                trans.Commit();
            }
        }
        public static OpenCloseTrans StartOpenCloseTrans(this Database db)
        {
            return new OpenCloseTrans();
        }
« Last Edit: November 20, 2011, 08:00:19 PM by csharpbird »