Author Topic: Count dims by layer?  (Read 1028 times)

0 Members and 1 Guest are viewing this topic.

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Count dims by layer?
« on: May 02, 2016, 11:52:01 AM »
Getting back into LISP.  I'm pretty sure I need to use lists for this, but it's been so long, I don't know where to start anymore.

NEED: Select dimensions using a selection box, group dimensions by layer, sort dimensions by length, paste text result.

Example and result attached.

Thanks for any help!

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Count dims by layer?
« Reply #1 on: May 02, 2016, 12:35:01 PM »
Here is one possible way:
  • Obtain a selection of dimensions using ssget.
  • Iterate over this selection using any of the methods described here.
  • Retrieve the layer (DXF group 8) & measurement (DXF group 42) for each dimension entity.
  • Use my Nested Assoc++ function to count the dimensions based on these two keys.
  • Format the result as required.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Count dims by layer?
« Reply #2 on: May 02, 2016, 12:41:41 PM »
Here's some code to get you started, based on the method described above:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dimcount ( / enx idx lay lst sel val )
  2.     ;; Define function and declare local variables
  3.  
  4.     ;; If the user makes a valid selection of dimensions
  5.     (if (setq sel (ssget '((0 . "*DIMENSION"))))
  6.         (progn
  7.             ;; Then do the following:
  8.  
  9.             ;; Iterate over the selection set
  10.             ;; [ Method 2a: http://lee-mac.com/selsetprocessing.html ]
  11.             (repeat (setq idx (sslength sel))
  12.                 ;; Obtain the DXF data for an item in the set
  13.                 (setq enx (entget (ssname sel (setq idx (1- idx))))
  14.                 ;; Obtain the dimension layer
  15.                       lay (cdr (assoc  8 enx))
  16.                 ;; Obtain the dimension measurement
  17.                       val (cdr (assoc 42 enx))
  18.                 ;; Update the association list with these entries
  19.                       lst (LM:nassoc++ (list lay val) lst)
  20.                 ) ;; end setq
  21.             ) ;; end repeat
  22.  
  23.             (print lst)
  24.         ) ;; end progn
  25.     ) ;; end if
  26.     ;; Suppress the value returned by the last evaluated expression
  27.     (princ)
  28. ) ;; end defun
  29.  
  30. ;; Nested Assoc++  -  Lee Mac
  31. ;; Increments the value of a key in an association list with possible nested structure,
  32. ;; or adds the set of keys to the list if not present.
  33. ;; key - [lst] List of keys & subkeys
  34. ;; lst - [lst] Association list (may be nil)
  35.  
  36. (defun LM:nassoc++ ( key lst / itm )
  37.     (if key
  38.         (if (setq itm (assoc (car key) lst))
  39.             (subst (cons (car key) (LM:nassoc++ (cdr key) (cdr itm))) itm lst)
  40.             (cons  (cons (car key) (LM:nassoc++ (cdr key) nil)) lst)
  41.         )
  42.         (if lst (list (1+ (car lst))) '(1))
  43.     )
  44. )

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Count dims by layer?
« Reply #3 on: May 02, 2016, 12:50:06 PM »
Here is one possible way:
  • Obtain a selection of dimensions using ssget.
  • Iterate over this selection using any of the methods described here.
  • Retrieve the layer (DXF group 8) & measurement (DXF group 42) for each dimension entity.
  • Use my Nested Assoc++ function to count the dimensions based on these two keys.
  • Format the result as required.

Thanks, Lee! I'll start messing with it around lunchtime.