Again add custom properties

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
sramp
Active Member
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm

Again add custom properties

Post by sramp » Tue Dec 10, 2013 6:42 pm

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

sramp
Active Member
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm

Re: Again add custom properties

Post by sramp » Wed Dec 11, 2013 11:12 am

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.

User avatar
Clive
Moderator
Posts: 1329
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Again add custom properties

Post by Clive » Wed Dec 11, 2013 12:21 pm

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 ?
For the best support please state your operating system, QCAD version and add any supporting DXF/DWG files, screenshots etc...

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Again add custom properties

Post by andrew » Wed Dec 11, 2013 1:02 pm

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();

sramp
Active Member
Posts: 26
Joined: Mon Dec 02, 2013 4:15 pm

Re: Again add custom properties

Post by sramp » Wed Dec 11, 2013 6:08 pm

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.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”