How to iterate through Dictionaries collection?

Discussion in 'AutoCAD' started by zzhang2002, Jan 5, 2005.

  1. zzhang2002

    zzhang2002 Guest

    Hi! I am trying to create a VBA program to iterate through dictionaries collection and print out all dictionary names. However, the return value of Dictionaries.Item is not AcadDictionary. Rather it returns IAcadObject. But IAcadObject doesn't have a way to cast its object to AcadDictionary object. So the following code doesn' work.

    Dim dictCol As AcadDictionaries
    Dim iAcadObj As IAcadObject
    Dim dict As AcadDictionary
    Dim i As Integer

    Set dictCol = ThisDrawing.Dictionaries
    For i = 0 To dictCol.Count - 1
    Set iAcadObj = dictCol(i)
    Debug.Print vbTab & iAcadObj.ObjectName
    If iAcadObj.ObjectName = "AcDbDictionary" Then
    Set dict = iAcadObj
    End If
    Next i

    How can I fix it?

    David
     
    zzhang2002, Jan 5, 2005
    #1
  2. zzhang2002

    Jeff Mishler Guest

    Here's a response I made to a similar question over at the AUGI forums:
    Sub getDicts()
    Dim oDicts As AcadDictionaries
    Dim odict As AcadObject

    Set oDicts = ThisDrawing.Dictionaries
    For Each odict In oDicts
    On Error Resume Next
    Debug.Print odict.Name
    If Err.Number <> 0 Then
    Err.Clear
    Debug.Print "No name for this object: " & odict.ObjectName & " - ID:
    " & odict.ObjectID
    'must do this since Groups, and other, are part of the Dictionaries
    Collection but have no Name property
    End If
    Next
    End Sub

    Function DictExists(strName As String) As Boolean
    Dim odict As AcadObject
    On Error Resume Next
    Set odict = ThisDrawing.Dictionaries.Item(strName)
    If Err.Number <> 0 Then
    DictExists = False
    Else
    DictExists = True
    End If
    Err.Clear
    On Error GoTo 0

    End Function
     
    Jeff Mishler, Jan 5, 2005
    #2
  3. zzhang2002

    fantum Guest

    Dim dictCol As AcadDictionaries
    Dim iAcadObj As IAcadObject
    Dim dict As AcadDictionary
    Dim i As Integer

    Set dictCol = ThisDrawing.Dictionaries
    For i = 0 To dictCol.Count - 1
    Set iAcadObj = dictCol(i)
    Debug.Print vbTab & iAcadObj.ObjectName
    'If iAcadObj.ObjectName = "AcDbDictionary" Then
    If TypeOf iAcadObj Is AcadDictionary Then
    Set dict = iAcadObj
    End If
    Next i
     
    fantum, Jan 5, 2005
    #3
  4. zzhang2002

    zzhang2002 Guest

    Thanks, Jeff.

    It looks like some items in AcadDictionaries collection is not a AcadDictionary object.

    David
     
    zzhang2002, Jan 6, 2005
    #4
  5. zzhang2002

    zzhang2002 Guest

    After the mirable TypeOf operator, I can set an IAcadObj to AcadDictionary. This is cool. Thanks.

    So not all AcDbDictionary is AcadDictionary.

    David
     
    zzhang2002, Jan 6, 2005
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.