What it does
Assign Selection to Existing Layer with the TOLAYER AutoLISP command
A conservative layer-assignment helper. It checks that the target layer already exists, then updates only the selected entities.
- AutoLISP command
- TOLAYER
- Difficulty
- Beginner
Applicable disciplines
CAD workflows that use this routine
Requirements and compatibility
What it needs
Software rows marked “Not tested” are not compatibility claims. Test the routine in a copy of your drawing and CAD version.
Downloadable source code
Review the assign-layer.lsp AutoLISP code
;; CADLisp Tools - Assign Selection to Existing Layer
(defun ct-tl:layer-state (name / row flags color)
(if (setq row (tblsearch "LAYER" name))
(progn
(setq flags (cdr (assoc 70 row)) color (cdr (assoc 62 row)))
(cond
((/= 0 (logand flags 16)) "xref-dependent")
((/= 0 (logand flags 4)) "locked")
((/= 0 (logand flags 1)) "frozen")
((< color 0) "off")
(T "usable")))))
(defun ct-tl:editable-p (data / row flags)
(and data
(setq row (tblsearch "LAYER" (cdr (assoc 8 data))))
(setq flags (cdr (assoc 70 row)))
(= 0 (logand flags 20))))
(defun c:TOLAYER (/ *error* changed data entity index layer set skipped state undo-started)
(defun *error* (message)
(if undo-started (command-s "_.UNDO" "_End"))
(if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
(prompt (strcat "\nTOLAYER error: " message)))
(princ))
(setq layer (getstring T "\nExisting target layer: ")
state (ct-tl:layer-state layer))
(cond
((null state) (prompt "\nLayer not found; no objects changed."))
((/= state "usable")
(prompt (strcat "\nTarget layer is " state "; no objects changed.")))
(T
(prompt "\nSelect objects to move: ")
(if (setq set (ssget))
(progn
(setq index 0 changed 0 skipped 0)
(if (= 0 (logand 8 (getvar "UNDOCTL")))
(progn (command "_.UNDO" "_Begin") (setq undo-started T)))
(repeat (sslength set)
(setq entity (ssname set index) data (and entity (entget entity)))
(if (and (ct-tl:editable-p data) (assoc 8 data)
(entmod (subst (cons 8 layer) (assoc 8 data) data)))
(progn (entupd entity) (setq changed (1+ changed)))
(setq skipped (1+ skipped)))
(setq index (1+ index)))
(if undo-started (progn (command "_.UNDO" "_End") (setq undo-started nil)))
(prompt (strcat "\nMoved " (itoa changed) " object(s); skipped " (itoa skipped) ".")))
(prompt "\nNo objects selected."))))
(princ))
Setup
How to install the assign-layer.lsp file
- 1Review the routine with your CAD standards owner.
- 2Load the file with APPLOAD.
- 3Confirm the destination layer is unlocked before use.
Command workflow
How to run the TOLAYER command
- 1Run TOLAYER.
- 2Type the exact name of an existing target layer.
- 3Select the objects to move and press Enter.
Worked example
Example: Move selected annotation objects to a standard annotation layer.
- Confirm the destination layer exists.
- Run TOLAYER and enter that layer name.
- Select only the intended annotations.
The selected objects now reference the requested layer.
Read before use
Notes and limitations
- Does not create missing layers.
- Locked-layer and referenced-object restrictions still apply.
- Does not change object overrides.
Version history