Author Topic: eKeyNotFound  (Read 519 times)

0 Members and 1 Guest are viewing this topic.

poncelet

  • Newt
  • Posts: 51
eKeyNotFound
« on: April 25, 2012, 01:49:53 pm »
Hello guys, i'm trying to create a class to manipulate layers. I got a eKeyNotFound when i dimension xr.
Code - vb.net: [Select]
  1.    Public Sub IsolateLayerType(ByVal Name As String)
  2.  
  3.        Using trans As Transaction = db.TransactionManager.StartTransaction()
  4.            Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
  5.            Dim v() As TypedValue
  6.  
  7.            For Each id As ObjectId In lt
  8.                Dim ltr As LayerTableRecord
  9.                ltr = trans.GetObject(id, OpenMode.ForWrite)
  10.  
  11.                Dim Dict As DBDictionary = trans.GetObject(ltr.ExtensionDictionary, OpenMode.ForRead, False)
  12.                Dim xr As Xrecord = trans.GetObject(Dict.GetAt("LayerType"), OpenMode.ForRead)
  13.  
  14.                v = xr.Data.AsArray
  15.                If v(0).Value.ToString = Name Then
  16.                    ltr.IsOff = False
  17.                Else
  18.                    ltr.IsOff = True
  19.                End If
  20.            Next
  21.  
  22.        End Using
  23.  
  24.    End Sub
  25.  
Ce que femme veut, Dieu le veut.

TJK44

  • Newt
  • Posts: 161
Re: eKeyNotFound
« Reply #1 on: April 26, 2012, 04:00:26 pm »
I believe this is because "LayerType" does not exist in the dictionary. You should check if the dictionary contains a record before trying to use the record.

fixo

  • Swamp Rat
  • Posts: 783
  • Pietari, Venäjä
Re: eKeyNotFound
« Reply #2 on: April 26, 2012, 11:38:24 pm »
Not sure about if this helps but you may want to use
Try...Catch statement and also add some check to recognize
object state, something like:
Code - vb.net: [Select]
  1.  <CommandMethod("iso")> _
  2.        Public Sub TestIso()
  3.            IsolateLayerType("0")
  4.        End Sub
  5.        Public Sub IsolateLayerType(ByVal Name As String)
  6.            Dim doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  7.            Dim ed = doc.Editor
  8.            Dim db = doc.Database
  9.            Dim result As Boolean = False
  10.            Try
  11.                Using trans As Transaction = db.TransactionManager.StartTransaction()
  12.  
  13.                    Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
  14.                    Dim v() As TypedValue
  15.                    For Each id As ObjectId In lt
  16.  
  17.                        Dim ltr As LayerTableRecord
  18.                        ltr = trans.GetObject(id, OpenMode.ForWrite)
  19.                        If db.Clayer = id Then
  20.                            Continue For
  21.                        End If
  22.  
  23.                        Dim Dict As DBDictionary = trans.GetObject(ltr.ExtensionDictionary, OpenMode.ForRead, False)
  24.                        Dim xr As Xrecord = TryCast(trans.GetObject(Dict.GetAt("LayerType"), OpenMode.ForRead), Xrecord)
  25.                        If xr Is Nothing Then
  26.                            ed.WriteMessage(vbLf + "Key not found")
  27.                            result = False
  28.                            Return
  29.                        End If
  30.                        v = xr.Data.AsArray
  31.                        If v(0).Value.ToString = Name Then
  32.  
  33.                            ltr.IsOff = False
  34.                        Else
  35.                            ltr.IsOff = True
  36.                        End If
  37.                        result = True
  38.                    Next
  39.                    trans.Commit()
  40.                End Using
  41.  
  42.            Catch ex As System.Exception
  43.                ed.WriteMessage(ex.ToString)
  44.            Finally
  45.                ed.WriteMessage(vbLf + "Program ended up with result of: {0}", result)
  46.            End Try
  47.        End Sub


~'J'~


edit:kdub->code = vbnet
« Last Edit: April 27, 2012, 02:04:28 am by Kerry »
\\\"Always drink upstream from the herd."\\\ - Will Rogers, was died in 1935 plane crash

--> Donate to TheSwamp <--

TJK44

  • Newt
  • Posts: 161
Re: eKeyNotFound
« Reply #3 on: April 27, 2012, 08:19:10 am »
Quote
If xr Is Nothing Then
                           ed.WriteMessage(vbLf + "Key not found")
                           result = False
                           Return
                       End If

fixo, correct me if I'm wrong but I don't think you'll ever reach this bit of code if the key is not found because there will be an error when trying to Dim xr if the record does not exist. I think the better way to do this is check if the dictionary contains the key and if it does then use it. Sorry if I'm wrong on this... I havent been programming CAD very long. Just my two cents.

poncelet

  • Newt
  • Posts: 51
Re: eKeyNotFound
« Reply #4 on: April 27, 2012, 09:37:06 am »
Thanks guys. You were right. Didn't check if xrecord exists.

Code - vb.net: [Select]
  1.    Public Sub IsolateLayerType(ByVal Name As String)
  2.  
  3.        Using trans As Transaction = db.TransactionManager.StartTransaction()
  4.            Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
  5.            Dim v() As TypedValue
  6.  
  7.            For Each id As ObjectId In lt
  8.                Dim ltr As LayerTableRecord
  9.                ltr = trans.GetObject(id, OpenMode.ForWrite)
  10.  
  11.                If ltr.ExtensionDictionary = ObjectId.Null Then
  12.                    ltr.IsOff = True
  13.                    Continue For
  14.                End If
  15.  
  16.                Dim Dict As DBDictionary = trans.GetObject(ltr.ExtensionDictionary, OpenMode.ForRead, False)
  17.  
  18.                If Not Dict.Contains("LayerType") Then
  19.                    ltr.IsOff = True
  20.                    Continue For
  21.                End If
  22.  
  23.                Dim xr As Xrecord = trans.GetObject(Dict.GetAt("LayerType"), OpenMode.ForRead)
  24.  
  25.                v = xr.Data.AsArray
  26.                If v(0).Value.ToString = Name Then
  27.                    ltr.IsOff = False
  28.                Else
  29.                    ltr.IsOff = True
  30.                End If
  31.            Next
  32.  
  33.            trans.Commit()
  34.        End Using
  35.  
  36.    End Sub
Ce que femme veut, Dieu le veut.

fixo

  • Swamp Rat
  • Posts: 783
  • Pietari, Venäjä
Re: eKeyNotFound
« Reply #5 on: April 27, 2012, 01:25:14 pm »
Quote
If xr Is Nothing Then
                           ed.WriteMessage(vbLf + "Key not found")
                           result = False
                           Return
                       End If

fixo, correct me if I'm wrong but I don't think you'll ever reach this bit of code if the key is not found because there will be an error when trying to Dim xr if the record does not exist. I think the better way to do this is check if the dictionary contains the key and if it does then use it. Sorry if I'm wrong on this... I havent been programming CAD very long. Just my two cents.
I think you are completely right, firstly we need to check if dictionary exists, then go further..
Regards,

~'J'~
\\\"Always drink upstream from the herd."\\\ - Will Rogers, was died in 1935 plane crash

--> Donate to TheSwamp <--

fixo

  • Swamp Rat
  • Posts: 783
  • Pietari, Venäjä
Re: eKeyNotFound
« Reply #6 on: April 27, 2012, 01:26:48 pm »
Thanks guys. You were right. Didn't check if xrecord exists.

Glad if this helps
Cheers :)

~'J'~
\\\"Always drink upstream from the herd."\\\ - Will Rogers, was died in 1935 plane crash

--> Donate to TheSwamp <--