Author Topic: Change layer by attribute value  (Read 1679 times)

0 Members and 1 Guest are viewing this topic.

bilançikur

  • Newt
  • Posts: 82
Change layer by attribute value
« on: November 27, 2023, 02:27:24 AM »
Hello everyone,

I hope to find some help here. I have searched the internet but cannot find a workable (understandable) solution, one that I can get started with, for example.

My challenge:
Suppose I have many blocks with many different block names, but they all have attributes and one of the attributes TAG is called "GROUP".
I would like to use a selection set and go through all the blocks to find out what value the TAG "GROUP" has.

If the value is "1" then I want to change the layer the block is on to "blah blah 1". If the value is "2" then "bladibla2".
The most ideal way would be to have some kind of list:

Code: [Select]
(setq CHANGES '(
                  ( "1" . "Bla Bla 1")
                  ( "2" . "Bladibla 2")
                  ( "3" . "My layername 3")
                  ( "4" . "My layername in case of 4")
            )
 )

And if there is no TAG "GROUP" in the block or if the value is not one of those in the list nothing should happen.

I have found a kind of close to solution lisp https://www.cadtutor.net/forum/topic/34583-move-blocks-to-other-layer-by-attribute-value/#comment-280640 but I do not understand the vlax/mapcar/lambda function. That said, it is my bad to not be able to find a solution myself therefor my request for help.

Thanks for those who are willing to help me out.

kozmos

  • Newt
  • Posts: 115
Re: Change layer by attribute value
« Reply #1 on: November 27, 2023, 07:00:06 AM »
somethingh like this?
Code - Auto/Visual Lisp: [Select]
  1. (Defun C:tt (/ NN SS VAL VLO)
  2.   (if (setq ss (ssget '((0 . "insert") (66 . 1))))
  3.     (repeat (setq nn (sslength ss))
  4.       (setq vlo (vlax-ename->vla-object (ssname ss (setq nn (1- nn)))))
  5.       (foreach att (vlax-safearray->list
  6.                      (vlax-variant-value (vla-getattributes vlo))
  7.                    )
  8.         (and (= (vla-get-objectname att) "AcDbAttribute")
  9.              (= (strcase (vla-get-tagstring att)) "GROUP")
  10.              (setq val
  11.                     (cdr (assoc (vla-get-textstring att)
  12.                                 '(("1" . "Bla Bla 1")
  13.                                   ("2" . "Bladibla 2")
  14.                                   ("3" . "My layername 3")
  15.                                   ("4" . "My layername in case of 4")
  16.                                  )
  17.                          )
  18.                     )
  19.              )
  20.              (vla-put-layer vlo val)
  21.         )
  22.       )
  23.     )
  24.   )
  25. )
  26.  
KozMos Inc.

bilançikur

  • Newt
  • Posts: 82
Re: Change layer by attribute value
« Reply #2 on: November 27, 2023, 08:22:31 AM »
Hello Kozmos,

Thanks for the reply, this is exactly what I mean.
I will try to understand the code and hopefully I can learn from it.

Have a nice day!

mhupp

  • Bull Frog
  • Posts: 250
Re: Change layer by attribute value
« Reply #3 on: November 27, 2023, 08:33:31 AM »
Another way with getpropertyvalue I think this only works with AutoCAD.
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-8E5913FC-09ED-4C70-AFB7-2431C062E899

Code - Auto/Visual Lisp: [Select]
  1. (defun C:tt (/ lst SS blk group)
  2.   (setq lst '(("1" . "Bla Bla 1") ("2" . "Bladibla 2") ("3" . "My layername 3") ("4" . "My layername in case of 4"))
  3.   (if (setq SS (ssget '((0 . "INSERT") (66 . 1))))
  4.     (foreach blk (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS))))
  5.       (if (setq group (getpropertyvalue blk "GROUP")
  6.         (vla-put-layer blk (cdr (assoc group lst)))
  7.       )
  8.     )
  9.   )
  10.   (princ)
  11. )

-Edit
untested might error if their isn't a value for group, or one that's not in the list above 1-4, or layers don't exist in drawing.
(only have notepad here)
« Last Edit: November 27, 2023, 08:37:38 AM by mhupp »