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

0 Members and 1 Guest are viewing this topic.

abe123456789

  • Mosquito
  • Posts: 10
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: 134
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: 10
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: 22
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: 951
  • 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