Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Sort and count block references

kinakira2013
Advocate

Sort and count block references

kinakira2013
Advocate
Advocate

Hi,

 

I have an ObjectIdCollection (of block references). I don't know how to sort and count block references via ObjectIdCollection.

Anyone can help me?

 

Thanks!

-Kin-

0 Likes
Reply
Accepted solutions (1)
666 Views
4 Replies
Replies (4)

_gile
Mentor
Mentor

Hi,

You should ellaborate what you mean with: "sort and count block references"



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes

kinakira2013
Advocate
Advocate

@_gile wrote:

Hi,

You should ellaborate what you mean with: "sort and count block references"


Hi _gile,

I mean that:

- Sort ascending follow the name of blocks (alpha, beta)

- Count: is the number of each type of block

I want to display a list name (sorted) and quantity of each block.

Example:

A1 : 10

A5 : 2

B3 : 2

C12 : 7

0 Likes

Anonymous
Not applicable

It looks like magic. You run the command, select everything and hit enter. Watch! There is a list of all blocks, sorted by name, including quantity. So what is this magic word? think.
The BCOUNT team is part of Express Tools, so if you are using AutoCAD LT you will not have any fun because the Express tools are not.

0 Likes

_gile
Mentor
Mentor
Accepted solution
  1. Create a new instance of Dictionary<string, int> where the key (string) will be the block name and the value (int) the number of blocks.
  2. Iterate through the ObjectId collection and, for each ObjectId:
    1. Open the block reference for read.
    2. Check if the dictionary contains a key equal to the block name:
      1. If the key is found, increment the value.
      2. Else create a new key with the block name and a value equal to 1.

This can also be done in a more declarative way using Linq queries (GroupBy, Count, ...).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub