Skip to content

Commit c9762b4

Browse files
committed
Move plotter configuration to configuration file
Removes the possibility to provide the configuration strings via the command line and moves the config over to the vpype configuration. This changes the command line option to -p or --profile. It also allows to configure a default profile using the `default_profile` option in the [gcode] section of the vypye configuration. The cmd line parameter is not required anymore. An error is thrown if there is no default set and no profile was provided explicitly.
1 parent cc6599b commit c9762b4

6 files changed

Lines changed: 244 additions & 397 deletions

File tree

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include vpype_gcode/bundled_configs.toml

README.md

Lines changed: 152 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,89 @@
11
# vpype-gcode
2-
Vpype plugin for gcode
2+
Vpype plugin to generate gcode and other text output.
33
See: https://github.com/abey79/vpype
44

55

6-
Gcode vpype plugin. Write gcode files for the vpype pipeline.
6+
Gcode vpype plugin. Write gcode files for the vpype pipeline. The output format can be customized by the user heavily to an extend that you can also output non gcode ascii text files.
77

8-
* `gwrite` write gcode geometries to disk
8+
* `gwrite` write geometries as gcode to a file
99

1010

1111
# Installing
1212
`$ pip install vpype-gcode`
1313

14-
# vpype gwrite --help
14+
# Usage
1515

16-
```Usage: vpype gwrite [OPTIONS] FILENAME
16+
```
17+
Usage: vpype gwrite [OPTIONS] FILENAME
1718
1819
Options:
19-
-v, --version TEXT version to write
20-
-h, --header TEXT header to write
21-
-m, --move TEXT move to write
22-
-l, --line TEXT header to write
23-
-b, --preblock TEXT preblock to write
24-
-B, --postblock TEXT postblock to write
25-
-c, --prelayer TEXT prelayer to write
26-
-C, --postlayer TEXT postlayer to write
27-
-h, --footer TEXT header to write
28-
-u, --unit TEXT unit for coordinates (i.e. in or mm)
29-
--negate_x change the sign of the x coordinates by multiplying
30-
them with -1
31-
32-
--negate_y change the sign of the y coordinates by multiplying
33-
them with -1
34-
35-
--invert_x invert or mirror all points on the middle of the x
36-
axis
37-
38-
--invert_y invert or mirror all points on the middle of the y
39-
axis
40-
41-
-r, --relative use relative coordinates
42-
--help Show this message and exit.
43-
```
20+
-p, --profile TEXT gcode writer profile from the vpype configuration file
21+
subsection 'gwrite'
4422
45-
# Versions
46-
```python
47-
'ninja':
48-
{
49-
'header': 'G20\nG17\nG90\n',
50-
'move': 'M380\nG00 X%.4f Y%.4f\nM381\n',
51-
'line': 'G01 X%.4f Y%.4f\n',
52-
'footer': 'M2\n',
53-
'unit': 'mm'
54-
},
55-
'gcode':
56-
{
57-
'header': 'G20\nG17\nG90\n',
58-
'move': 'G00 X%.4f Y%.4f\n',
59-
'line': 'G01 X%.4f Y%.4f\n',
60-
'footer': 'M2\n',
61-
'unit': 'mm'
62-
},
63-
'gcode_relative':
64-
{
65-
'header': 'G20\nG17\nG91\n',
66-
'move': 'G00 X%.4f Y%.4f\n',
67-
'line': 'G01 X%.4f Y%.4f\n',
68-
'footer': 'M2\n',
69-
'relative': True,
70-
'unit': 'mm'
71-
}
72-
'csv':
73-
{
74-
'header': "#Operation, X-value, Y-value\n",
75-
'move': "Move, %f, %f\n",
76-
'line': "Line-to, %f, %f\n"
77-
},
78-
'json':
79-
{
80-
'header': '{\n',
81-
'footer': '}\n',
82-
'prelayer': '\t"Layer": {\n',
83-
'preblock': '\t\t"Block": [\n',
84-
'move': '\t\t{\n\t\t\t"X": %d,\n\t\t\t"Y": %d\n\t\t}',
85-
'line': ',\n\t\t{\n\t\t\t"X": %d,\n\t\t\t"Y": %d\n\t\t}',
86-
'postblock': '\n\t\t],\n',
87-
'postlayer': '\t},\n',
88-
}
23+
--help Show this message and exit.
8924
```
9025

26+
# Profiles
27+
This plugin supports different output profiles which configure the way the resulting output is formatted. Output profiles are flexible in a way that they can also be used to generate non gcode files, i.e. JSON or CSV files.
9128

92-
# Examples
93-
94-
## Convert SVG -> GCode
95-
96-
`vpype read butterfly.svg gwrite --version gcode butterfly.gcode`
97-
98-
Loads up a svg and writes it in default gcode.
99-
100-
`vpype begin grid -o 25 25 10 10 circle 0 0 100 end gwrite --version ninja --negate_y --footer M99 test.gcode`
29+
## Predefined Profiles
30+
There are several predefined output profiles as part of the release:
10131

102-
Here we are creating a grid of circles then we are `gwrite` in version `ninja` with a flipped/negated y-axis and a footer of M99 rather than our default footer in `ninja` that is `M2\n`
32+
- `ninja`
33+
- `gcode`
34+
- `gcode_relative`
35+
- `csv`
36+
- `json`
10337

104-
Let's say our gcode is so different that it's an CSV file.
38+
Check the [source code](vpype_gcode/bundled_configs.toml) for how these profiles are defined.
10539

106-
`vpype begin grid -o 25 25 10 10 circle 0 0 100 end gwrite --header "#Operation, X-value, Y-value\n" --move "Move, %d, %d\n" --line "Line-to, %d, %d\n" test.csv`
40+
## Defining Your Own Profiles
41+
In case you want to define your own output format to make the suit your needs, it is possible to define your own profiles either in `~/.vpype.toml` or any other file. In the latter case, you must instruct vpype to load the configuration using the [`--config`](https://vpype.readthedocs.io/en/latest/reference.html#cmdoption-vpype-c) global option.
10742

108-
This produces:
109-
```csv
110-
#Operation, X-value, Y-value
111-
Move, 26, 0
112-
Line-to, 26, 0
113-
Line-to, 26, -1
114-
Line-to, 26, -2
115-
Line-to, 26, -3
116-
Line-to, 25, -4
117-
Line-to, 25, -5
118-
Line-to, 25, -6
119-
...
43+
Inside the configuration file you can specify a new output profile using the following format:
12044
```
121-
122-
Giving it a simplier example:
123-
`vpype rect 0 0 100 100 gwrite --header "#Operation, X-value, Y-value\n" --move "Move, %d, %d\n" --line "Line-to, %d, %d\n" test.csv`
124-
125-
```csv
126-
#Operation, X-value, Y-value
127-
Move, 0, 0
128-
Line-to, 0, 100
129-
Line-to, 100, 100
130-
Line-to, 100, 0
131-
Line-to, 0, 0
45+
[gwrite.my_own_plotter]
46+
unit = "mm"
47+
invert_y = true
48+
header = "M3 G21\n"
49+
prelayer = "(Start Layer)\n"
50+
preblock = "(Start Block)\n"
51+
move = """G00 Z5
52+
G00 X%.4f Y%.4f
53+
M3 S1000
54+
G4 P0.3
55+
G01 Z1 F3500
56+
"""
57+
line = """G01 X%.4f Y%.4f Z1\n"""
58+
postblock = """G00 Z 5.0000
59+
M5 S0
60+
G4 P0.5\n"""
61+
footer = """M5
62+
G00 X0.0000 Y0.0000
63+
M2"""
13264
```
13365

134-
This produces a plain text CSV file of the rectangle.
135-
136-
Now using versions we could have done `--version csv` for this, but you can simply give the parts needed to perform your output.
137-
138-
139-
# Formatting
140-
66+
You can use the following options inside a profile. You only need to provide the options where you need to change the default. If you want a newline character in an option, you can either use escape sequences (`\n`) or you use TOML multi line strings wrapped in `""""`.
67+
68+
### Output Control
69+
- `unit`: Defines the [vpype unit](https://vpype.readthedocs.io/en/stable/fundamentals.html#units) which should be used in the output format. Defaults to `mm`.
70+
- `invert_x`: Inverts/Mirrors the output relative to the middle point of the x axis when set to `true`. Defaults to `false`.
71+
- `invert_y`: Inverts/Mirrors the output relative to the middle point of the y axis when set to `true`. Defaults to `false`. This option can be helpful if your output does not have the x=0, y=0 coordinates at the top left (the default) but instead at the bottom left.
72+
- `flip_x`: Flips the X axis by multiplying all values with -1 and therefore turning them negative when set to `true`. Defaults to `false`.
73+
- `flip_y`: Flips the Y axis by multiplying all values with -1 and therefore turning them negative when set to `true`. Defaults to `false`.
74+
- `relative`: Use relative coordinates (so the difference to the previous point) instead of absolute coordinates when set to `true`. Defaults to `false`
75+
76+
### Output Format
77+
All of the options below default to an empty text which means no output is generated.
78+
- `header`: Output to be generated at the start of the file as a header
79+
- `footer`: Output to be generated at the end of the file as a footer
80+
- `preblock`: Output to be generated before a line is started
81+
- `postblock`: Output to be generated after a line is finished.
82+
- `move`: Output to be generated at the start of a line for its first coordinate pair.
83+
- `line`: Output to be generated to all subsequent coordinate pairs of a line.
84+
85+
## Output structure
14186
The gwrite command gives you access to write to a variety of formats that fit the given outline. We're writing generic ascii. Since gcode can have more flavors than a Baskin Robbins™, it's best to simply draw broad strokes as to what ascii output should look like. Here we define the various elements without any regard to the gcode it will largely be producing.
142-
14387
```
14488
<header>
14589
<prelayer>
@@ -190,55 +134,105 @@ The gwrite command gives you access to write to a variety of formats that fit th
190134
<postlayer>
191135
<footer>
192136
```
137+
## Default Profile
138+
To prevent having to provide the profile on every invocation of the gcode plugin, you can define a default profile which will be used when no other profile is provided on the command line. You can do so by setting the `default_profile` configuration variable inside the `gcode` section of the vpype configuration file:
139+
```
140+
[gcode]
141+
default_profile = "gcode"
142+
```
193143

194-
This is the secret sauce of gwrite, it writes generic ascii which can be themed as functional gcode.
144+
# Examples
145+
## Convert SVG -> GCode
146+
Loads up a svg and writes it in default gcode.:
147+
`vpype read butterfly.svg gwrite --profile gcode butterfly.gcode`
148+
149+
Create a grid of circles, then we are `gwrite` using the `ninja` profile:
150+
`vpype begin grid -o 25 25 10 10 circle 0 0 100 end gwrite --profile ninja test.gcode`
151+
152+
153+
## Convert SVG -> CSV
154+
The `csv` profile is bundled with this package and defined as follows:
155+
```
156+
[gwrite.csv]
157+
header = "#Operation, X-value, Y-value\n"
158+
move = "Move, %f, %f\n"
159+
line = "Line-to, %f, %f\n"
160+
```
161+
162+
Using this profile you can generate a CSV for a given input into vpype:
163+
`vpype begin grid -o 25 25 10 10 circle 0 0 100 end gwrite -p csv test.csv`
195164

196-
For example if you write version `json`:
197-
```python
198-
'json':
199-
{
200-
'header': '{\n',
201-
'footer': '}\n',
202-
'prelayer': '\t"Layer": {\n',
203-
'preblock': '\t\t"Block": [\n',
204-
'move': '\t\t{\n\t\t\t"X": %d,\n\t\t\t"Y": %d\n\t\t}',
205-
'line': ',\n\t\t{\n\t\t\t"X": %d,\n\t\t\t"Y": %d\n\t\t}',
206-
'postblock': '\n\t\t],\n',
207-
'postlayer': '\t},\n',
208-
}
165+
This produces:
166+
```csv
167+
#Operation, X-value, Y-value
168+
Move, 26, 0
169+
Line-to, 26, 0
170+
Line-to, 26, -1
171+
Line-to, 26, -2
172+
Line-to, 26, -3
173+
Line-to, 25, -4
174+
Line-to, 25, -5
175+
Line-to, 25, -6
176+
...
209177
```
178+
Giving it a simpler example, this produces a plain text CSV file of the rectangle.
179+
`vpype rect 0 0 100 100 gwrite -p csv test.csv`
210180

211-
Sending our rectangle to Json:
181+
```csv
182+
#Operation, X-value, Y-value
183+
Move, 0, 0
184+
Line-to, 0, 100
185+
Line-to, 100, 100
186+
Line-to, 100, 0
187+
Line-to, 0, 0
188+
```
189+
This is the secret sauce of gwrite, it writes generic ascii which can be themed as functional gcode.
212190

213-
`vpype rect 0 0 100 100 gwrite --version json test.json`
191+
## Convert SVG -> JSON
192+
The `json` profile is already bundled with this package. It is defined as following:
193+
```
194+
[gwrite.json]
195+
header = "{\n"
196+
footer = "}\n"
197+
prelayer = "\t\"Layer\": {\n"
198+
preblock = "\t\t\"Block\": [\n"
199+
move = "\t\t{\n\t\t\t\"X\": %d,\n\t\t\t\"Y\": %d\n\t\t}"
200+
line = ",\n\t\t{\n\t\t\t\"X\": %d,\n\t\t\t\"Y\": %d\n\t\t}"
201+
postblock = "\n\t\t],\n"
202+
postlayer = "\t},\n"
203+
```
204+
Using this profile, you can generate JSON for the rectangle:
205+
`vpype rect 0 0 100 100 gwrite -p json test.json`
214206

215207
```json
216208
{
217-
"Layer": {
218-
"Block": [
219-
{
220-
"X": 0,
221-
"Y": 0
222-
},
223-
{
224-
"X": 0,
225-
"Y": 100
226-
},
227-
{
228-
"X": 100,
229-
"Y": 100
230-
},
231-
{
232-
"X": 100,
233-
"Y": 0
234-
},
235-
{
236-
"X": 0,
237-
"Y": 0
238-
}
239-
],
240-
},
209+
"Layer": {
210+
"Block": [
211+
{
212+
"X": 0,
213+
"Y": 0
214+
},
215+
{
216+
"X": 0,
217+
"Y": 100
218+
},
219+
{
220+
"X": 100,
221+
"Y": 100
222+
},
223+
{
224+
"X": 100,
225+
"Y": 0
226+
},
227+
{
228+
"X": 0,
229+
"Y": 0
230+
}
231+
],
232+
},
241233
}
242234
```
243235

244-
Strictly speaking json shouldn't have the last 2 commas there, but it's for demonstration purposes.
236+
Strictly speaking json shouldn't have the last 2 commas there, but it's for demonstration purposes.
237+
238+

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"License :: OSI Approved :: MIT License",
2121
"Operating System :: OS Independent"
2222
),
23+
include_package_data=True,
2324
install_requires=[
2425
"click",
2526
"vpype",
@@ -29,4 +30,4 @@
2930
[vpype.plugins]
3031
gwrite=vpype_gcode.gwrite:gwrite
3132
""",
32-
)
33+
)

vpype_gcode/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)