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:
<CommandMethod("iso")> _
Public Sub TestIso()
IsolateLayerType("0")
End Sub
Public Sub IsolateLayerType(ByVal Name As String)
Dim doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed = doc.Editor
Dim db = doc.Database
Dim result As Boolean = False
Try
Using trans As Transaction = db.TransactionManager.StartTransaction()
Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
Dim v() As TypedValue
For Each id As ObjectId In lt
Dim ltr As LayerTableRecord
ltr = trans.GetObject(id, OpenMode.ForWrite)
If db.Clayer = id Then
Continue For
End If
Dim Dict As DBDictionary = trans.GetObject(ltr.ExtensionDictionary, OpenMode.ForRead, False)
Dim xr As Xrecord = TryCast(trans.GetObject(Dict.GetAt("LayerType"), OpenMode.ForRead), Xrecord)
If xr Is Nothing Then
ed.WriteMessage(vbLf + "Key not found")
result = False
Return
End If
v = xr.Data.AsArray
If v(0).Value.ToString = Name Then
ltr.IsOff = False
Else
ltr.IsOff = True
End If
result = True
Next
trans.Commit()
End Using
Catch ex As System.Exception
ed.WriteMessage(ex.ToString)
Finally
ed.WriteMessage(vbLf + "Program ended up with result of: {0}", result)
End Try
End Sub
~'J'~edit:kdub->code = vbnet