Author Topic: Layer on and off button  (Read 473 times)

0 Members and 1 Guest are viewing this topic.

abe123456789

  • Mosquito
  • Posts: 13
Layer on and off button
« on: May 14, 2022, 11:55:42 PM »
Good evening,

I am trying to create a command that turn on the layer if the layer is off and vise versa.
Sort like the light bulb on the layers tab, but i keep getting this error.

; ----- Error around expression -----
;
(C:AG_LAYTOG)


; error : too few / too many arguments at [C:AG_LAYTOG]

What am i doing wrong?


Thank you in advance.

Code: [Select]

(defun c:Ag_LayTog (fpStat / LayPropFp EntLayPropFp)

;;get entity
  (setq LayPropFp (tblobjname "Layer" "Floor Plan"))
;;turn entity to list
  (setq EntLayPropFp (entget LayPropFp))
;(setq fpStat (subst (cons 62  7) (assoc 62 EntLayPropFp) EntLayPropFp))


  (if (= fpStat t)
    (progn
      (setq fpStat (subst (cons 62 -7) (assoc 62 EntLayPropFp) EntLayPropFp))  ; turn off
      (entmod fpStat)
      (setq fpStat nil)
    )  ;(progn
    (progn
      (setq fpStat (subst (cons 62 7) (assoc 62 EntLayPropFp) EntLayPropFp))  ; turn on
      (entmod fpStat)
      (setq fpStat t)

    )  ;progn

  )    ;end if


)      ;end of defund




mhupp

  • Newt
  • Posts: 187
Re: Layer on and off button
« Reply #1 on: May 15, 2022, 12:56:19 AM »
The error your getting is because of the first line of code.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Ag_LayTog (fpStat / LayPropFp EntLayPropFp)
Anything to the left of the forward slash needs to be given when calling the lisp.

Example
http://docs.autodesk.com/ACDMAC/2014/ENU/index.html?url=files/GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D.htm,topicNumber=d30e313220

Code - Auto/Visual Lisp: [Select]
  1. (defun ARGTEST ( arg1 arg2 / ccc )
  2.   (setq ccc "Constant string")
  3.   (strcat ccc ", " arg1 ", " arg2)
  4. )
  5.  
  6. (ARGTEST "String 1" "String 2")
  7. "Constant string, String 1, String 2"

Maybe use this code by ronjonp its one simple if statement.
https://www.theswamp.org/index.php?topic=22016.msg265793#msg265793

Code - Auto/Visual Lisp: [Select]
  1. (defun C:Ag_LayTog (/ lay)
  2.   (if (= (vla-get-LayerOn (setq lay (vlax-ename->vla-object (tblobjname "layer" "Floor Plan")))) :vlax-true)
  3.     (vla-put-layeron lay :vlax-false)
  4.     (vla-put-layeron lay :vlax-true)
  5.   )
  6.   (princ)
  7. )
  8.  

You could also check multiple layers with the following
(Ag_LayTog-lst '("Floor Plan" "NewLayer1" "NewLayer2" "NewLayer3"))

Code - Auto/Visual Lisp: [Select]
  1. (defun Ag_LayTog-lst (lst / lay)
  2.   (foreach name lst
  3.     (if (= (vla-get-LayerOn (setq lay (vlax-ename->vla-object (tblobjname "layer" name)))) :vlax-true)
  4.       (vla-put-layeron lay :vlax-false)
  5.       (vla-put-layeron lay :vlax-true)
  6.     )
  7.   )
  8.   (princ)
  9. )

abe123456789

  • Mosquito
  • Posts: 13
Re: Layer on and off button
« Reply #2 on: May 15, 2022, 12:20:50 PM »


ronjonp code is what im looking for it is so simple.
Any recommendations on what books to learn active x ?


I currently have
Autocad Platform customization - Lee ambrosius
Visual Lisp Developer bible - David Stein
The language and its development enviroment - Reinaldo N. Togores

Thank you mhupp


stevej

  • Newt
  • Posts: 24
Re: Layer on and off button
« Reply #3 on: May 15, 2022, 08:16:05 PM »
As an alternative, this is what I've used for years. A simple layer toggle.
This example is for the TEXT layer. If the layer is OFF, it is turned ON. If the layer is ON, it is turned OFF.

Code: [Select]
(defun C:TOGGLETEXT (/ elist clr)
  (setq elist (entget (tblobjname "LAYER" "TEXT"))
        clr   (assoc 62 elist)
  )
  (entmod (subst (cons 62 (- (cdr clr))) clr elist))
  (princ)
)

If the layer you wish to toggle might not yet be in the drawing, a check for the layer can be performed first with this version, which includes a notification that the layer does not exist..

Code: [Select]
(defun C:TOGGLETEXT (/ elist clr)
  (if (not (tblsearch "LAYER" "TEXT"))
    (alert
      (strcat "                   TEXT Layer"
              "\n    Does not exist on this drawing"
      )
    )
    (progn
      (setq elist (entget (tblobjname "LAYER" "TEXT"))
            clr   (assoc 62 elist)
      )
      (entmod (subst (cons 62 (- (cdr clr))) clr elist))
    )
  )
  (princ)
)

Steve

BIGAL

  • Swamp Rat
  • Posts: 1063
  • 40 + years of using Autocad
Re: Layer on and off button
« Reply #4 on: May 15, 2022, 10:44:31 PM »
abe12345678 Reinaldo N. Togores has 4 books available I bought them on kindle way cheaper than hard copy. Nice thing is can copy and paste code.
A man who never made a mistake never made anything

Lee Mac

  • Seagull
  • Posts: 12711
  • London, England
Re: Layer on and off button
« Reply #5 on: June 06, 2022, 03:58:03 PM »
I've always quite liked this alternative -
Code - Auto/Visual Lisp: [Select]
  1. (defun laytog ( lay / obj )
  2.     (if (setq obj (tblobjname "layer" lay))
  3.         (vlax-put (setq obj (vlax-ename->vla-object obj)) 'layeron (~ (vlax-get obj 'layeron)))
  4.     )
  5.     (princ)
  6. )
[Method Explanation]

Example:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ag_laytog ( ) (laytog "Floor Plan"))




tombu

  • Bull Frog
  • Posts: 278
  • ByLayer=>Not0
Re: Layer on and off button
« Reply #6 on: June 07, 2022, 08:14:52 AM »
I prefer to toggle layers frozen/thawed and have used a modified version of Jimmy Bergmark's https://jtbworld.com/autocad-layer-toggle-freeze-lsp for that purpose as it toggles lists of layers not just one at a time.
If the layer isn't in the drawing I modified it to use Lee Mac's "Steal From Drawing" http://www.lee-mac.com/steal.html to import the layer from a template file with all the properties we use for that layer.
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D