You may want to give more detail on what "with no success" mean. Do you mean that you do know there are block references with give block name existing in the drawing, but the code does not find it (thus, function returns the list with Count=0)?
If that is the case, then there could be 2 causes, seeing from your code:
1. the code compares block reference's name against a list of known block names here:
If listOfBlockRefNames.Contains(blockRef.Name) Then
The reason of this code being problematic is the block names in the list and the block reference's name could be literally the same but in difference case (upper or lower), then the condition in the "If..." could return False.
2. You did not mention, but if the block in interest is dynamic block, the block reference's name could be an anonymous block name, generated by AutoCAD. So, if the block may possibly be a dynamic one, you need to find its "effective" name, not BlockReference.Name.
Also, since you only are interested in a given set of block names, you can loop through the block name list, instead of loop through entire block record table. This will make the code logic easier to follow. Something like:
Private Function GetBlockReferenceFromList(db as Database, tran As Trsnaction, blkNames as List(of String)) As ...
Dim listOfBlockRef As New List(of BlockReference)
Dim blockTable as BlockTable=....
For Each blkName As String in blkNames
If blockTable.Has(blkName) Then
Dim brefs=GetBlockRefs(tran, blockTable(blkName))
If brefs.Count>0 Then
listOfBlockref.AddRange(brefs)
End If
End If
Next
Return lisOfBlockRef
End Function
Private Function GetBlockRefs(tran as Transaction, blkId as ObjectId) As List(of BlockReference)
Dim brefs As New List(of BlockReference)()
Dim blockRecord=DirectCast(tran.GetObject(blkId,OpenMode.ForRead), BlockTableRecord)
''Get direct BlockReference
Dim blks=FindBlockReferences(tran, blockRecord)
If blks.Count>0 Then brefs.AddRange(blks)
''If the block is dynamic
If blkRecord.IsDynamic Then
Dim anonyBlkIds=blockRecord.GetAnonymousBlockIds()
If anonyBlkIds IsNot Nothing AndAlso anonyBlkIds.Count>0 Then
For Each blkId As ObjectId in anonyBlkIds
Dim anonyBlkRecord=DirectCast(tran.GetObject(blkId,OpenMode.ForRead), BlockTableRecord)
blks=FindBlockReferences(tran, anonyBlkRecord)
If blks.Count>0 Then brefs.AddRange(blks)
End If
End If
End if
Return brefs
End Function
Private Function FindBlockreferences(tran As Transaction, blkRecord As BlockTableRecord) As List(of BlockReference)
Dim brefs As New List(of BlockReference)()
Dim idCollection=blkRecord.GetBlockReferenceIds(True, False)
If idCollection IsNot Nothing AndAlso idCollection.Count>0 Then
For Each id As ObjectId in idCollection
bref.Add((DirectCast(tran.GetObject(id, OpenMode.ForRead), Blockreference))
Next
End If
Return brefs
End Function
The code is not tested, but you get the idea:
1. Use BlockTable.Has() to see if a block definition with particular name exists in the database. Here you avoid having to compare block name by yourself (eliminating potential character's CASE issue); With BlockTable.Has() method while loop through your block name list, you only go deeper, if the block name is indeed a defined BlockTableRecord;
2. If the blocks in interest could be dynamic block, you must make sure the corresponding anonymous block references are also searched
HTH