• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Visual LISP, AutoLISP and General Customization

    Reply
    Valued Contributor
    msarqui
    Posts: 87
    Registered: ‎09-14-2010
    Accepted Solution

    Laydel by lisp

    272 Views, 9 Replies
    01-27-2013 04:29 PM

    Hi guys,

     

    Based on the tip from *Paul Turvill : http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Delete-a-Layer-with-Laydel/td-p/21742... I made this routine:

     

    (defun c:smileyvery-happy:eleteLayerTavail ()

    (setq ss (ssget "x" '((8 . "Tx-Travail"))))

    (command "_.erase" ss "")

    (princ)

    )

     

    The routine works great but I notice that if I don’t have this layer, it will return an error. Could someone update this to just ignore if the drawing don’t have the layer Tx-Travail?

    I need this because I will use this routine together with another routines and in some cases I will not have the layer specified.

     

    Thanks for help!

    Please use plain text.
    *Expert Elite*
    Posts: 2,070
    Registered: ‎11-24-2009

    Re: Laydel by lisp

    01-27-2013 07:10 PM in reply to: msarqui

    msarqui wrote:

    Hi guys,

     

    Based on the tip from *Paul Turvill : http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Delete-a-Layer-with-Laydel/td-p/21742... I made this routine:

     

    (defun c:smileyvery-happy:eleteLayerTavail ()

    (setq ss (ssget "x" '((8 . "Tx-Travail"))))

    (command "_.erase" ss "")

    (princ)

    )

     

    The routine works great but I notice that if I don’t have this layer, it will return an error. Could someone update this to just ignore if the drawing don’t have the layer Tx-Travail?

    I need this because I will use this routine together with another routines and in some cases I will not have the layer specified.

     

    Thanks for help!


    (defun c:DeleteLayerTavail  ()
          (if (setq ss (ssget "x" '((8 . "Tx-Travail"))))
          		(command "_.erase" ss ""))
          (princ)
          )
    
    

    or 

     

    (defun DeleteLayer (lay)
          (cond ((and
    					(tblsearch "LAYER" lay)
    					(setq ss (ssget "x" (list (cons 8 lay))))
              (repeat (setq i (sslength ss))
                    	(entdel (ssname ss (setq i (1- i)))))
          		)))
          (princ)
          )

     

    (deleteLayer "layername")

     

    Please use plain text.
    Valued Contributor
    msarqui
    Posts: 87
    Registered: ‎09-14-2010

    Re: Laydel by lisp

    02-11-2013 04:56 PM in reply to: pbejse

    Why it does not work if the layer is not in the current space.

    Can be set to erase the layer in both spaces like the LAYDEL command?

    Thanks!

    Please use plain text.
    *Expert Elite*
    Posts: 2,070
    Registered: ‎11-24-2009

    Re: Laydel by lisp

    02-11-2013 06:58 PM in reply to: msarqui

    msarqui wrote:

    Why it does not work if the layer is not in the current space.

    Can be set to erase the layer in both spaces like the LAYDEL command?

    Thanks!


     

    Try this instead

     

    (defun DeleteLayer  (lay)
      (vlax-for layout  (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for i  (vla-get-block layout)
          (if (eq (vla-get-layer i) lay)
            (vla-delete i)))
        )
      )

     

    Please use plain text.
    Valued Contributor
    msarqui
    Posts: 87
    Registered: ‎09-14-2010

    Re: Laydel by lisp

    02-12-2013 10:08 AM in reply to: pbejse

    Hello pbejse

     

    I start to understand and do some basic routines but my knowledge is still very small. So when I see these VLA things I do not understand at all. This is like chinese language to me :smileysad:

    So, I tried this :

     

    (defun c:smileyvery-happy:eleteLayer (lay)
    (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for Tx-Travail (vla-get-block layout)
    (if (eq (vla-get-layer Tx-Travail) lay)
    (vla-delete Tx-Travail)))
    )
    )

     

    And I got this error:  Command: too few arguments

     

    Thanks for your time.

    Please use plain text.
    *Expert Elite*
    Posts: 2,070
    Registered: ‎11-24-2009

    Re: Laydel by lisp

    02-12-2013 08:05 PM in reply to: msarqui

    msarqui wrote:

    Hello pbejse

     

    I start to understand and do some basic routines but my knowledge is still very small. So when I see these VLA things I do not understand at all. This is like chinese language to me :smileysad:

    So, I tried this :

      

    And I got this error:  Command: too few arguments

     

    Thanks for your time.


     msarqui

     

    To use  snippet i posted  

    (DeleteLayer "Tx-Travail")

     

    It is a generic routine that accepts a layer name as argument 

     

    If you need to a command to work for a specific layer 

     

    (defun c:smileyvery-happy:eleteLayerTavail ()(DeleteLayer "Tx-Travail"))

     

    Also take note that the layer name is case sensitive: in that case we can modify the routine to

     

    (defun DeleteLayer  (lay)
      (vlax-for layout  (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for i  (vla-get-block layout)
          (if (eq  (strcase (vla-get-layer i))
                       (strcase lay))
            (vla-delete i)))
        )
      )

     

    Better yet , to use the routine for one or more layer names

     

    (defun DeleteLayer  (lst)
      (vlax-for layout  (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for i  (vla-get-block layout)
          (if (member (strcase (vla-get-layer i))
                      (mapcar 'strcase lst))
            (vla-delete i)))
        )
      )

     

    (DeleteLayer '("Tx-Travail" "otherLayer" "AnotherLayer"))

     

    Now, going back to your original post

     

    (defun c:smileyvery-happy:eleteLayerTavail ()(DeleteLayer '("Tx-Travail")))

     

    HTH

    Please use plain text.
    Valued Contributor
    msarqui
    Posts: 87
    Registered: ‎09-14-2010

    Re: Laydel by lisp

    02-13-2013 04:49 PM in reply to: pbejse

    This two works great and realy fast:

     

    (defun c:dlt1 ()(DeleteLayer "Tx-Travail"))
    (defun DeleteLayer (lay)
    (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for i (vla-get-block layout)
    (if (eq (strcase (vla-get-layer i))
    (strcase lay))
    (vla-delete i)))
    )
    )

    ;------------------------------------------------------------------------;

    (defun c:dlt2 ()(DeleteLayer "Tx-Travail"))
    (defun DeleteLayer (lay)
    (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for i (vla-get-block layout)
    (if (eq (vla-get-layer i) lay)
    (vla-delete i)))
    )
    )

    ;------------------------------------------------------------------------;

     

    But this one with the list returns "nil" and do nothing:

     

    (defun c:dlt3 ()(DeleteLayer '("Tx-Travail" "Tx-1" "Tx-2" "Tx-3")))
    (defun DeleteLayer (lst)
    (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for i (vla-get-block layout)
    (if (member (strcase (vla-get-layer i))
    (mapcar 'strcase lst))
    (vla-delete i)))
    )
    )

    ;------------------------------------------------------------------------;

     

    Regards,

    Please use plain text.
    *Expert Elite*
    Posts: 2,070
    Registered: ‎11-24-2009

    Re: Laydel by lisp

    02-13-2013 07:39 PM in reply to: msarqui

    msarqui wrote:

     

    But this one with the list returns "nil" and do nothing:

     

    (defun c:dlt3 ()(DeleteLayer '("Tx-Travail" "Tx-1" "Tx-2" "Tx-3")))
    (defun DeleteLayer (lst)
    (vlax-for layout (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for i (vla-get-block layout)
    (if (member (strcase (vla-get-layer i))
    (mapcar 'strcase lst))
    (vla-delete i)))
    )
    )

    ;------------------------------------------------------------------------;

     

    Regards,


    Works on my end , its either there are no objects on the listed layer or the layer doesnt exist. or worse the entities are on a locked layer.

     

    What you can do is add a sub to unlock all layers then invoke the command

     

    (defun DeleteLayer  (lst / n)
      (setq n 0)
      (vlax-for layout  (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for i  (vla-get-block layout)
          (if (member (strcase (vla-get-layer i)) (mapcar 'strcase lst))
            	(progn (vla-delete i)
                      	(setq n (1+ n)))
            )
          )
        )
      (princ (strcat "\n" (itoa n)  " Objects deleted"))
      (princ)
      )

     HTH

     

    Please use plain text.
    Valued Contributor
    msarqui
    Posts: 87
    Registered: ‎09-14-2010

    Re: Laydel by lisp

    02-15-2013 08:05 AM in reply to: pbejse

    I got this error: Command: Automation Error. On locked layer

     

    So I made this:

     

    (defun DeleteLayer  (lst / n)
    (command "-layer" "s" "0" "u" "*" "on" "*" "t" "*" "")
      (setq n 0)
      (vlax-for layout  (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (vlax-for i  (vla-get-block layout)
          (if (member (strcase (vla-get-layer i)) (mapcar 'strcase lst))
             (progn (vla-delete i)
                       (setq n (1+ n)))
            )
          )
        )
      (princ (strcat "\n" (itoa n)  " Objects deleted"))
      (princ)
      )

     

    And now it works. :smileyhappy:

    Please use plain text.
    *Expert Elite*
    Posts: 2,070
    Registered: ‎11-24-2009

    Re: Laydel by lisp

    02-16-2013 03:00 AM in reply to: msarqui

    msarqui wrote:

    I got this error: Command: Automation Error. On locked layer

     

    So I made this:

     

    (defun DeleteLayer  (lst / n)
    (command "-layer" "s" "0" "u" "*" "on" "*" "t" "*" "")
    .....

    And now it works. :smileyhappy:


    Good for you msarqui :smileyhappy:

     

    "i'll leave the "unlocking" to the user"

     

    BTW: (command "_layerp");;<----- 

     

    Please use plain text.