As a start....
I would save that allblocks variable differently. As you have it, what gets set into that variable isn't a selection set, but what the (sssetfirst) function returns, which is a list in which the selection is the second item:
(nil <Selection set: XXX>)
That will require an extra step to get that selection into a variable of its own, so that the routine can step through it.
I would save the selection set directly [in a little more concise way], and then if you want it highlighted as the rest of the routine does its thing [which shouldn't be necessary], do that:
(setq allblocks (ssget "_X" '((0 . "INSERT"))))
(sssetfirst nil allblocks)
That selection will include any Xrefs in the drawing -- would you have any of those, and if so, should they also be numbered?
That will also include things in all spaces. If you want only those in the current space [which makes labeling them easier], the (ssget) filter can limit the selection that way. Or if you want them in all spaces, the routine can be made to move around among them to label everything.
Say more about what you mean by "somewhere close to the Block" for a Text location. It would be easiest if the Text can have its insertion point at the Block's insertion point. But that assumes the insertion points are in some logical relationship to, and presumably within, the Blocks' content objects. If Blocks are not well defined, and some insertion points may be off in the hinterlands somewhere, that won't give helpful results. In such a case, it would be necessary to dig out the Block's bounding box and use that somehow to place the Text. But if they are well defined, let's take the example of a Block whose insertion point is in the center -- would it be acceptable to number it in the middle of it, or do you need the number outside but somewhere close? [That would also require bounding-box operations.] In what direction? How close? etc.... And would some Blocks be close enough to each other that a Text label "somewhere close to" one Block could overlap another Block? Is that a problem?
Kent Cooper, AIA