How to create complex 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.

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

Re: How to create complex script

Post by CVH » Sun Oct 08, 2023 6:06 am

Hi,

I see no reason to make the Dialog an action object aka: this.dialog
The dialog lives only as long as it is active and data is retrieved from it.

This always worked for me:

Code: Select all

/**
 * Shows the Krokus dialog.
 * Retrieves user preferences.
 *
 * \return True if the dialog was accepted, false if it was rejected.
 */
Krokus.prototype.showDialog = function() {
    // Create a dialog based on the UI file:
    var dialog = WidgetFactory.createDialog(Krokus.includeBasePath, "KrokusDialog.ui");
    // List of QWidgets in dialog:
    var widgets = getWidgets(dialog);
    // Load persistent dialog entries:
    WidgetFactory.restoreState(dialog);

    // Activate the Krokus dialog:
    if (!dialog.exec()) {    // When the dialog was rejected
        dialog.destroy();
        EAction.activateMainWindow();
        return false;   // Dialog rejected
    }
    // -> Continue with the dialog accepted

    // Retrieve user preferences:
    this.radius = widgets["Radius"].getValue();    // RMathLineEdit
/*    
 * Add additional parameters here
 */    

    // Save persistent dialog entries and finish:
    WidgetFactory.saveState(dialog);
    dialog.destroy();
    return true;    // Dialog accepted
};
This is for example called from inside a main function called by the beginEvent:

Code: Select all

    // Show Dialog and terminate tool if canceled:
    if (!this.showDialog()) {    // When the dialog was rejected
        return;    // Return to terminate in beginEvent
    }
    // -> Continue with the dialog accepted
A bit odd that in ShaperExport.js the full list of QWidgets is retrieved and then some QWidgets are addressed as:
var unitCombo = this.dialog.findChild("OutputUnitsComboBox")
While this does the same: var unitCombo = widgets["OutputUnitsComboBox"];
There are other odd/wrong things in this "Shaper" code too, I would refer to well established QCAD resources: https://github.com/qcad/qcad

About CLOG ... Probably, but I have no purpose for it there whole the qDebug thing doesn't work on a Windows platform.
I know that I need to log that to a file but it doesn't work out because I can't reach the protected Programs area from within an OS command line.
I settled with using the EAction user messages/warnings, works immediate and I don't have to reload a log file each trial, each error, each ... :wink:

As last may I remark that there is an odd buddy declared in your UI file:
https://github.com/marekw2143/krokus/bl ... C19-L12C19

Regards,
CVH

marekw2143
Junior Member
Posts: 10
Joined: Sat Sep 09, 2023 1:36 pm

Re: How to create complex script

Post by marekw2143 » Sat Oct 14, 2023 2:43 pm

thanks great for help CVH,

Currently I'm trying to acheive behaviour described by following pseudocode:

Code: Select all

for(var i =0;i<N;i++){
 var doc = createNewDocument("document" + i);
 drawSomethingInDocument( doc );
 saveDocument( doc );
}
what I've found, is the documentation related to file (https://www.qcad.org/doc/qcad/latest/de ... _file.html), some code related to save/save as funtionality (https://github.com/qcad/qcad/blob/maste ... ve/Save.js, https://github.com/qcad/qcad/blob/maste ... /SaveAs.js).

When I'm trying following:

Code: Select all

  var di = EAction.getDocumentInterface();
  var name = "fajnyPlik1";
  var version = "";
  var fileName = di.getCorrectedFileName(name, version);
  di.exportFile(name, version);
i'm noticing following error:

Code: Select all

RFileExporterRegistry::getFileExporter: No suitable exporter found
How can i create brand new document from within the script?

Can I somehow use SaveAs component defined in https://github.com/qcad/qcad/blob/maste ... /SaveAs.js ?
Can components from https://github.com/qcad/qcad/tree/master/scripts be referenced in custom scripts?

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

Re: How to create complex script

Post by CVH » Sat Oct 14, 2023 9:06 pm

marekw2143 wrote:
Sat Oct 14, 2023 2:43 pm
Currently I'm trying to acheive behaviour described by following pseudocode:
Question: A GUI document or an offscreen document?
My first reply (Mon Oct 02, 2023 4:53 pm) included a link to an example that creates an offscreen document with the simple API.
It draws something on it (in the background) and exports it as a dxf file named 'qcad_createdrawing.dxf'.
A GUI document is more intended for drawing with user interaction.

For the pseudo code, ok, basically the above link in a for-next loop. :wink:

For the other code section see added remarks:

Code: Select all

  // Retrieve the current active GUI document:
  var di = EAction.getDocumentInterface();

  // Set variable 'name':    #CVH# As file name it requires a suffix, a file extension.
  // I think that it diversifies on extension: 'dxf' => dxf exporter, 'svg' => svg exporter, and so on.
  // Probably there is no exporter configured for no extension.
  var name = "fajnyPlik1";

  // Set variable export 'version':    #CVH# The export file method reverts to default="" when not declared, the dxf version is then by preferences.
  var version = "";

  // Set variable 'filename':    #CVH# .getCorrectedFileName() is intended to clean up unalloyed characters in the strings or other custom things.
  // All depending on the source of the string, the usage, OS type, ..., custom exporter code. For example an asterix, a double point, length =8, ...
  var fileName = di.getCorrectedFileName(name, version);

  // Export the document with given name in given version:    #CVH# Remark that you use 'name' and not 'filename'
  di.exportFile(name, version);
Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”