Author Topic: change the insertion layer of All blocks to its original layer of Creation  (Read 2606 times)

0 Members and 1 Guest are viewing this topic.

adham7

  • Mosquito
  • Posts: 6
Hi,
How can i change the insertion layer of All blocks in the drawing to its original layer of Creation
by modifying this lisp which convert block creation layer to layer "0"
e.g
As attached
        the "tree" Block lie on layer "0" , i need to change its layer to layer "tree"
        the "Wmh" Block lie on layer "TREE" , i need to change its layer to layer "water Manhole"
Thanks

Code: [Select]
;;;;Lisp to Change Block Layer To Layer 0

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

ronjonp

  • Needs a day job
  • Posts: 7526
Here's a quick one:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ e i s)
  2.   (if (setq s (ssget '((0 . "INSERT") (2 . "WMH,TREE"))))
  3.     (repeat (setq i (sslength s))
  4.       (entmod
  5.         (subst (cons 8
  6.                      (if (= "TREE" (cdr (assoc 2 (setq e (entget (ssname s (setq i (1- i))))))))
  7.                        "TREE"
  8.                        "WATER MANHOLE"
  9.                      )
  10.                )
  11.                (assoc 8 e)
  12.                e
  13.         )
  14.       )
  15.     )
  16.   )
  17.   (princ)
  18. )
« Last Edit: July 08, 2019, 09:55:56 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

adham7

  • Mosquito
  • Posts: 6
Here's a quick one:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ e i s)
  2.   (if (setq s (ssget '((0 . "INSERT") (2 . "WMH,TREE"))))
  3.     (repeat (setq i (sslength s))
  4.       (entmod
  5.         (subst (cons 8
  6.                      (if (= "TREE" (cdr (assoc 2 (setq e (entget (ssname s (setq i (1- i))))))))
  7.                        "TREE"
  8.                        "WATER MANHOLE"
  9.                      )
  10.                )
  11.                (assoc 8 e)
  12.                e
  13.         )
  14.       )
  15.     )
  16.   )
  17.   (princ)
  18. )


Thank you for Reply ronjonp ,
But this two Blocks are just Example to Explain What I need,
I want to check Every Block in the drawing and change its insertion layer to its Creation layer. (may be a Block like "Door" is created on layer "Door" and inserted in current layer like "wall" or so i want to change its insertion layer to "Door")


kpblc

  • Bull Frog
  • Posts: 396
Code - Auto/Visual Lisp: [Select]
  1. (defun t1 (lst / selset count)
  2.           ;|
  3. lst - list like
  4.    '((<BlockName> . <LayerName>)
  5.      (<BlockName> . <LayerName>)
  6.      <...>
  7.      (<BlockName> . <LayerName>)
  8.      )
  9. All blocks not in lst will be on layer named by block name
  10. BlockName could contain wildcards
  11. sample:
  12. (t1 '(("wmh" . "water Manhole")))
  13. (t1 '(("tre*" . "TreeLikeSomthing")))
  14. (t1 '(("w?h" . "water Manhole123") ("tre*" . "TreeLikeSomthing")))
  15. |;
  16.   (if (= (type (setq selset (vl-catch-all-apply (function (lambda () (ssget "_:L" '((0 . "insert"))))))))
  17.          'pickset
  18.          ) ;_ end of =
  19.     (foreach ent (mapcar (function
  20.                            (lambda (x / n la)
  21.                              (setq n (strcase (vla-get-effectivename (vlax-ename->vla-object x))))
  22.                              (cons x
  23.                                    (cond ((cdar (vl-remove-if-not (function (lambda (a) (wcmatch n (strcase (car a))))) lst)))
  24.                                          (t n)
  25.                                          ) ;_ end of cond
  26.                                    ) ;_ end of cons
  27.                              ) ;_ end of lambda
  28.                            ) ;_ end of function
  29.                          ((lambda (/ tab item)
  30.                             (repeat (setq tab  nil
  31.                                           item (sslength selset)
  32.                                           ) ;_ end setq
  33.                               (setq tab (cons (ssname selset (setq item (1- item))) tab))
  34.                               ) ;_ end of repeat
  35.                             ) ;_ end of lambda
  36.                           )
  37.                          ) ;_ end of mapcar
  38.       (entmod (subst (cons 8 (cdr ent)) (assoc 8 (entget (car ent))) (entget (car ent))))
  39.       ) ;_ end of foreach
  40.     ) ;_ end of if
  41.   (vla-endundomark adoc)
  42.   ) ;_ end of defun
Sorry for my English.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
What the OP calls 'original layer of creation' is a bit confusing. After looking at the dwg I think the code should look at the objects inside the block definition. Their layer should determine the layer for the references of that block.
An interesting question is then what should happen if a block definition contains objects on multiple layers?

ronjonp

  • Needs a day job
  • Posts: 7526
Here's a quick one:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ e i s)
  2.   (if (setq s (ssget '((0 . "INSERT") (2 . "WMH,TREE"))))
  3.     (repeat (setq i (sslength s))
  4.       (entmod
  5.         (subst (cons 8
  6.                      (if (= "TREE" (cdr (assoc 2 (setq e (entget (ssname s (setq i (1- i))))))))
  7.                        "TREE"
  8.                        "WATER MANHOLE"
  9.                      )
  10.                )
  11.                (assoc 8 e)
  12.                e
  13.         )
  14.       )
  15.     )
  16.   )
  17.   (princ)
  18. )


Thank you for Reply ronjonp ,
But this two Blocks are just Example to Explain What I need,
I want to check Every Block in the drawing and change its insertion layer to its Creation layer. (may be a Block like "Door" is created on layer "Door" and inserted in current layer like "wall" or so i want to change its insertion layer to "Door")
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ a e i s)
  2.   (if (setq s (ssget '((0 . "INSERT"))))
  3.     (repeat (setq i (sslength s))
  4.       (setq e (entget (ssname s (setq i (1- i)))))
  5.       ;; Check layer of one item within the block .. not efficient but free nonetheless :)
  6.       (if (setq a (entnext (tblobjname "block" (cdr (assoc 2 e)))))
  7.         (entmod (subst (assoc 8 (entget a)) (assoc 8 e) e))
  8.       )
  9.     )
  10.   )
  11.   (princ)
  12. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

adham7

  • Mosquito
  • Posts: 6
Perfect
Thanks ronjonp
thank you so much! It is working.
 I appreciate it.

ronjonp

  • Needs a day job
  • Posts: 7526
Perfect
Thanks ronjonp
thank you so much! It is working.
 I appreciate it.
Glad to help :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC