Jump to content

Change Layer-Keep original properties LISP routine


Recommended Posts

Posted

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

 

Posted (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 by mhupp
Posted (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 by ronjonp
Posted

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 

Posted (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 by mhupp
Posted

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

 

  • Like 1
  • 2 months later...
Posted
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).

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...