ronjonp Posted September 9, 2022 Posted September 9, 2022 FWIW, ;; This (if (= nil wid_obj) (entmod (append ent (list (cons 370 wid_lay)))) ) ;; Could be written like this (if (not wid_obj) (entmod (append ent (list (cons 370 wid_lay)))) ) ;; OR this :) (or wid_obj (entmod (append ent (list (cons 370 wid_lay))))) Quote
mhupp Posted September 9, 2022 Posted September 9, 2022 (edited) This is what I use instead of if not. (if wid_obj (progn) ;do nothing (entmod (append ent (list (cons 370 wid_lay)))) ) Dropping the cdr will keep it (62 . #) So all you have to do is append (setq col_lay (assoc 62 layer_props)) (if (assoc 62 ent) ;checks for color code of entity (entmod (append ent col_lay)) ) Edited September 9, 2022 by mhupp Quote
ronjonp Posted September 9, 2022 Posted September 9, 2022 (edited) That works too. I have not read this thread too closely but wouldn't it be easier to create a new layer that matches the properties of the old layer then move the object to the new layer? This reminds me of the many add a prefix or suffix to a layer codes that have been written throughout the years. Edited September 9, 2022 by ronjonp Quote
Emmanuel Delay Posted September 9, 2022 Posted September 9, 2022 Sure, you can do that. In other languages (like Javascript) 0 and "" also get evaluated as not (!), that's why in my mind I want to see the expression only with nil and nothing else. Just for my state of peace Quote
mhupp Posted September 9, 2022 Posted September 9, 2022 (edited) 5 minutes ago, ronjonp said: That works too. I have not read this thread too closely but wouldn't it be easier to create a new layer that matches the properties of the old layer then move the object to the new layer? The lisp was to originally move objects to a new layer and keep their properties from the old layer. If the entity's were bylayer everything works as intended but if they have anything set it could be changed if the layer prop are different then the entity's so like @Emmanuel Delay did you have to test to see if they need to be changed or not. Edited September 9, 2022 by mhupp Quote
3dwannab Posted September 12, 2022 Posted September 12, 2022 Thanks @Emmanuel Delay. That works great. I've just added some error handling. ; Given a layer name, return a list of the properties of that layer. ; https://www.cadtutor.net/forum/topic/60523-change-layer-keep-original-properties-lisp-routine/?do=findComment&comment=600356 (defun layer_get_properties (Lay /) laydata (entget (tblobjname "Layer" Lay)) ) (defun c:Lay_Zero_Keep_Colour (/ *error* acDoc ss i obj Lay ent layer_props col_obj col_lay wid_obj wid_lay typ_obj typ_lay) (defun *error* (errmsg) (and acDoc (vla-EndUndoMark acDoc)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*")) (princ (strcat "\n<< Error: " errmsg " >>\n")) ) ) (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object))) (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc)) ;; user selects objects (setq ss (ssget)) (setq i 0) (repeat (sslength ss) ;; do for all select objects: (setq obj (ssname ss i)) (setq ent (entget obj)) ;; layer of the object (setq Lay (cdr (assoc 8 ent))) ;; layer properties (setq layer_props (layer_get_properties Lay)) ;; COLOR - property 62 ;; color of the object / color of the layer (setq col_obj (cdr (assoc 62 ent))) (setq col_lay (cdr (assoc 62 layer_props))) ;; Now we'll see if the object has set the color, or if the color is ByLayer. ;; If the color is ByLayer, then we should copy the color of the layer and set it to the object. (if (= nil col_obj) ;; object layer is set to ByLayer. (entmod (append ent (list (cons 62 col_lay)))) ) ;; Line width - property 370 (setq wid_obj (cdr (assoc 370 ent))) (setq wid_lay (cdr (assoc 370 layer_props))) (if (= nil wid_obj) (entmod (append ent (list (cons 370 wid_lay)))) ) ;; Line type - property 6 (setq typ_obj (cdr (assoc 6 ent))) (setq typ_lay (cdr (assoc 6 layer_props))) (if (= nil typ_obj) (entmod (append ent (list (cons 6 typ_lay)))) ) ;; set layer to "0" (entmod (subst (cons 8 "0") (assoc 8 ent) ent)) (setq i (+ i 1)) ) (*error* nil) (princ) ) 1 Quote
saunambon654 Posted November 14, 2022 Posted November 14, 2022 On 9/12/2022 at 3:13 PM, 3dwannab said: Thanks @Emmanuel Delay. That works great. I've just added some error handling. ; Given a layer name, return a list of the properties of that layer. ; https://www.cadtutor.net/forum/topic/60523-change-layer-keep-original-properties-lisp-routine/?do=findComment&comment=600356 (defun layer_get_properties (Lay /) laydata (entget (tblobjname "Layer" Lay)) ) (defun c:Lay_Zero_Keep_Colour (/ *error* acDoc ss i obj Lay ent layer_props col_obj col_lay wid_obj wid_lay typ_obj typ_lay) (defun *error* (errmsg) (and acDoc (vla-EndUndoMark acDoc)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*")) (princ (strcat "\n<< Error: " errmsg " >>\n")) ) ) (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object))) (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc)) ;; user selects objects (setq ss (ssget)) (setq i 0) (repeat (sslength ss) ;; do for all select objects: (setq obj (ssname ss i)) (setq ent (entget obj)) ;; layer of the object (setq Lay (cdr (assoc 8 ent))) ;; layer properties (setq layer_props (layer_get_properties Lay)) ;; COLOR - property 62 ;; color of the object / color of the layer (setq col_obj (cdr (assoc 62 ent))) (setq col_lay (cdr (assoc 62 layer_props))) ;; Now we'll see if the object has set the color, or if the color is ByLayer. ;; If the color is ByLayer, then we should copy the color of the layer and set it to the object. (if (= nil col_obj) ;; object layer is set to ByLayer. (entmod (append ent (list (cons 62 col_lay)))) ) ;; Line width - property 370 (setq wid_obj (cdr (assoc 370 ent))) (setq wid_lay (cdr (assoc 370 layer_props))) (if (= nil wid_obj) (entmod (append ent (list (cons 370 wid_lay)))) ) ;; Line type - property 6 (setq typ_obj (cdr (assoc 6 ent))) (setq typ_lay (cdr (assoc 6 layer_props))) (if (= nil typ_obj) (entmod (append ent (list (cons 6 typ_lay)))) ) ;; set layer to "0" (entmod (subst (cons 8 "0") (assoc 8 ent) ent)) (setq i (+ i 1)) ) (*error* nil) (princ) ) It is totally good, except one case: the entities in block (with have layer 0, color=bylayer). Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.