TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: AcDbZombieEntity on July 10, 2013, 08:11:47 AM

Title: Sorting "nested" attributes by name
Post by: AcDbZombieEntity on July 10, 2013, 08:11:47 AM
Hi,

i've got here many blocks with many attributes, a lot of them have the same attributes but sorted crazy unique in each block definition.

- Is there any method to "resort" them all with lisp, alphabetical order by name and not the promt or value?
- Is there an way to get battman working with command?

P.S. with out an lost of allready entered values  :evil:
Title: Re: Sorting "nested" attributes by name
Post by: Tharwat on July 10, 2013, 08:28:30 AM
- Is there any method to "resort" them all with lisp, alphabetical order by name and not the promt or value?

e.g .

Code: [Select]
(setq l '("c" "e" "d" "a" "f" "b"))
(acad_strlsort l)
Title: Re: Sorting "nested" attributes by name
Post by: cadplayer on July 10, 2013, 08:51:34 AM
Are you meaning tagname from attributdefinition you would like sort. Do you want rebuild a block with attributes.
Another way without lisp is, you take a copy of block, explode them and than use command attredef and you can by clicking on attdef byself choice direction which attdef should be sort.
Itīs a bit difficult to do that with lisp, you need a function which sort all tagnames from attdef and than you have to entmod them. Iīm not sure if itīs the best way.
Title: Re: Sorting "nested" attributes by name
Post by: irneb on July 10, 2013, 09:24:30 AM
I don't think the BAttMan options are available to Lisp.

The "simplest" method would be for the lisp to recreate the block definition using the BLOCK command, but select the attdef's in the correct order:Repeat the process for each block definition.
Title: Re: Sorting "nested" attributes by name
Post by: Lee Mac on July 10, 2013, 09:38:46 AM
The "simplest" method would be for the lisp to recreate the block definition using the BLOCK command, but select the attdef's in the correct order

However, this method would not work for attributed dynamic blocks.



Try the following:
Code - Auto/Visual Lisp: [Select]
  1. ;; Sort Tags  -  Lee Mac
  2. (defun c:sorttags ( / cmd lst )
  3.         (if
  4.             (and
  5.                 (= :vlax-false (vla-get-isxref blk))
  6.                 (= :vlax-false (vla-get-islayout blk))
  7.             )
  8.             (progn
  9.                 (vlax-for obj blk
  10.                     (if (= "AcDbAttributeDefinition" (vla-get-objectname obj))
  11.                         (setq lst (cons obj lst))
  12.                     )
  13.                 )
  14.                 (foreach att (vl-sort lst '(lambda ( a b ) (< (vla-get-tagstring a) (vla-get-tagstring b))))
  15.                     (vla-copy   att)
  16.                     (vla-delete att)
  17.                 )
  18.                 (setq lst nil)
  19.             )
  20.         )
  21.     )
  22.     (setq cmd (getvar 'cmdecho))
  23.     (setvar 'cmdecho 0)
  24.     (vl-cmdf "_.attsync" "_N" "*")
  25.     (setvar 'cmdecho cmd)
  26.     (princ)
  27. )
Title: Re: Sorting "nested" attributes by name
Post by: AcDbZombieEntity on July 10, 2013, 10:09:57 AM
Awesome Lee, thanks a lot. learned something new with

Code: [Select]
               (foreach att (vl-sort lst '(lambda ( a b ) (< (vla-get-tagstring a) (vla-get-tagstring b))))
                   (vla-copy   att)
                   (vla-delete att)
               )

Title: Re: Sorting "nested" attributes by name
Post by: Lee Mac on July 10, 2013, 10:16:03 AM
You're very welcome.

Great username BTW  :-P
Title: Re: Sorting "nested" attributes by name
Post by: MP on July 10, 2013, 01:45:16 PM
Welcome to the swamp brother zombie!
Title: Re: Sorting "nested" attributes by name
Post by: alanjt on July 11, 2013, 04:44:30 PM
Awesome Lee, thanks a lot. learned something new with

Code: [Select]
               (foreach att (vl-sort lst '(lambda ( a b ) (< (vla-get-tagstring a) (vla-get-tagstring b))))
                   (vla-copy   att)
                   (vla-delete att)
               )

 :kewl:
Title: Re: Sorting "nested" attributes by name
Post by: Lee Mac on July 11, 2013, 07:43:00 PM
Awesome Lee, thanks a lot. learned something new with

Code: [Select]
               (foreach att (vl-sort lst '(lambda ( a b ) (< (vla-get-tagstring a) (vla-get-tagstring b))))
                   (vla-copy   att)
                   (vla-delete att)
               )

 :kewl:

 :-)