Posts: 415
Registered: ‎09-23-2004
Message 1 of 2 (221 Views)

Creating A Table With Preset Column Widths

221 Views, 1 Replies
03-25-2013 03:48 PM

Hello, I have been able to create a table, populate the Title and Headers with text.  Which is all I want it to do for now.  The problem is, I can calculate how wide I want each column, but it never gets drawn with the correct column width.

Anyone know how to make it use the correct column width specified?  Keep in mind all of the column widths may differ.

Attached is my code.

Public Sub AddTable(ByVal dwg_scale As Double, ByVal dwgTextHeight As Double)
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Using tableTrans As Transaction = db.TransactionManager.StartTransaction()
            Dim txtTbl As TextStyleTable = tableTrans.GetObject(db.TextStyleTableId, OpenMode.ForRead)
            Dim smdTable As New Table()
            smdTable.TableStyle = db.Tablestyle
            Dim tblStyle As TableStyle = tableTrans.GetObject(smdTable.TableStyle, OpenMode.ForWrite)
            Dim hdrCellHgt As Double, dtaCellHgt As Double, smdCol0Width As Double, smdCol1Width As Double
            Dim smdCol2Width As Double, smdCol3Width As Double, smdCol4Width As Double
            Dim tableLocX As Double, tableLocY As Double, tableLoc As Point3d

            hdrCellHgt = ((dwgTextHeight * 2) + (dwgTextHeight * 1.5)) 'this is setting the header row height
            dtaCellHgt = (dwgTextHeight * 2) 'this is setting the title and data row heights
            smdCol0Width = 13.0 * dwg_scale
            smdCol1Width = 14.8 * dwg_scale
            smdCol2Width = 14.8 * dwg_scale
            smdCol3Width = 19.6 * dwg_scale
            smdCol4Width = 25.4 * dwg_scale
            tableLocX = (((smdCol0Width + smdCol1Width + smdCol2Width + smdCol3Width + smdCol4Width) / 2.0) * -1)
            tableLocY = ((dtaCellHgt * 2) + hdrCellHgt)
            tableLoc = New Point3d(tableLocX, tableLocY, 0)

            tblStyle.SetTextStyle(txtTbl("Romans"), RowType.TitleRow)
            tblStyle.SetTextStyle(txtTbl("Romans"), RowType.HeaderRow)
            tblStyle.SetTextStyle(txtTbl("Romans"), RowType.DataRow)
            smdTable.Position = tableLoc
            smdTable.InsertRows(0, dtaCellHgt, 1)
            smdTable.Rows(0).Style = "Title"
            smdTable.Cells(0, 0).TextString = "DISCRETE COMPONENTS"
            smdTable.InsertRows(1, hdrCellHgt, 1)
            smdTable.InsertRows(2, dtaCellHgt, 1)
            smdTable.InsertColumns(0, smdCol0Width, 1)
            smdTable.InsertColumns(1, smdCol1Width, 1)
            smdTable.InsertColumns(2, smdCol2Width, 1)
            smdTable.InsertColumns(3, smdCol3Width, 1)
            smdTable.InsertColumns(4, smdCol4Width, 1)
            smdTable.DeleteColumns(5, 1)
            smdTable.DeleteRows(3, 1)
            smdTable.Rows(1).Style = "Header"
            smdTable.Rows(1).TextHeight = dwgTextHeight
            smdTable.Rows(2).Style = "Data"
            smdTable.Rows(2).TextHeight = dwgTextHeight
            smdTable.Rows(1).Alignment = CellAlignment.MiddleCenter
            smdTable.Rows(2).Alignment = CellAlignment.MiddleCenter
            smdTable.Cells.Alignment = CellAlignment.MiddleCenter
            smdTable.Cells.TextHeight = dwgTextHeight
            smdTable.Cells(1, 0).TextString = "ITEM NO."
            smdTable.Cells(1, 1).TextString = "X"
            smdTable.Cells(1, 2).TextString = "Y"
            smdTable.Cells(1, 3).TextString = "Z (HEIGHT)"
            smdTable.Cells(1, 4).TextString = "COMPONENT TYPE"

            Dim bt As BlockTable = tableTrans.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim smdTableBtr As BlockTableRecord = tableTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            tableTrans.AddNewlyCreatedDBObject(smdTable, True)

        End Using

    End Sub


*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 2 (194 Views)

Re: Creating A Table With Preset Column Widths

03-25-2013 11:25 PM in reply to: mgorecki

Try example from this page


Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.