TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MeasureUp on September 06, 2012, 01:46:09 AM

Title: How to Check if An Entity Exists
Post by: MeasureUp on September 06, 2012, 01:46:09 AM
Hi,
Say I drawn a line and I assigned it (setq Entity1 (entlast)), then after a couple of operations I want to check if it still exists.
I will copy the line if it exists.
Here is my code:
Code: [Select]
(if Entity1
(command "_.copy" Entity1 "" pause pause)
)

Is there anything wrong?
Thanks for your help.
Title: Re: How to Check if An Entity Exists
Post by: Kerry on September 06, 2012, 02:08:02 AM
Subject to anything changing the value of the selected entity, or the selected entity being erased or on a locked layer
the code you posted should work.

Are you having difficulty ?

Is it possible that the selected entity variable is out of scope ??
Title: Re: How to Check if An Entity Exists
Post by: MeasureUp on September 06, 2012, 02:13:13 AM
Thanks for your quick reply.
I haven't found any error with my code but just not sure if it is correct.
Title: Re: How to Check if An Entity Exists
Post by: irneb on September 06, 2012, 03:41:19 AM
Generally you'd not have an issue if you know what has happened between creating the line and copying it. Though if some random stuff happened in between, it might be possible that the line was erased. Unfortunately your Entity1 variable would still contain the ename of the line, even after it was erased, so your if statement (as you have it now) would erroneously "think" it should continue with the copy command.

If you want to make sure it still exists, you can check if you can obtain it's DXF data (using entget). If entget returns a list (instead of nil) the entity exists. You can then further check from that list on which layer it resides, and then using tblsearch check if that layer is thawed / on / unlocked.

There might be other ways too, e.g. vlax-ename->vla-object, ssadd, (ssget "X") with ssmemb, etc.
Title: Re: How to Check if An Entity Exists
Post by: Lee Mac on September 06, 2012, 06:28:45 AM
For an entity:

Code: [Select]
(entget <entity>)
For a VLA-Object:

Code: [Select]
(vlax-erased-p <VLA-Object>)
Title: Re: How to Check if An Entity Exists
Post by: Didge on September 06, 2012, 08:30:58 AM
I tend to use

Code: [Select]
(if (and Entity1 (entget Entity1)) ......

This avoids errors when trying to entget a nil value.
Title: Re: How to Check if An Entity Exists
Post by: MeasureUp on September 06, 2012, 10:50:36 PM
...
Unfortunately your Entity1 variable would still contain the ename of the line, even after it was erased, so your if statement (as you have it now) would erroneously "think" it should continue with the copy command.

If you want to make sure it still exists, you can check if you can obtain it's DXF data (using entget). If entget returns a list (instead of nil) the entity exists. You can then further check from that list on which layer it resides, and then using tblsearch check if that layer is thawed / on / unlocked.

There might be other ways too, e.g. vlax-ename->vla-object, ssadd, (ssget "X") with ssmemb, etc.
Exactly.
I delected Entity1 by using "erase" command.
But when I use (entdel Entity1) it still find the name of Entity1.
Thanks to irneb and everyone's help.
Title: Re: How to Check if An Entity Exists
Post by: Lee Mac on September 07, 2012, 07:19:47 AM
I delected Entity1 by using "erase" command.
But when I use (entdel Entity1) it still find the name of Entity1.

When you erase an entity (either using the erase command / entdel / vla-delete), you are only setting the erase flag for that entity. The entity can subsequently be 'unerased' by toggling the erase flag using the entdel function.

Only when the drawing is closed is the entity actually deleted; to attempt to offer an explanation for this behaviour, I would hazard a guess that the save command does not write the data to file for entities whose erase flag is set, and hence such entities will not be present in the drawing database when the drawing is re-opened.