Author Topic: quantify the number of blocks per layer  (Read 1889 times)

0 Members and 1 Guest are viewing this topic.

nini007

  • Newt
  • Posts: 58
quantify the number of blocks per layer
« on: October 20, 2022, 05:00:49 AM »
Hello forum,

I need your lights :-).
I have the attached lisp from Lee Mac "CountV1-5.lsp" which is excellent which I use often.

For the needs of several colleagues in the same principle of Lee Mac's lisp is it possible to quantify the number of blocks per layer and create a table?

Thanks in advance
Best regards

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: quantify the number of blocks per layer
« Reply #1 on: October 20, 2022, 03:20:42 PM »
Where most forms of counting tasks are concerned, I would typically employ the technique I describe in my Block Counter tutorial, wherein the program constructs & updates an association list, incrementing the integer value associated with the appropriate key for every object processed in the selection.

For cases such as this where multiple keys are involved (here, we have two keys: 'block name' & 'layer'), you can employ my Nested Assoc++ function, as demonstrated by the following example:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:blklaycount ( / enx idx lst sel )
  2.     (if (setq sel (ssget '((0 . "INSERT"))))
  3.         (progn
  4.             (repeat (setq idx (sslength sel))
  5.                 (setq idx (1- idx)
  6.                       enx (entget (ssname sel idx))
  7.                       lst (LM:nassoc++ (list (LM:name->effectivename (cdr (assoc 2 enx))) (cdr (assoc 8 enx))) lst)
  8.                 )
  9.             )
  10.             (princ (strcat "\n" (LM:padbetween "" "" "=" 80)))
  11.             (princ (strcat "\n" (LM:padbetween "Block" (LM:padbetween "Layer" "Quantity" " " 50) " " 80)))
  12.             (princ (strcat "\n" (LM:padbetween "" "" "=" 80)))
  13.             (foreach blk lst
  14.                 (princ (strcat "\n" (LM:padbetween (car blk) (itoa (apply '+ (mapcar 'cadr (cdr blk)))) " " 80)))
  15.                 (princ (strcat "\n" (LM:padbetween "" "" "-" 80)))
  16.                 (foreach lay (cdr blk)
  17.                     (princ (strcat "\n" (LM:padbetween "" (LM:padbetween (car lay) (itoa (cadr lay)) "." 50) " " 80)))
  18.                 )
  19.                 (princ (strcat "\n" (LM:padbetween "" "" "-" 80)))
  20.             )
  21.         )
  22.     )
  23.     (princ)
  24. )
  25.  
  26. ;; Pad Left  -  Lee Mac
  27. ;; str - [str] String to pad to given length
  28. ;; cha - [str] Padding character(s)
  29. ;; len - [int] Minimum length of returned string
  30.  
  31. (defun LM:padbetween ( st1 st2 cha len )
  32.     (if (< (+ (strlen st1) (strlen st2)) len)
  33.         (LM:padbetween (strcat st1 cha) st2 cha len)
  34.         (strcat st1 st2)
  35.     )
  36. )
  37.                
  38. ;; Block Name -> Effective Block Name  -  Lee Mac
  39. ;; blk - [str] Block name
  40.  
  41. (defun LM:name->effectivename ( blk / rep )
  42.     (if
  43.         (and (wcmatch blk "`**")
  44.             (setq rep
  45.                 (cdadr
  46.                     (assoc -3
  47.                         (entget
  48.                             (cdr (assoc 330 (entget (tblobjname "block" blk))))
  49.                            '("AcDbBlockRepBTag")
  50.                         )
  51.                     )
  52.                 )
  53.             )
  54.             (setq rep (handent (cdr (assoc 1005 rep))))
  55.         )
  56.         (cdr (assoc 2 (entget rep)))
  57.         blk
  58.     )
  59. )    
  60.  
  61. ;; Nested Assoc++  -  Lee Mac
  62. ;; Increments the value of a key in an association list with possible nested structure,
  63. ;; or adds the set of keys to the list if not present.
  64. ;; key - [lst] List of keys & subkeys
  65. ;; lst - [lst] Association list (may be nil)
  66.  
  67. (defun LM:nassoc++ ( key lst / itm )
  68.     (if key
  69.         (if (setq itm (assoc (car key) lst))
  70.             (subst (cons (car key) (LM:nassoc++ (cdr key) (cdr itm))) itm lst)
  71.             (cons  (cons (car key) (LM:nassoc++ (cdr key) nil)) lst)
  72.         )
  73.         (if lst (list (1+ (car lst))) '(1))
  74.     )
  75. )
  76.  

nini007

  • Newt
  • Posts: 58
Re: quantify the number of blocks per layer
« Reply #2 on: October 21, 2022, 04:50:09 AM »
Hello,

I thank you for your message :-).
It's exactly that :yes:.
Is it possible that it imports the table into AutoCAD and has the selection <ALL> option by default?

Thanks in advance
Best regards

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: quantify the number of blocks per layer
« Reply #3 on: October 21, 2022, 05:17:04 AM »
LM:nassoc++, lisp operator override

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: quantify the number of blocks per layer
« Reply #4 on: October 21, 2022, 06:30:06 AM »
Just a little joke directed at LM, I know C++ guys think they have a great sense of humor, kinda like IRS employees

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: quantify the number of blocks per layer
« Reply #5 on: October 21, 2022, 03:39:40 PM »

nini007

  • Newt
  • Posts: 58
Re: quantify the number of blocks per layer
« Reply #6 on: October 26, 2022, 09:31:06 AM »
Hello :-),

I allow myself to return to my previous question, if it is not possible to import the table into AutoCAD. Is it possible to have a formatting of the result in the command window (F2) in order to be able to do a clean copy-paste?

Thanks in advance
Best regards

DEVITG

  • Bull Frog
  • Posts: 479
Re: quantify the number of blocks per layer
« Reply #7 on: October 26, 2022, 01:06:38 PM »
@ mini007, why not to use DATAEXTRACTION?, it can make an ACAD table and a XLS file.  Then you can sort column by layers
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

nini007

  • Newt
  • Posts: 58
Re: quantify the number of blocks per layer
« Reply #8 on: October 27, 2022, 09:21:49 AM »
Hello,
Thank you for your message.
Indeed with the DATAEXTRACTION command it exports me an xls table but a little complicated. This implies that I have to make a macro of this file because it is an export that I have to do regularly. I tried Lee Mac's lisp again with a more complex file and the data is well formatted, I find it easier to leave from its data.

thank you for your help
Best regards

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: quantify the number of blocks per layer
« Reply #9 on: October 27, 2022, 09:03:40 PM »
Lee's code make a list of the block details "lst" so its a case of adding on the "make table", like many need some time.
A man who never made a mistake never made anything