Change block objects to layer 0 and keep objects colors and other properties

Change block objects to layer 0 and keep objects colors and other properties

Anonymous
Not applicable
5,125 Views
17 Replies
Message 1 of 18

Change block objects to layer 0 and keep objects colors and other properties

Anonymous
Not applicable

Hello. I was looking for a lisp that could change any objcects inside a block to layer 0 but i don't want to change its lintypes, linewights and colors. I only need the inside layers to be changed so i can freeze the layer if a want to, but when turning it on i want it to have the objects properties and not the layers properties where i placed it.

 

"setbylayer" does the oposite, keeping the inside layers but changing the properties i wanto to change. And i found a very interesting "block layer to 0" lisp, but it changes everything to layer 0 and sets all inside objects properties to by layer.

 

im looking for some lisp that does the same but keeping the other properties as they are.

 

Hope somebody can help me

 

Thankyou very much

0 Likes
5,126 Views
17 Replies
Replies (17)
Message 2 of 18

Sea-Haven
Mentor
Mentor

Something like this you did not say how to pick block name.

 

(vlax-for block (vla-get-blocks doc) 
(if   (= (vla-get-name block) "your block name")
(vlax-for ent block 
(vla-put-layer ent "0") 
)
)
)
0 Likes
Message 3 of 18

Anonymous
Not applicable

no, in my case i have several diferent blocks so i would like to pick various different blocks and aply the layer 0 to the objects inside those blocks. i'm not sure but starting with the "blok to layer0" lisp anyone can change the code to keep the other object properties unchanged.

here is the block to layer0 lisp:

 

(defun c:blkto0 ( / idx lst sel )
(if (setq sel (ssget '((0 . "INSERT"))))
(repeat (setq idx (sslength sel))
(block->0 (cdr (assoc 2 (entget (ssname sel (setq idx (1- idx)))))))
)
)
(command "_.regen")
(princ)
)
(defun block->0 ( blk / ent enx )
(cond
( (member blk lst))
( (setq ent (tblobjname "block" blk))
(while (setq ent (entnext ent))
(entmod (subst-append 8 "0" (subst-append 62 256 (setq enx (entget ent)))))
(if (= "INSERT" (cdr (assoc 0 enx)))
(block->0 (cdr (assoc 2 enx)))
)
)
(setq lst (cons blk lst))
)
)
)
(defun subst-append ( key val lst / itm )
(if (setq itm (assoc key lst))
(subst (cons key val) itm lst)
(append lst (list (cons key val)))
)

0 Likes
Message 4 of 18

Moshe-A
Mentor
Mentor

@Anonymous  hi,

 

Here is some info you should know about AutoCAD block reference (insert). say you have some blocks reference in "layer1", if you freeze it, all blocks goes to 'sleep' 😀. so to answer your question, change all those blocks to some "sleep" layer,  and you can do it quickly using QSelect or SSX commands.

 

Moshe

 

0 Likes
Message 5 of 18

Anonymous
Not applicable

yes, in the freeze case i realized that but if i wanto to turn layon or layoff all the other layers inside the block that are not in layer 0 stay on! 

0 Likes
Message 6 of 18

Moshe-A
Mentor
Mentor

i prefer sending the block to 'sleep' layer (and freeze it) instead of modifying it and send it to 0 layer. doing latest is more work and you are losing it's origin state. modify the block only if you have no other option.

 

0 Likes
Message 7 of 18

Anonymous
Not applicable

i don't have any problem with that but i'm working with a team that only works with autocad sometimes and when they do, they want to be quick turning layers on and off instead of freezing it. The reason to ask for this kind of lisp is to regularize the blocks and have the same workflow for everyone in a template. They realized that changing inside block layers to 0 and keep other properties unchanged would do what they want but i have almost 300 bloks and have to get inside of each one, and do it manualy 😕

 

0 Likes
Message 8 of 18

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... I was looking for a lisp that could change any objcects inside a block to layer 0 but i don't want to change its lintypes, linewights and colors. ....


Try BENL0CL.lsp, >here< [= Block Entities, including in Nested blocks, to Layer 0 with Color & Linetype from entity's original layer].  {Note that there's a zero in the name, not a capital O.}  If you don't want to do it to parts of Nested Blocks, use the one earlier in that thread without the N in the name.

 

If it does what you want regarding colors and linetypes, lineweights can be added.

Kent Cooper, AIA
0 Likes
Message 9 of 18

Anonymous
Not applicable

yes, is somethig like this, but is not perfect. i noticed that the lisp changed the enteties to the layer properties but those enteties that are in "by layer" get the layer properties instead of remaining "by layer" 

 

in my mind i have the vision to enter inside the block select all objetcs and just change to layer 0, seams so simple but i don't have any idea how to do it in code 😄 

0 Likes
Message 10 of 18

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

yes, is somethig like this, but is not perfect. i noticed that the lisp changed the enteties to the layer properties but those enteties that are in "by layer" get the layer properties instead of remaining "by layer" 

 

... to enter inside the block select all objetcs and just change to layer 0....


That seems to contradict your original description, "...change any objcects inside a block to layer 0 but i don't want to change its lintypes, linewights and colors."  Does that not mean that if something is on a Layer that's red, you want it to change to being on Layer 0 but still be red, because that's the color of its original Layer?  If an entity's properties that are ByLayer remain that way, and the entity is put on Layer 0, it will change its visible properties [except any of them that happen to be the same as Layer 0's properties].  Is that what you want?  That's comparatively simple -- see my BENL0 command, >here<.

Kent Cooper, AIA
0 Likes
Message 11 of 18

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:
.... see my BENL0 command, >here<.

... and you can incorporate the adjustment in the next Message there.

Kent Cooper, AIA
0 Likes
Message 12 of 18

Anonymous
Not applicable

i'm not a native english speaker so that's why i may have a little difficulty to express myself, i'm sorry 😅

that's what i want, if in some cases there are "by layer" enteties inside the block, i want them to change as the "outside" layer of the block changes its visual porperties but all the other colors remain unchanged

 

0 Likes
Message 13 of 18

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... if in some cases there are "by layer" enteties inside the block, i want them to change as the "outside" layer of the block changes its visual porperties but all the other colors remain unchanged


Then yes, I think the BENL0 command will do what you want.

Kent Cooper, AIA
0 Likes
Message 14 of 18

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

.... Try BENL0CL.lsp ... [= Block Entities, including in Nested blocks, to Layer 0 with Color & Linetype from entity's original layer].  ....

If it does what you want regarding colors and linetypes, lineweights can be added.


In case someone finds this and wants that kind of functionality but also including lineweights, attached is a modification that adds them, the cumbersomely-named BENL0CLW.lsp.  The second L is still for Linetype; the W is for lineWeight.

Kent Cooper, AIA
0 Likes
Message 15 of 18

john.uhden
Mentor
Mentor

I think you are crazy to be subjecting layer "0" to freezing or turning off.

I guess it's okay if you like masochism, but if you work with others on the same files, then your idea is criminal.  I trust you have a different vocation you can pursue.

John F. Uhden

0 Likes
Message 16 of 18

LDShaw
Collaborator
Collaborator

Replied to the wrong post Sorry to open this up again.

See if this does what your looking for.

 

(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)
 )

 

I've been using it for a while. You can do singles or just pick all for everything in the DWG

0 Likes
Message 17 of 18

timothy_crouse
Collaborator
Collaborator

This lisp uses functionality borrowed from LEE MAC APPLY TO BLOCK OBJECTS

Apply to Block Objects | Lee Mac Programming (lee-mac.com)

Its purpose is to set objects or objects inside the block to LAY 0 and set properties to BYLAY.

I created it because I did not see the same results from the SETBYLAYER command.

 

 

I should warn you in advance this is a GPT spinoff so if you have the skills to streamline or improve the operation please do.

 

Best Regards

-Tim C.

0 Likes
Message 18 of 18

aliff_ahtenk
Enthusiast
Enthusiast

Can this adjust to another conditions which is
ByBlock -> to follow ByLayer condition?

0 Likes