new script for dropdown text options

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
Roy83
Newbie Member
Posts: 8
Joined: Thu Oct 31, 2024 1:40 pm

new script for dropdown text options

Post by Roy83 » Wed Nov 13, 2024 9:47 am

hello.
i use qcad 3.31.2 on a windows 10

I have a script so that I get text from a selection list in the drawing.
That is this script:

Code: Select all

// Create a list of text options
var textOptions = ["Text Option 1", "Text Option 2", "Text Option 3"];

// Show a simple input dialog with a dropdown list
var selectedText = QInputDialog.getItem(
    EAction.getMainWindow(),          // Parent widget (Main window of QCAD)
    "Select Text Option",            // Title of the dialog
    "Choose a text option:",         // Prompt text
    textOptions,                     // List of options
    0,                               // Default selected index
    false                             // Editable combo box
);

// Check if a valid option was selected (not canceled)
if (selectedText !== "") {
    // Get the current document and interface
    var appWin = EAction.getMainWindow();
    var di = appWin.getDocumentInterface();
    var doc = di.getDocument();

    // Define position for the text (e.g., at coordinates 10, 10)
    var position = new RVector(10, 10);

    // Create text data using the selected option
    var textData = new RTextData(
        position,           // Position of the text
        position,           // Alignment point
        1.0,                // Text height
        0,                  // Text angle
        RS.VAlignTop,       // Vertical alignment
        RS.HAlignLeft,      // Horizontal alignment
        RS.LeftToRight,     // Text direction
        RS.Exact,           // Line spacing style
        1.0,                // Line spacing factor
        selectedText,       // Text content
        "Standard",         // Text font
        false,              // Bold
        false,              // Italic
        0.0,                // Tracking
        false               // Background color (disabled)
    );

    // Add the text entity to the document
    var textEntity = new RTextEntity(doc, textData);
    di.applyOperation(new RAddObjectOperation(textEntity));
}
if i load this in the script shell its working, but then i must paste this code everytime when i wil use this.
Is there a way for qcad to automatically load this script into qcad.
or can you get it bij the Misc menu?
If I now put it in the script folder, I only see the box with the text option, but QCAD does not start.
thanks fot the help.
Attachments
Dropdown.js
(1.87 KiB) Downloaded 1015 times

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

Re: new script for dropdown text options

Post by andrew » Wed Nov 13, 2024 10:01 am

Yes, you can add your script to a menu. In fact, all QCAD menus are implemented as scripts.

Have a look at the various example scripts of QCAD to see how this is done, for example the text example:

https://github.com/qcad/qcad/blob/maste ... /ExText.js

Your script snippet would mainly just go into the beginEvent function of such a script. In the init function, you can name the menu and decide where it goes (replace "DrawExamplesMenu" with "MiscMenu" to place your menu in the "Misc" menu).

Your script has to be placed in a folder with the same name as the script, for example

Code: Select all

Misc/MyScript/MyScript.js

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

Re: new script for dropdown text options

Post by CVH » Wed Nov 13, 2024 10:16 am

Hi,

Please remark that what you call the text angle is in fact the column width, zero for no width or not as text in a column.
QCAD will not break up words to fit a text line in a column

Code: Select all

        0,                  // Text angle
The text angle is way down:

Code: Select all

        0.0,                // Tracking
The last parameter has nothing to do with the background, it is a boolean to set simple text or not.

Code: Select all

        false               // Background color (disabled)
Also see reference: RTextData

Regards,
CVH

Roy83
Newbie Member
Posts: 8
Joined: Thu Oct 31, 2024 1:40 pm

Re: new script for dropdown text options

Post by Roy83 » Wed Nov 13, 2024 10:52 am

Hello CVH.
Thank you for your message.
In principle it is not necessary for qcad to split the text.
I want to use this for a simple selection list for many different materials that I have.

But unfortunately I can't figure out how to add this to the qcad menu.
Could someone help me out? :oops:

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

Re: new script for dropdown text options

Post by CVH » Thu Nov 14, 2024 7:20 am

Roy83 wrote:
Wed Nov 13, 2024 10:52 am
In principle it is not necessary for qcad to split the text.
Not in a column, then zero is fine but it is not called the Text Angle parameter as by your remark aside the entry.
Roy83 wrote:
Wed Nov 13, 2024 10:52 am
But unfortunately I can't figure out how to add this to the qcad menu.
Could someone help me out?
Let us take Andrew's proposal and fill in the blanks.

Copy the code from the open source Text Example at GitHub.
At the top you will find a button to copy the raw data to your clipboard.

Paste the clipboard content to a new file in a plain text editor.
Everything from line 19 to line 76 is basically required.
Add a custom header to it if you like.

We must give it a different and yet unused new class name, otherwise it will overwrite existent code.
This is not really problematic for the Text Example script but it will be a major issue when you use a QCAD core class name. :wink:
Replace every occurrence of 'ExText' with for example 'MyScript', it starts with a capital letter.

As per example, the file is saved at ...QCAD/scripts/Misc/MyScript/MyScript.js
Where the 3 dots stand for your QCAD installation path. QCAD, QCADCAM or whatever custom path is used.

A few modifications to the example script are required, referring to the line numbers at GitHub:
Line 23-24 ... '>>>' means replace by what follows

Code: Select all

 * \ingroup ecma_misc_examples_draw >>>  * \ingroup ecma_misc
 * Adds a text entity to the drawing. >>>  * Adds a text entity to the drawing by dialog.
 
Line 46 ... Since the release of QCAD v3.26.0 no longer ignored

Code: Select all

              0.0,                 // text width (ignored for now) >>> // column width (none = zero)
Line 69 ... Set a more appropriate tool name, for example "Text by dialog"

Code: Select all

    var action = new RGuiAction(qsTr("Text"), RMainWindowQt.getMainWindow());
Line 73 ... And change the tooltip to something meaningful.

Code: Select all

    action.setStatusTip(qsTr("Draw text"));
Line 75 ... As per example under the Misc menu, this is an array with the places where to include in the GUI

Code: Select all

     action.setWidgetNames(["DrawExamplesMenu"]); >>> action.setWidgetNames(["MiscMenu"]);
Can't help you out with sort orders ... All depends on to what it belongs to and 'CUSTOM' is not really defined.
GroupSortOrder relates to other groups in the same collection (Menu, Toolbar, ...)
SortOrder is the place within this group.
There is this (2015) and the Misc group would be 51000.

Almost there ... The text example script puts a text 'Test' at position (5,10)
Line 52-53 ... Replace that with your parameter, you used the standard font

Code: Select all

              "Test",              // the text >>> selectedText,       // Text content
              "Arial",      // font >>> "Standard",         // Text font
Your code for a text option by dialog can be inserted in the beginEvent at line 34.
Making the creation of a text entity conditional, closing with a curly bracket at line 64.

Save this file, restart QCAD, the new tool should pop up under the Misc menu.

Regards,
CVH

Roy83
Newbie Member
Posts: 8
Joined: Thu Oct 31, 2024 1:40 pm

Re: new script for dropdown text options

Post by Roy83 » Fri Nov 15, 2024 7:03 am

Hello.
Thank you very much for the detailed explanation. :lol:
I did indeed manage to get my 'dropdown' under the misc options.
Then I paste my code from line 34 to get the dropdown options, but then the code no longer works or I am doing something wrong.
I just don't fully understand the bottom 2 lines.
Your code for a text option by dialog can be inserted in the beginEvent at line 34.
Making the creation of a text entity conditional, closing with a curly bracket at line 64.
Attachments
dropdown toegevoegd.jpg
dropdown toegevoegd.jpg (1.28 MiB) Viewed 61180 times

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

Re: new script for dropdown text options

Post by CVH » Fri Nov 15, 2024 8:38 am

Roy83 wrote:
Fri Nov 15, 2024 7:03 am
Then I paste my code from line 34 to get the dropdown options, but then the code no longer works or I am doing something wrong.
I just don't fully understand the bottom 2 lines.
Please include your script.
Or as text in a code box, or as JS file.

The original Text Example is basically:

Code: Select all

ExText.prototype.beginEvent = function() {
    EAction.prototype.beginEvent.call(this);
    ....
    // Creating and casting a new text entity at (10,10):
    ....
    ....
    this.terminate();
}; // End of beginEvent
You add a combo box and add the text conditional:

Code: Select all

Whatever.prototype.beginEvent = function() {
    EAction.prototype.beginEvent.call(this);
    // Create a list of text options:
    ....
    // Show a simple input dialog with a dropdown list:
    ....
    // Check if a valid option was selected (not canceled):
    if (selectedText !== "") {
        // Creating and casting a new text entity at (10,10):
        ....
        ....
    } // End of condition

    this.terminate();
}; // End of beginEvent
In the end you probably want to make it interactive.
Not inserted at coordinate (10, 10) but placed where you indicate with the mouse cursor.
Then have a look at the tutorial: Interactive Script Actions

The native language for coding and for this forum is English but I understand Dutch just fine. :wink:

BTW: What is the reason that the top Toolbar's have a double height?

Regards,
CVH

Roy83
Newbie Member
Posts: 8
Joined: Thu Oct 31, 2024 1:40 pm

Re: new script for dropdown text options

Post by Roy83 » Fri Nov 15, 2024 11:23 am

Thanks CVH you are awesome. :mrgreen:
It works now! :lol:
Now I understand a bit how it works.
The toolbar has a double height because I have the cad tools toolbar turned on.
Nice that you also understand dutch.

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

Re: new script for dropdown text options

Post by CVH » Fri Nov 15, 2024 1:03 pm

Roy83 wrote:
Fri Nov 15, 2024 11:23 am
The toolbar has a double height because I have the cad tools toolbar turned on.
Got it, by default this is at the left of your drawing panel.
Kinda interesting when it switches to display Snapping options.

Would be glad to assist in Dutch, off-forum by Private Message if that is required.
I am from Belgium near (in) Antwerp. :wink:

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”