Author Topic: How to Check if An Entity Exists  (Read 4725 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
How to Check if An Entity Exists
« 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to Check if An Entity Exists
« Reply #1 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 ??
« Last Edit: September 06, 2012, 02:14:54 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to Check if An Entity Exists
« Reply #2 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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to Check if An Entity Exists
« Reply #3 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to Check if An Entity Exists
« Reply #4 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>)

Didge

  • Bull Frog
  • Posts: 211
Re: How to Check if An Entity Exists
« Reply #5 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.
Think Slow......

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to Check if An Entity Exists
« Reply #6 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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: How to Check if An Entity Exists
« Reply #7 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.