Jump to content

Change to prev layer


RBrigido

Recommended Posts

Hi guys, what's up?

 

I'm trying to create a lisp routine that change me to the Layer -1.

 

As a example: I have Objects on layers A, B, C and D. If I click under an object in a Layer D, the currency layer turns layer C. If I click under an object in a Layer B, the currency layer turns layer A.

 

I made some tried like a simply code:

 

Quote

(defun c:LayerChange (/ ent layer)
  (setq ent (car (entsel "\nSelect a object in Layer Z: ")))
  (if (setq layer (cdr (assoc 8 (entget ent))))
    (progn
      (command "_.-layer" "_s" (strcat layer) "")
      (prompt (strcat "\nMovido para o Layer " (getvar 'clayer))))
    (prompt "\nSelect a valid object  in Layer Z."))
  (princ))

 

And also a code more bigger:

 

Quote

(defun layer-previous (layer-name)
  (setq layer-list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
  (setq previous-layer nil)
  (setq found-layer nil)

  (vlax-for layer layer-list
    (setq current-layer-name (vla-get-name layer))
    (cond
      ((and found-layer (not previous-layer))
       (setq previous-layer current-layer-name))
      ((eq current-layer-name layer-name)
       (setq found-layer t))
      (t (setq previous-layer current-layer-name)))
    )
  previous-layer
)

(defun c:layer-previous ()
  (setq obj (car (entsel "\nSelect an object: ")))
  (if (and obj (member (cdr (assoc 0 (entget obj))) '("ARC" "CIRCLE" "ELLIPSE" "LINE" "LWPOLYLINE" "POLYLINE" "SPLINE")))
    (progn
      (setq layer-name (cdr (assoc 8 (entget obj))))
      (setq previous-layer (layer-previous layer-name))
      (if previous-layer
        (command "_.-LAYER" "_SET" previous-layer "")
        (prompt (strcat "\nThe selected object is on the topmost layer (\"" layer-name "\").")))
    )
    (prompt "\nSelect a valid object.")
  )
  (princ)
)

(princ)
 

 

But this last one just say that no one object is valid.

 

Can someone help me with that?

 

Many thanks

 

Rodrigo

Link to comment
Share on other sites

Try this: (Noting the code tags are the <> button above)

 

(defun c:PrevLay ( / MyLayers MySS MyEnt Lay-Name) ;;Local variables defined, initially nil when LISP runs
  ;;Sub function that can get the liosty of layers
  ;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/get-list-of-all-layers-in-lisp/td-p/822262
  (defun Table (s / d r)
    (while (setq d (tblnext s (null d)))
      (setq r (cons (cdr (assoc 2 d)) r))
    ) ; end while
   r ; return r
  ) ; end defun

  (setq MyLayers (acad_strlsort (Table "layer")) ) ; use sub function above and get a sorted list of layers
  (setq MySS (ssget "_+.:E:S" '((-4 . "<OR") (0 . "*LINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "CIRCLE")  (-4 . "OR>"))) ) ;select an item, filtered list
  (if (= (sslength MySS) 1) ; If a valid object was selected
    (progn
      (setq MyEnt (ssname MySS 0)) ; get entity name
      (setq Lay-Name (cdr (assoc 8 (entget MyEnt)))) ; get entity layer
      (setq Lay-Name_Pos (vl-position Lay-Name MyLayers)) ; find layer position in layers list
      (if (= Lay-Name_Pos 0)
        (setq New_Lay_Pos 0) ; if layer is the 1st in the lsit, do nothing
        (setq New_Lay_Pos (- Lay-Name_Pos 1)) ; go up 1 layer aphabetically
      ) ; end if
    ) ; end progn
  ) ; end if
  (command "_.-LAYER" "_SET" (nth New_Lay_Pos MyLayers) "") ; make active layer
)

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, Steven P said:

Try this: (Noting the code tags are the <> button above)

 

(defun c:PrevLay ( / MyLayers MySS MyEnt Lay-Name) ;;Local variables defined, initially nil when LISP runs
  ;;Sub function that can get the liosty of layers
  ;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/get-list-of-all-layers-in-lisp/td-p/822262
  (defun Table (s / d r)
    (while (setq d (tblnext s (null d)))
      (setq r (cons (cdr (assoc 2 d)) r))
    ) ; end while
   r ; return r
  ) ; end defun

  (setq MyLayers (acad_strlsort (Table "layer")) ) ; use sub function above and get a sorted list of layers
  (setq MySS (ssget "_+.:E:S" '((-4 . "<OR") (0 . "*LINE")(0 . "ARC")(0 . "ELLIPSE")(0 . "CIRCLE")  (-4 . "OR>"))) ) ;select an item, filtered list
  (if (= (sslength MySS) 1) ; If a valid object was selected
    (progn
      (setq MyEnt (ssname MySS 0)) ; get entity name
      (setq Lay-Name (cdr (assoc 8 (entget MyEnt)))) ; get entity layer
      (setq Lay-Name_Pos (vl-position Lay-Name MyLayers)) ; find layer position in layers list
      (if (= Lay-Name_Pos 0)
        (setq New_Lay_Pos 0) ; if layer is the 1st in the lsit, do nothing
        (setq New_Lay_Pos (- Lay-Name_Pos 1)) ; go up 1 layer aphabetically
      ) ; end if
    ) ; end progn
  ) ; end if
  (command "_.-LAYER" "_SET" (nth New_Lay_Pos MyLayers) "") ; make active layer
)

 

 

 

Worked really fine.

 

My intention is click under a line like Kerbline and change me to Kerbline_LVL and start to create a Point Levels. The lisp routine to create point levels is working fine, but I was stalling on the part of switching to the previous layer.

 

Thank you very much Steven!!

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...