Page 1 of 1

Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 2:37 pm
by YourRuler
Good morning!
I'm doing some research into how viable the QCad system is for our purposes.
I have the example plugin (support/examples/exampleplugin on the repo) compiling, and I can see the menu item that it adds to the miscMenu.
My understanding that the Proper way to proceed is to have an emca Script which creates an instance of that class to call it's functions?

I added a public function to the example,

Code: Select all

virtual int getInt();
How do I call this function from an emca script now?

I'm sure it's simple e_confused

Any help would be greatly appreciated. :)

Re: Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 2:58 pm
by andrew
YourRuler wrote:I'm sure it's simple e_confused
No, this is not trivial as you will have to make your C++ function scriptable (i.e. provide script bindings for your class / function). RExamplePlugin has some example code on how to do this (see RExamplePlugin::initScriptExtensions and all the static functions it refers to).

However, depending on what you are intending to do this might all not be necessary. C++ is really only needed if your add-on needs to call into an existing C++ library.

Perhaps you can give some more information about what you are trying to achieve with QCAD or your add-on?

Re: Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 3:10 pm
by YourRuler
andrew wrote:
YourRuler wrote:I'm sure it's simple e_confused
No, this is not trivial as you will have to make your C++ function scriptable (i.e. provide script bindings for your class / function). RExamplePlugin has some example code on how to do this (see RExamplePlugin::initScriptExtensions and all the static functions it refers to).

However, depending on what you are intending to do this might all not be necessary. C++ is really only needed if your add-on needs to call into an existing C++ library.

Perhaps you can give some more information about what you are trying to achieve with QCAD or your add-on?
Thanks for your reply,

I am prototyping a USB Device which uses a laser to scan the contours of an object in a 2-Dimensional space.
I have an API built for retrieving all of the information from the laser and the other components on my device in the form of a DLL which I want to tie in to get live drawing of the object as you scan it. I am just using getInt() as a simple example function. in the future such a function is intended to call underlying functions in my API and then return the results.

Re: Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 4:03 pm
by YourRuler
After you specifically mentioned RExamplePlugin::initScriptExtensions I had a careful look at what was happening and followed the code trail backwards.
I managed to recreate the MyClassToString function as MyClassGetInt, and was able to work through the process from there.
I apologize for asking a question that, as it turns out, I didn't need much help on. I'll blame my unfamiliarity with c++ :shock:
I can now add new scriptable functions which make calls to my external libraries and return the values.

is there a best-practice for returning more complicated objects to the script?

Re: Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 4:06 pm
by andrew
OK, in that case, making your class / functions scriptable might be the best way forward indeed.

I've added some more advanced example script bindings to the example plugin:
https://github.com/qcad/qcad/tree/maste ... mpleplugin

To check if your plugin has been loaded successfully, check Help > About... > Plugins:
Screen Shot 2017-03-24 at 16.06.15.png
Screen Shot 2017-03-24 at 16.06.15.png (35.96 KiB) Viewed 7808 times
You can use the script console to test your class:
Misc > Development > Script Shell
Screen Shot 2017-03-24 at 16.02.06.png
Screen Shot 2017-03-24 at 16.02.06.png (133.11 KiB) Viewed 7808 times

Re: Calling c++ functions from emca script

Posted: Fri Mar 24, 2017 4:12 pm
by andrew
YourRuler wrote:is there a best-practice for returning more complicated objects to the script?
For more complex objects, it depends a lot if the type can be copied (e.g. a data type such as a matrix or vector) or if it cannot be copied (e.g. a widget).

In the first case, you can use qScriptValueFromValue to convert the complex type to a QScriptValue in most cases.

In the second case, you would use engine->newQObject(cppResult, QScriptEngine::QtOwnership); to essentially create a script wrapper around a pointer.