Author Topic: Create and Populate Autocad Table  (Read 9352 times)

0 Members and 1 Guest are viewing this topic.

fixo

  • Guest
Re: Create and Populate Autocad Table
« Reply #15 on: July 18, 2008, 05:47:30 AM »
Jeff M is my teacher too :)
Sorry I could not be able to explain the things
that you asked for becuase of my poor English level
Better yet I will to write the code you need :)
and Jeff M will explain it much better :)

(Change the text height, layout name etc to your suit)

Code: [Select]
Private Sub MakeTableStyle()
' creates a TableStyle object
     Dim oDict As AcadDictionary
     Dim aColor As New AcadAcCmColor
     Dim oTblSty As AcadTableStyle
     Dim sKeyName As String
     Dim sClassName As String
     'grab the tablestyle dictionary object
     Set oDict = ThisDrawing.Database.Dictionaries.Item("acad_tablestyle")
     sKeyName = "Block Table"
     sClassName = "AcDbTableStyle"
     'create the TableStyle object in the dictionary
     Set oTblSty = oDict.AddObject(sKeyName, sClassName)
     With oTblSty
          .Name = "Block Table"
          .Description = "Style For The Block Info"
          .HorzCellMargin = 0.03
          .TitleSuppressed = False
          .SetTextHeight 3, 0.93625
          .SetGridVisibility 3, 3, True
          .SetAlignment 3, acMiddleCenter
          aColor.SetRGB 244, 0, 0
     End With
End Sub



Sub BlockToTable()
     Dim oTable As AcadTable
     Dim oEnt As AcadEntity
     Dim fstRef As AcadBlockReference
     Dim blkRef As AcadBlockReference
     Dim bname As String
     Dim varPt As Variant
     Dim attVar() As Object
     Dim attObj As AcadAttributeReference
     Dim row As Long, col As Long
     Dim i As Long, j As Long
     Dim tmpStr As String
     Dim attColl As Collection
     Dim acCol As New AcadAcCmColor
     
     On Error GoTo Err_Control
     
     If ThisDrawing.ActiveSpace = acPaperSpace Then
     ThisDrawing.ActiveSpace = acModelSpace
     End If
     
     On Error Resume Next
     ThisDrawing.Utility.GetEntity oEnt, varPt, vbCrLf & "Select block to import data to table"
     If Err Then
          Err.Clear
     End If
     On Error GoTo 0
     
     
     If Not oEnt Is Nothing Then
          If TypeOf oEnt Is AcadBlockReference Then
               Set blkRef = oEnt
          End If
     End If

     If Not blkRef.HasAttributes Then
          MsgBox "This block does not have an attributes"
          Exit Sub
     End If
     
     If blkRef.IsDynamicBlock Then
          bname = blkRef.EffectiveName
     Else
          bname = blkRef.Name
     End If
     
     Dim oSset As AcadSelectionSet
     Dim ftype(1) As Integer
     Dim fdata(1) As Variant
     Dim dxfCode, dxfValue
     
          With ThisDrawing.SelectionSets
               While .Count > 0
                    .Item(0).Delete
               Wend
          Set oSset = .Add("$Blocks$")
          End With
         
     ftype(0) = 0: ftype(1) = 2
     fdata(0) = "INSERT": fdata(1) = bname
     dxfCode = ftype: dxfValue = fdata
                   
     oSset.Select acSelectionSetAll, , , dxfCode, dxfValue
     MsgBox oSset.Count
     Set oEnt = oSset.Item(0)
     Set fstRef = oEnt
     
     attVar = fstRef.GetAttributes
     ReDim tmp(0 To UBound(attVar) + 1) As String
     
     Set attColl = New Collection
     tmp(0) = "Block Name"
     For i = 0 To UBound(attVar)
     tmp(i + 1) = attVar(i).TagString
     Next
     attColl.Add tmp, "headers"
     
     For j = 1 To oSset.Count
     Set oEnt = oSset.Item(j - 1)
     Set blkRef = oEnt
     attVar = blkRef.GetAttributes
     tmp(0) = bname
     For i = 0 To UBound(attVar)
     tmp(i + 1) = attVar(i).TextString
     Next
     attColl.Add tmp, "row " & CStr(j)

     Next j
     Call MakeTableStyle
     ThisDrawing.ActiveLayout = ThisDrawing.Layouts("C-02") '<--change the tab name here
     DoEvents
     
     Dim pt As Variant
     pt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Pick the upper left point of table:")
     
     Set oTable = ThisDrawing.PaperSpace.AddTable(pt, oSset.Count + 2, UBound(tmp) + 1, 2.5, 10)

     oTable.RegenerateTableSuppressed = True
     oTable.HorzCellMargin = 0.03
     oTable.TitleSuppressed = False
     oTable.HeaderSuppressed = False
     oTable.SetTextHeight 7, 0.18
     row = 0
     col = 0
     acCol.SetRGB 143, 189, 164
     tmpStr = "Block Attributes Info"
     oTable.SetRowHeight row, 0.25
     oTable.SetCellTextHeight row, col, 0.18
     oTable.SetCellBackgroundColor row, col, acCol
     acCol.SetRGB 173, 43, 0
     oTable.SetCellContentColor row, col, acCol
     oTable.SetText row, col, tmpStr
     oTable.SetCellAlignment row, col, acMiddleCenter
     row = 1
     oTable.SetRowHeight row, 0.25
     
     For i = 0 To UBound(tmp)
          acCol.SetRGB 236, 237, 238
          oTable.SetCellTextHeight row, i, 0.15
          oTable.SetCellBackgroundColor row, i, acCol
          acCol.SetRGB 0, 0, 180
          oTable.SetCellContentColor row, i, acCol
          tmpStr = attColl.Item(1)(i)
          If i <> UBound(tmp) Then
          oTable.SetColumnWidth i, 2#
          Else
          oTable.SetColumnWidth i, 3#
          End If
          oTable.SetText row, i, tmpStr
          oTable.SetCellAlignment row, i, acMiddleCenter
          acCol.SetRGB 0, 0, 180
     Next

     For row = 2 To attColl.Count
     oTable.SetRowHeight row, 0.21
     For i = 0 To UBound(tmp)
          acCol.SetRGB 236, 237, 238
          oTable.SetCellTextHeight row, i, 0.12
          oTable.SetCellBackgroundColor row, i, acCol
          acCol.SetRGB 0, 0, 180
          oTable.SetCellContentColor row, i, acCol
          tmpStr = attColl.Item(row)(i)
          If i <> UBound(tmp) Then
          oTable.SetColumnWidth i, 2#
          Else
          oTable.SetColumnWidth i, 3#
          End If
          oTable.SetText row, i, tmpStr
          oTable.SetCellAlignment row, i, acMiddleCenter
     Next
     Next
     oTable.RegenerateTableSuppressed = False
     Set acCol = Nothing
Err_Control:
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub

~'J'~

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #16 on: July 18, 2008, 10:12:50 AM »
Wow, that's some great additions!

The error handling is so simple and easy to understand... that was one of the things I planned on adding in as I go, so thank you for saving me from searching for a solution.

I read through it all, slowly, and you've given me some extra tools to use, thank you.

I like how  you changed how I counted attributes.  Before I had to set a new integer (c) to count with, rather than using the previous (i) because I did not know how to add the attributes, and it was giving me wrong output when I tried.  I knew my method was not the best, so thank you for showing me the right way.

I have one question though.  I am currently trying to find a solution.  How can I take the array of object in a selection set... and reverse the order?

To explain:  When it creates the table, it places the information starting with the block I placed LAST, and then descending in order to the block I placed FIRST... I wonder: is there a way to reverse the order it reads the attributes and dxf codes as it loops?  I guess I'm wanting to loop in reverse order.  I've tried a couple guesses but they've failed.

Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
Re: Create and Populate Autocad Table
« Reply #17 on: July 18, 2008, 12:07:12 PM »
I have one question though.  I am currently trying to find a solution.  How can I take the array of object in a selection set... and reverse the order?
This small change will do that, Josh. The code between the first & last lines is all I modified. This Steps thru the SS from the end to the the beginning.
Code: [Select]
'<snip>
     attColl.Add tmp, "headers"
     
     Dim idx As Integer
     idx = 0
     For j = oSset.Count To 1 Step -1
        Set oEnt = oSset.Item(j - 1)
        Set blkRef = oEnt
        attVar = blkRef.GetAttributes
        tmp(0) = bname
        For i = 0 To UBound(attVar)
            tmp(i + 1) = attVar(i).TextString
        Next
        idx = 1 + idx
        attColl.Add tmp, "row " & CStr(idx)

     Next j
     Call MakeTableStyle
'<snip>

Jeff_M

  • King Gator
  • Posts: 3946
  • C3D user & customizer
Re: Create and Populate Autocad Table
« Reply #18 on: July 18, 2008, 12:12:20 PM »
I like how  you changed how I counted attributes.  Before I had to set a new integer (c) to count with, rather than using the previous (i) because I did not know how to add the attributes, and it was giving me wrong output when I tried.  I knew my method was not the best, so thank you for showing me the right way.
FYI, your method was just fine, based on the example I showed you. What ~'J'~ did was streamline how the table data was added to it which, in turn, changed how the data was gathered. That is why he was able to re-use the counter. Note that he still used 2 counters, though, I & J, just in different ways. :-)

jnieman

  • Guest
Re: Create and Populate Autocad Table
« Reply #19 on: July 18, 2008, 12:15:08 PM »
Ah, I see, then, Jeff.  I guess when I used another integer I thought that there must've been a more elegant way, but did not push too hard for elegance, since I'm a beginner.

I don't have time for now to get the reverse put in there, but will add that in soon.  I was approaching the method of counting wrong.

Thanks again to both of you for the extremely helpful guidance and support.  Couldn't have done this without you guys.