91
Tables / Apply a minimum column width setting
« on: June 25, 2012, 06:21:56 AM »Code: [Select]
// Apply a minimum column width setting
[CommandMethod("ChangeTableColumnsWidths", "CtCw", CommandFlags.UsePickSet)]
public static void ChangeTableColumnsWidths()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
try
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect table to fix columns");
peo.SetRejectMessage("\nYou have to select table...");
peo.AddAllowedClass(typeof(Table), false);
peo.AllowNone = false;
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId id = per.ObjectId;
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
// Open the table for read content
Table tb = tr.GetObject(id, OpenMode.ForRead) as Table;
List<List<double>> tbcontent = new List<List<double>>();
// to simplify code look up to the first cell of header rows, ignore title row
Cell cl = tb.Cells[1, 0];
// get text height
double th = (double)cl.TextHeight;//cl.Contents[0].TextHeight;
double ratio = 1.25;// ratio between height and width of characters, approx., calc by yourself for your font
double lwd = th * ratio;
for (int i = 1; i < tb.Rows.Count; i++)
{
List<double> tmpline = new List<double>();
for (int j = 0; j < tb.Columns.Count; j++)
{
tmpline.Add(tb.Cells[i, j].Value == null ? 0.0 : tb.Cells[i, j].TextString.Length); //if empty cell then add zero
}
tbcontent.Add(tmpline);
}
List<double> raws = new List<double>();
for (int j = 0; j < tb.Columns.Count; j++)
{
List<double> tmp = new List<double>();
for (int i = 0; i < tbcontent.Count; i++)
{
List<double> tmpline = tbcontent[i];
double twid = tmpline[j] * lwd;
tmp.Add(twid);
}
raws.Add(tmp.Max());
}
//open for write
tb.UpgradeOpen();
//aplly new column width
for (int i = 0; i < raws.Count; i++)
{
tb.Columns[i].Width = raws[i];
}
tb.Width = raws.Sum();//optional, set table common with
tb.UpgradeOpen();
tr.Commit();
}
}
catch (System.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
}
}VB.NET
Code: [Select]
' Apply a minimum column width setting
<CommandMethod("ChangeTableColumnsWidths", "CtCw", CommandFlags.UsePickSet)> _
Public Shared Sub ChangeTableColumnsWidths()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Try
Dim peo As New PromptEntityOptions(vbLf & "Select table to fix columns")
peo.SetRejectMessage(vbLf & "You have to select table...")
peo.AddAllowedClass(GetType(Table), False)
peo.AllowNone = False
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status <> PromptStatus.OK Then
Return
End If
Dim id As ObjectId = per.ObjectId
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
' Open the table for read content
Dim tb As Table = TryCast(tr.GetObject(id, OpenMode.ForRead), Table)
Dim tbcontent As New List(Of List(Of Double))()
' to simplify code look up to the first cell of header rows, ignore title row
Dim cl As Cell = tb.Cells(1, 0)
' get text height
Dim th As Double = CDbl(cl.TextHeight)
'cl.Contents[0].TextHeight;
Dim ratio As Double = 1.25
' ratio between height and width of characters, approx., calc by yourself for your font
Dim lwd As Double = th * ratio
For i As Integer = 1 To tb.Rows.Count - 1
Dim tmpline As New List(Of Double)()
For j As Integer = 0 To tb.Columns.Count - 1
'if empty cell then add zero
tmpline.Add(If(tb.Cells(i, j).Value Is Nothing, 0.0, tb.Cells(i, j).TextString.Length))
Next
tbcontent.Add(tmpline)
Next
Dim raws As New List(Of Double)()
For j As Integer = 0 To tb.Columns.Count - 1
Dim tmp As New List(Of Double)()
For i As Integer = 0 To tbcontent.Count - 1
Dim tmpline As List(Of Double) = tbcontent(i)
Dim twid As Double = tmpline(j) * lwd
tmp.Add(twid)
Next
raws.Add(tmp.Max())
Next
'open for write
tb.UpgradeOpen()
'aplly new column width
For i As Integer = 0 To raws.Count - 1
tb.Columns(i).Width = raws(i)
Next
tb.Width = raws.Sum()
'optional, set table common with
tb.UpgradeOpen()
tr.Commit()
End Using
Catch ex As System.Exception
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(vbLf & ex.Message & vbLf & ex.StackTrace)
Finally
End Try
End Sub