Best strategies to identify entities

Discussion forum for contributors and developers who are using the QCAD version 3 ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Best strategies to identify entities

Postby sramp » Sat Jan 18, 2014 9:43 am

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
sramp
Active Member
 
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm

Re: Best strategies to identify entities

Postby andrew » Thu Jan 30, 2014 11:11 am

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.
User avatar
andrew
Site Admin
 
Posts: 2556
Joined: Fri Mar 30, 2007 6:07 am

Re: Best strategies to identify entities

Postby andrew » Thu Jan 30, 2014 12:31 pm

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.
User avatar
andrew
Site Admin
 
Posts: 2556
Joined: Fri Mar 30, 2007 6:07 am

Re: Best strategies to identify entities

Postby sramp » Fri Jan 31, 2014 11:41 am

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
sramp
Active Member
 
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm

Re: Best strategies to identify entities

Postby andrew » Mon Feb 10, 2014 4:27 pm

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
User avatar
andrew
Site Admin
 
Posts: 2556
Joined: Fri Mar 30, 2007 6:07 am

Re: Best strategies to identify entities

Postby sramp » Mon Feb 10, 2014 6:14 pm

Andrew,
thank you very much.
Very useful function !
sramp
Active Member
 
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm


Return to QCAD 3 Developers and Contributors

Who is online

Users browsing this forum: No registered users and 0 guests

cron