Jump to content
Vittorio

LISP - select objects of specific type

Recommended Posts

Vittorio

Hello Everybody, this is my first post and I'd like to know a little bit more about the ssget command.

 

I already searched the world wide web for various search keywords, but couldn't find references / lists, especially of the Associative codes and object types.

 

I also read a lot of articles about that command, but most of them use these Associative codes:

http://www.lee-mac.com/ssget.html

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2017/ENU/AutoCAD-AutoLISP/files/GUID-0F37CC5E-1559-4011-B8CF-A3BA0973B2C3-htm.html

http://www.afralisp.net/autolisp/tutorials/selection-sets.php

They

 

Since I am a total newbie with AutoLISP please explain things easily.

 

What I would like to do in the first place is selecting objects via LISP, since there seems to be no command line version of SELECT, QSELECT etc.

 

and therefore I need:

 

  • a list of "names" of object types that exist in AutoLISP (eg. TEXT, LINE, etc)
  • all neccessary parameters for the ssget function

 

 

Let's say I want to select a table in a drawing, how can this be done?

What if there are more than one tables?

 

Many thanks in advance

Share this post


Link to post
Share on other sites
Lee Mac

Welcome to CADTutor Vittorio. :)

 

If you have already studied the documentation for the ssget function as found on my site, or in the Autodesk documentation, then you should already be familiar with the parameters which may be supplied to the function: the mode string argument to determine the type of selection, the point & point list arguments when specifying a selection window/polygon, and the filter list argument to filter the properties of objects which may be selected.

 

Since your question is predominantly asking about the filter list argument and the various entity types which may be filtered, I would first suggest that you refer to a DXF Reference.

 

To understand the entity types available, refer to the ENTITIES section of the reference.

 

For example, for a Table, the DXF reference shows that the entity type (DXF group 0) is "ACAD_TABLE". Therefore, to obtain a selection of all tables in the drawing, you might use:

(ssget "_X" '((0 . "ACAD_TABLE")))

Share this post


Link to post
Share on other sites
Vittorio
For example, for a Table, the DXF reference shows that the entity type (DXF group 0) is "ACAD_TABLE".

Therefore, to obtain a selection of all tables in the drawing, you might use:

(ssget "_X" '((0 . "ACAD_TABLE")))

 

I did, but nothing happened. Here's a copy of the ACAD Text Window:

Command: APPLOAD
selectTable.lsp successfully loaded.

Command: SELECTTABLE
<Selection set: 18867>

Command: SELECTTABLE
<Selection set: 1888d>

Command: SELECTTABLE
<Selection set: 1888f>

Command: (ssget "_X" '((0 . "ACAD_TABLE")))
<Selection set: 188ca>

Command: SELECTTABLE
<Selection set: 188d4>

 

here is the LISP selectTable.lsp

(defun c:selectTable nil
 (vl-load-com)
(ssget "_X" '((0 . "ACAD_TABLE")))
)

 

As I already said, I'm a total noob in AutoLISP.

 

 

 

Since your question is predominantly asking about the filter list argument and the various entity types which may be filtered, I would first suggest that you refer to a DXF Reference.

 

To understand the entity types available, refer to the ENTITIES section of the reference.

 

So here is what I am understanding:

 

The ssget syntax as described on your site or at Autodesk Help:

(ssget [mode-string] [pt1 [pt2]] [pt-list] [filter-list])

 

ssget - required

[mode-string] - "_X" (Extended Search - Entire Drawing Database)

[pt1 [pt2]] - "" (Point 1, Point 2, ...) => empty: no points selected

[pt-list] - "" (Point List) => empty: no point list

[filter-list] - here we go... I just don't get this part

 

 

  • Why is the whole thing eclosed in double parentheses?
  • Why is there an apostroph before the first parenthesis?
  • Why is there a dot between the group code and the entity name?
  • What exactly does the "0" stand for? Group Code? There are many 0s in the reference.
  • Is the "0" bound to the entity name? If so, why is there no "0" or entity name for the LINE object?

 

Here is a part of the Reference:

Table group codes:

Group code / Description

0 / Entity name (ACAD_TABLE)

 

Line group codes:

Group code / Description

100 / Subclass marker (AcDbLine)

Share this post


Link to post
Share on other sites
BIGAL

A couple more hints you can filter more than one type of entity in one go

 

(ssget "_X" '((0 . "Line,Lwpolyline"))) finds lines and poly lines

(ssget "_X" '((0 . "Text,Mtext")))  finds text and mtext
(ssget "_X" '((0 . "*Text")))  finds text and mtext

(ssget "_X" '((0 . "Insert"))) one that is not obvious this finds blocks.

(ssget "_X" '((0 . "*Text")(8 . "123")))  finds text on layer 123

Share this post


Link to post
Share on other sites
Vittorio

I tried and pasted the code into command line, but all that AutoCAD tells me is: "" with XY being an incremented number / letter

 

There is nothing selected after applying (ssget "_X" '((0 . "Line,Lwpolyline"))) for example...

 

How can I actually select objects in AutoLISP?

Share this post


Link to post
Share on other sites
BIGAL

At its most simplest (setq SS (ssget)) you then have a selection set of objects. Note the SS this the variable name that is holding our selection of objects. All the above are examples of ssget but we all forgot to save the selection to a variable. In saying that there are situations where you dont need to assign to a variable. Like move copy erase etc.

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

Again you need to explain what is it your trying to do, ignore the ssget for the moment do you want to pick objects and do something with them ? Explain what it is you want to do.

 

an example 
(command "Erase" (ssget "X" '((8 . "0"))) "") erase objects on layer "0"

 

(setq ss (ssget "X" '((0 . "TEXT")))) 
(repeat (setq x (sslength ss))
(setq obj (ssname ss x))
... do stuff here like
.... get insertion point
(setq x (+ x 1))
)

Edited by BIGAL

Share this post


Link to post
Share on other sites
rlx

maybe OP just want to use the select command in combo with ssget?

 

 

 (command "select" ss) 

 

 

But I agree he has to give more info in what he is trying to do...

 

 

gr. Rlx

Share this post


Link to post
Share on other sites
ronjonp

You might also study THIS.

Share this post


Link to post
Share on other sites
BIGAL

An example of selecting all objects on one layer without knowing layer name.

 

(setq ent (entget (car (entsel))))
(setq layname (cdr (assoc 8 ent)))
(setq  ss (ssget "x" (list (cons 8  layname))))
(alert (strcat (rtos (sslength ss) 2 0) " Objects found"))))

Share this post


Link to post
Share on other sites
Vittorio

Thanks for all the replies!

 

What I actually want to do ist some batch scripting with a set of input files (eg. drawing001.dwg, drawing002.dwg, drawing003.dwg, etc.)

 

The script may look like this:

 

_.model _.laythw _.layon _.regenall _.zoom e 
[i][color="red"]... some code here (calling the LISP to select the desired objects) ...[/color][/i]
[i][color="green"]... some code here (manipulating the selected objects) ...[/color][/i]
_.save  _.quit  

Share this post


Link to post
Share on other sites
ronjonp

Here's how you'd run your code in a script.

_.model _.laythw _.layon _.regenall _.zoom e

(c:nameofyourcodealreadyloaded)

_.save _.quit

Share this post


Link to post
Share on other sites
Vittorio

Thanks!

 

The problem is not how to include a LISP in a Script (nevertheless, thanks for the answer how to do it),

but the LISP routine itself

 

What I actually need is a LISP to select objects of a given type.

 

For example, a LISP to select all Tables in a drawing or all Blocks in a drawing or any Polylines... anyway, you get the point

Share this post


Link to post
Share on other sites
BIGAL

1st add the starting steps into the lisp makes the script a bit easier

 

open dwg1
(load "myfixuppperlisp")
open dwg2
(load "myfixuppperlisp")
.............

 

; myfixerupper
(defun myfixer ( / ss )
(setvar 'ctab "Model")
(command "_.laythw" "_.layon" "_.regenall" "_.zoom" "e") 
(setq ss (ssget "X" '((0 . "INSERT")))) ; select all block objects
(command "chprop" ss "" "la" "0" "") ; change to layer 0
(command "close" "Y")
)

(myfixer)

Share this post


Link to post
Share on other sites
Vittorio
1st add the starting steps into the lisp makes the script a bit easier

 

open dwg1
(load "myfixuppperlisp")
open dwg2
(load "myfixuppperlisp")
.............

 

; myfixerupper
(defun myfixer [color="red"][b]( / ss )[/b][/color]
(setvar [color="red"][b]'ctab "Model"[/b][/color])
(command "_.laythw" "_.layon" "_.regenall" "_.zoom" "e") 
(setq ss (ssget "X" '((0 . "INSERT")))) ; select all block objects
(command "chprop" ss "" "la" "0" "") ; change to layer 0
(command "close" "Y")
)

(myfixer)

 

Thanks!

 

Could you please explain a few things to me? As I already said, I am a LISP newbie with no experience at all :(

 

defun myfixer defines the function, but what does (/ss) exactly do?

And setvar somehow sets a variable, but your syntax differs from the Autodesk KB/Help.

 

Now, let me try:

...
(setq txt (ssget "X" '((0 . "TEXT")))) ; select all single line text objects
(command "_.erase" txt "") ; erase them all!
...

Is that correct?

Share this post


Link to post
Share on other sites
Grrr
Thanks!

 

Could you please explain a few things to me? As I already said, I am a LISP newbie with no experience at all :(

 

defun myfixer defines the function, but what does (/ss) exactly do?

 

Localising Variables

 

 

Now, let me try:

...
(setq txt (ssget "X" '((0 . "TEXT")))) ; select all single line text objects
(command "_.erase" txt "") ; erase them all!
...

Is that correct?

 

Yes.

Share this post


Link to post
Share on other sites
ronjonp

You might want to check that there is a selection or your command call will fail:

(if (setq txt (ssget "X" '((0 . "TEXT")))) ; select all single line text objects
 (command "_.erase" txt ""); erase them all!
 (print "No text to erase...")
)

Share this post


Link to post
Share on other sites
BIGAL

Good idea ronjonp most of the stuff I do for work expects the user to know what they are doing and pick a text when thats what they are being asked to do. When learning putting in error checking is a very good idea for the sake of 5 characters (if ) something I dont do often enough look at Lee's and Tharwats code always have error checks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×