Author Topic: block reference change in selection set  (Read 879 times)

0 Members and 1 Guest are viewing this topic.

bjbj

  • Mosquito
  • Posts: 2
block reference change in selection set
« on: October 28, 2021, 11:57:07 PM »
Hi, Im bj. I need a help.
I just googled lisp for changing block reference, and most of them come from Lee_MAC programming. thanks again!
I want to change the reference of blocks only I selected, so I post my code. please check my error thanks.
I just started lisp yesterday, and I do not have any deep knowledge in lisp. thanks.

---------------------------------------------------------------------------------------------------
(defun c:wow ( / d n )
    (if (ssget "X" '((0 . "INSERt") (410 . "MODEL")))
      (vlax-for blk (vla-get-ActiveSelectionSet (setq d (vla-get-ActiveDocument (vlax-get-acad-object))))
         (vlax-for b blk
            (vlax-for a b
               (setq n (vla-get-objectname a))
               (cond
                  (   (= "AcDbBlockReference" n)
                     (if (= :vlax-true (vla-get-hasattributes a))
                        (foreach x
                           (append
                              (vlax-invoke a 'getattributes)
                              (vlax-invoke a 'getconstantattributes)
                           )
                           (vl-catch-all-apply 'vla-put-layer (list x "0"))
                        )
                     )
                  )
                  (   (= "AcDbAttributeDefinition" n)
                     (vl-catch-all-apply 'vla-put-layer (list a "0"))
                  )
               )
            )
         )
      )
    )
    (vla-regen d acallviewports)
    (princ)
)
(vl-load-com) (princ)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: block reference change in selection set
« Reply #1 on: October 29, 2021, 06:44:30 PM »
Consider the following code -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:wow ( / idx lst sel )
  2.     (if (setq sel (ssget '((0 . "INSERT"))))
  3.         (repeat (setq idx (sslength sel))
  4.             (processblock (entget (ssname sel (setq idx (1- idx)))))
  5.         )
  6.     )
  7.     (command "_.regen")
  8.     (princ)
  9. )
  10. (defun processblock ( enx / att atx blk ent )
  11.     (if (= 1 (cdr (assoc 66 enx)))
  12.         (progn
  13.             (setq att (entnext (cdr (assoc -1 enx)))
  14.                   atx (entget att)
  15.             )
  16.             (while (= "ATTRIB" (cdr (assoc 0 atx)))
  17.                 (entmod (subst '(8 . "0") (assoc 8 atx) atx))
  18.                 (setq att (entnext att)
  19.                       atx (entget  att)
  20.                 )
  21.             )
  22.         )
  23.     )
  24.     (cond
  25.         (   (member (setq blk (cdr (assoc 2 enx))) lst))
  26.         (   (setq ent (tblobjname "block" blk))
  27.             (while (setq ent (entnext ent))
  28.                 (setq enx (entget ent))
  29.                 (cond
  30.                     (   (= "ATTDEF" (cdr (assoc 0 enx)))
  31.                         (entmod (subst '(8 . "0") (assoc 8 enx) enx))
  32.                     )
  33.                     (   (= "INSERT" (cdr (assoc 0 enx)))
  34.                         (processblock enx)
  35.                     )
  36.                 )
  37.             )
  38.             (setq lst (cons blk lst))
  39.         )
  40.     )
  41. )

The above essentially changes the layer of attribute references held by each selected block reference, and then proceeds to iterate over the components of the block definition (if a definition of the same name has not already been procesed), changing the layer of attribute definitions and recursively processing any nested block references found therein. Lexical scoping of the lst variable is also used so that it can be referenced independently of the recursive call, whilst remaining local to the c:wow function.

bjbj

  • Mosquito
  • Posts: 2
Re: block reference change in selection set
« Reply #2 on: October 31, 2021, 09:41:45 PM »
Thanks ! Lee_MAC, I did not expected your reply. I think the lisp language need a lot of time to cover.. Recursive is my weakness, I need more time to understand your reply but your code really works well. Thanks again for your direct reply, I will check and study with your site. Thank again!