Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
ksmith84
Posts: 52
Registered: ‎05-21-2013
Message 1 of 4 (395 Views)
Accepted Solution

Need lisp to change layer of all nested objects

395 Views, 3 Replies
04-17-2014 10:30 AM

Howdy, I am looking for a lisp routine to add to search selected block/blocks to find any objects on layer "A-EquipLwr" and have them changed to layer "-PATT". Both layers are existing in the dwg, all the blocks have only 2 layers contained in them (A-EquipLwr and 0), but there are multiple variations of the block for different purposes. Does anyone have a lisp that will do this layer change in 1 step?  Thank you in advance.

 

Note: I want to keep the layer name as it is, since it is connected to other objects in the drawing which are not a part of blocks.


~¤~
^C^CMoonwalk

Try the following quickly written code:

 

(defun c:fixblocks ( / a b f i l s )
(setq a "A-EQUIPLWR"
b "-PATT"
)
(defun f ( n / e x )
(if (and (not (member n l)) (setq e (tblobjname "block" n)))
(progn
(setq l (cons n l))
(while (setq e (entnext e))
(if (= (strcase a) (strcase (cdr (assoc 8 (setq x (entget e))))))
(entmod (subst (cons 8 b) (assoc 8 x) x))
)
(if (= "INSERT" (cdr (assoc 0 x))) (f (cdr (assoc 2 x))))
)
)
)
)
(if (setq s (ssget '((0 . "INSERT"))))
(repeat (setq i (sslength s))
(f (cdr (assoc 2 (entget (ssname s (setq i (1- i)))))))
)
)
(command "_.regen")
(princ)
)

 

Due to the recursive nature of the code, the above should also account for nested blocks, nested to any depth.

*Expert Elite*
Lee_Mac
Posts: 1,270
Registered: ‎12-29-2009
Message 2 of 4 (381 Views)

Re: Need lisp to change layer of all nested objects

04-17-2014 11:21 AM in reply to: ksmith84

Try the following quickly written code:

 

(defun c:fixblocks ( / a b f i l s )
    (setq a "A-EQUIPLWR"
          b "-PATT"
    )
    (defun f ( n / e x )
        (if (and (not (member n l)) (setq e (tblobjname "block" n)))
            (progn
                (setq l (cons n l))
                (while (setq e (entnext e))
                    (if (= (strcase a) (strcase (cdr (assoc 8 (setq x (entget e))))))
                        (entmod (subst (cons 8 b) (assoc 8 x) x))
                    )
                    (if (= "INSERT" (cdr (assoc 0 x))) (f (cdr (assoc 2 x))))
                )
            )
        )
    )
    (if (setq s (ssget '((0 . "INSERT"))))
        (repeat (setq i (sslength s))
            (f (cdr (assoc 2 (entget (ssname s (setq i (1- i)))))))
        )
    )
    (command "_.regen")
    (princ)
)

 

Due to the recursive nature of the code, the above should also account for nested blocks, nested to any depth.

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
ksmith84
Posts: 52
Registered: ‎05-21-2013
Message 3 of 4 (376 Views)

Re: Need lisp to change layer of all nested objects

04-17-2014 11:30 AM in reply to: Lee_Mac
It works like a charm! Thank you once again Lee! It seems I've got more code studying to do haha.

~¤~
^C^CMoonwalk
*Expert Elite*
Lee_Mac
Posts: 1,270
Registered: ‎12-29-2009
Message 4 of 4 (369 Views)

Re: Need lisp to change layer of all nested objects

04-17-2014 11:44 AM in reply to: ksmith84

Excellent to hear! - You're most welcome ksmith :smileyhappy:

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.