Drawing a number for every block in a drawing

Drawing a number for every block in a drawing

thomas.schive
Enthusiast Enthusiast
1,700 Views
7 Replies
Message 1 of 8

Drawing a number for every block in a drawing

thomas.schive
Enthusiast
Enthusiast

Hello!

 

I have a dwg with a lot of blocks, and I'd like to do the following:

 

- Count all the blocks in the drawing

- Insert a number (1, 2, 3, 4 ... etc.) just as a text with an insertion point somewhere close to the block for every block found in the drawing. I think that the first could be specified by the user and then it could count itself.

- Create a new layer for the inserted numbers.

 

Any tips to get this to work as a LISP routine?

 

So far I've begun working with some pieces, as to setq all the blocks I used:

 

(setq allblocks
        (sssetfirst nil
            (ssget "X" (list (cons 0 "INSERT")))
        )

)

 

Which seems to work fine as to select all the blocks at least, allthough some VLA-functions might to the job better?

 

I'll also guess that the insert text-function could be solved using eval and append, but I'm not quite sure how yet ...

 

/ts

 

 

0 Likes
Accepted solutions (1)
1,701 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

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
Message 3 of 8

thomas.schive
Enthusiast
Enthusiast

Thanks.

 

There will be no blocks in xrefs here. Current space should be enough.

 

/ts

0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant

@thomas.schive wrote:

.... 

There will be no blocks in xrefs here. Current space should be enough.

 

....

I wasn't asking about Blocks in  Xrefs, but Xrefs themselves, which are also "INSERT"-class objects that (ssget) will find.  If there would be no Xrefs in the current space, we're good.

 

[And I think we crossed in the mail -- I added some more to the end of Post 2 in the meantime.]

Kent Cooper, AIA
0 Likes
Message 5 of 8

thomas.schive
Enthusiast
Enthusiast

Oh, my bad. None of those either. No xrefs in the current space.

 

/ts

0 Likes
Message 6 of 8

braudpat
Mentor
Mentor

 

Hello Thomas

 

Maybe this routine "B2P - Block2Point" could help you and can be a GOOD starting point of your routine !?

 

French Humour: it has been written a by Lisp Beginner !!

 

Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 7 of 8

thomas.schive
Enthusiast
Enthusiast

So far I've got something like this, that writes out my total amount of blocks in the beginning and a number for every place I click (getpoint) / so if I click at the blocks I'll get my number (+1 for every time):

 

(princ "Skriv TBP for aa kjoere LISPen")
(defun c:TBP ( / punkta)
(setq allblocks (ssget "X" (list (cons 0 "INSERT"))))
(setq T_count 0)
    (foreach block (ssnamex allblocks)
        (setq T_count (1+ T_count))
    )
(princ T_count)
(setq T_count 0)
    (while (setq
        punkta (getpoint "\nVelg punkt "))
            (setq T_count (1+ T_count))
            (eval (append '(command "._text" punkta)
                (if (zerop (cdr (assoc 40 (entget (tblobjname "style" (getvar 'textstyle))))))
                    '(0.12))
                    '(0 (rtos T_count 2 0)))))
(princ)
);_ defun

 

But I'd like it to skip the "getpoint"-part and just draw the numbers itself, and here's where I'm stuck.

 

/ts

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

Here's a way to do it [lightly tested and without some of the usual enhancements]:

 

(defun c:TBP (/ allblocks int n)
  (prompt "\nTo Label Blocks with incrementing numbers,")
  (if (setq allblocks (ssget "_X" (list '(0 . "INSERT") (cons 410 (getvar 'ctab)))))
    (if (setq int (getint "\nStarting number or <exit>: "))
      (progn
        (repeat (setq n (sslength allblocks)) ; then
          (command "_.text" "_style" "YourDesiredStyle"
            "_mc" "_none" (cdr (assoc 10 (entget (ssname allblocks (setq n (1- n))))))
              ;; Middle-Center-justified insertion point at Block's
            YourDesiredHeight 0 (itoa int); height, rotation & text content
;; omit height if Style is fixed-height ); command (setq int (1+ int)) ); repeat ); progn (prompt "\nFunction terminated at User request."); else ); if (prompt "\nNo Block(s) found in current space."); else ); if (princ) ); defun

That puts the numbers middle-center justified at the Blocks' insertion points.  See the various questions at the end of Post 2.

Kent Cooper, AIA