Search code examples
c#.netautocadautocad-plugin

C#.NET Autocad 2021 API: Accessing specific BlockTableRecords within a given BlockTable?


I'm looking for advice regarding the particulars of the BlockTable class. I know that although you can get an enumerator for all the blocktablerecords a blocktable contains, I can't seem to find a more efficient way of accessing a particular BlockTableRecord's ObjectID. I know that the computer has access to such information... I can't imagine the BlockTable.Has() function working without it, anyway. I've checked the documentation too, but it doesn't actually list the functions or properties of the BlockTable class anywhere :/

My current code relies on a user-inputted string to retrieve the block, which successfully returns the proper bool result when plugged into BlockTable.Has().

Any help is appreciated! Tyler


Solution

  • BlockTable wrapper implements an indexer which can be used with either a string or ObjectId. i.e. myBlkTable["theBlockName"]

    Note: there is no guarantee that this is "efficient", and may just be enumerating the array under the hood. You'll want to run some time trials to see what is more efficient.

    It is always good to ask yourself whether the micro-optimization you're looking for is worth the time you're spending on it. Sounds like you are just working on a user command, and even if there was an efficient lookup, would that make any difference for the user experience? The answer is probably no. This kind of thing only becomes important with massive collections that get queried repeatedly in a loop.