TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jpcadconsulting on August 11, 2015, 02:10:43 PM

Title: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 11, 2015, 02:10:43 PM
Hi gang,

I'm trying to write a routine that allows me to pick on an attribute (field) in a block, and create a selection set of all other blocks of that name that have the same value in that field.  Make sense?

In other words I want to click on the "Species" attribute of a block called "Tree1" and I want AutoCAD to select all other "Tree1" blocks that have a similar value for "Species"

The FIND command does not seem to work because (I think) the value of the "species" attribute is a field driven by a lookup table.  I believe this problem is compounded by the fact that these are dynamic blocks and once a parameter is changed, they take on a unique name...

Thanks in advance for any help.

-JP

Title: Re: Create selection set based on picked field attributes
Post by: Tharwat on August 11, 2015, 03:22:39 PM
Give this a go.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s e l sd ss nm)
  2.   ;; Tharwat 11.08.2015 ;;
  3.   (if (and (setq s (nentsel "\nPick on Attribute in a block :"))
  4.            (eq (cdr (assoc 0 (setq e (entget (car s))))) "ATTRIB")
  5.            (eq (cdr (assoc 0 (setq l (entget (cdr (assoc 330 e))))))
  6.                "INSERT"
  7.            )
  8.            (setq sd (ssadd)
  9.                  s  (strcase (cdr (assoc 1 e)))
  10.            )
  11.            (setq ss
  12.                   (ssget
  13.                     "_X"
  14.                     (list
  15.                       '(0 . "INSERT")
  16.                       (cons 410 (getvar 'CTAB))
  17.                       (cons
  18.                         2
  19.                         (strcat "`*U*,"
  20.                                 (setq
  21.                                   nm (vla-get-effectivename
  22.                                        (vlax-ename->vla-object (cdr (assoc -1 l)))
  23.                                      )
  24.                                 )
  25.                         )
  26.                       )
  27.                       '(66 . 1)
  28.                     )
  29.                   )
  30.            )
  31.       )
  32.     ((lambda (i / sn o)
  33.        (while (setq sn (ssname ss (setq i (1+ i))))
  34.          (if
  35.            (and (eq nm
  36.                     (vla-get-effectivename
  37.                       (setq o (vlax-ename->vla-object sn))
  38.                     )
  39.                 )
  40.                 (vl-some
  41.                   '(lambda (x) (eq s (strcase (vla-get-textstring x))))
  42.                   (vlax-invoke o 'getAttributes)
  43.                 )
  44.            )
  45.             (ssadd sn sd)
  46.          )
  47.        )
  48.      )
  49.       -1
  50.     )
  51.   )
  52.   (sssetfirst nil sd)
  53.   (princ)
  54.  
Title: Re: Create selection set based on picked field attributes
Post by: nobody on August 11, 2015, 11:02:54 PM
Not sure but QSelect might work for you?
Hi gang,

I'm trying to write a routine that allows me to pick on an attribute (field) in a block, and create a selection set of all other blocks of that name that have the same value in that field.  Make sense?

In other words I want to click on the "Species" attribute of a block called "Tree1" and I want AutoCAD to select all other "Tree1" blocks that have a similar value for "Species"

The FIND command does not seem to work because (I think) the value of the "species" attribute is a field driven by a lookup table.  I believe this problem is compounded by the fact that these are dynamic blocks and once a parameter is changed, they take on a unique name...

Thanks in advance for any help.

-JP
Title: Re: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 12, 2015, 02:10:57 PM
Thanks Tharwat, but it does not seem to do anything.

Loads fine, runs fine, not throwing errors, but no result...

Thoughts?
Title: Re: Create selection set based on picked field attributes
Post by: Tharwat on August 12, 2015, 02:19:14 PM
I tested the codes after posting them and it did work , anyway can you upload a sample drawing consists of the blocks that you wan to run the program on ?
Title: Re: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 12, 2015, 02:24:18 PM
I will, and it does work fine on blocks with regular attributes.

The tree blocks in the drawing are dynamic.

So I think it grabs the block's unique name (which is something like *U58 and not "tree")

I think the code needs something like:

Code: [Select]
(vla-get-Effectivename)
But I would not know how to implement...

Thanks so much for your help.
Title: Re: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 12, 2015, 02:44:13 PM
So I added the following lines at the end of the code so it woudl report the variable values.

Code: [Select]
(princ s)
(princ e)
(princ l)
(princ sd)
(princ ss
)

And it does report the following:

JAPANESE WHITE PINE((-1 . <Entity name: 7ffffb816e0>) (0 . ATTRIB) (5 . 944E) (102 . {ACAD_XDICTIONARY) (360 . <Entity name: 7ffffb816f0>) (102 . }) (330 . <Entity name: 7ffffb81610>) (100 . AcDbEntity) (67 . 0) (410 . Model) (8 . 0) (62 . 2) (100 . AcDbText) (10 3108.78 2977.12 0.0) (40 . 3.125) (1 . JAPANESE WHITE PINE) (50 . 0.0019959) (41 . 1.0) (51 . 0.0) (7 . Standard) (71 . 0) (72 . 1) (11 3136.2 2980.3 0.0) (210 0.0 0.0 1.0) (100 . AcDbAttribute) (280 . 0) (2 . CNAME) (70 . 1) (73 . 0) (74 . 3) (280 . 1))((-1 . <Entity name: 7ffffb81610>) (0 . INSERT) (5 . 9441) (102 . {ACAD_XDICTIONARY) (360 . <Entity name: 7ffffb81620>) (102 . }) (330 . <Entity name: 7ffffbac9f0>) (100 . AcDbEntity) (67 . 0) (410 . Model) (8 . L-PLNT-TREE-CONIF) (100 . AcDbBlockReference) (66 . 1) (2 . *U3) (10 3136.18 2985.94 0.0) (41 . 1.0) (42 . 1.0) (43 . 1.0) (50 . 0.0019959) (70 . 0) (71 . 0) (44 . 0.0) (45 . 0.0) (210 0.0 0.0 1.0))<Selection set: 1eb>nilnil

So it looks like it sees the blocks with similar attributes, but it's not selecting them as it does with the test blocks.

Hope this helps.
Title: Re: Create selection set based on picked field attributes
Post by: Tharwat on August 12, 2015, 02:47:23 PM
I am sorry , I forgot to include the dynamic , anyway I updated the codes above so try it and let me know.
Title: Re: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 12, 2015, 02:52:47 PM
Don't apologize!  ;-)  It works great and it's going to help a great deal!

Thanks so much!!!

-JP
Title: Re: Create selection set based on picked field attributes
Post by: Tharwat on August 12, 2015, 02:55:23 PM
Don't apologize!  ;-)  It works great and it's going to help a great deal!

Thanks so much!!!

-JP

Nice, you are most welcome.  :-)
Title: Re: Create selection set based on picked field attributes
Post by: jpcadconsulting on August 18, 2015, 12:41:58 PM
I have to tell you Tharwat,

Users at this office are writing epic songs about your LISP abilities and singing them around campfires!!! ;-)

Super useful tool.  Thanks again!
Title: Re: Create selection set based on picked field attributes
Post by: ronjonp on August 18, 2015, 12:59:58 PM
Harvesting attributes & using these useful functions (http://www.theswamp.org/index.php?topic=8163.msg104627#msg104627), one could make a fairly versatile filtering routine  :)

Title: Re: Create selection set based on picked field attributes
Post by: Tharwat on August 18, 2015, 01:05:57 PM
I have to tell you Tharwat,

Users at this office are writing epic songs about your LISP abilities and singing them around campfires!!! ;-)

Super useful tool.  Thanks again!

 :-)

Thank you jpcadconsulting , for sure that gives me the honor.
Title: Re: Create selection set based on picked field attributes
Post by: Lee Mac on August 18, 2015, 01:06:43 PM
Harvesting attributes & using these useful functions (http://www.theswamp.org/index.php?topic=8163.msg104627#msg104627), one could make a fairly versatile filtering routine  :)

FWIW, below are some optimisations for those functions spawned from a PM discussion with MP a couple of 4(!) years ago (which subsequently lead to this thread (http://www.theswamp.org/index.php?topic=40165) demonstrating a generic form of the same technique).

Code - Auto/Visual Lisp: [Select]
  1. (defun _GetVlaAtoms ( )
  2.     (eval
  3.         (list 'defun '_GetVlaAtoms '( )
  4.             (list 'quote
  5.                 (vl-sort
  6.                     (vl-remove-if-not
  7.                         (function
  8.                             (lambda ( sym )
  9.                                 (wcmatch (vl-symbol-name sym) "vla`-*")
  10.                             )
  11.                         )
  12.                         (atoms-family 0)
  13.                     )
  14.                     (function
  15.                         (lambda ( a b )
  16.                             (<
  17.                                 (vl-symbol-name a)
  18.                                 (vl-symbol-name b)
  19.                             )
  20.                         )
  21.                     )
  22.                 )
  23.             )
  24.         )
  25.     )
  26.     (_GetVlaAtoms)
  27. )
  28.  
  29. (defun _GetVlaProperties ( )
  30.     (eval
  31.         (list 'defun '_GetVlaProperties '( )
  32.             (list 'quote
  33.                 (mapcar
  34.                     (function
  35.                         (lambda ( sym )
  36.                             (substr (vl-symbol-name sym) 9)
  37.                         )
  38.                     )
  39.                     (vl-remove-if-not
  40.                         (function
  41.                             (lambda ( sym )
  42.                                 (wcmatch (strcase (vl-symbol-name sym)) "VLA`-GET`-*")
  43.                             )
  44.                         )
  45.                         (_GetVlaAtoms)
  46.                     )
  47.                 )
  48.             )
  49.         )
  50.     )
  51.     (_GetVlaProperties)
  52. )
  53.  
  54. (defun _GetVlaMethods ( )
  55.     (eval
  56.         (list 'defun '_GetVlaMethods '( )
  57.             (list 'quote
  58.                 (mapcar
  59.                     (function
  60.                         (lambda ( sym )
  61.                             (substr (vl-symbol-name sym) 5)
  62.                         )
  63.                     )
  64.                     (vl-remove-if
  65.                         (function
  66.                             (lambda ( sym )
  67.                                 (wcmatch (strcase (vl-symbol-name sym)) "VLA`-GET`-*,VLA`-PUT`-*")
  68.                             )
  69.                         )
  70.                         (_GetVlaAtoms)
  71.                     )
  72.                 )
  73.             )
  74.         )
  75.     )
  76.     (_GetVlaMethods)
  77. )
  78.  

P.S. Nice work Ron :)
Title: Re: Create selection set based on picked field attributes
Post by: ronjonp on August 18, 2015, 01:57:20 PM
Harvesting attributes & using these useful functions (http://www.theswamp.org/index.php?topic=8163.msg104627#msg104627), one could make a fairly versatile filtering routine  :)

FWIW, below are some optimisations for those functions spawned from a PM discussion with MP a couple of 4(!) years ago (which subsequently lead to this thread (http://www.theswamp.org/index.php?topic=40165) demonstrating a generic form of the same technique).

Code - Auto/Visual Lisp: [Select]
  1. (defun _GetVlaAtoms ( )
  2.     (eval
  3.         (list 'defun '_GetVlaAtoms '( )
  4.             (list 'quote
  5.                 (vl-sort
  6.                     (vl-remove-if-not
  7.                         (function
  8.                             (lambda ( sym )
  9.                                 (wcmatch (vl-symbol-name sym) "vla`-*")
  10.                             )
  11.                         )
  12.                         (atoms-family 0)
  13.                     )
  14.                     (function
  15.                         (lambda ( a b )
  16.                             (<
  17.                                 (vl-symbol-name a)
  18.                                 (vl-symbol-name b)
  19.                             )
  20.                         )
  21.                     )
  22.                 )
  23.             )
  24.         )
  25.     )
  26.     (_GetVlaAtoms)
  27. )
  28.  
  29. (defun _GetVlaProperties ( )
  30.     (eval
  31.         (list 'defun '_GetVlaProperties '( )
  32.             (list 'quote
  33.                 (mapcar
  34.                     (function
  35.                         (lambda ( sym )
  36.                             (substr (vl-symbol-name sym) 9)
  37.                         )
  38.                     )
  39.                     (vl-remove-if-not
  40.                         (function
  41.                             (lambda ( sym )
  42.                                 (wcmatch (strcase (vl-symbol-name sym)) "VLA`-GET`-*")
  43.                             )
  44.                         )
  45.                         (_GetVlaAtoms)
  46.                     )
  47.                 )
  48.             )
  49.         )
  50.     )
  51.     (_GetVlaProperties)
  52. )
  53.  
  54. (defun _GetVlaMethods ( )
  55.     (eval
  56.         (list 'defun '_GetVlaMethods '( )
  57.             (list 'quote
  58.                 (mapcar
  59.                     (function
  60.                         (lambda ( sym )
  61.                             (substr (vl-symbol-name sym) 5)
  62.                         )
  63.                     )
  64.                     (vl-remove-if
  65.                         (function
  66.                             (lambda ( sym )
  67.                                 (wcmatch (strcase (vl-symbol-name sym)) "VLA`-GET`-*,VLA`-PUT`-*")
  68.                             )
  69.                         )
  70.                         (_GetVlaAtoms)
  71.                     )
  72.                 )
  73.             )
  74.         )
  75.     )
  76.     (_GetVlaMethods)
  77. )
  78.  

P.S. Nice work Ron :)
Thanks!  :)