Calling c++ functions from emca script

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
YourRuler
Newbie Member
Posts: 9
Joined: Thu Mar 23, 2017 7:41 pm

Calling c++ functions from emca script

Post by YourRuler » Fri Mar 24, 2017 2:37 pm

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. :)

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

Re: Calling c++ functions from emca script

Post by andrew » Fri Mar 24, 2017 2:58 pm

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?

YourRuler
Newbie Member
Posts: 9
Joined: Thu Mar 23, 2017 7:41 pm

Re: Calling c++ functions from emca script

Post by YourRuler » Fri Mar 24, 2017 3:10 pm

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.

YourRuler
Newbie Member
Posts: 9
Joined: Thu Mar 23, 2017 7:41 pm

Re: Calling c++ functions from emca script

Post by YourRuler » Fri Mar 24, 2017 4:03 pm

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?

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

Re: Calling c++ functions from emca script

Post by andrew » Fri Mar 24, 2017 4:06 pm

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 7750 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 7750 times

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

Re: Calling c++ functions from emca script

Post by andrew » Fri Mar 24, 2017 4:12 pm

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.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”