I am in the process of building my library of subroutines for .net programming with acad.
I have one that retrieves a table record, like a layer or linetype, based on the name or objectID.
I have only done it for the layer name so far, and want to mutate this so it looks up any kind of table object based on the name:
Public Function getLayerObjNet(ByVal name As String) As LayerTableRecord
Dim db As Database = HostApplicationServices.WorkingDatabase()
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim layTblRec As LayerTableRecord = Nothing
Using myT As Transaction = tm.StartTransaction()
Try
Dim layTbl As LayerTable = CType(myT.GetObject(db.LayerTableId, OpenMode.ForRead, False), LayerTable)
If layTbl.Has(name) Then
layTblRec = CType(myT.GetObject(layTbl.Item(name), OpenMode.ForRead), LayerTableRecord)
End If
myT.Commit()
Catch
layTblRec = Nothing
Finally
myT.Dispose()
End Try
End Using
Return layTblRec
End Function
I am wondering these things:
1) To make this work for ObjectID input, I think I can just make another function to set up an overload:
Public Function getLayerObjNet(ByVal oID as ObjectID) As LayerTableRecord
...
Is that a good way to do this? (may be the only way...)
2) Is my error checking ok, any comments welcome.
3) If I want to add a param to make this work for all tables, it would be like this:
Public Function getAnyObjNet(ByVal name As String, ByVal tableType As String) As LayerTableRecord
Now, of course, the return type cannot be LayerTableRecord, it has to switch to the type specified in the tableType param.
Is there an easy way to do this? Can I somehow tell it to look up the object type from a string name? Maybe that is what CType can do, I thought that only casts one object type to another though.
Once I know how to look up an object type with a string, I can make much more generic code, that adapts to the object types I am feeding in.
It would be great if there was something like:
GetObjectType("LayerTableRecord") because I would use a variable for the string of course. I have a feeling that is super easy...
thanks for any help.