Selection Lisp counting objects selected

Selection Lisp counting objects selected

Anonymous
Not applicable
5,212 Views
9 Replies
Message 1 of 10

Selection Lisp counting objects selected

Anonymous
Not applicable

I have a simple lisp that selects (and holds using sssetfirst) Mtest & Text.

 

I would like it to display on the command line how many objects are selected, I cannot get it to do this.

 

My code has the offending line blanked off using ;;

 

It works fine with the line not used & no variables declared

 

(defun C:SELECT_TEXT ( /sset)
(sssetfirst nil (ssget '((0 . "TEXT,MTEXT"))))
;;(princ (strcat "\n "(itoa (sslength sset)) items selected"))
(princ)
)

 

0 Likes
Accepted solutions (2)
5,213 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

Hi sbanister,

 

His lack of space beetwen / and variable and your variblable is not used (no setq in your programme).

 

Try this code :

 

(defun C:SELECT_TEXT (/ sset)
(setq sset (ssget '((0 . "TEXT,MTEXT"))))
(princ (strcat "\n" (itoa (sslength sset)) " items selected"))
(princ)
)

 

The same shortly without variable.

 

(defun C:SELECT_TEXT ()
(princ (strcat "\n" (itoa (sslength (ssget '((0 . "TEXT,MTEXT"))))) " items selected"))
(princ)
)

 

Sorry for my english I'm french.

 

Olivier

 

 

0 Likes
Message 3 of 10

Anonymous
Not applicable

Hi sbanister,Oliver

 

Oliver's code is forcing user to select text, it is not going to count how many objects are already selected, because he removed sssetfirst.

 

If I understand well sbanister want to count how many text objects are selected.

This is the version with sssetfirst:

 

(defun C:SELECT_TEXT ( / sset)
(sssetfirst nil (setq sset (ssget '((0 . "TEXT,MTEXT")))))
(princ (strcat "\n" (itoa (sslength sset)) " items selected"))
(princ)
)

Sbanister, problem with your code was, beside missing space while localizing sset variable, there was no definition of the same.

You need to use setq sset , to define this variable and store selection set in it so, that you could later use sslength to see the number of objects in selection set. Second problem was in strcat function, which is connecting strings in one. You need to put " after itoa function.

 

 Hope this help,

dicra

 

0 Likes
Message 4 of 10

Anonymous
Not applicable
Accepted solution

@Anonymous wrote:

Hi sbanister,Oliver

 

Oliver's code is forcing user to select text, it is not going to count how many objects are already selected, because he removed sssetfirst.

 


Oliver, 

Sorry my mistake, I just made few tests, looks like there is no need for sssetfirst in this code.

 

Hope you don't mind.

 

dicra

0 Likes
Message 5 of 10

hmsilva
Mentor
Mentor

Hi sbanister,

in addition to what Olivier have pointed out, 'the lack of space beetwen / and variable', when we code, is essential to check/validate, the return from a function, before providing that return to to another function as argument.
In your code, if the user press enter before select any object, the code will throw an error as '; error: bad argument type: lselsetp nil'...


I would suggest something like this:

 

(defun C:SELECT_TEXT (/ len sset)
  (if (setq sset (ssget '((0 . "TEXT,MTEXT"))))
    (princ (strcat "\n" (itoa (setq len (sslength sset))) (if (> len 1) " items" " item") " selected"))
    (princ "\n 0 items selected")
  )
(princ)
)

 

Hope that helps
Henrique

EESignature

Message 6 of 10

Anonymous
Not applicable

Thanks both of you, Dicra's code sorted it though as I wanted the selection to remain selected. Hence I used sssetfirst.

 

Very useful if I need to quickly delete or copy text from a congested area of a drawing

0 Likes
Message 7 of 10

Anonymous
Not applicable

sbanister, Your welcome,

 

I would suggest you, to use Henrique's code, because of , what he is saying for the errors which are possible, when there are no text objects in selection set.

 

 

 

dicra

0 Likes
Message 8 of 10

Anonymous
Not applicable

I take your point, but it doesn't retain selection (sssetfirst)

0 Likes
Message 9 of 10

Anonymous
Not applicable

If you insist for sssetfirs, whit Henrique's remarks for error, maybe something like this:

 

(defun C:SELECT_TEXT ( / sset)
(sssetfirst nil (setq sset (ssget '((0 . "TEXT,MTEXT")))))

  (if sset
    (princ (strcat "\n "(itoa (sslength sset))" items selected"))
    (princ "\n 0 items selected")
    )
  (princ)
)

 

dicra

Message 10 of 10

Anonymous
Not applicable
Accepted solution

Spot on, thanks Dicra.

 

 

0 Likes