Author Topic: Help needed to get the code to work  (Read 608 times)

0 Members and 1 Guest are viewing this topic.

cadd4la

  • Newt
  • Posts: 27
Help needed to get the code to work
« on: May 09, 2023, 11:30:12 PM »
Hi everyone,

I need help getting this code to work. I'm looking for the code to create a new layer with a given name, color, and layer description, then move all the items on a given layer to this new layer.

Code: [Select]
(defun c:move_from_defpoints ()
  (setq ssl0 (ssget "x" '((8 . "Defpoints"))))
  (if (not ssl0)
    (progn
      (princ "\nNo objects found on Defpoints layer.")
      (exit)
    )
    (progn
      (setq layername "fromDefpoints")
      (if (tblsearch "LAYER" layername)
        (command "_-LAYDEL" layername "")
      )
      (command "_-LAYER" "N" layername "C" "200" "" "Items moved from the Defpoints layer" "")
      (command "_.CHPROP" ssl0 "" "LA" layername "")
      (princ (strcat "\nObjects moved to layer: " layername))
    )
  )
  (princ)
)

I was able to get the code to make the new layer with the name only but it will not make the layer color or the layer description that I want and it changes the current layer color to the color that I want the new layer to be.

Thanks,

Cadd4la
« Last Edit: May 09, 2023, 11:34:50 PM by cadd4la »
Windows 10 x64 - AutoCAD 2023

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Help needed to get the code to work
« Reply #1 on: May 10, 2023, 12:06:53 PM »
Welcome to TheSwamp. Here's some code with comments, maybe you can learn from it. My first advice would be to try and stay away from command calls:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:move_from_defpoints (/ layername newlayer ssl0);;<-Localize variables ( anything SETQ )
  2.   (if (not (setq ssl0 (ssget "x" '((8 . "Defpoints")))))
  3.     (princ "\nNo objects found on Defpoints layer.")
  4.     (progn (setq layername "fromDefpoints")
  5.            ;; Create the new layer object
  6.                                    layername
  7.                           )
  8.            )
  9.            ;; Change the color
  10.            (vla-put-color newlayer 200)
  11.            ;; Add a description
  12.            (vla-put-description newlayer "Items moved from the Defpoints layer")
  13.            ;; Convert the selection set to a list and go through each item
  14.            (foreach e (mapcar 'cadr (ssnamex ssl0))
  15.              ;; Catch-all just incase layer defpoints is locked ( or we could check above )
  16.              (vl-catch-all-apply 'vla-put-layer (list (vlax-ename->vla-object e) layername))
  17.            )
  18.            ;; Print your message
  19.            (princ (strcat "\nObjects moved to layer: " layername))
  20.     )
  21.   )
  22.   (princ)
  23. )
  24. ;; Loads ActiveX functions vla-*
« Last Edit: May 10, 2023, 12:10:47 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cadd4la

  • Newt
  • Posts: 27
Re: Help needed to get the code to work
« Reply #2 on: May 12, 2023, 10:46:45 AM »
Ronjonp,

Thank you for your help, it works great!

Cadd4la
Windows 10 x64 - AutoCAD 2023