Author Topic: Working with Tables and Cell Styles.  (Read 2422 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
Working with Tables and Cell Styles.
« on: November 08, 2007, 11:58:03 AM »
I am using a table to physically draw out a bill of material on a drawing. My problem is when I create the table in code I can set the overall cell width of the table but I can't figure out how to set the width of individual columns. For example I would want my quantity column to have a fairly narrow width while my description field should be much wider.


Can anyone give me any pointers? I have tried searching the docs with no luck. Please note that I am using code from this post:

http://through-the-interface.typepad.com/through_the_interface/2007/06/creating_an_aut.html

Fatty

  • Guest
Re: Working with Tables and Cell Styles.
« Reply #1 on: November 08, 2007, 12:45:21 PM »
Try this one I wrote for my own needs:

Code: [Select]
Public Sub BlockSymbolsToTable()

        Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction()

            Try
                Dim blkTbl As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim blkTblRec As BlockTableRecord = CType(trans.GetObject(blkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
                Dim stId As ObjectId = AcadTableUtilities.makeTableStyle
                Dim ptable As Table = Nothing
                ptable = New Table

                ptable.Position = New Point3d(0, 0, 0)
                ptable.InsertRows(0, 3.0, 1)
                ptable.InsertColumns(0, 7.5, 3)
                ptable.SetTextHeight(0, 0, 1.5)
                ptable.SetAlignment(0, 0, CellAlignment.MiddleCenter)
                ptable.SetTextString(0, 0, "Drawing Block Images")
                Dim reg As TableRegion = New TableRegion(0, 0, 0, 3)
                ptable.MergeCells(reg)
                ptable.TableStyle = stId

                ptable.SetColumnWidth(0, 8.0)
                ptable.SetColumnWidth(1, 36.0)
                ptable.SetColumnWidth(2, 40.0)
                ptable.SetColumnWidth(3, 30.0)

                ptable.SetTextHeight(1, 0, 1.5)
                ptable.SetTextHeight(1, 1, 1.5)
                ptable.SetTextHeight(1, 2, 1.5)
                ptable.SetTextHeight(1, 3, 1.5)

                ptable.SetTextString(1, 0, "Image")
                ptable.SetTextString(1, 1, "Block Name")
                ptable.SetTextString(1, 2, "Description")
                ptable.SetTextString(1, 3, "Remarks")
                Dim irow As Long = 2
                Dim btiter As IEnumerator = blkTbl.GetEnumerator
                While btiter.MoveNext
                    Dim blkId As ObjectId = btiter.Current
                    Dim btr As BlockTableRecord = trans.GetObject(blkId, OpenMode.ForRead)
                    If btr.IsAnonymous = False And _
                    btr.IsLayout = False And _
                    btr.IsFromExternalReference = False And _
                        btr.IsFromOverlayReference = False Then
                        Dim bname As String = btr.Name
                        ed.WriteMessage(vbCr & bname)
                        ptable.InsertRows(irow, 2.5, 1)
                        ptable.GenerateLayout()

                        ptable.SetBlockTableRecordId(irow, 0, blkId, True)
                        ptable.SetTextString(irow, 1, bname)
                        ptable.SetTextString(irow, 2, "Here is must be description")

                        ptable.SetAlignment(irow, 1, CellAlignment.MiddleCenter)
                        ptable.SetAlignment(irow, 2, CellAlignment.MiddleCenter)
                        ptable.SetAlignment(irow, 3, CellAlignment.MiddleCenter)
                        ptable.SetTextHeight(irow, 0, 1.5)
                        ptable.SetTextHeight(irow, 1, 1.5)
                        ptable.SetTextHeight(irow, 2, 1.5)
                        ptable.SetTextHeight(irow, 3, 1.5)

                        irow += 1
                    End If
                End While

                blkTblRec.AppendEntity(ptable)
                trans.AddNewlyCreatedDBObject(ptable, True)

                ptable.RecomputeTableBlock(True)
                ed.Regen()
                ptable.Dispose()
                blkTbl.Dispose()
                blkTblRec.Dispose()
            Catch ex As System.Exception
                MsgBox(ex.StackTrace)
            End Try
            trans.Commit()
            trans.Dispose()
        End Using
    End Sub

~'J'~

TR

  • Guest
Re: Working with Tables and Cell Styles.
« Reply #2 on: November 08, 2007, 01:07:05 PM »
That should get me going. Thank you Fatty.

Fatty

  • Guest
Re: Working with Tables and Cell Styles.
« Reply #3 on: November 08, 2007, 02:46:45 PM »
I would glad if this would helps
Cheers :)

~'J'~