.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to lookup table object based on name, and table name as params

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
306 Views, 3 Replies

How to lookup table object based on name, and table name as params

I am trying to make a function to take a name and table type, and return the record if found.
So this is what I have so far for the "generic" table function:

Public Function getAnyObjNet(ByVal name As String, ByVal tableType As String) As SymbolTableRecord
Dim db As Database = HostApplicationServices.WorkingDatabase()
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim symTblRec As SymbolTableRecord = Nothing
Using myT As Transaction = tm.StartTransaction()
Try
Dim symTbl As SymbolTable = CType(myT.GetObject(db.LayerTableId, OpenMode.ForRead, False), tableType)
'the above line does not work
'cannot use tableType as variable for object type, must use explicit name
'like LayerTable
'need ideas here
If symTbl.Has(name) Then
symTblRec = CType(myT.GetObject(symTbl.Item(name), OpenMode.ForRead), SymbolTableRecord)
'the above works but I would prefer to send back correct cast of object even though function says
'"As SymbolTableRecord.."
End If
myT.Commit()
Catch
symTblRec = Nothing
Finally
myT.Dispose()
End Try
End Using
Return symTblRec
End Function

So while I can stay generic in most places, I must specify what kind of table I want to look up at some point.
I am also wondering if I can return a specific object type like LayerTableRecord, even though my function says "as
SymbolTableRecord".

Any takers? Any comments welcome on the code, I am not experienced with .net error checking.

thanks
James Maeding
Civil Engineer and Programmer
jmaeding - athunsaker - com
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

I guess one idea is to use a conditional statement to issue the right ctype code, but that would not teach me
anything... 🙂

James Maeding
|>I am trying to make a function to take a name and table type, and return the record if found.
|>So this is what I have so far for the "generic" table function:
|>
|>Public Function getAnyObjNet(ByVal name As String, ByVal tableType As String) As SymbolTableRecord
|> Dim db As Database = HostApplicationServices.WorkingDatabase()
|> Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
|> Dim symTblRec As SymbolTableRecord = Nothing
|> Using myT As Transaction = tm.StartTransaction()
|> Try
|> Dim symTbl As SymbolTable = CType(myT.GetObject(db.LayerTableId, OpenMode.ForRead, False), tableType)
|> 'the above line does not work
|> 'cannot use tableType as variable for object type, must use explicit name
|> 'like LayerTable
|> 'need ideas here
|> If symTbl.Has(name) Then
|> symTblRec = CType(myT.GetObject(symTbl.Item(name), OpenMode.ForRead), SymbolTableRecord)
|> 'the above works but I would prefer to send back correct cast of object even though function says
|> '"As SymbolTableRecord.."
|> End If
|> myT.Commit()
|> Catch
|> symTblRec = Nothing
|> Finally
|> myT.Dispose()
|> End Try
|> End Using
|> Return symTblRec
|> End Function
|>
|>So while I can stay generic in most places, I must specify what kind of table I want to look up at some point.
|>I am also wondering if I can return a specific object type like LayerTableRecord, even though my function says "as
|>SymbolTableRecord".
|>
|>Any takers? Any comments welcome on the code, I am not experienced with .net error checking.
|>
|>thanks
|>James Maeding
|>Civil Engineer and Programmer
|>jmaeding - athunsaker - com
James Maeding
Civil Engineer and Programmer
jmaeding - athunsaker - com
Message 3 of 4
NathTay
in reply to: Anonymous

The attached function retrieves the ObjectId of the SymbolTableRecord. The function requires the ObjectId of the SymbolTable and the name of the SymbolTableRecord as as string.

The function finds the non-erased item when there are erased ones with the same name. It returns ObjectId.Null if the record does not exist.

Regards - Nathan
Message 4 of 4
Anonymous
in reply to: Anonymous

Thanks for the code.
That is a bit different than what my goal is though, although still very useful, since it requires a sub before (get
tableId) and after (cast to right type) to do its job.

The thing about that code is you have to first look up the specific table to feed into that code.
That defeats the thing I want to do which is have one function to look stuff up by name.
Dare I say "just like with the tblsearch function in lisp..." now I'm in trouble 🙂

You get the idea though, one function to adapt itself to the right object types based on the string input of:
getTableRecord(byval name as string, byval tablename as string).

It all comes down to the idea of using variables for object types.
Is there a way to dim a variable to a type stored in a string var?
like:
dim myVar as TypeVar
where TypeVar is a string containing the object class type, like string, or integer...
thx

Nathan Taylor <>
|>The attached function retrieves the ObjectId of the SymbolTableRecord. The function requires the ObjectId of the SymbolTable and the name of the SymbolTableRecord as as string.
|>
|>The function finds the non-erased item when there are erased ones with the same name. It returns ObjectId.Null if the record does not exist.
|>
|>Regards - Nathan
James Maeding
Civil Engineer and Programmer
jmaeding - athunsaker - com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost