Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
msarqui
Posts: 131
Registered: ‎09-14-2010
Message 1 of 10 (589 Views)
Accepted Solution

Laydel by lisp

589 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!

*Expert Elite*
pbejse
Posts: 2,460
Registered: ‎11-24-2009
Message 2 of 10 (579 Views)

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")

 

Distinguished Contributor
msarqui
Posts: 131
Registered: ‎09-14-2010
Message 3 of 10 (519 Views)

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!

*Expert Elite*
pbejse
Posts: 2,460
Registered: ‎11-24-2009
Message 4 of 10 (511 Views)

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)))
    )
  )

 

Distinguished Contributor
msarqui
Posts: 131
Registered: ‎09-14-2010
Message 5 of 10 (493 Views)

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.

*Expert Elite*
pbejse
Posts: 2,460
Registered: ‎11-24-2009
Message 6 of 10 (472 Views)

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

Distinguished Contributor
msarqui
Posts: 131
Registered: ‎09-14-2010
Message 7 of 10 (452 Views)

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,

*Expert Elite*
pbejse
Posts: 2,460
Registered: ‎11-24-2009
Message 8 of 10 (444 Views)

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

 

Distinguished Contributor
msarqui
Posts: 131
Registered: ‎09-14-2010
Message 9 of 10 (425 Views)

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:

*Expert Elite*
pbejse
Posts: 2,460
Registered: ‎11-24-2009
Message 10 of 10 (410 Views)

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");;<----- 

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.