Using QCAD Simple API most effectively and successfully? [SOLVED]

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
Joseph1916
Active Member
Posts: 36
Joined: Wed Jul 12, 2023 2:58 pm
Location: Florida

Using QCAD Simple API most effectively and successfully? [SOLVED]

Post by Joseph1916 » Mon Aug 07, 2023 6:35 pm

To All:

I'm new to programming since the late 1970's and early 1980's..... I could use tip's on the QCAD's Simple API, such as how it is being used for development?

I'm getting feedback that executing with the ECMAScript shell one line at a time is effective.
The below links was helpful:

[url]https://www.freecodecamp.org/news/objec ... rogramming[/url]
[url]https://dmitripavlutin.com/javascript-c ... ete-guide/[/url]


Joseph1916
Last edited by Joseph1916 on Tue Aug 15, 2023 12:54 pm, edited 4 times in total.

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

Re: Using QCAD Simple API most effectively and successfully?

Post by andrew » Mon Aug 07, 2023 7:37 pm

Please refer to our scripting tutorials at:
https://qcad.org/en/tutorial-script-programming

Feel free to post back if you have questions. Please be as specific as possible, so we can help effectively.

Joseph1916
Active Member
Posts: 36
Joined: Wed Jul 12, 2023 2:58 pm
Location: Florida

Re: Using QCAD Simple API most effectively and successfully?

Post by Joseph1916 » Tue Aug 08, 2023 6:05 pm

[attachment=3]Header.png[/attachment][attachment=3]Header.png[/attachment]

OS: Ubuntu Linux 22.04
QCAD Version: 3.28.1


I have included 4 attachments dealing with the *.ui file type. I am assuming they appear above and in reverse order. The 4 reflect the content of ExMyMinimal.ui. This file executed on my Ubuntu Linux 22.04 fine.

I am intending to use this format as a template for *.iu files when needing widgets, custom widgets to obtain a functional GUI.

Should that work? Add any comments......

Another post to you will be about the associated ExMyMinimal.js file.

Jsseph1916
Attachments
iu_footer_lines.png
iu_footer_lines.png (5.31 KiB) Viewed 10511 times
Custom_widget.png
Custom_widget.png (13.96 KiB) Viewed 10511 times
WidgetShot.png
WidgetShot.png (45.76 KiB) Viewed 10511 times
Header.png
Header.png (10.21 KiB) Viewed 10511 times

Joseph1916
Active Member
Posts: 36
Joined: Wed Jul 12, 2023 2:58 pm
Location: Florida

Re: Using QCAD Simple API most effectively and successfully?

Post by Joseph1916 » Tue Aug 08, 2023 8:06 pm

Andrew;
This posting deals with the content of ExMyMinimal.js file. Line 1-57 inclusively. Again they might be in reverse order.
Line 1-10 sets up the functions required structure with include files and main function call

[attachment=3]js_function_declaratiom.png[/attachment]

the use of the term "prototype" is of interest to me, as it appears multiple times......
the same is true of the term "this" .......


[attachment=2]js_prototype.png[/attachment]
The reserved term "var" appears in in some of my snippets. Considering the work being done, that is not many variables !!


[attachment=1]js_vavable_assignment_+.png[/attachment]

[attachment=0]js.initialize_slot.png[/attachment]

ends with an initialization call, amazing !

I hope this makes some sense. Trying to learn this new world we all have, almost with each passing day....

Thank You !!

Joseph1916
I will be out tomorrow..
Attachments
js.initialize_slot.png
js.initialize_slot.png (38.96 KiB) Viewed 10503 times
js_vavable_assignment_+.png
js_vavable_assignment_+.png (40.5 KiB) Viewed 10503 times
js_prototype.png
js_prototype.png (49.69 KiB) Viewed 10503 times
js_function_declaratiom.png
js_function_declaratiom.png (25.21 KiB) Viewed 10503 times

CVH
Premier Member
Posts: 3480
Joined: Wed Sep 27, 2017 4:17 pm

Re: Using QCAD Simple API most effectively and successfully?

Post by CVH » Wed Aug 09, 2023 1:26 am

Joseph1916,
You are probably following this tutorial:
https://qcad.org/en/tutorial-interactive-script-actions
It is fully explained step by step, If you read carefully and understand each step then the final code make sense.

May I remark that adding screenshots is not helping, using a code panel is the proper way.
Code may be tens to several hundreds lines long. :wink:
We can't copy/paste and test/correct code from a picture.

Indeed these attachments are listed below and seemingly in reverse order.
But these sentences in your post tells me that you tried to insert them inline with your text.

Code: Select all

[attachment=3]js_function_declaratiom.png[/attachment]
Why they don't occur inline is a mystery for me.
Where they inserted, removed and re-inserted?
BTW: The above is a code panel otherwise the forum thinks that I am also inserting a picture inline in my reply.

Correct, var is the most common way to declare variables.
One should always declare variables.
Remark that event and preview in ExMyMinimal.prototype.pickCoordinate = function(event, preview) {...}
are also declared variable names of arguments that are passed on to the pickCoordinate function.

Variables have block scope meaning that the exists within the function block from { to }.
Outside that block they have no meaning, leaving the block they do not retain their value and if unused for a while they are removed by what is called a Garbage Collector. (Otherwise the memory will quickly be filled with unused variables :wink: )
Joseph1916 wrote:
Tue Aug 08, 2023 8:06 pm
Considering the work being done, that is not many variables !!
Indeed, the power of the intensive QCAD API.
With a document, an operation and a circle shape we add a circle entity to the drawing.
Previewing or actually casting it is handled in the pickCoordinate function.
Remark that the pickCoordinate and getOperation functions are called by QCAD on regular basis to update the screen.

In the last step of the tutorial we add a basic Option Toolbar UI, read the value on change and add a circle with this radius.
To create basic to complex UI files for the Option Toolbar or for a dialog I can advise Qt Designer.
One tiny mistake/typo in the XML file and it won't work, seemingly without a clear reason, not debug-able.

About prototype and this:
this is in fact your class.
this.pos is practically the same as a variable but it is called an object.
An object of your class with the name 'pos'.
With a subtle difference: It is passed by reference and not by value.

prototype is harder to explain but what interest us is that the class objects are shared among all class members.
this.radius in slotRadiusChanged{...} is the same thing as in getOperation{...}. Both are 'prototyped'. :wink:

Because of ExMyMinimal.prototype = new EAction(); the meaning of this can also be EAction ...
... this.updatePreview(); executes https://github.com/qcad/qcad/blob/maste ... n.js#L1711
... and this.applyOperation(); executes https://github.com/qcad/qcad/blob/maste ... n.js#L1753

Remark then for example that the pickCoordinate function of the EAction class does nothing:
https://github.com/qcad/qcad/blob/maste ... n.js#L1784
Without a tool, your tool, running one can click wherever one wants in the drawing.
Picking coordinates (and moving the cursor) does nothing at all.
Once your tool is running then it previews the circle or adds the circle to the drawing.
ExMyMinimal.prototype.pickCoordinate {} overwrites the standard behavior.
Joseph1916 wrote:
Tue Aug 08, 2023 8:06 pm
ends with an initialization call, amazing !
Mostly the order has no meaning, we tend to end with the initialization or include it as a separate script file.

This is how QCAD retrieves all tool scripts and initialize them on startup.
It looks for sub folders in the 'scripts' folder. See: https://github.com/qcad/qcad/tree/master/scripts
In each folder it looks for the *.js file with the same name as the folder.
This is the main tool script file to implement.
In that file it looks for the init function or it looks for a *Init.js file again with the same prefix as the folder name.

It may be that in your QCAD installation the 'script' folder is fairly empty compared with GitHub.
All standard tool scripts also come in compiled form :wink:
Not to worry, everything on GitHub is there and more when it is QCAD Pro or QCAD/CAM.


Finally, nothing here is QCAD Simple API.
This is mentioned in the Shell tutorial under 'Running commands': https://qcad.org/en/tutorial-the-script-shell
And an overview of the Simple API functions: https://www.qcad.org/doc/qcad/latest/de ... imple.html
One can write simple code for the Shell or for an Addon but ... Interactive ... That requires the tutorial mentioned at the beginning.
addCircle(cx,cy, radius) will add a circle entity at (cx, cy) without using a cursor, without a preview.

Regards,
CVH

Joseph1916
Active Member
Posts: 36
Joined: Wed Jul 12, 2023 2:58 pm
Location: Florida

Re: Using QCAD Simple API most effectively and successfully?

Post by Joseph1916 » Thu Aug 10, 2023 4:19 pm

Having Internet problems. I will be back when I figure out the issue. Uncovered Internet provider has shut me down now till Saturday August 12th. As of now I only have cell phone connection. I have to make do till Saturday my time.

The Internet provider now informs me not till 11:59 pm of August 12th.

I am beside. Myself. Hope to be online soon.

Joseph1916
Active Member
Posts: 36
Joined: Wed Jul 12, 2023 2:58 pm
Location: Florida

Re: Using QCAD Simple API most effectively and successfully? [SOLVED]

Post by Joseph1916 » Tue Aug 15, 2023 12:38 am

I see at this point I was not familiar enough with the use of other tools I already have at my disposal. Or have not gained enough knowledge about QCAD.

I WILL continue exploring Scripting to fully appreciate the Simple API.

I note l have been reading on Javascript and becoming pretty sure I will not require C++. Everything can be done with Javascript. With the use of QCAD "Simple" API .

I am going to post "SOLVED" to this thread.
Read the Documentation and comprehend them !!

Joseph1916

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”