How do I run a script that takes stdin commands

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
daniel_frac
Junior Member
Posts: 10
Joined: Thu Nov 14, 2019 4:15 pm

How do I run a script that takes stdin commands

Post by daniel_frac » Thu Jan 23, 2020 11:22 am

Dear Support

I'm using QCAD 3.24 professional in Windows. I'm creating a CLI script, that ideally can interact with a session and take stdin command prompts.

The reason for this because loading the qcad executable every time I want to run the same script is slow about 0.5 sec, so I would like to keep QCAD session open and run the script in different documents.

Best Regards

Daniel Hung

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

Re: How do I run a script that takes stdin commands

Post by andrew » Thu Jan 23, 2020 4:41 pm

The easiest way to communicate with a running instance of QCAD is to start another instance of QCAD and pass some arguments.

For example:
1. Run QCAD and let it running
2. On a command line, run QCAD again with parameters.

Examples

This will open a file in the running instance of QCAD:

Code: Select all

qcad.exe somefile.dxf
This will run a scripts in the running instance:

Code: Select all

qcad.exe -exec script.js
For example, to trigger File > Open:

Code: Select all

qcad.exe -exec scripts/File/OpenFile/OpenFile.js
Alternatively, you could also create a script that listens on stdin for input and create some kind of a protocol to communicate with QCAD.

daniel_frac
Junior Member
Posts: 10
Joined: Thu Nov 14, 2019 4:15 pm

Re: How do I run a script that takes stdin commands

Post by daniel_frac » Mon Jan 27, 2020 4:10 pm

Dear support

But all this is provided that qcad is a run as a full foreground application, like running the program as "C:\\Program Files\\QCAD\\qcad.exe" which would launch the gui.

if I run QCAD as "C:\\Program Files\\QCAD\\qcad.exe" -platform offscreen -no-gui your suggestion doesn't seem to work?

I want to wrap this in a Python script and run it as a fully automated background process.

Best Regards

Daniel Hung

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

Re: How do I run a script that takes stdin commands

Post by andrew » Mon Jan 27, 2020 7:12 pm

daniel_frac wrote:
Mon Jan 27, 2020 4:10 pm
if I run QCAD as "C:\\Program Files\\QCAD\\qcad.exe" -platform offscreen -no-gui your suggestion doesn't seem to work?
-no-gui can be used for command line tools only, for example:

Code: Select all

qcad.exe -no-gui -autostart mycommandlinetoolscript.js
For more information about command line tool scripts, see also:
https://qcad.org/en/tutorial-command-line-tool-scripts

It does not make sense to start the QCAD application itself without a GUI, as QCAD is an interactive GUI application that cannot run without GUI. With -autostart you can provide an alternative script as application to launch. Perhaps, you can do this with your own scripts instead of keeping a QCAD instance open. It all depends much on your use case and the type of scripts you are implementing.

daniel_frac
Junior Member
Posts: 10
Joined: Thu Nov 14, 2019 4:15 pm

Re: How do I run a script that takes stdin commands

Post by daniel_frac » Tue Jan 28, 2020 1:14 pm

Hi

Yeah that's exactly what I do. In fact my launch script code is:

qcad.exe -platform offscreen -no-gui -allow-multiple-instances -quit -ignore-script-files -autostart myscript.js

The problem is that even with -ignore-script-files it seem to take 0.5 sec to load every instance of the execution. I have -allow-multiple-instances to permit parallel execution but to scale I will relying in hardware rather than optimise my workflow from the software side, which is what I want.

What do you mean by this?

"Perhaps, you can do this with your own scripts instead of keeping a QCAD instance open. It all depends much on your use case and the type of scripts you are implementing."

Can I create a js script that uses stream pipes? If that's the case I'm happy to interact with the executed script by qcad, but as far as I can tell that cannot be done with the ECMAscript implementation provided by QCAD.

I do wrap the whole process in python using a subprocess library, which allows for pipe workflows but the application that I'm running needs to support it (pipes) on the first place.

Best Regards

Daniel Hung

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

Re: How do I run a script that takes stdin commands

Post by andrew » Tue Jan 28, 2020 2:28 pm

daniel_frac wrote:
Tue Jan 28, 2020 1:14 pm
qcad.exe -platform offscreen -no-gui -allow-multiple-instances -quit -ignore-script-files -autostart myscript.js
Yes, that's the recommended way to run a command line tools.

Perhaps, you can delete some unused plugins to speed up the execution?
Can I create a js script that uses stream pipes?
I never had the need to read from stdin in QCAD, sorry. It might not be possible out of the box (i.e. without a C++ plugin providing some interface with stdin).

ilyasozkurt
Newbie Member
Posts: 9
Joined: Sat Nov 19, 2022 3:20 pm

Re: How do I run a script that takes stdin commands

Post by ilyasozkurt » Mon Nov 21, 2022 8:24 pm

Hello there,

I found a way to implement this. You can create a UNIX socket communication between QCAD and you application trough javascript engine. It allows you to do it. I've done it for my project.

These kinds of stuff more related with QT app engine. It has lots of modules that you can directly use in Javascript engine. FYI.

Post Reply

Return to “QCAD 'How Do I' Questions”