Page 1 of 1

Again add custom properties

Posted: Tue Dec 10, 2013 6:42 pm
by sramp
Hi All,
I'm new with scripting and till now I have spent several hours reading and studying the scripts.
I have experimented with success the use of the Qt api to create custom user interface elements and I'm really impressed by the results.
QCAD is really a powerful enviroment. Many thanks to Qt and Andrew.
Now I'm facing the QCAD api and I need some help.
Reading documentation and the forum it seems that it doesn't exist a unique and persistent identifier for the entities of the drawing (DXF/DWG). Am I correct ?
In my project I need to retrieve at different times and also after drawing revisions some entities.

I've tried to use custom properties to store an unique id, but I 'm encountering a problem in my test script.
This is an excerpt of my test code :
var di = this.getDocumentInterface();
    var document = this.getDocument();

    // find all selected entities:
    var ids = document.querySelectedEntities();
    var i;
    var op = new RAddObjectsOperation();
   for (i=0; i<ids.length; i++) 
        {
        	debugger;
        	var id = ids;
        	var entity = document.queryEntityDirect(id);
        	//var entity = document.queryEntity(id);
   			entity.setProperty(new RPropertyTypeId("TEST"), 25);
		 	//entity.setCustomProperty("QCAD","TEST",25);
		
			op.addObject(entity);
       		di.applyOperation(op);
       }	
    this.terminate();


Using queryEntityDirect the entity is deleted.
Using queryEntity the entity is not deleted, but the custom property is not added, QCAD become instable and crash.
No problems when I read the custom property ( obviously created with QCAD gui).

I'm using QCAD-PRO with a Mac (open -n QCAD-Pro.app --args -enable-script-debugger -enable-xdata ) :

QCAD version: 3.4.3.0
Date: Nov 12 2013
Qt version: 4.8.4
Compiler version: gcc 4.2.1
OS: Mac
OS version: Mac OS X 10.7

Hope somebody out of here is able to kick me in the right direction.
Thanks
sramp

Re: Again add custom properties

Posted: Wed Dec 11, 2013 11:12 am
by sramp
It seems I have found the problem with my code.
When I created the RPropertyTypeId I have omitted to put the title of the custom property.
//entity.setProperty(new RPropertyTypeId("TEST"), 25); 
 entity.setProperty(new RPropertyTypeId("QCAD","TEST"), 25);
After this modifications everything works as expected.

Re: Again add custom properties

Posted: Wed Dec 11, 2013 12:21 pm
by Clive
sramp wrote:I have experimented with success the use of the Qt api to create custom user interface elements and I'm really impressed by the results.
This sounds really interesting, any chance of a screenshot of what you've managed to do ?

Re: Again add custom properties

Posted: Wed Dec 11, 2013 1:02 pm
by andrew
queryEntity should be used in this case. queryEntityDirect is faster but should only be used if the entity will not be modified in any way.
The call to applyOperation should be outside of the loop (this might have caused the crash).
Calling setCustomProperty or setProperty with a custom property ID is equivalent.
sramp wrote:
var di = this.getDocumentInterface();
    var document = this.getDocument();

    // find all selected entities:
    var ids = document.querySelectedEntities();
    var i;
    var op = new RAddObjectsOperation();
    for (i=0; i<ids.length; i++) {
        var id = ids;
        var entity = document.queryEntity(id);
        entity.setCustomProperty("QCAD","TEST",25);
        op.addObject(entity);
    }	
    di.applyOperation(op);
    this.terminate();

Re: Again add custom properties

Posted: Wed Dec 11, 2013 6:08 pm
by sramp
Thanks Andrew,
yes, I realized to move outside the loop the RAddObjectsOperation creation and applyOperation later this morning.
Now I'm looking for a fast way to find an entity with a specific custom properties, but I suppose I have to iterate all the entities.