queryEntity

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

queryEntity

Post by sramp » Thu Jan 23, 2014 5:17 pm

Hello,
I'm encountering a problem when I try to get an entity from the document.
I wrote this code to debug the problem, but QCAD-Pro crashes when I execute the queryEntity.
I have checked step by step with the debugger and I have seen that document is not null.
this.theEntityId is the id of a line obtained with getId() method.
Please, anybody can suggest me where I'm wrong?

Code: Select all

Entry.prototype.evidenzia = function () {
	qDebug("entity length = " + this.theEntityId);
	var document = this.getDocument();
	var entity = document.queryEntity(this.theEntityId);
	qDebug("entity length = " + entity.getId());
}
Here the diagnostic report

Code: Select all

Process:         QCAD-Pro [1857]
Path:            /Applications/QCAD-Pro.app/Contents/MacOS/QCAD-Pro
Identifier:      org.qcad
Version:         3.4 (3.4.5)
Code Type:       X86 (Native)
Parent Process:  launchd [211]

Date/Time:       2014-01-23 16:53:38.772 +0100
OS Version:      Mac OS X 10.7.5 (11G63)
Report Version:  9

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x000000005d1621e4

VM Regions Near 0x5d1621e4:
    MALLOC_LARGE (freed)   0000000018700000-0000000018c55000 [ 5460K] rw-/rwx SM=PRV
-->
    __TEXT                 000000008fe9f000-000000008fed2000 [  204K] r-x/rwx SM=COW  /usr/lib/dyld

Application Specific Information:
objc[1857]: garbage collection is OFF

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libqcadcore.dylib                   0x01286520 RDocument::queryEntity(int) const + 16
1   libqcadecmaapi.dylib                0x0021045c REcmaDocument::queryEntity(QScriptContext*, QScriptEngine*) + 492
2   QtScript                            0x03112fd8 QScriptable::QScriptable() + 3256
3   QtScript                            0x0302acc1 0x2f59000 + 859329
Thank you
sramp

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

Re: queryEntity

Post by andrew » Fri Jan 24, 2014 11:53 am

Can you provide more context?

Did you try to debug your code using the integrated script debugger?:
// some code
debugger;   // start debugging from here
// some code
Then run QCAD with the -enable-script-debugger switch:
./qcad -enable-script-debugger

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

Re: queryEntity

Post by sramp » Fri Jan 24, 2014 3:51 pm

Andrew,
thanks for the reply.
I'm confused since sometimes the code is executed without problem.
Sometime I'm not able to obtain the document ( document=null).

Code: Select all

ven gen 24 12:37:37 2014 Uncaught exception at scripts/Italsoft/Entry/Entry.js:279: TypeError: Result of expression 'document' [null] is not an object.
Sometime I got a crash when I use the entity.
I've noticed less problems after removing the QDebug messages.
I have tried also in a Win7 system, more or less same results.

Code: Select all

Entry.prototype.beginEvent = function () {

...
 this.adapter.selectionChanged.connect(this, "handleSelection");
...
 this.widgets["test"].clicked.connect(this, "test");
...

}

...

Entry.prototype.test = function () {
debugger;
	var document = this.getDocument();
	// this.theEntityId is filled in the handleSelection function
	var entity = document.queryEntity(this.theEntityId);
	var msg="entity length = " + entity.getLength();
	QMessageBox.information(null, "Title", msg);
}
I launch the app with this command :

Code: Select all

open -n ./QCAD-Pro.app --args -enable-script-debugger -enable-xdata -exec /Applications/QCAD-Pro.app/Contents/Resources/scripts/Italsoft/Entry/Entry.js
I create a new document and I draw some lines.
I select one ( in this way my script get the id of the selected entity).
When I push the "test" button my function is executed.
If the application crashes, it crashes also the debugger window.
Any idea ?

Thank you
sramp

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

Re: queryEntity

Post by andrew » Thu Jan 30, 2014 11:03 am

The -enable-script-debugger switch might cause crashes.

The reason is that the debugger is consulted regularly. The debugger then processes all queued events. This interferes with the order in which commands are intended to be executed.

Long story short, if your application 'randomly' crashes, disable the debugger which should solve the problem. Only enable the debugger to do actual debugging (step through) and keep in mind that the debugger has an impact on the order in which events are processed.

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

Re: queryEntity

Post by sramp » Fri Jan 31, 2014 11:22 am

Andrew,
thank you for the hint, it's good to know.
Concerning this specific case I've written a test script and I have found that if I set my document in the beginEvent method everything works.
I'm pretty new with Javascript, so I post here my code, maybe I'm wrong somewhere.

Code: Select all

include("../WidgetFactory.js");
include("../Test.js");

function TestQueryEntity(guiAction) {
    Test.call(this, guiAction);
    this.document;
    this.mw;
    this.widgets;
}

TestQueryEntity.prototype = new Test();
TestQueryEntity.includeBasePath = includeBasePath;

TestQueryEntity.prototype.beginEvent = function () {
    Test.prototype.beginEvent.call(this);

	// this fixes the problem
    this.document = this.getDocument();
    
    // TestQueryEntity.ui is a window with a push button "action"
    this.mw = WidgetFactory.createWidget("scripts/Test/TestQueryEntity", "TestQueryEntity.ui");
    
    this.widgets = getWidgets(this.mw);
    this.mw.show();
    this.widgets["action"].clicked.connect(this, "action");
    this.terminate();
};

TestQueryEntity.prototype.action = function () {

	// this causes the problem
    // var document = this.getDocument();
    
    if (isNull(this.document)) {
        QMessageBox.information(null, "Test", "document isNull");
    } else {


        var ids = this.document.querySelectedEntities();
        if (ids.length > 0) {
            var id = ids[ids.length - 1];
            var entity = this.document.queryEntity(id);
            if (isNull(entity)) {
                QMessageBox.information(null, "Test", "queryEntity returned null");
            } else {
                var value = entity.getLength();
                var msg = "Length = " + value;
                QMessageBox.information(null, "Test", msg);
            }
        } else {
            QMessageBox.information(null, "Test", "no entities selected");
        }
    }
}

TestQueryEntity.init = function (basePath) {
    var action = new RGuiAction(qsTr("&TestQueryEntity"),
        RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/TestQueryEntity.js");
    action.setSortOrder(10);
    EAction.addGuiActionTo(action, Test, true, false, false);
}; 

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”