Does dictionary exist?

Discussion in 'AutoCAD' started by Matt W, Oct 13, 2004.

  1. Matt W

    Matt W Guest

    Quick question: What's the best way to determine if a dictionary (an
    ACADDICTIONARY object) exists?
    Right now I'm using an On Error Goto statement but I was wondering if
    there's a better way (kind of like TBLSEARCH in LSP)?

    I haven't seen anything yet, so I'm inclined to think that what I'm doing
    currently is the best way. Am I wrong??



    ACAD 2005
     
    Matt W, Oct 13, 2004
    #1
  2. Matt..Dictionaries have and Exists Method

    dict.Exists

    Schilling...OUCH><
     
    Paul Richardson, Oct 13, 2004
    #2
  3. Matt W

    Matt W Guest

    Thanks!
    I didn't even see that.


    I had to turn it off after the 1st I was so disgusted.
    I decided to watch Bob the Builder with my son instead. :)

    Let's hope Pedro can stop the bleeding tonight.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Matt..Dictionaries have and Exists Method
    |
    | dict.Exists
    |
    | Schilling...OUCH><
    |
    | | > Quick question: What's the best way to determine if a dictionary (an
    | > ACADDICTIONARY object) exists?
    | > Right now I'm using an On Error Goto statement but I was wondering if
    | > there's a better way (kind of like TBLSEARCH in LSP)?
    | >
    | > I haven't seen anything yet, so I'm inclined to think that what I'm
    doing
    | > currently is the best way. Am I wrong??
    | >
    | >
    | >
    | > ACAD 2005
    | >
    | > --
    | > I support two teams: the Red Sox and whoever beats the Yankees.
    | >
    | >
    | >
    |
    |
     
    Matt W, Oct 13, 2004
    #3
  4. Matt W

    Matt W Guest

    Okay... I've searched high and low...

    Where is the Exists method??
    I can't find it.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Matt..Dictionaries have and Exists Method
    |
    | dict.Exists
    |
    | Schilling...OUCH><
    |
    | | > Quick question: What's the best way to determine if a dictionary (an
    | > ACADDICTIONARY object) exists?
    | > Right now I'm using an On Error Goto statement but I was wondering if
    | > there's a better way (kind of like TBLSEARCH in LSP)?
    | >
    | > I haven't seen anything yet, so I'm inclined to think that what I'm
    doing
    | > currently is the best way. Am I wrong??
    | >
    | >
    | >
    | > ACAD 2005
    | >
    | > --
    | > I support two teams: the Red Sox and whoever beats the Yankees.
    | >
    | >
    | >
    |
    |
     
    Matt W, Oct 13, 2004
    #4
  5. Paul was talking about the Scripting runtime dictionary, not AutoCAD's
    dictionary.

    Any particular reason why you are checking to see if it exists?

    --
    R. Robert Bell


    Okay... I've searched high and low...

    Where is the Exists method??
    I can't find it.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Matt..Dictionaries have and Exists Method
    |
    | dict.Exists
    |
    | Schilling...OUCH><
    |
    | | > Quick question: What's the best way to determine if a dictionary (an
    | > ACADDICTIONARY object) exists?
    | > Right now I'm using an On Error Goto statement but I was wondering if
    | > there's a better way (kind of like TBLSEARCH in LSP)?
    | >
    | > I haven't seen anything yet, so I'm inclined to think that what I'm
    doing
    | > currently is the best way. Am I wrong??
    | >
    | >
    | >
    | > ACAD 2005
    | >
    | > --
    | > I support two teams: the Red Sox and whoever beats the Yankees.
    | >
    | >
    | >
    |
    |
     
    R. Robert Bell, Oct 13, 2004
    #5
  6. Sorry, *Exists* only works for the Items in the Dictionary...
     
    Paul Richardson, Oct 13, 2004
    #6
  7. all the samples I have use On Error...
     
    Paul Richardson, Oct 13, 2004
    #7
  8. Matt W

    Matt W Guest

    That's what I thought.

    If the dictionary doesn't exist, I'd like the program to do one thing. If
    it does exist, do something else.
    I was just wondering if there was a better way than what I'm currently doing
    (On Error....)

    Thanks guys!

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Paul was talking about the Scripting runtime dictionary, not AutoCAD's
    | dictionary.
    |
    | Any particular reason why you are checking to see if it exists?
    |
    | --
    | R. Robert Bell
     
    Matt W, Oct 13, 2004
    #8
  9. I'm asking because you *are* aware that the Add method will return the
    Dictionary object if it already exists, aren't you?

    --
    R. Robert Bell


    That's what I thought.

    If the dictionary doesn't exist, I'd like the program to do one thing. If
    it does exist, do something else.
    I was just wondering if there was a better way than what I'm currently doing
    (On Error....)
     
    R. Robert Bell, Oct 13, 2004
    #9
  10. Matt W

    Matt W Guest

    Okay... You lost me.
    If you use the Add method, then the dictionary *will* exist.
    Wha...??? I'm not following you.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | I'm asking because you *are* aware that the Add method will return the
    | Dictionary object if it already exists, aren't you?
    |
    | --
    | R. Robert Bell
    |
    |
    | | That's what I thought.
    |
    | If the dictionary doesn't exist, I'd like the program to do one thing. If
    | it does exist, do something else.
    | I was just wondering if there was a better way than what I'm currently
    doing
    | (On Error....)
    |
    |
    |
     
    Matt W, Oct 13, 2004
    #10
  11. Well, in most cases where someone is concerned if a dictionary exists it is
    so that they don't use the Add method "accidentally" to create a dictionary
    that already exists. But in that context it doesn't matter, Add blissfully
    returns a dictionary in either case.

    What I do is use the Add method without checking for dictionary existence,
    but then perform the "exists" check on the XRecord data itself.

    --
    R. Robert Bell


    Okay... You lost me.
    If you use the Add method, then the dictionary *will* exist.
    Wha...??? I'm not following you.

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | I'm asking because you *are* aware that the Add method will return the
    | Dictionary object if it already exists, aren't you?
    |
    | --
    | R. Robert Bell
    |
    |
    | | That's what I thought.
    |
    | If the dictionary doesn't exist, I'd like the program to do one thing. If
    | it does exist, do something else.
    | I was just wondering if there was a better way than what I'm currently
    doing
    | (On Error....)
    |
    |
    |
     
    R. Robert Bell, Oct 13, 2004
    #11
  12. Matt W

    Matt W Guest

    Okaaaaay... Now I'm following you.
    Since I've got you on the line, let me ask you this...

    I've started dabbling with dictionaries recently in an effort to create a
    kind of layer management system.
    I've got two buttons on a toolbar; one that saves the current layer state,
    the other restores it after freezing all layers, thus, leaving you with only
    the layers you *want* to see.

    So my question is... Is there a better, more efficient way to do this? Would
    you suggest *not* using a dictionary??

    Here's what I've got.

    Code:
    Option Explicit
    
    Public Const strErrPath = "S:\CADDStds\Error_Logs\"
    
    
    Public Sub SaveLayerState()
    Dim oXrec As AcadXRecord
    Dim oDict As AcadDictionary
    Dim oLayer As AcadLayer
    Dim vType() As Integer
    Dim vData() As Variant
    Dim i As Integer
    
    On Error GoTo ErrMsg
    
    ReDim Preserve vData(0 To i)
    ReDim Preserve vType(0 To i)
    
    Set oDict = ThisDrawing.Dictionaries.Add("BVH_LAYERS")
    For Each oLayer In ThisDrawing.Layers
    vType(i) = 1
    vData(i) = oLayer.Name & "~!~" & oLayer.TrueColor.ColorIndex & "~!~"
    & oLayer.Linetype & "~!~" & oLayer.Plottable & "~!~" & oLayer.Lock & "~!~" &
    oLayer.LayerOn & "~!~" & oLayer.Freeze
    Set oXrec = oDict.AddXRecord("BVH_LAYER_STATES")
    oXrec.SetXRecordData vType, vData
    i = i + 1
    ReDim Preserve vData(0 To i)
    ReDim Preserve vType(0 To i)
    Next
    
    Set oXrec = Nothing
    Exit Sub
    
    ErrMsg:
    WriteError Err.Number, Err.Description, "Layer_State.dvb :: modMain.Main
    :: SaveLayerState", Now, ""
    End Sub
    
    Public Sub RestoreLayerState()
    Dim oLayer As AcadLayer
    Dim color As AcadAcCmColor
    Dim varResult As Variant
    Dim myDict As AcadDictionary
    Dim i As Integer
    Dim myXRec As AcadXRecord
    Dim dxfCode, dxfData
    Dim iLayer As Integer
    
    On Error GoTo ErrMsg
    
    Set myDict = ThisDrawing.Dictionaries.Item("BVH_LAYERS")
    Set myXRec = myDict.Item("BVH_LAYER_STATES")
    myXRec.GetXRecordData dxfCode, dxfData
    
    ThisDrawing.SetVariable "regenmode", 0
    
    On Error Resume Next
    For Each oLayer In ThisDrawing.Layers
    oLayer.Freeze = True
    oLayer.LayerOn = False
    Next oLayer
    
    For i = LBound(dxfData) To UBound(dxfData)
    varResult = Split(dxfData(i), "~!~", , vbTextCompare)
    Set color = New AcadAcCmColor
    With color
    .ColorMethod = acColorMethodByACI
    .ColorIndex = varResult(1)
    End With
    Set oLayer = ThisDrawing.Layers(varResult(0))
    With oLayer
    .TrueColor = color
    .Linetype = varResult(2)
    .Plottable = varResult(3)
    .Lock = varResult(4)
    .LayerOn = varResult(5)
    .Freeze = varResult(6)
    End With
    Next i
    ThisDrawing.SetVariable "regenmode", 1
    ThisDrawing.Regen acAllViewports
    Exit Sub
    
    ErrMsg:
    WriteError Err.Number, Err.Description, "Layer_State.dvb :: modMain.Main
    :: RestoreLayerState", Now, ""
    Resume Next
    End Sub
    
    Public Sub WriteError(iErrNum As Integer, sDesc As String, sSource As
    String, sDate As String, sOptionalInfo As String)
    '// Writes all errors to err.log
    Dim F As Integer
    F = FreeFile
    Open strErrPath & Format(Now, "mm-dd-yy__hh.mm.ss") & ".log" For Output
    As #F
    Print #F, "Error Number: " & iErrNum
    Print #F, "Description: " & sDesc
    Print #F, "Source: " & sSource
    Print #F, "Date: " & sDate
    Print #F, "More Info: " & sOptionalInfo
    Print #F, ""
    Close #F
    End Sub
    
    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Well, in most cases where someone is concerned if a dictionary exists it
    is
    | so that they don't use the Add method "accidentally" to create a
    dictionary
    | that already exists. But in that context it doesn't matter, Add blissfully
    | returns a dictionary in either case.
    |
    | What I do is use the Add method without checking for dictionary existence,
    | but then perform the "exists" check on the XRecord data itself.
    |
    | --
    | R. Robert Bell
     
    Matt W, Oct 13, 2004
    #12
  13. Any reason you are not using the LayerStateManger object? (I might be
    missing something.)

    Your ReDim Preserve in the loop is going to kill performance. Use the
    Layer.Count property to ReDim the array before going into the loop, removing
    the need for a ReDim at each iteration.

    --
    R. Robert Bell


    Okaaaaay... Now I'm following you.
    Since I've got you on the line, let me ask you this...

    I've started dabbling with dictionaries recently in an effort to create a
    kind of layer management system.
    I've got two buttons on a toolbar; one that saves the current layer state,
    the other restores it after freezing all layers, thus, leaving you with only
    the layers you *want* to see.

    So my question is... Is there a better, more efficient way to do this? Would
    you suggest *not* using a dictionary??

    Here's what I've got.

    Code:
    Option Explicit
    
    Public Const strErrPath = "S:\CADDStds\Error_Logs\"
    
    
    Public Sub SaveLayerState()
    Dim oXrec As AcadXRecord
    Dim oDict As AcadDictionary
    Dim oLayer As AcadLayer
    Dim vType() As Integer
    Dim vData() As Variant
    Dim i As Integer
    
    On Error GoTo ErrMsg
    
    ReDim Preserve vData(0 To i)
    ReDim Preserve vType(0 To i)
    
    Set oDict = ThisDrawing.Dictionaries.Add("BVH_LAYERS")
    For Each oLayer In ThisDrawing.Layers
    vType(i) = 1
    vData(i) = oLayer.Name & "~!~" & oLayer.TrueColor.ColorIndex & "~!~"
    & oLayer.Linetype & "~!~" & oLayer.Plottable & "~!~" & oLayer.Lock & "~!~" &
    oLayer.LayerOn & "~!~" & oLayer.Freeze
    Set oXrec = oDict.AddXRecord("BVH_LAYER_STATES")
    oXrec.SetXRecordData vType, vData
    i = i + 1
    ReDim Preserve vData(0 To i)
    ReDim Preserve vType(0 To i)
    Next
    
    Set oXrec = Nothing
    Exit Sub
    
    ErrMsg:
    WriteError Err.Number, Err.Description, "Layer_State.dvb :: modMain.Main
    :: SaveLayerState", Now, ""
    End Sub
    
    Public Sub RestoreLayerState()
    Dim oLayer As AcadLayer
    Dim color As AcadAcCmColor
    Dim varResult As Variant
    Dim myDict As AcadDictionary
    Dim i As Integer
    Dim myXRec As AcadXRecord
    Dim dxfCode, dxfData
    Dim iLayer As Integer
    
    On Error GoTo ErrMsg
    
    Set myDict = ThisDrawing.Dictionaries.Item("BVH_LAYERS")
    Set myXRec = myDict.Item("BVH_LAYER_STATES")
    myXRec.GetXRecordData dxfCode, dxfData
    
    ThisDrawing.SetVariable "regenmode", 0
    
    On Error Resume Next
    For Each oLayer In ThisDrawing.Layers
    oLayer.Freeze = True
    oLayer.LayerOn = False
    Next oLayer
    
    For i = LBound(dxfData) To UBound(dxfData)
    varResult = Split(dxfData(i), "~!~", , vbTextCompare)
    Set color = New AcadAcCmColor
    With color
    .ColorMethod = acColorMethodByACI
    .ColorIndex = varResult(1)
    End With
    Set oLayer = ThisDrawing.Layers(varResult(0))
    With oLayer
    .TrueColor = color
    .Linetype = varResult(2)
    .Plottable = varResult(3)
    .Lock = varResult(4)
    .LayerOn = varResult(5)
    .Freeze = varResult(6)
    End With
    Next i
    ThisDrawing.SetVariable "regenmode", 1
    ThisDrawing.Regen acAllViewports
    Exit Sub
    
    ErrMsg:
    WriteError Err.Number, Err.Description, "Layer_State.dvb :: modMain.Main
    :: RestoreLayerState", Now, ""
    Resume Next
    End Sub
    
    Public Sub WriteError(iErrNum As Integer, sDesc As String, sSource As
    String, sDate As String, sOptionalInfo As String)
    '// Writes all errors to err.log
    Dim F As Integer
    F = FreeFile
    Open strErrPath & Format(Now, "mm-dd-yy__hh.mm.ss") & ".log" For Output
    As #F
    Print #F, "Error Number: " & iErrNum
    Print #F, "Description: " & sDesc
    Print #F, "Source: " & sSource
    Print #F, "Date: " & sDate
    Print #F, "More Info: " & sOptionalInfo
    Print #F, ""
    Close #F
    End Sub
    
    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Well, in most cases where someone is concerned if a dictionary exists it
    is
    | so that they don't use the Add method "accidentally" to create a
    dictionary
    | that already exists. But in that context it doesn't matter, Add blissfully
    | returns a dictionary in either case.
    |
    | What I do is use the Add method without checking for dictionary existence,
    | but then perform the "exists" check on the XRecord data itself.
    |
    | --
    | R. Robert Bell
     
    R. Robert Bell, Oct 13, 2004
    #13
  14. Matt W

    Matt W Guest

    I didn't know anything about it until right now. :)

    Plus we have a tendency in this office to clean our drawings quite
    frequently. There's a little program we use that will purge the drawing a
    couple of times, run an audit and fix any errors that are found, remove
    annonymous groups, and last but not least, it gets rid of those pesky layer
    states that have a way of showing up in our drawings.

    The layer state removal portion is pretty slick. Have you ever seen it
    before??!?
    I found it on the NG written by some guy... I can't remember his name... Bob
    Rell?? Robert something I think...
    The name eludes me. ;)

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    | Any reason you are not using the LayerStateManger object? (I might be
    | missing something.)
    |
    | Your ReDim Preserve in the loop is going to kill performance. Use the
    | Layer.Count property to ReDim the array before going into the loop,
    removing
    | the need for a ReDim at each iteration.
    |
    | --
    | R. Robert Bell
     
    Matt W, Oct 13, 2004
    #14
  15. Well you *must* be confusing me with someone else, since I've only written
    stuff to delete layer _filters_, not states… BwaHaHa!!!

    --
    R. Robert Bell


    I didn't know anything about it until right now. :)

    Plus we have a tendency in this office to clean our drawings quite
    frequently. There's a little program we use that will purge the drawing a
    couple of times, run an audit and fix any errors that are found, remove
    annonymous groups, and last but not least, it gets rid of those pesky layer
    states that have a way of showing up in our drawings.

    The layer state removal portion is pretty slick. Have you ever seen it
    before??!?
    I found it on the NG written by some guy... I can't remember his name... Bob
    Rell?? Robert something I think...
    The name eludes me. ;)
     
    R. Robert Bell, Oct 13, 2004
    #15
  16. Matt W

    Matt W Guest

    Ahhh.... touché
    So tell me a little more about this layer states thing.
    Is it a reference to a DLL??
    How does it work and what is it exactly??
    Is it like the Snapshots in the layer manager??


    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    "R. Robert Bell" wrote in message
    | Well you *must* be confusing me with someone else, since I've only written
    | stuff to delete layer _filters_, not states… BwaHaHa!!!
    |
    | --
    | R. Robert Bell
    |
    |
    | "Matt W" wrote in message
    | | I didn't know anything about it until right now. :)
    |
    | Plus we have a tendency in this office to clean our drawings quite
    | frequently. There's a little program we use that will purge the drawing a
    | couple of times, run an audit and fix any errors that are found, remove
    | annonymous groups, and last but not least, it gets rid of those pesky
    layer
    | states that have a way of showing up in our drawings.
    |
    | The layer state removal portion is pretty slick. Have you ever seen it
    | before??!?
    | I found it on the NG written by some guy... I can't remember his name...
    Bob
    | Rell?? Robert something I think...
    | The name eludes me. ;)
    |
    |
    |
     
    Matt W, Oct 13, 2004
    #16
  17. Not that anyone has asked, but I don't like this feature :-(

    Although I too have code that utilizes it.
     
    Bobby C. Jones, Oct 13, 2004
    #17
  18. Matt W

    Matt W Guest

    Don't like what feature: Dictionaries??

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    "Bobby C. Jones" <bjones (at) beazer (dot) com> wrote in message
    | Not that anyone has asked, but I don't like this feature :-(
    |
    | Although I too have code that utilizes it.
    | --
    | Bobby C. Jones
    |
    | | > I'm asking because you *are* aware that the Add method will return the
    | > Dictionary object if it already exists, aren't you?
    | >
    | > --
    | > R. Robert Bell
    | >
    | >
    | > | > That's what I thought.
    | >
    | > If the dictionary doesn't exist, I'd like the program to do one thing.
    If
    | > it does exist, do something else.
    | > I was just wondering if there was a better way than what I'm currently
    | doing
    | > (On Error....)
    | >
    | >
    | >
    |
    |
     
    Matt W, Oct 13, 2004
    #18
  19. Bobby is whining that Add should throw an exception if the object already
    exists.

    Granted, it should, as most Add methods will do this. But Bobby has caved to
    expedience, just as I have. <esg>

    --
    R. Robert Bell


    Don't like what feature: Dictionaries??

    --
    I support two teams: the Red Sox and whoever beats the Yankees.


    "Bobby C. Jones" <bjones (at) beazer (dot) com> wrote in message
    | Not that anyone has asked, but I don't like this feature :-(
    |
    | Although I too have code that utilizes it.
    | --
    | Bobby C. Jones
     
    R. Robert Bell, Oct 13, 2004
    #19
  20. Nope. Dictionaries I like. The fact that the Add method will return an
    existing item in a collection, I don't like.
     
    Bobby C. Jones, Oct 13, 2004
    #20
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.