Author Topic: How to correctly Insert block in Table for 2011  (Read 3299 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
How to correctly Insert block in Table for 2011
« on: February 10, 2011, 02:37:32 AM »
In 2011 you get the obsolete warning if you use

Table.SetBlockTableRecordId() Method------- It does work

How do you use the recomended Table.Cells[row,column].Contents.BlockTableRecordId

Had problems getting it to work. Possibly the Contents part. Have not found much info on it.

kaefer

  • Guest
Re: How to correctly Insert block in Table for 2011
« Reply #1 on: February 10, 2011, 05:37:59 AM »
How do you use the recomended Table.Cells[row,column].Contents.BlockTableRecordId

It would appear that there's no support for multiple Contents with CellContentTypes.Block.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to correctly Insert block in Table for 2011
« Reply #2 on: February 12, 2011, 03:13:26 PM »
I was not calling  Table.Cells[i, j].Contents.Add();         

That got it working

Code: [Select]
Table.Cells[i, j].Contents.Add();                       
Table.Cells[i, j].Contents[0].BlockTableRecordId = ObjectID;

kaefer

  • Guest
Re: How to correctly Insert block in Table for 2011
« Reply #3 on: February 13, 2011, 06:13:58 PM »
I was not calling  Table.Cells[i, j].Contents.Add();         

That got it working

Code: [Select]
Table.Cells[i, j].Contents.Add();                       
Table.Cells[i, j].Contents[0].BlockTableRecordId = ObjectID;

Same problem here. To correct myself: Of course you can have multiple Contents with ContentTypes.Block.
Now it works. See the example below (simple block counter again).

But, but, but... How would you get around the obsolete NumRows and NumColumns properties?

Code: [Select]
        [CommandMethod("BLKCNTTBL", CommandFlags.UsePickSet)]
        public static void BlkCntTbl()
        {
            Editor ed = AcApp.DocumentManager.MdiActiveDocument.Editor;
            Database db = AcApp.DocumentManager.MdiActiveDocument.Database;
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.MessageForAdding = "Select blocks (or enter for all): ";
            pso.AllowDuplicates = false;
            SelectionFilter sf =
                new SelectionFilter(new TypedValue[]{ new TypedValue(0, "INSERT") });
            PromptSelectionResult psr = ed.GetSelection(pso, sf);
            if (psr.Status == PromptStatus.Error) psr = ed.SelectAll(sf);
            if (!(psr.Status == PromptStatus.OK) && (psr.Value.Count > 0)) return;

            string title =
                "Blocks in " +
                System.IO.Path.GetFileNameWithoutExtension(db.Filename);

            Dictionary<string, int> dict = new Dictionary<string, int>();
            using(Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach(SelectedObject so in psr.Value)
                {
                    BlockReference br = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    if(dict.ContainsKey(br.Name)) dict[br.Name] += 1;
                    else dict.Add(br.Name, 1);
                }
                PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
                if(pr.Status == PromptStatus.OK)
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord cspace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    Table tb = new Table();
                    tb.Position = pr.Value;
                    tb.NumRows = 2; // "Use Table.Rows.Count instead."
                    tb.NumColumns = 2;  // "Use Table.Columns.Count instead."
                    tb.TableStyle = db.Tablestyle;
                    TableStyle ts = (TableStyle)tr.GetObject(tb.TableStyle, OpenMode.ForRead);
                    double textHeigth = ts.TextHeight(RowType.DataRow);
                    tb.Columns[0].Width = textHeigth * 24;
                    tb.Columns[1].Width = textHeigth * 8;
                    int y = 0;
                    tb.Cells[y, 0].TextString = title;
                    y += 1;
                    tb.Cells[y, 0].TextString = "Block name";
                    tb.Cells[y, 1].TextString = "Count";
                    y += 1;
                    foreach(KeyValuePair<string,int> entry in dict)
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[entry.Key], OpenMode.ForRead);
                        if (!(btr.IsLayout || btr.IsAnonymous))
                        {
                            tb.InsertRows(y, textHeigth * 4, 1);
                            tb.Cells[y, 0].ContentLayout = CellContentLayout.StackedHorizontal;
                            CellContentsCollection cc = tb.Cells[y, 0].Contents;
                            cc.Add();
                            cc[0].TextString = entry.Key;
                            cc.Add();
                            cc[1].BlockTableRecordId = bt[entry.Key];
                            tb.Cells[y, 1].TextString = entry.Value.ToString();
                            y += 1;
                        }
                    }
                    tb.GenerateLayout();
                    cspace.AppendEntity(tb);
                    tr.AddNewlyCreatedDBObject(tb, true);
                }
                tr.Commit();
            }
        }

Have fun, Thorsten

kaefer

  • Guest
Re: How to correctly Insert block in Table for 2011
« Reply #4 on: February 14, 2011, 05:50:35 AM »
Here's a pic for BLKCNTTBL, with a drawing from http://images.autodesk.com/adsk/files/blocks_and_tables_-_metric.dwg

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to correctly Insert block in Table for 2011
« Reply #5 on: February 14, 2011, 07:20:48 PM »
My understanding you use InsertRows & InsertColumns
You can set the settings for a row and use InsertRowsAndInherit(index where to start, index to copy settings, # of rows)


I have no idea why this keeps making the last row a title.

I used Tuples just messing around no real reason


Code: [Select]

       [CommandMethod("CreateSymbolLegend")]
            public void CreateSymbolLegend()
            {
                List<Tuple<ObjectId, string, string>> blockinfo = new List<Tuple<ObjectId, string, string>>();
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;

                using (Transaction trx = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;

                    BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);
         
                    foreach (ObjectId objId in bt)
                    {                       
                        BlockTableRecord btr = trx.GetObject(objId, OpenMode.ForRead) as BlockTableRecord;
                        if (btr.IsLayout) continue;
                        blockinfo.Add(Tuple.Create(btr.ObjectId, btr.Name, btr.Comments));
                    }

                    Tuple<ObjectId, string, string>[] blockArr = blockinfo.ToArray();

                    Table tbl = new Table();
                    tbl.TableStyle = db.Tablestyle;
                    tbl.Position = Point3d.Origin;
         
                    tbl.InsertRows(0, 1,  1);
                    tbl.Rows[0].Style = "Data";
                    tbl.InsertRowsAndInherit(1, 0, blockArr.Length - 2);
                    tbl.InsertColumns(0, 1, 1);
                   
 
                     
                   
                    for (int i = 0; i < blockArr.Length; i++)
                    {               
                        tbl.Cells[i, 0].Contents.Add();
                        tbl.Cells[i, 0].Contents[0].BlockTableRecordId = blockArr[i].Item1;
                        tbl.Cells[i, 1].Value = blockArr[i].Item3;
                    }

                    tbl.GenerateLayout();
                 
                    btrMs.UpgradeOpen();
                    btrMs.AppendEntity(tbl);
                    trx.AddNewlyCreatedDBObject(tbl, true);
                   
                        trx.Commit();
                }
            }
« Last Edit: February 14, 2011, 07:26:53 PM by Jeff H »

kaefer

  • Guest
Re: How to correctly Insert block in Table for 2011
« Reply #6 on: February 15, 2011, 03:37:06 AM »
My understanding you use InsertRows & InsertColumns
You can set the settings for a row and use InsertRowsAndInherit(index where to start, index to copy settings, # of rows)

Thanks, that does the trick nicely.

Quote
I have no idea why this keeps making the last row a title.

Because your're inserting your data rows before the pre-existing title row, maybe?

Quote
Code: [Select]
                   tbl.InsertRows(0, 1,  1);
                    tbl.Rows[0].Style = "Data";
                    tbl.InsertRowsAndInherit(1, 0, blockArr.Length - 2);
                    tbl.InsertColumns(0, 1, 1);

So I'd go with something like this instead:
Code: [Select]
                   TableStyle ts = (TableStyle)tr.GetObject(tbl.TableStyle, OpenMode.ForRead);
                    double textHeigth = ts.TextHeight(RowType.DataRow);
                    tbl.Cells[0, 0].TextString <- "title";
                    tbl.InsertRows(1, textHeigth * 2,  1);
                    tbl.Rows[1].Style <- "_Data"; // i18n!
                    tbl.InsertRowsAndInherit(1, 1, blockArr.Length - 1);
                    tbl.Columns[0].Width <- textHeigth * 16;
                    tbl.InsertColumnsAndInherit(1, 0, 1);

Cheers

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to correctly Insert block in Table for 2011
« Reply #7 on: February 15, 2011, 03:56:14 AM »
So I'd go with something like this instead:
Code: [Select]
                   TableStyle ts = (TableStyle)tr.GetObject(tbl.TableStyle, OpenMode.ForRead);
                    double textHeigth = ts.TextHeight(RowType.DataRow);
                    tbl.Cells[0, 0].TextString <- "title";
                    tbl.InsertRows(1, textHeigth * 2,  1);
                    tbl.Rows[1].Style <- "_Data"; // i18n!
                    tbl.InsertRowsAndInherit(1, 1, blockArr.Length - 1);
                    tbl.Columns[0].Width <- textHeigth * 16;
                    tbl.InsertColumnsAndInherit(1, 0, 1);

Cheers
Thank you, Thank you

As usual you were dead on.