Empty Selection Princ Error

Empty Selection Princ Error

ebsoares
Collaborator Collaborator
944 Views
3 Replies
Message 1 of 4

Empty Selection Princ Error

ebsoares
Collaborator
Collaborator

Hi, all.

 

I wrote a lisp (with the help of a few folks in this forum - thanks again) that selects entities of a user's choice. It works without any issues whatsoever (it does what it's supposed to). One little thing still remains, however. It's unrelated to the main task itself (selecting elements), but with the "princ" command at the end.
Here's the code:

(defun C:SelectTargetEntity (/ steSelection steString)
	
	; Prep initget with all possibilities:
	(initget (strcat "Text MText MLeader Point"))
	
	; Set *answerSTE* with user input:
	(setq *answerSTE*
		(cond
			( (getkword ; offer user a list of options
				(strcat "\nChoose [Text/MText/MLeader/POint] <"
					(setq *answerSTE* ; define default or previous choice
						(cond (*answerSTE*) ("MLeader"))
					) ">: "
				)
			) )
			(*answerSTE*) ; option if user just presses enter
		)
	)
	
	; Set steString entity type based on *answerSTE* variable:
	(cond
		((eq *answerSTE* "Text") (setq steString "Text"))
		((eq *answerSTE* "MText") (setq steString "MText"))
		((eq *answerSTE* "MLeader") (setq steString "MultiLeader"))
		((eq *answerSTE* "Point") (setq steString "Point"))
	)
	
	; Set filter based on requested entity type:
	(setq filter (list (cons 0 steString)))
	
	; Prep filtered selection:
	(setq steSelection (ssget "_:L" filter))
	
	; Maintains selection:
	(sssetfirst nil steSelection)
	
	; Print amount of items selected:
	(princ (strcat "\n" (itoa (sslength steSelection)) " " *answerSTE* " objects selected!"))
		; gives an error when steSelection is empty...
	
	(princ)
)

I suppose that not the entire lisp was needed, but put that in there just for context.
The problem is that the command line displays this error message when the selection is empty: "error: bad argument type: lselsetp nil"

 

Is there a way to fix this? Even though this routine would not likely be used if there were no elements to be selected, I'd still like to have a more robust code - and any possible fix might be used for other routines in the future.

 

Thanks in advance.

 

Edgar

0 Likes
Accepted solutions (1)
945 Views
3 Replies
Replies (3)
Message 2 of 4

john.uhden
Mentor
Mentor
Accepted solution

Yes.

 

(if (= (type steSelection) 'PICKSET)
  (princ (strcat "\n" (itoa (sslength steSelection)) " " *answerSTE* " objects selected!"))
)

John F. Uhden

0 Likes
Message 3 of 4

Kent1Cooper
Consultant
Consultant

@john.uhden's suggestion looks like it would not report at all if there's no selection.  If you want it to report that nothing was selected, this should do: 

 

(princ (strcat "\n" (itoa (if steSelection (sslength steSelection) 0)) " " *answerSTE* " objects selected!"))

Kent Cooper, AIA
0 Likes
Message 4 of 4

ebsoares
Collaborator
Collaborator

Thanks for the quick responses, John and Kent.

 

Kent, I couldn't get your code to work on my end. Whether there were objects selected or not, this error message still pops up on the command line: "error: bad argument type: stringp nil"...

John, your code was fine, I just added an else statement to handle the case where nothing was selected:

(if (= (type steSelection) 'PICKSET)
	(princ (strcat "\n" (itoa (sslength steSelection)) " " *answerSTE* " objects selected"))
	(princ "\nNothing selected")
)

And now the routine behaves as expected Smiley Happy

 

Again, thank you both for helping me out!

 

Edgar

0 Likes