Jump to content

Counting Blocks


Michaels

Recommended Posts

Hello there,

 

I wonder why this routine is not counting my blocks ,

(defun c:Bcount (/ Blk)
 (setq Blk (ssget "_x" '((0 . "BLOCK"))))
 (alert (strcat "You have :" (sslength Blk) "Blocks .."))
 (princ)
 )

 

Any help.

 

Thanks

Link to comment
Share on other sites

  • Replies 33
  • Created
  • Last Reply

Top Posters In This Topic

  • BlackBox

    13

  • Lee Mac

    10

  • Michaels

    8

  • David Bethel

    3

Top Posters In This Topic

Hint: You need to be counting INSERTS, not blocks... a block is the definition, the insert is the 'block reference' a representation of the definition in the drawing.

 

Also, you will need to allow for this:

(sslength nil)

Link to comment
Share on other sites

YES. great hint Lee.

 

I modified it, but it gives me an error,

 

; error: bad argument type: stringp 12

 

Why is that please ?

Link to comment
Share on other sites

This may be what you need:

 

(defun c:Bcount (/ Blk)
(setq Blk (ssget "_x" '((0 . "[color=red]INSERT[/color]"))))
(alert (strcat "You have :" [color=red](itoa [/color](sslength Blk)[color=red])[/color] "Blocks .."))
(princ)
)

Edited by BlackBox
Changed "BLOCK" to "INSERT"
Link to comment
Share on other sites

Or this, perhaps? :geek:

 

(defun c:Bcount ()
(alert (strcat "You have :" [color=red](itoa [/color](sslength 
 (ssget "_x" '((0 . "[color=red]INSERT[/color]"))))[color=red])[/color] "Blocks .."))
(princ)
)

Link to comment
Share on other sites

That's perfect RenderMan.

 

But don't forget to change the ((0 . "BLOCK")) TO '((0 . "INSERT")) as Lee indicated to Please.

 

Many thanks.

Link to comment
Share on other sites

Or this, perhaps? :geek:

(defun c:Bcount ()
(alert (strcat "You have :" [color=red](itoa [/color](sslength 
 (ssget "_x" '((0 . "[color=red]INSERT[/color]"))))[color=red])[/color] "Blocks .."))
(princ)
)

 

All right, Nice one. :)

 

Thank you so much.

Link to comment
Share on other sites

Oooo... Sorry, sorry. It appears you're too late, as that has already been done, my friend! 8)

 

I noticed it right after posting, and quickly fixed accordingly.

 

Cheers! :beer:

Link to comment
Share on other sites

Or this, perhaps? :geek:

 

(defun c:Bcount ()
(alert (strcat "You have :" [color=red](itoa [/color](sslength 
 (ssget "_x" '((0 . "[color=red]INSERT[/color]"))))[color=red])[/color] "Blocks .."))
(princ)
)

 

Type

 

(sslength nil)

 

at the command line.

Link to comment
Share on other sites

Type

 

(sslength nil)

 

at the command line.

 

Okay... I must be missing something, Lee??? :?

 

Command: (sslength nil)
; error: bad argument type: lselsetp nil

Link to comment
Share on other sites

Hint: You need to be counting INSERTS, not blocks... a block is the definition, the insert is the 'block reference' a representation of the definition in the drawing.

 

Also, you will need to allow for this:

(sslength nil)

 

 

Okay... I must be missing something, Lee??? :?

 

Command: (sslength nil)
; error: bad argument type: lselsetp nil

 

Ohhh! I get it, now!!! :ouch:

 

Will re-write accordingly.....

Link to comment
Share on other sites

Ohhh! I get it, now!!! :ouch:

 

Will re-write accordingly.....

 

Sorry, Michaels. In my haste, I may have led you astray with the combined alert-ssget combo. :oops:

 

Doing so, precludes any error checking :ouch: - limited as an IF statement may be, it is better than nothing!

 

There still may be a better solution (Lee?), but this is certainly preferable over my earlier post:

 

(defun c:Bcount (/ blockCount)
 [color=red](if [/color](setq blockCount (itoa (sslength (ssget "_x" '((0 . "INSERT"))))))
   (alert (strcat "You have : " blockCount " Blocks ..."))
   [color=red](alert "No Blocks Detected In Active Drawing. "))[/color]
(princ)) ;_end defun

Link to comment
Share on other sites

Either used routine when Blocks not found, or (sslength nil), the same outcome would come up.

Command: bcount ; error: bad argument type: lselsetp nil

Command:

Command: (sslength nil)

; error: bad argument type: lselsetp nil

Link to comment
Share on other sites

Again I urge you to consider

 

(sslength nil)

 

Wow - I have been silly today. :oops: I know better!

 

Of course, if there is no selection set (ss), then (sslength ss) = nil, which throws an error.

 

(defun c:Bcount (/ ss)
 (if (setq ss (ssget "_x" '((0 . "INSERT"))))
   (alert (strcat "You have : " (itoa (sslength ss)) " Blocks ..."))
   (alert "No Blocks Detected In Active Drawing. "))
(princ)) ;_end defun

Link to comment
Share on other sites

Exactly - so we must allow for no blocks: i.e. ssget returning nil.

 

EDIT: You got it :)

 

Thanks for making me think it through, Lee! :thumbsup:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...