autocadautolispbricscad

Is there a way to quickly set a selection to a specific layer using AutoLisp?


I am learning autolisp and currently using Bricscad. I would like to select all objects on screen, then run a lisp program to set the selected objects to a specific layer.

Here is the code I have tried:

(defun c:aametal (/)
    (command "._chprop" "_la" "AAMETAL" "" "")
    
    (princ "Entities Set to AAMETAL Successfully")
)

It works if I copy "._chprop" "_la" "AAMETAL" "" "" and paste it in to the command line, but will not run when I load it.


Solution

  • You'll need to acquire & pass the selection to the command - this can be achieved using the ssget function, e.g.:

    (defun c:aametal ( / sel )
        (if (setq sel (ssget "_:L"))
            (progn
                (command "_.chprop" sel "" "_la" "AAMETAL" "")
                (princ "Entities Set to AAMETAL Successfully")
            )
            (princ "\nNo objects found.")
        )
        (princ)
    )
    

    Here I'm using the "_:L" mode string to exclude objects on locked layers - you can find more information about ssget mode strings here.

    Note that this assumes that the target layer already exists in the drawing.