Now, if I remove the command session flag, and make the drawing current manually it works if I also add this line: legendtbl.GenerateLayout()
And by doing the above, I can define the insertion point too.
So it is something about making active the drawing, where the table it is going to be placed...
[CommandMethod("LegendTest")] //, CommandFlags.Session)] // test only
public void LegendBuilderCommand()
PromptPointOptions ppo = new PromptPointOptions("\nLegend Table insertion point: ");
ppo.AllowArbitraryInput = false;
ppo.AllowNone = false;
PromptPointResult pr = ed.GetPoint(ppo); //"\nLegend Table insertion point: ");
if (pr.Status != PromptStatus.OK) return;
//Point3d pt = Point3d.Origin;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
if (!btr.Name.ToUpper().Equals("*MODEL_SPACE"))
scale = 1.0;
ObjectId tblId;
if (lb_LegendTable.SelectedIndex != -1)
{
DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
DBDictionary dict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
tblId = (ObjectId)dict[lb_LegendTable.SelectedValue.ToString()];
}
else
tblId = db.Tablestyle;
TableStyle tstyle = (TableStyle)tr.GetObject(tblId, OpenMode.ForRead);
double titleHgt = tstyle.TextHeight(RowType.TitleRow) * scale;
double headerHgt = tstyle.TextHeight(RowType.HeaderRow) * scale;
double dataHgt = tstyle.TextHeight(RowType.DataRow) * scale;
double hzMrgn = tstyle.HorizontalCellMargin * scale;
double vrtMrgn = tstyle.VerticalCellMargin * scale;
AcDb.Table legendtbl = new AcDb.Table();
double rowHgt = dataHgt + (2.0 * vrtMrgn);
double colOneWdth = headerHgt * 7.0; //6 characters in SYMBOL + 1 for spacing
double colTwoWdth = dataHgt * maxdesclength;
legendtbl.TableStyle = tblId;
legendtbl.InsertRows(1, rowHgt, blklist.Count + 1);
legendtbl.VerticalCellMargin = vrtMrgn;
legendtbl.HorizontalCellMargin = hzMrgn;
legendtbl.DeleteColumns(0, 3);
legendtbl.InsertColumns(0, colOneWdth, 1);
legendtbl.InsertColumns(1, colTwoWdth, 1);
legendtbl.Cells.Borders.Bottom.IsVisible = false;
legendtbl.Cells.Borders.Horizontal.IsVisible = false;
legendtbl.Cells.Borders.Left.IsVisible = false;
legendtbl.Cells.Borders.Right.IsVisible = false;
legendtbl.Cells.Borders.Top.IsVisible = false;
legendtbl.Cells.Borders.Vertical.IsVisible = false;
legendtbl.Rows[0].Height = titleHgt + (2.0 * vrtMrgn);
legendtbl.Rows[0].TextHeight = titleHgt;
legendtbl.Rows[1].TextHeight = headerHgt;
legendtbl.Cells[0, 0].TextString = "Symbol Legend";
legendtbl.Cells[1, 0].TextString = "Symbol";
legendtbl.Cells[1, 1].TextString = "Denotes";
legendtbl.Cells[1, 0].Alignment = CellAlignment.MiddleCenter;
legendtbl.Cells[1, 1].Alignment = CellAlignment.MiddleLeft;
legendtbl.SuppressRegenerateTable(true);
for (int i = 0; i < blklist.Count; i++)
{
BlockTableRecord blkid = (BlockTableRecord)tr.GetObject(usedBlks[blklist[i]], OpenMode.ForRead);
string desc = blkid.Comments;
if (desc == "")
desc = blklist[i];
//legendtbl.Cells[i + 2, 0].VerticalLine.Margin = vrtMrgn; //This doesn't work even though we are told to use it...2010
legendtbl.Cells[i + 2, 0].BlockTableRecordId = usedBlks[blklist[i]];
legendtbl.Cells[i + 2, 1].TextString = desc;
legendtbl.Rows[i + 2].TextHeight = dataHgt;
legendtbl.Cells[i + 2, 0].Alignment = CellAlignment.MiddleCenter;
legendtbl.Cells[i + 2, 1].Alignment = CellAlignment.MiddleLeft;
}
legendtbl.SuppressRegenerateTable(false);
legendtbl.Position = pr.Value; //pt
legendtbl.GenerateLayout(); // <<<<
btr.UpgradeOpen();