Selection Based on Entity Type

Selection Based on Entity Type

ebsoares
Collaborator Collaborator
1,699 Views
12 Replies
Message 1 of 13

Selection Based on Entity Type

ebsoares
Collaborator
Collaborator

Hi, all.

 

I've been working on a routine that allows the user to create a selection (like a window selection) that contains only the desired entity type. For example, if the user would like to make a window selection and select only MTexts, he would call this command, choose "MText" (or type "MT"), do a window selection, and end up with only the MText objects caught in that window.

 

I've tested the portion that filters the selection without the user input and it works. For example, if in "(setq filter '((0 . seString)))" I change the variable seString to, say, "Text", then I could select only text objects.

 

However, this routine as it is does not work. I think it can't set the variable seString to the same variable as *answers*, but am not sure.

 

 

I'd totally appreciate it if anyone could help me finish this task...

 

Here's my code so far (keep in mind I'm still sort of a newbie in Lisp):

 

(defun C:test (/ seSelection seString)
	; Prep initget with all possibilities:
	(initget "MLeader MText Text")
	; Set *answerSE* with user input:
	(setq *answerSE*
		(cond
			( (getkword ; offer user a list of options
				(strcat "\nChoose [MLeader/MText/Text] <"
					(setq *answerSE* ; define default or previous choice
						(cond (*answerSE*) ("MLeader"))
					) ">: "
				)
			) )
			(*answerSE*) ; option if user just presses enter
		)
	)
	; Set seString entity type based on *answerSE* variable:
	(cond
		((eq *answerSE* "Text") (setq seString "Text"))
		((eq *answerSE* "MText") (setq seString "MText"))
		((eq *answerSE* "MLeader") (setq seString "MultiLeader"))
	)
	; Set filter based on requested entity type:
	(setq filter '((0 . seString)))
	; Prep filtered selection:
	(setq seSelection (ssget "_:L" filter))
	; Maintains selection:
	(sssetfirst nil seSelection)
	(princ)
)

 

By the way, I have to thank Lee Mac for his beautiful explanation on how to get user input (Thanks, Lee Mac 🙂 )

 

Edgar

 

0 Likes
Accepted solutions (4)
1,700 Views
12 Replies
Replies (12)
Message 2 of 13

SeeMSixty7
Advisor
Advisor
Accepted solution

I commend you on working through problems like these and learning AutoLISP.

 

The part where you call

(setq filter '((0 . seString)))

 

basically says set filter variable to QUOTE (Exactly what you see) '((0 . seString))   That means seString is never evaluated by the function.

 

instead build the list with the dotted pair using the list function combined with cons to create the dotted pair as shown below.

(setq filter (list (cons 0 seString)))

 

Good luck.

Message 3 of 13

ebsoares
Collaborator
Collaborator

Fantastic! That did it! Thanks @SeeMSixty7!

 

With that done, if I could trouble you with one more question... The selection prepped for the user (here "[MLeader/MText/Text]") allows them to simply type "ML" for MLeaders, for example, but I tried using "TeXt" in order to accept "TX" but it didn't work... is there a way to do that? Or the simple keystrokes have to always be consecutive letters?

 

Thanks again Smiley Happy

 

Edgar 

0 Likes
Message 4 of 13

SeeMSixty7
Advisor
Advisor
Accepted solution

If your options are simply MLeader, MText, or Text. I would simply go with Text and just use the single letter T. You don't have to use two chars?

 

Hopefully that helps?

 

0 Likes
Message 5 of 13

ebsoares
Collaborator
Collaborator
That's helpful, yes. Come to think of it, it wasn't a bright question...
Thanks again
0 Likes
Message 6 of 13

ebsoares
Collaborator
Collaborator

Last question... hopefully it won't take much more of your time...

 

I added

(princ (strcat "\n" (length seSelection) *answerSE* " objects selected"))

right under

(sssetfirst nil seSelection)

in order to print something like "15 Text objects selected", but instead it gives me this line of error: Select objects: ; error: bad argument type: listp <Selection set: 39>

Do I have to encapsulate that line inside a "prog"?

 

Thanks in advance,

 

Edgar

0 Likes
Message 7 of 13

SeeMSixty7
Advisor
Advisor

the length function will not work on a selection set.

 

(setq mysslen (sslength seSelection)) This returns an integer so you will need to convert it to a string.

 

(princ (strcat "\n" (itoa mysslen) " " *answers* " objects selected!"))

 

 

Have FUN!

 

0 Likes
Message 8 of 13

ebsoares
Collaborator
Collaborator

Sorry, @SeeMSixty7, but still receiving an error message:

Select objects: ; error: bad argument type: stringp nil

Perhaps the variable mysslen isn't getting populated with the sslength number?

0 Likes
Message 9 of 13

SeeMSixty7
Advisor
Advisor
Accepted solution

Ah!

 

Variable typo!

 

(princ (strcat "\n" (itoa mysslen) " " *answers* " objects selected!"))

 

should be

 

(princ (strcat "\n" (itoa mysslen) " " *answerSe* " objects selected!"))

 

Problem is *answers* is nil and has no value so when you try an concatenate it with a string you get the stringp nil error.

 

Good luck.

Message 10 of 13

ebsoares
Collaborator
Collaborator

Hehe Smiley Very Happy didn't catch that on my end either...

 

Thanks to you (and Lee Mac) this program is done!

 

Awesome!

 

Edgar

0 Likes
Message 11 of 13

SeeMSixty7
Advisor
Advisor

Sweet!  Glad to hear it.

 

0 Likes
Message 12 of 13

ВeekeeCZ
Consultant
Consultant
Accepted solution

@ebsoares wrote:

Fantastic! That did it! Thanks @SeeMSixty7!

 

...one more question... The selection prepped for the user (here "[MLeader/MText/Text]") allows them to simply type "ML" for MLeaders, for example, but I tried using "TeXt" in order to accept "TX" but it didn't work... is there a way to do that? Or the simple keystrokes have to always be consecutive letters?

 

...

It could be done

 

(initget "TX LT MT") 
(getkword "[MText/MLeader/TeXt]")

It works good if you use command line, no matter if keyboard input or mouse click. But it's not working with Cursor-Dynamic-Input. So I would not recommend that.

Message 13 of 13

ebsoares
Collaborator
Collaborator

I see. It might be a nice option down the line, if/when we start adding more and more entity types for the users to choose from.

Thanks, @ВeekeeCZ

Edgar

0 Likes