Author Topic: Getting Tables by name  (Read 2202 times)

0 Members and 1 Guest are viewing this topic.

WOWENS

  • Newt
  • Posts: 59
Getting Tables by name
« on: March 02, 2016, 08:42:48 PM »
I'm trying to get all the custom tables in my drawing I can do it with the editor but I don't want to use the editor I prefer to use just the datebase the following is what I have and guidance would be great.

Dim filterfor() As TypedValue = {New TypedValue(0, "ACAD_TABLE"), New TypedValue(1000, "TableName")}

Dim sf As SelectionFilter = New SelectionFilter(filterfor)

Dim psr As PromptSelectionResult = ed.SelectAll(sf)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Getting Tables by name
« Reply #1 on: March 03, 2016, 02:15:31 AM »
Hi,

I am confused of what you are trying to do !
How would you make a selection of ALL tables and you would like to use database ?

Can you explain a bit more ?

WOWENS

  • Newt
  • Posts: 59
Re: Getting Tables by name
« Reply #2 on: March 03, 2016, 07:00:30 AM »
well I have a lot of drawings I need to read the table from so I was thinking of using
ReadDwgFile but I can't use the editor then. example of getting all block BlockReference would be like this
Dim MyBTR As BlockTableRecord = DirectCast(SpaceId.GetObject(OpenMode.ForRead), BlockTableRecord)
except for I want the table

WOWENS

  • Newt
  • Posts: 59
Re: Getting Tables by name
« Reply #3 on: March 03, 2016, 09:13:07 AM »
I found away..

Private Shared Function GetTables(_Database As Database, TableStyleName As String) As List(Of Table)
            Dim ReturnValue As New List(Of Table)

            Using tran As Transaction = _Database.TransactionManager.StartTransaction()
                Dim layoutDict As DBDictionary = DirectCast(tran.GetObject(_Database.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)

                For Each entry As DictionaryEntry In layoutDict
                    Dim oLayout As Layout = DirectCast(tran.GetObject(DirectCast(entry.Value, ObjectId), OpenMode.ForRead), Layout)
                    Dim oBlockTableRecord As BlockTableRecord = DirectCast(tran.GetObject(oLayout.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)

                    For Each oObjectId As ObjectId In oBlockTableRecord
                        Dim oDBObject As DBObject = DirectCast(tran.GetObject(oObjectId, OpenMode.ForRead), DBObject)
                        If TypeOf oDBObject Is Table Then
                            Dim oTable As Table = TryCast(oDBObject, Table)
                            If oTable IsNot Nothing Then
                                If oTable.TableStyleName.ToUpper = TableStyleName.ToUpper Then
                                    ReturnValue.Add(oTable)
                                End If
                            End If
                        End If
                    Next
                Next
            End Using

            Return ReturnValue
        End Function

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Getting Tables by name
« Reply #4 on: March 03, 2016, 01:26:19 PM »
(1)Transaction.GetObject() returns a DbObject so you shouldn't have to cast the return type. 

(2)Then it looks like you're testing if oDBObject is of Type Table.  Then the very next line you're using TryCast() which will always return a non null object since it had to pass your first test.
Revit 2019, AMEP 2019 64bit Win 10

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Getting Tables by name
« Reply #5 on: March 03, 2016, 02:07:26 PM »
I would try something like this.

Code - Visual Basic: [Select]
  1. For Each objectId As ObjectId In oBlockTableRecord
  2.     If objectId.ObjectClass.DxfName = "TABLE DXF NAME" Then
  3.         Dim table = DirectCast(trans.GetObject(objectId, OpenMode.ForRead), Table)
  4.         If table.TableStyleName.ToUpper() = TableStyleName.ToUpper() Then
  5.             ReturnValue.Add(table)
  6.         End If
  7.     End If
  8. Next
  9.  

Checking the DXF name before opening the object is quicker then opening it and then checking.  For one thing you are not needlessly opening the object.  Second, you know for a fact that it is a table so you can open it up as a table without any trycasts.  Hopefully this helps.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Getting Tables by name
« Reply #6 on: March 03, 2016, 02:39:28 PM »
Here is another way using the Named Object Dictionary.  Since more than likely you are going to have an active transaction when calling this method I used the top transaction instead of creating a new one.


Code - Visual Basic: [Select]
  1. Public Shared Function GetTablesByNameFromLayouts(database As Database, tableName As String) As List(Of Table)
  2.         Dim tables = New List(Of Table)
  3.         Dim transaction = database.TransactionManager.TopTransaction()
  4.         If transaction = Nothing Then
  5.                 Throw New Autodesk.AutoCad.Runtime.Exception(ErrorStatus.NoActiveTransactions, "This method reqires an active transaction.")
  6.         End If
  7.         Dim nod = DirectCast(transaction.GetObject(database.NamedObjectsDictionaryId, OpenMode.ForRead), DBDictionary)
  8.         Dim layouts = DirectCast(transaction.GetObject(nod.GetAt("ACAD_LAYOUT"), OpenMode.ForRead), DBDictionary)
  9.         For Each entry As DBDictionaryEntry in layouts
  10.                 Dim layout = DirectCast(transaction.GetObject(entry.Value, OpenMode.ForRead), Layout)
  11.                 Dim blockTableRecord = DirectCast(transaction.GetObject(layout.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
  12.                 For Each objectId As ObjectId In blockTableRecord
  13.                         If objectId.ObjectClass.DxfName = "TABLE DXF NAME" Then
  14.                                 Dim table = DirectCast(transaction.GetObject(objectId, OpenMode.ForRead), Table)
  15.                                 If table.TableStyleName.ToUpper() = tableName.ToUpper() Then
  16.                                         tables.Add(table)
  17.                                 End If
  18.                         End If
  19.                 Next
  20.         Next
  21.         Return tables
  22. End Function


I did this off the top of my head and didnt run or debug the code so crossing my fingers it is correct.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Getting Tables by name
« Reply #7 on: March 03, 2016, 03:10:03 PM »
I think you can also get the TableStyle from Table Style Dictionary and get tables with GetPersistentReactorIds()

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Getting Tables by name
« Reply #8 on: March 03, 2016, 03:18:44 PM »
Wowens,


You can find alot of helpful methods and extensions over at the Autocad Extension Library
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

WOWENS

  • Newt
  • Posts: 59
Re: Getting Tables by name
« Reply #9 on: March 03, 2016, 05:00:36 PM »
Thanks to all for your help and guidance