Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
52 Posts
1 Kudo
Registered: ‎05-21-2013
Post 1 of 4
Accepted Solution

Need lisp to change layer of all nested objects

455 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*
1,311 Posts
335 Kudos
Registered: ‎12-29-2009
Post 2 of 4

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
52 Posts
1 Kudo
Registered: ‎05-21-2013
Post 3 of 4

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*
1,311 Posts
335 Kudos
Registered: ‎12-29-2009
Post 4 of 4

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
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!