Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lisp Code to clear all objects on specific layers

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
GeryKnee
3603 Views, 18 Replies

Lisp Code to clear all objects on specific layers

I need a lisp code to do the following

 1. Select Objects On Screen getting the propt <<select objects>> on acad text window

     For examble suppose obj1,objj2 etc are the selected objects

 2. Get the obj1,objj2 etc layers of selected objects

 3. Retrieve all model space objects on these layers

 4. delete all the above objects

 5. show the message <<everything done>> on acad text window

 

Can anyone help me please?

18 REPLIES 18
Message 2 of 19
Kent1Cooper
in reply to: GeryKnee

That sounds exactly like what the LAYDEL command does.  [That is, assuming you want the Layer(s) Purged in the process, and any objects on those Layers that are included in Block definitions to also be removed from those Block definitions.  If not, something could be worked out that would remove top-level objects but not those in Block definitions.]

Kent Cooper, AIA
Message 3 of 19
GeryKnee
in reply to: Kent1Cooper

Yes i need a code that erases objects on layers being part of drawing , without deleting

 a) included in Block definitions and

 b) the layer object

 

thanx

Gery

Message 4 of 19
GeryKnee
in reply to: Kent1Cooper

a code that erases objects on layers being part of drawing , without deleting

 a) included in Block definitions and

 b) the layer object

 Additionally not to ask confirmation (yes/no) about to delete

 

thanx

Gery

Message 5 of 19
Kent1Cooper
in reply to: GeryKnee

A quick Search reveals this thread.  The code that they "use already" in the first message may do just what you want, although it looks like to only one Layer at a time, but that can be fixed if it works for you otherwise.

Kent Cooper, AIA
Message 6 of 19
GeryKnee
in reply to: Kent1Cooper

 

Yes , this sould be escactly what i want , if sould be working for more than one selected objects (-> layers) but it's impossimble for me to change it.

Thanx,

Gery

Message 7 of 19
GeryKnee
in reply to: Kent1Cooper

 

The following code

 is what i need but sould be better erasing more than 1 layer's objects

(defun c:dl1 () ;erases all objects on one selected layer
   (if(setq ent (car (entsel "\nSelect layer to remove: ")))
        (progn
           (setq l_name (cdr (assoc 8 (entget ent)))
                 ss   (ssget "X" (list (cons 8 l_name)))
                 cntr (1- (sslength ss))
           )
           (if(>= cntr 0)
              (while(>= cntr 0)
                    (setq ssent (ssname ss cntr))
                    (entdel ssent)
                    (setq cntr (1- cntr))
               )
           )
        )
   )
   (princ "\ndone")
   (princ)
)

 

 

Message 8 of 19
Kent1Cooper
in reply to: GeryKnee

Something like this should do it [untested]:

 

(defun c:dlm (/ ssL nL ssE nE); erases all objects on multiple selected layers
  (prompt "\nTo Delete everything on objects' Layers, ")

  (if (setq ssL (ssget)); selection set for Layers to delete objects from
    (repeat (setq nL (sslength ssl))

      (if (setq l_name (cdr (assoc 8 (entget (ssname ssL (setq nL (1- nL)))))))

        ;; Using (if) above because if more than one thing on the same Layer is selected,

        ;; after deleting objects on the Layer of the first one, it will return nil for others,

        ;; which would cause an error in the (ssget) following:
        (progn

          (setq ssE (ssget "_X" (list (cons 8 l_name)))); selection set for Entities to delete
          (repeat (setq nE (sslength ssE))
            (entdel (ssname ssE (setq nE (1- nE))))

          ); repeat
        ); progn

      ); if

    ); repeat
  ); if
  (prompt "\nDone.")
  (princ)
); defun

 

EDIT:  Would you ever need to account for any locked Layer(s)?  If so, it could be made to just unlock the Layer of each selected object and proceed.  But since Layers presumably don't get locked without good reason, it would be better to either skip those Layers or warn the User of any locked Layer, and ask whether they want to unlock it and delete its objects.

Kent Cooper, AIA
Message 9 of 19
GeryKnee
in reply to: Kent1Cooper

 

That's excactly what i needed.

Thanks very much Kent,

Gery

Message 10 of 19
Lee_Mac
in reply to: Kent1Cooper

I would recommend the following approach in order to minimise the number of times the ssget function needs to iterate over the entire drawing database, retrieving entities which match the given filter list.

 

Also, I have included (410 . "Model") in the ssget filter list as I believe the OP only wanted Modelspace objects to be deleted:

 

(defun c:dlm ( / i l n s )
    (if (setq s (ssget "_:L" '((410 . "Model"))))
        (progn
            (repeat (setq i (sslength s))
                (if (not (member (setq n (cdr (assoc 8 (entget (ssname s (setq i (1- i))))))) l))
                    (setq l (vl-list* "," n l))
                )
            )
            (setq s (ssget "_X" (list (cons 8 (apply 'strcat (cdr l))) '(410 . "Model"))) n 0)
            (repeat (setq i (sslength s))
                (if (entdel (ssname s (setq i (1- i)))) (setq n (1+ n)))
            )
            (princ (strcat "\nDeleted " (itoa n) " objects."))
        )
    )
    (princ)
)

 

Lee

 

Message 11 of 19
marko_ribar
in reply to: Lee_Mac


@Lee_Mac wrote:

I would recommend the following approach in order to minimise the number of times the ssget function needs to iterate over the entire drawing database, retrieving entities which match the given filter list.

 

Also, I have included (410 . "Model") in the ssget filter list as I believe the OP only wanted Modelspace objects to be deleted:

 


We don't know that...

I suggest :

(setq s (ssget "_:L" (list (cons 410 (getvar 'ctab)))))

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 12 of 19
Lee_Mac
in reply to: marko_ribar

marko_ribar wrote:
Lee_Mac wrote:

I would recommend the following approach in order to minimise the number of times the ssget function needs to iterate over the entire drawing database, retrieving entities which match the given filter list.

 

Also, I have included (410 . "Model") in the ssget filter list as I believe the OP only wanted Modelspace objects to be deleted:

 

 

We don't know that...

I suggest :

(setq s (ssget "_:L" (list (cons 410 (getvar 'ctab)))))

 

 

Yes we do:

 

GeryKnee wrote:

I need a lisp code to do the following

 1. Select Objects On Screen getting the propt <<select objects>> on acad text window

     For examble suppose obj1,objj2 etc are the selected objects

 2. Get the obj1,objj2 etc layers of selected objects

 3. Retrieve all model space objects on these layers

 4. delete all the above objects

 5. show the message <<everything done>> on acad text window

 

Can anyone help me please?

 

Message 13 of 19
marko_ribar
in reply to: Lee_Mac

Sorry, I didn't read whole topic... My suggestion was more general nature... So if OP change his mind, it'll work anyway...

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 14 of 19
GeryKnee
in reply to: Kent1Cooper

 

After the successfull code


(defun c:EraLayeObj1 (/ ssL nL ssE nE); erases all objects on multiple selected layers
  (prompt "\nTo Delete everything on objects' Layers, ")
  (if (setq ssL (ssget)); selection set for Layers to delete objects from
    (repeat (setq nL (sslength ssl))
      (if (setq LayerName (cdr (assoc 8 (entget (ssname ssL (setq nL (1- nL)))))))
        ;; Using (if) above because if more than one thing on the same Layer is selected,
        ;; after deleting objects on the Layer of the first one, it will return nil for others,
        ;; which would cause an error in the (ssget) following:
        (progn
          (setq ssE (ssget "_X" (list (cons 8 LayerName))));selection set for Entities to delete
          (repeat (setq nE (sslength ssE))
            (entdel (ssname ssE (setq nE (1- nE))))
          ); repeat
        ); progn
      ); if
    ); repeat
  ); if
  (prompt "\nDone.")
  (princ)
)

 

 

How sould be written in a different way :

 

1) Function to erase all objects on layer named LayerName
(defun fEraLayObj (/  LayerName)

 

  ...

  ...

)

 

2) Command Calling the Function fEraLayObj to erase all objects on specific layers passing LayerNames as parameters to Function 
(defun c:EraLayeObjCall1 ()

  fEraLayObj ( "Layer1" )

  fEraLayObj ( "Layer2" )

  fEraLayObj ( "Layer3" )

  ...

)

 

2) Command that erases all objects on multiple selected layers  Calling the Function fEraLayObj
(defun c:EraLayeObjCall2 ()
  (prompt "\nTo Delete everything on objects' Layers, ")

  ...

)

 

Hope you can help me in this.

 

Gery

Message 15 of 19
Lee_Mac
in reply to: GeryKnee

Since the ssget filter list accepts wildcard patterns, you could call the following function with a comma-delimited list of layer names:

 

(defun eraseonlayer ( lay / i s )
    (if (setq s (ssget "_X" (list (cons 8 lay) '(410 . "Model"))))
        (repeat (setq i (sslength s))
            (entdel (ssname s (setq i (1- i))))
        )
    )
    (princ)
)

 

For example:

 

(eraseonlayer "layer1,layer2,layer3")

 

Or:

 

(eraseonlayer "layer[123]")

 

Message 16 of 19
GeryKnee
in reply to: Lee_Mac

 

Hello Lee_Mac

The codo is ok

 

when the layernames are not known defined by name (lay1,lay2 ..) but is a collection of layers coming from user multiselection of objects using ssget function how the code sould be calling the eraseonlayer function  ?

 

 That's what is the new syntax of


(defun c:EraLayeObj1 (/ ssL nL ssE nE); erases all objects on multiple selected layers
  (prompt "\nTo Delete everything on objects' Layers, ")
  (if (setq ssL (ssget)); selection set for Layers to delete objects from
    (repeat (setq nL (sslength ssl))
      (if (setq LayerName (cdr (assoc 8 (entget (ssname ssL (setq nL (1- nL)))))))
        ;; Using (if) above because if more than one thing on the same Layer is selected,
        ;; after deleting objects on the Layer of the first one, it will return nil for others,
        ;; which would cause an error in the (ssget) following:
        (progn
          (setq ssE (ssget "_X" (list (cons 8 LayerName))));selection set for Entities to delete
          (repeat (setq nE (sslength ssE))
            (entdel (ssname ssE (setq nE (1- nE))))
          ); repeat
        ); progn
      ); if
    ); repeat
  ); if
  (prompt "\nDone.")
  (princ)
)

   .. if uses eraseonlayer to erase each one sel object's layer?

 

Message 17 of 19
Lee_Mac
in reply to: GeryKnee

As a modification of my earlier post:

 

(defun c:dlm ( / i l n s )
    (if (setq s (ssget "_:L" '((410 . "Model"))))
        (progn
            (repeat (setq i (sslength s))
                (if (not (member (setq n (cdr (assoc 8 (entget (ssname s (setq i (1- i))))))) l))
                    (setq l (vl-list* "," n l))
                )
            )
            (eraseonlayer (apply 'strcat (cdr l)))
        )
    )
    (princ)
)
(defun eraseonlayer ( lay / i s )
    (if (setq s (ssget "_X" (list (cons 8 lay) '(410 . "Model"))))
        (repeat (setq i (sslength s))
            (entdel (ssname s (setq i (1- i))))
        )
    )
    (princ)
)
(princ)

 

Message 18 of 19
Anonymous
in reply to: Lee_Mac

hi lee mac i have a  autolisp code and command to invoke this code is "test".

if i type test in autocad command prompt   the lisp code executes but i like to use excell vba to open autocad and input test in autocad command prompt in autocad and run it automatically  , do u know code for this

 

 

i used code like

ACad.activedocument.sendcommand("test" & VBcr )

 

 

; error: bad argument type: VLA-OBJECT nil

Message 19 of 19
Lee_Mac
in reply to: Anonymous

Your question appears to be unrelated to the question discussed in this thread. Please first search the forum & web for answers to your question, and if you cannot find a suitable answer, please post your question in a new thread. You may also find this article of benefit.

 

Lee

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost