You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cq-cli is a Command Line Interface for executing CadQuery scripts and converting their output to another format. It uses a plugin system where "codecs" can be placed in the cqcodecs directory and will be automatically loaded and used if a user selects that codec from the command line.
18
+
cq-cli is a Command Line Interface for executing CadQuery scripts and converting their output to another format. It uses a plugin system where "codecs" can be placed in the `cqcodecs` directory and will be automatically loaded and used if a user selects that codec from the command line.
20
19
21
-
It is possible to specify input and output files using arguments, but cq-cli also allows the use to the stdin, stdout and stderr streams so that it can be used in a pipeline.
20
+
Input and output files can be specified via arguments, but cq-cli also supports stdin, stdout, and stderr streams so that it can be used in a pipeline.
22
21
23
-
Linux, MacOS and Windows are supported, but some features may be used differently or may not work the same in Windows.
22
+
**Requires Python 3.11 or later.**Linux, macOS, and Windows are supported, though some features may behave differently on Windows.
24
23
25
24
## Getting Help
26
25
27
-
In addition to opening an issue on this repo, there is a [CadQuery Discord channel](https://discord.gg/qz3uAdF) and a [Google Group](https://groups.google.com/g/cadquery)that you can join to ask for help getting started with cq-cli.
26
+
In addition to opening an issue on this repo, there is a [CadQuery Discord channel](https://discord.gg/qz3uAdF) and a [Google Group](https://groups.google.com/g/cadquery)where you can ask for help getting started with cq-cli.
28
27
29
-
## Installation (pip)
28
+
## Installation
30
29
31
-
***Note:*** It probably goes without saying, but it is best to use a Python virtual environment when installing a Python package like cq-cli.
30
+
### uv (preferred) ⭐️
32
31
33
-
These instructions cover installing cq-cli using pip. If you want a stand-alone installation that does not require any of the Python infrastructure, read the Stand-Alone section below.
34
-
35
-
cq-cli is not available on PyPI, so it must be installed using pip and git. git must be installed for this process to work.
32
+
[uv](https://docs.astral.sh/uv/) is the recommended way to install and run cq-cli. It handles Python version management and virtual environments automatically.
Once the installation is complete, the CLI can be invoked as:
59
56
```
60
-
uv venv --python 3.12
61
-
source .venv/bin/activate
62
-
uv sync --dev
57
+
cq-cli --help
58
+
```
59
+
or:
60
+
```
61
+
python -m cq_cli.main --help
63
62
```
64
-
65
-
## Installation (Stand-Alone)
66
-
67
-
**Please note:** This method is not recommended now that cq-cli can be installed via pip, but it is still an option if it is not possible to use a Python virtual environment.
68
-
69
-
Download a binary distribution that is appropriate for your operating system from the [latest PyInstaller workflow run with a green checkmark](/actions/workflows/pyinstaller.yml), extract the zip file, and make sure to put the cq-cli binary in the PATH. Then the CLI can be invoked as `cq-cli` (`cq-cli.exe` on Windows).
70
-
71
-
If installing on Windows, the [latest redistributable for Visual Studio](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads) will need to be installed.
72
-
73
-
74
-
## Drawbacks of Stand-Alone Installation
75
-
76
-
* The file (and directory) size for cq-cli is very large. cq-cli uses pyinstaller to package the binaries for each platform, and must embed all needed dependencies. The OCP and OCCT library dependencies add a minimum of ~270 MB of data on top of the included Python distribution. It is possible that the pyinstaller spec file could be optimized. If you are interested in helping with this, please let us know by opening an issue.
77
-
* Startup times for the single binary are relatively slow. If startup and execution time is important to you, consider using the pyinstaller_dir.spec spec file with pyinstaller: `pyinstaller pyinstaller_dir.spec`.
Command line utility for converting CadQuery script output to various other output formats.
72
+
Command line utility for converting CadQuery script output to various output formats.
84
73
85
-
optional arguments:
86
-
*`-h`, `--help` Show this help message and exit
87
-
*`--codec` CODEC The codec to use when converting the CadQuery output. Must match the name of a codec file in the cqcodecs directory.
88
-
*`--getparams` GETPARAMS
89
-
*`--infile` INFILE The input CadQuery script to convert.
90
-
*`--outfile` OUTFILE File to write the converted CadQuery output to. Prints to stdout if not specified.
91
-
*`--errfile` ERRFILE File to write any errors to. Prints to stderr if not specified.
92
-
*`--params` PARAMS A colon and semicolon delimited string (no spaces) of key/value pairs representing variables and their values in the CadQuery script. i.e. var1:10.0;var2:4.0;
93
-
*`--outputopts` OPTS A colon and semicolon delimited string (no spaces) of key/value pairs representing options to pass to the selected codec. i.e. width:100;height:200;
94
-
*`--validate` VALIDATE Setting to true forces the CLI to only parse and validate the script and not produce converted output.
74
+
| Argument | Description |
75
+
|---|---|
76
+
|`-h`, `--help`| Show help message and exit |
77
+
|`--codec CODEC`| The codec to use for conversion (e.g. `step`, `stl`, `svg`, `dxf`, `glb`, `gltf`, `threejs`). Can be omitted if `--outfile` has a recognised extension. Multiple codecs can be specified separated by `;` — must match the number of `--outfile` entries. |
78
+
|`--infile INFILE`| The input CadQuery script (`.py`) or FreeCAD file (`.fcstd`). Reads from stdin if omitted. |
79
+
|`--outfile OUTFILE`| File to write the converted output to. Prints to stdout if omitted. Multiple output files can be specified separated by `;`. |
80
+
|`--errfile ERRFILE`| File to write errors to. Prints to stderr if omitted. |
81
+
|`--params PARAMS`| Parameters to pass to the script. Accepts: a JSON file path, a JSON string (`{"width":10}`), or a colon/semicolon delimited string (`width:10;height:5;`). |
82
+
|`--outputopts OPTS`| Codec-specific options as a colon/semicolon delimited string. e.g. `width:100;height:200;`|
83
+
|`--getparams GETPARAMS`| Analyse the script and write parameter metadata as JSON. Pass a file path to write to a file, or `true` to print to stdout. |
84
+
|`--validate VALIDATE`| Set to `true` to validate the script without producing output. |
85
+
|`--expression EXPRESSION`| A Python expression to evaluate and render (e.g. `my_shape(x=5)`). Useful for rendering a specific part from a file that contains multiple functions. |
8. Convert a CadQuery script to STL, passing in output options to change the quality of the resulting STL. Explanation of linear vs angular deflection can be found [here](https://dev.opencascade.org/doc/occt-7.1.0/overview/html/occt_user_guides__modeling_algos.html#occt_modalg_11_2).
125
+
10. Convert a CadQuery script to STL, adjusting mesh quality. Explanation of linear vs angular deflection can be found [here](https://dev.opencascade.org/doc/occt-7.1.0/overview/html/occt_user_guides__modeling_algos.html#occt_modalg_11_2).
If you want to help improve and expand cq-cli, the following steps should get you up and running with a development setup. There is a [CadQuery Discord channel](https://discord.gg/qz3uAdF) and a [Google Group](https://groups.google.com/g/cadquery) that you can join to ask for help getting started.
153
-
154
-
1. Create a Python virtual environment and activate it. Attept to avoid the bleeding-edge version of Python as there may be problems.
155
-
2. Clone this repository: `git clone .git`
156
-
3. cd into the repository directory: `cd cq-cli`
157
-
4. Do a local editable installation via pip: `pip install -e .`
158
-
159
-
### Adding a Codec
160
-
161
-
The codec plugin system is based on naming conventions so that cq-cli knows what codec options to accept from the user. When adding a codec, make sure to place it in the `cqcodecs` directory and follow the naming convention `cq_codec_[your codec name].py`. The `your codec name` part of the filename will automatically be used as the codec name that the user specifies.
152
+
### Development Setup
162
153
163
-
A good example to start with when creating your own codec would be `cqcodecs/cq_codec_step.py` as it shows a simple implementation of the codec that relies on CadQuery to do all the heavy lifting. At the very least, your codec needs to have a `convert` function that takes in a [CQGI BuildResult object](https://cadquery.readthedocs.io/en/latest/cqgi.html#cadquery.cqgi.BuildResult) and returns a string representing the converted model(s). As an alternative, cq-cli will pass the output file name, which makes it possible to write the output to the outfile path directly from the codec. If `None` is returned from the `convert` function, cq-cli will assume that the output was written directly to the output file by the codec.
154
+
The recommended way to set up a development environment is with [uv](https://docs.astral.sh/uv/).
164
155
165
-
For pyinstaller to know about the new dynamically loaded codec, it must be added to the `hiddenimports` array in both cq-cli_onfile_pyinstaller.spec and cq-cli_dir_pyinstaller.spec files. Leave the `.py` off of the name when adding to the array, for instance `codec.cq_codec_step` is the string used for the STEP codec. When only running a codec locally and not contributing it, this step is not required.
A test is required when adding a codec to cq-cli, but is easy to add. Add a file named `test_[your codec name]_codec.py` in the tests directory, and add the test to it. `test_step_codec.py` would be a good template to base any new tests off of.
173
+
Run the test suite:
174
+
```
175
+
pytest -v --ignore=tests/test_freecad.py
176
+
```
170
177
171
-
### Exit Codes
178
+
### Adding a Codec
172
179
173
-
Applications can return a non-zero exit code to let the user know what went wrong. Below is a listing of the exit codes for cq-cli and what they mean.
180
+
The codec plugin system is based on naming conventions so that cq-cli knows what codec options to accept from the user. When adding a codec, place it in the `cqcodecs` directory and follow the naming convention `cq_codec_[your codec name].py`. The `your codec name` part of the filename will automatically be used as the codec name specified by the user.
174
181
175
-
***0:** Operation was successful with no errors detected.
176
-
***1:** A CadQuery script could not be read from the given `infile`.
177
-
***2:** There was a usage error with the parameters that were passed to cq-cli (too few parameters, not the correct ones, etc).
178
-
***3:** A codec for converting the results of the CadQuery script was not provided.
179
-
***100:** There was an unknown error while running the CadQuery script and obtaining a result (build error, possibly from OCCT).
180
-
***200:** There was an unknown error while running a codec to convert the results of the CadQuery script.
182
+
A good starting point is [cqcodecs/cq_codec_step.py](src/cq_cli/cqcodecs/cq_codec_step.py), which shows a simple codec implementation that relies on CadQuery to do the heavy lifting. At minimum, your codec needs a `convert` function that accepts a [CQGI BuildResult object](https://cadquery.readthedocs.io/en/latest/cqgi.html#cadquery.cqgi.BuildResult) and returns a string or bytes representing the converted model. If the codec writes the output file directly, return `None` and cq-cli will assume the output was written to disk.
181
183
182
-
### pyinstaller
184
+
### Adding a Codec Test
183
185
184
-
If building cq-cli to run as a stand-alone app is required, there are two modes to build it in: `onefile` and `dir` (directory). onefile mode creates a single file for the app, which is easy to distribute but takes longer to start up run on each execution. dir mode creates a directory with numerous dependency files in it, including the cq-cli binary file, and starts up faster than the single file. However, the directory distribution can take up more than twice the disk space and can be messier to distribute. A PyInstaller spec file has been provided for both modes, and selecting between them only requires the addition of a command line argument. The commands to build in each type of mode are outlined below.
186
+
A test is required when adding a codec to cq-cli. Add a file named `test_[your codec name]_codec.py`in the `tests` directory. [tests/test_step_codec.py](tests/test_step_codec.py) is a good template.
185
187
186
-
There are a few packages, including PyInstaller, must be installed via conda or pip before executing either of the `pyinstaller` commands below.
187
-
```
188
-
pip install pyinstaller
189
-
pip install path
190
-
```
191
-
The output for both of the commands will be in the `dist` directory. If the mode argument is left off, `onefile` is assumed.
188
+
### Exit Codes
192
189
193
-
#### pyinstaller onefile
194
-
```
195
-
pyinstaller cq-cli_pyinstaller.spec onefile
196
-
```
190
+
| Code | Meaning |
191
+
|---|---|
192
+
|**0**| Operation completed successfully. |
193
+
|**1**| The CadQuery script could not be read from `--infile`. |
194
+
|**2**| Usage error — incorrect or insufficient arguments. |
195
+
|**3**| No valid codec was provided or could be inferred. |
196
+
|**100**| Error while running the CadQuery script (build error, possibly from OCCT). |
197
+
|**200**| Error while running the conversion codec. |
0 commit comments