Jump to content

Need LISP program to select all objects in layer


Vigilante
 Share

Recommended Posts

Hey guys, I'm sure somebody could answer this quickly so I didn't bother searching very long for the answer, I couldn't find it.

 

Basically when I'm "cleaning up" a drawing from somebody else, I have to delete a lot of stuff. So I use QSELECT to grab everything in the layer, and then delete it.

This process takes to long, and I have to do it over and over for each layer I don't need. And if there are a lot of layers, trying to find them in qselect can be monotonous.

 

So basically, I want a LISP program where I just type the command, such as, say, SALL, or whatever, and then the program runs. It has me then click on any single object, and then instantly it selects every object that is in the same layer as the one I clicked, INCLUDING the one I clicked, of course.

 

I have a similar program that is used to DELETE every oject in a layer, but I don't want it to automatically delete everything. I need to see what is selected before I delete it all. So all I want is to click on an object, and have every object of that layer become selected.

 

Know any programs?

 

Thanks!

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • Happy Hobbit

    5

  • Tharwat

    4

  • Vigilante

    3

  • CarlB

    2

Here's a "Q & D" lisp.

 

(defun c:SALL ()
 (setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
 (setq TargLayer (assoc 8 (entget TargEnt)))
 (sssetfirst nil (ssget "_X" (list TargLayer)))
 (princ)
)

Link to comment
Share on other sites

Hey that's great!

 

I forgot to mention I had acad 2002. But it worked just the same.

 

Since it is so small, would you mind explaining what that code is doing, for my lisping research?

 

Otherwise, thanks again, that will do nicely.

Link to comment
Share on other sites

Ok, but mostly I'll just copy from the reference manual as there's not much to the logic.

 

 

(defun c:SALL ()
  ;;define a function named "sall"
  (setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
 ;;prompt user to select object, entsel stores pick pint & entity name
;; 'car' extracts name, set it to variable 'Targent'

[left](setq TargLayer (assoc 8 (entget TargEnt)))[/left]

;;extract layer name of entity using 'entget' to retrieve entity data

[left];;'assoc' to extract portion of data pair, '8' is the layer name group code [/left]



[left](sssetfirst nil (ssget "_X" (list TargLayer)))

;;ssget used to select all items (the 'x' option) having same layer name
[left];;sssetfirst used to grip & highlight items
(princ)
)[/left]


[/left]


Link to comment
Share on other sites

Thanks for the tip.

 

I combined that code with another lisp called TLEN which gives the length of connected lines.

So now when I need to measure this thing, all I do is type my command, and then click an object. It automatically selects everything in the layer, then measures it.

Very good, this process used to take a long time, joining lines, using the measure tool, QSELECT, making generalized guesses. Now it takes a few seconds and is quite accurate!

Link to comment
Share on other sites

(defun c:LL2(/)
 (princ"click object belong to a layer to copy or move all objects layer")
 (setq myobjectsbylayer (ssget "X" (list (cons 8 (cdr (assoc 8 (entget (car (entsel)))))))))
 (initget "Copy Move")
 (setq x(getkword"\n hit inter for copy with base point or type <M> for move >"))
 (if(not x)
    (progn
      (setq mypoint (getpoint "Specify base point or press Enter for 0,0: ")) 
        (if (not mypoint) (setq mypoint (list 0 0))) 
        (command "copybase" mypoint myobjectsbylayer "")
      )
    )
  
  (if(= x "Move")
    (progn
    (setq mypoint (getpoint "Specify base point ")) 
    (command"move"myobjectsbylayer"" mypoint)
      )
    )
)

Edited by SLW210
Code Tags
Link to comment
Share on other sites

If you want to select more that one layer at a time.

;;=============================================================
;;     Sel.lsp by Charles Alan Butler
;;            Copyright 2004
;;   by Precision Drafting & Design All Rights Reserved.
;; 
;;    Version 1.0 Beta  July 23,2004
;;    Version 1.1 Beta  July 13,2005
;;
;;   Creates a selection set of objects on a layer(s)
;;   User picks objects to determine the layer(s)
;;   Then User selects objects for ss or presses enter to 
;;   get all objects on the selected layer(s)
;;   You may select the selection set before starting this
;;   routine. Then select the layers to keep in the set
;;=============================================================
(defun c:sel (/ ent lay ss lay:lst lay:prompt ss:first ent:lst)
 ;;  get anything already selected
 (setq ss:first (cadr(ssgetfirst))
       ss (ssadd))

 ;;  Get user selected layers
 (if ss:first
   (setq lay:prompt "\nSelect the object to choose layers to keep.")
   (setq lay:prompt "\nSelect object for layer filter.")
 )
 (while (setq ent (entsel lay:prompt))
   (setq ent:lst (cons (car ent) ent:lst))
   (setq lay:lst
          (cons (setq lay (cdr(assoc 8 (entget (car ent))))) lay:lst))
   (prompt (strcat "\n*-* Selected Layer -> " lay))
 )
 ;;  Un HighLite the entities
 (and ent:lst (mapcar '(lambda (x) (redraw x 4)) ent:lst))

 (if (> (length lay:lst) 0); got layers to work with
   (progn
     (setq lay "")
     (setq lay:lst (vl-sort lay:lst '<)) ; removes douplicates
     (foreach itm  lay:lst ; combine lay names into one , del string
       (setq lay (strcat lay itm ",")))
     (setq lay (substr lay 1 (1- (strlen lay)))); remove the last ,
     (if ss:first ; ALREADY GOT SELECTION SET
       (while (setq ent (ssname ss:first 0))
         (if (member (cdr(assoc 8 (entget ent))) lay:lst)
           (ssadd (ssname ss:first 0) ss)
         )
         (ssdel (ssname ss:first 0) ss:first)
       )
       (progn ; else get a selection set to work with
         (prompt (strcat "\nOK >>--> Select objects for Selection set or "
                         "ENTER for All objects on layer(s) " lay))
         ;;  get objects using filter with user select
         (if (null (setq ss (ssget (list (cons 8 lay)))))
           ;; or get ALL objects using filter
           (setq ss (ssget "_X" (list (cons 8 lay)(cons 410 (getvar "ctab")))))
         )
       )
     )
     (if (> (sslength ss) 0)
       (progn
         (prompt (strcat "\n" (itoa (sslength ss))
                     " Object(s) selected on layer(s) " lay
                     "\nStart an ACAD command."))
         (sssetfirst nil ss)
       )
       (prompt "\n***  Nothing Selected  ***")
     )
   )
 )
 (princ)
)
(prompt "\nSelect on Layer loaded, Enter Sel to run.")
(princ)

Link to comment
Share on other sites

  • 8 years later...

Can it be amended to have a (princ "\n items selected") as well?

 

Ok, but mostly I'll just copy from the reference manual as there's not much to the logic.

 

 

(defun c:SALL ()
  ;;define a function named "sall"
  (setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
 ;;prompt user to select object, entsel stores pick pint & entity name
;; 'car' extracts name, set it to variable 'Targent'

[left](setq TargLayer (assoc 8 (entget TargEnt)))[/left]

;;extract layer name of entity using 'entget' to retrieve entity data

[left];;'assoc' to extract portion of data pair, '8' is the layer name group code [/left]



[left](sssetfirst nil (ssget "_X" (list TargLayer)))

;;ssget used to select all items (the 'x' option) having same layer name
[left];;sssetfirst used to grip & highlight items
(princ)
)[/left]


[/left]


Link to comment
Share on other sites

(setq ss  (ssget "_X" (list TargLayer)))
(alert (strcat "Items selected " (sslength SS)))
(sssetfirst nil ss)

 

I think I must be pasting it into the wrong place or something, I'm getting " error: bad argument type: stringp 78"

 

What I'm trying is:

 

(defun c:SALL ()
   (setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
(setq TargLayer (assoc 8 (entget TargEnt)))
(setq ss  (ssget "_X" (list TargLayer)))
(alert (strcat "Items selected " (sslength SS)))
(sssetfirst nil ss)
(princ)
)

Link to comment
Share on other sites

 

What I'm trying is:

(defun c:SALL ()
   (setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
(setq TargLayer (assoc 8 (entget TargEnt)))
(setq ss  (ssget "_X" (list TargLayer)))
(alert (strcat "Items selected " (sslength SS)))
(sssetfirst nil ss)
(princ)
)

 

Consider this .

 

(defun c:sall (/ s ss)
 (if (and (setq s (car (entsel "\nSelect object on layer to select All : ")))
          (setq ss (ssget "_X" (list (assoc 8 (entget s)))))
          )
   (alert (strcat "Items selected < " (itoa (sslength ss)) " >"))
   )
 (sssetfirst nil ss)
 (princ)
 )

Link to comment
Share on other sites

Excellent Tharwat, this works very well.

 

I made a couple of slight alterations though, deleted the "_X" as I need to choose what to select & changed from an 'alert' to a 'princ'. Hope you don't mind

 

(defun c:SALL  (/ s ss)
 (if (and (setq s (car (entsel "\nSelect object on layer to select : ")))
          (setq ss (ssget (list (assoc 8 (entget s)))))
          )
(princ (strcat "\n "(itoa (sslength ss))" items selected"))
   )
 (sssetfirst nil ss)
 (princ)
 )

Link to comment
Share on other sites

Excellent Tharwat, this works very well.

 

I made a couple of slight alterations though, deleted the "_X" as I need to choose what to select & changed from an 'alert' to a 'princ'. Hope you don't mind

 

Certainly don't mind at all , Happy coding .

Link to comment
Share on other sites

  • 3 months later...
Consider this .

 

(defun c:sall (/ s ss)
 (if (and (setq s (car (entsel "\nSelect object on layer to select All : ")))
          (setq ss (ssget "_X" (list (assoc 8 (entget s)))))
          )
   (alert (strcat "Items selected < " (itoa (sslength ss)) " >"))
   )
 (sssetfirst nil ss)
 (princ)
 )

 

Many times I need to change the height or size of all text on a layer, and I've found Tharwat's program works perfectly for that purpose, with one exception: it selects everything on the layer and not just the text.

After much searching and trying to understand this simple program, some mental block has made me unable to make it select only the TEXT objects on the layer. Sadly, mostly in desperation, I found myself wishing that ssget had a "_T" option.

Will one of you kind souls please explain how it may be done?

 

Thanks,

Steve

Link to comment
Share on other sites

Try changing line 3 to:

(setq ss (ssget "_X" (list (assoc 8 (entget s)) '(0 . "TEXT,MTEXT"))))

 

Or if you don't want to include mtext, then:

   (setq ss (ssget "_X" (list (assoc 8 (entget s)) '(0 . "TEXT"))))

Link to comment
Share on other sites

Many times I need to change the height or size of all text on a layer, and I've found Tharwat's program works perfectly for that purpose, with one exception: it selects everything on the layer and not just the text.

After much searching and trying to understand this simple program, some mental block has made me unable to make it select only the TEXT objects on the layer. Sadly, mostly in desperation, I found myself wishing that ssget had a "_T" option.

Will one of you kind souls please explain how it may be done?

 

Thanks,

Steve

 

Hi Steve.

 

Try this:

 

(defun c:Test (/ s h ss doc i e)
 ;;    Tharwat 24.10.2015    ;;
 (princ
   "\nSelect Text on layer to change height for all texts on the same layer :"
 )
 (if (and (setq s (ssget "_+.:S:L:E" '((0 . "TEXT,MTEXT"))))
          (setq h (getdist "\nSpecify text height :"))
          (setq ss
                 (ssget "_X"
                        (list '(0 . "TEXT,MTEXT") (assoc 8 (entget (ssname s 0))))
                 )
          )
     )
   (progn
     (vla-startUndomark
       (setq doc (vla-get-activedocument (vlax-get-acad-object)))
     )
     (repeat (setq i (sslength ss))
       (entmod
         (subst (cons 40 h)
                (assoc 40 (setq e (entget (ssname ss (setq i (1- i))))))
                e
         )
       )
     )
     (vla-Endundomark doc)
   )
 )
 (princ)
)(vl-load-com)

Link to comment
Share on other sites

Both Happy Hobbit's modification suggestion and Tharwat's new program work perfectly, and many thanks to both of you for your responses.

However, as to placement of the

(0 . "TEXT,MTEXT")

what's the difference between Happy Hobbit's

(list (assoc 8 (entget s)) '(0 . "TEXT,MTEXT"))

and Tharwat's

(list '(0 . "TEXT,MTEXT") (assoc 8 (entget (ssname s 0))))

They both work just fine within their respective routines, but are not interchangeable.

 

Steve

Edited by StevJ
Link to comment
Share on other sites

You're welcome Steve.

 

In regard to you question about the differences , actually there is no difference in the result if you replace the position of the DXf codes in the above routines .

Link to comment
Share on other sites

Hey guys, I'm sure somebody could answer this quickly so I didn't bother searching very long for the answer, I couldn't find it.

 

Basically when I'm "cleaning up" a drawing from somebody else, I have to delete a lot of stuff. So I use QSELECT to grab everything in the layer, and then delete it.

This process takes to long, and I have to do it over and over for each layer I don't need. And if there are a lot of layers, trying to find them in qselect can be monotonous.

 

So basically, I want a LISP program where I just type the command, such as, say, SALL, or whatever, and then the program runs. It has me then click on any single object, and then instantly it selects every object that is in the same layer as the one I clicked, INCLUDING the one I clicked, of course.

 

I have a similar program that is used to DELETE every oject in a layer, but I don't want it to automatically delete everything. I need to see what is selected before I delete it all. So all I want is to click on an object, and have every object of that layer become selected.

 

Know any programs?

 

Thanks!

 

check this one

its one of the best lisp out there for selection , it has many options and i cant live without it.. hope you like it :)

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

 Share


×
×
  • Create New...