Nate Holt's Blog

September 28, 2009

Nate’s Simple AutoLISP – Lesson 007

Filed under: Tutorials — nateholt @ 10:12 pm

Using the selection set function “ssget” to track down and remove instances of an invisible block.

This exercise will be short and [hopefully] sweet. The idea for this one came from personal experience… today.

Let’s say that you create a block instance consisting of just one attribute definition. You define its tag name to be “BY_CUSTOMER” and you leave the value blank, planning to fill in with different values later.

autolisp007a
You go ahead and block this single attribute definition with a block name of “CUSTOMER_FLAG”…

autolisp007b

 

… and you sprinkle a bunch of instances of this block across your drawing. You keep working away. A couple hours later you’re ready to go back and assign values to the BY_CUSTOMER attribute on each inserted instance of your CUSTOMER_FLAG block.

Hey… where are they? Don’t see them. You know you inserted them at multiple places in your drawing. But there’s nothing to pick on!

The block was just a single attribute and, unfortunately, you created it with a blank default value ( ! ).

Okay, let’s erase them and have a “do over”. But hey, how can I erase something I can’t select… something I can’t even see?!

Well, could try the QTEXT trick. Turn QTEXT “ON” and type REGEN. All blank attribute values now should display a tiny dot. But how can I tell which dot is which without picking one at a time and listing what it is?

Quick solution using the AutoLISP function “SSGET” with a filter

The solution uses AutoLISP and is easy… we can do it all right at the “Command:” prompt.

What we’ll do is make a “selection set” of all instances of this invisible / un-selectable block. We’ll do this by scanning ALL entities in our active drawing and filter out everything except blocks that carry the block name “CUSTOMER_FLAG”. A block instance carries the assigned block name in the “2” entity sub-record. We’ll make a really simple filter by just looking for entities that have “CUSTOMER_FLAG” as the value of their “2” subrecord.

Type this at the “Command:” prompt:

(setq ss (ssget “_X”  ‘((2 . “CUSTOMER_FLAG”))))  [Enter]

If any matches found, the message “<Selection set xx>” will display below the “Command:” prompt. What we’ve done here is defined a dummy variable we’re calling “ss” and it will carry the selection set returned by the call to “(ssget …)”.

Let’s see how many it found. Type this:

(sslength ss) [Enter]

… and it will display the number of instances of CUSTOMER_FLAG that were found in the drawing.

Okay, now let’s blow them away so we can PURGE our drawing. Type this:

(command “_ERASE” ss “”) [Enter]

… and they’re gone! The above just launches the normal AutoCAD ERASE command. We pass the selection set variable we created above, “ss” to this command along with an extra “” at the end to kick out of the “select objects” loop. Finally, launch the PURGE command. You should now see that block CUSTOMER_FLAG is a candidate for purging. Yes! Blow it away.

 

Caution

The above solution will probably work as expected 99.9% of the time. But there are some entities that “share” the “2” sub-record. For example, an ATTDEF entity uses the “2” subrecord to carry the attribute tag name. So if we had some orphaned ATTDEFs floating around in our drawing and they happened to have a tag name of “CUSTOMER_FLAG”, then these CUSTOMER_FLAG ATTDEFs would be erased along with the CUSTOMER_FLAG block instances.

To be really, really safe, we could redo our selection set filtering like this to make sure that we’re only dealing with block instances AND the block instance is named “CUSTOMER_FLAG”. The entity type is carried in sub-record type zero. Block insert entity type is “INSERT”. So, a fool-proof, command-prompt-entered query to build the selection set might look like this:

(setq ss (ssget “_X”  ‘((-4 . “<AND”)(0 . “INSERT”)(2 . “CUSTOMER_FLAG”)(-4 . “AND>”))))  [Enter]

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: