How do I convert many blocks to one layer?

How do I convert many blocks to one layer?

Jake_Kohlmeier
Explorer Explorer
756 Views
6 Replies
Message 1 of 7

How do I convert many blocks to one layer?

Jake_Kohlmeier
Explorer
Explorer

Hello, I am currently working with a very large building layout drawing, and every single item in the drawing is set as a block, all with different layering. There are over 1200 individual blocks, all under seemingly random names and each with their own layer.

 

How do I convert all of these blocks to all be in the same layer? If I highlight all and switch layers, it only converts non-blocks.

 

Thank you

0 Likes
Accepted solutions (1)
757 Views
6 Replies
Replies (6)
Message 2 of 7

pendean
Community Legend
Community Legend
0 Likes
Message 3 of 7

LDShaw
Collaborator
Collaborator

See if this does what you want. You should be able to to one or the whole dwg.  

(defun c:fixblock (/ *error* adoc lst_layer func_restore-layers)

 (defun *error* (msg)
   (func_restore-layers)
   (vla-endundomark adoc)
   (princ msg)
   (princ)
   ) ;_ end of defun

 (defun func_restore-layers ()
   (foreach item lst_layer
     (vla-put-lock (car item) (cdr (assoc "lock" (cdr item))))
     (vl-catch-all-apply
       '(lambda ()
          (vla-put-freeze
            (car item)
            (cdr (assoc "freeze" (cdr item)))
            ) ;_ end of vla-put-freeze
          ) ;_ end of lambda
       ) ;_ end of vl-catch-all-apply
     ) ;_ end of foreach
   ) ;_ end of defun

 (vl-load-com)
 (vla-startundomark
   (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
   ) ;_ end of vla-startundomark
 (if (and (not (vl-catch-all-error-p
                 (setq selset
                        (vl-catch-all-apply
                          (function
                            (lambda ()
                              (ssget '((0 . "INSERT")))
                              ) ;_ end of lambda
                            ) ;_ end of function
                          ) ;_ end of vl-catch-all-apply
                       ) ;_ end of setq
                 ) ;_ end of vl-catch-all-error-p
               ) ;_ end of not
          selset
          ) ;_ end of and
   (progn
     (vlax-for item (vla-get-layers adoc)
       (setq
         lst_layer (cons (list item
                               (cons "lock" (vla-get-lock item))
                               (cons "freeze" (vla-get-freeze item))
                               ) ;_ end of list
                         lst_layer
                         ) ;_ end of cons
         ) ;_ end of setq
       (vla-put-lock item :vlax-false)
       (vl-catch-all-apply
         '(lambda () (vla-put-freeze item :vlax-false))
         ) ;_ end of vl-catch-all-apply
       ) ;_ end of vlax-for
     (foreach blk_def
              (mapcar
                (function
                  (lambda (x)
                    (vla-item (vla-get-blocks adoc) x)
                    ) ;_ end of lambda
                  ) ;_ end of function
                ((lambda (/ res)
                   (foreach item (mapcar
                                   (function
                                     (lambda (x)
                                       (vla-get-name
                                         (vlax-ename->vla-object x)
                                         ) ;_ end of vla-get-name
                                       ) ;_ end of lambda
                                     ) ;_ end of function
                                   ((lambda (/ tab item)
                                      (repeat (setq tab  nil
                                                    item (sslength selset)
                                                    ) ;_ end setq
                                        (setq
                                          tab
                                           (cons
                                             (ssname selset
                                                     (setq item (1- item))
                                                     ) ;_ end of ssname
                                             tab
                                             ) ;_ end of cons
                                          ) ;_ end of setq
                                        ) ;_ end of repeat
                                      tab
                                      ) ;_ end of lambda
                                    )
                                   ) ;_ end of mapcar
                     (if (not (member item res))
                       (setq res (cons item res))
                       ) ;_ end of if
                     ) ;_ end of foreach
                   (reverse res)
                   ) ;_ end of lambda
                 )
                ) ;_ end of mapcar
       (vlax-for ent blk_def
         (vla-put-layer ent "0")
         (vla-put-color ent 0)
         (vla-put-lineweight ent aclnwtbyblock)
         (vla-put-linetype ent "byblock")
         ) ;_ end of vlax-for
       ) ;_ end of foreach
     (func_restore-layers)
     (vla-regen adoc acallviewports)
     ) ;_ end of progn
   ) ;_ end of if
 (vla-endundomark adoc)
 (princ)
 )

  

0 Likes
Message 4 of 7

Jake_Kohlmeier
Explorer
Explorer
How do I run this code? I'm still learning AutoCAD.
0 Likes
Message 5 of 7

LDShaw
Collaborator
Collaborator
Accepted solution

That is a big question. 
How computer literate are you? 
The simplest way is to drag this lsp into the dwg  drawing area (not command area) this will load it for THAT dwg. Then run fixblock. This will put everything in the blocks to layer 0. Once there whatever layer the block was brought into will be it's layer for everything.  

0 Likes
Message 6 of 7

Jake_Kohlmeier
Explorer
Explorer
This worked incredibly, I had absolutely no idea it was this easy. Thank you so much, you saved me hours of mind numbing work.
Message 7 of 7

LDShaw
Collaborator
Collaborator

lol yup. 100 lines of code can make something (look) easy. 

I've been using that one for years. 

0 Likes