Page 1 of 1

Best strategies to identify entities

Posted: Sat Jan 18, 2014 9:43 am
by sramp
Hi,
please I would like to know which are the best strategies to identify the elements of the drawing in a unique way and retrieve them subsequently.
I need to retrieve, after save operations, some elements of the drawing like layers and entities.
Reading the forum I understand that the id could change after drawing modifications and save operations.
For this reason I have decide to set a custom property with a unique key in the entities that I need to retrieve.
Please I would like to know which are the best strategies to find them after a save operation.
Further I need to know if also the layer id can change after modifications or save operation. I mean the id I get from entity.getLayerId.
I suppose that if I know the layer where is located the entity I'm looking for my search could be faster. Am I correct ?
For this reason I would like to incorporate the layer id in my unique key.
Thank you very much
sramp

Re: Best strategies to identify entities

Posted: Thu Jan 30, 2014 11:11 am
by andrew
sramp wrote:For this reason I have decide to set a custom property with a unique key in the entities that I need to retrieve.
Yes, this should work fine.
sramp wrote:Please I would like to know which are the best strategies to find them after a save operation.
Iterate through all entities and find the ones with the custom property set to the appropriate value / ID. This should be reasonably fast.
sramp wrote:Further I need to know if also the layer id can change after modifications or save operation. I mean the id I get from entity.getLayerId.
Yes, all object ID's (entity IDs, layer IDs, block IDs, ...) are not persistent between saves.
sramp wrote:I suppose that if I know the layer where is located the entity I'm looking for my search could be faster. Am I correct ?
No. Entities are not in a any way ordered by layer. The layer is just an attribute.
Some optimizations exist for looking up entities by block.
sramp wrote:For this reason I would like to incorporate the layer id in my unique key.
This would be difficult to maintain. Imagine the user changes the layer an entity is on. The unique ID stored as custom property would have to be adjusted at that point.

Another thing to keep in mind is that entities can be copied. If a line entity with a custom property "MyID" set to 7 is copied, the copy will also have a custom property "MyID" which is also set to 7. I.e. the ID is no longer unique.

Re: Best strategies to identify entities

Posted: Thu Jan 30, 2014 12:31 pm
by andrew
I've had a brief look again at object handles which are actually supposed to be persistent between save / open operations.

It looks like due to a bug in the importer code, object handles are imported but then overwritten with new ones.

This problem should be fixed for the next release. This means that you can simply use object handles to uniquely identify entities, layers, etc.
This should also eliminate the problem with double IDs after copying entities, etc.

Re: Best strategies to identify entities

Posted: Fri Jan 31, 2014 11:41 am
by sramp
Andrew,
thank you very much for your very useful answers.
Thank you again to have put in evidence the problems about entities duplicates.
It is a real issue and could create some problems.
I'm glad to know about the object handles, this will simplify a lot my script.
Best
sramp

Re: Best strategies to identify entities

Posted: Mon Feb 10, 2014 4:27 pm
by andrew
sramp: QCAD 3.4.6 is now available with persistent object handles. An example script which selects an entity based on a handle entered as hexadecimal number is available in:
scripts/Misc/MiscSelect/SelectByHandle

Re: Best strategies to identify entities

Posted: Mon Feb 10, 2014 6:14 pm
by sramp
Andrew,
thank you very much.
Very useful function !