Author Topic: SetBlockTableRecordId Problem with AutoCAD 2008  (Read 1430 times)

0 Members and 1 Guest are viewing this topic.

csharpbird

  • Newt
  • Posts: 64
SetBlockTableRecordId Problem with AutoCAD 2008
« on: November 17, 2011, 09:41:10 PM »
Code: [Select]
        [CommandMethod("Test")]
        public static void Test()
        {
            Document doc=Application.DocumentManager.MdiActiveDocument;
            Database db=doc.Database;
            Editor ed=doc.Editor;
            using (Transaction trans=db.TransactionManager.StartTransaction())
            {               
                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();
            }
        }
When running the codes,AutoCAD will throw an exception:

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: SetBlockTableRecordId Problem with AutoCAD 2008
« Reply #1 on: November 17, 2011, 10:34:37 PM »
Can you post a drawing where this fails? I don't have 2008, but 2009's API should be the same and it works fine in a test 2009 dwg.

csharpbird

  • Newt
  • Posts: 64
Re: SetBlockTableRecordId Problem with AutoCAD 2008
« Reply #2 on: November 18, 2011, 03:15:54 AM »
I test the code using db_samp.dwg under the AutoCAD's sample folder.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: SetBlockTableRecordId Problem with AutoCAD 2008
« Reply #3 on: November 18, 2011, 10:37:12 AM »
OK, the code fails when you add a block to the table which contains attributes. If I add code to omit btr's whose HasAttributes properties is true then it works without error.

So I tested some code I've been using to create legends for some time with this drawing. It, too, fails in the same manner. But I was pretty sure it worked with attributed blocks in other drawings. Further testing says I must not have tested this in 2009 (most of my coding is done in 2010) because the same code (mine) works fine in 2010.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: SetBlockTableRecordId Problem with AutoCAD 2008
« Reply #4 on: November 18, 2011, 11:04:09 AM »
Here's a link to Kean's blog from a few years ago. Look down in the comments where this problem was discussed.
http://through-the-interface.typepad.com/through_the_interface/2007/06/creating_an_aut_1.html