Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ For example every element accepts the value of `index`. You would encode that in

- `document_start` and `document_end` accept the following:
- `filename` file name of the file being saved
- `global_index` the global index starting from 0
- `global_index1` the global index starting from 1

- `layer_start`, `layer_end` and `layer_join` accept the following:
- `x` the last_x position before this layer, this is 0 for the first layer
- `y` the last_y position before this layer, this is 0 for the first layer
- `ix` the last integer x location before this layer, this is 0 for the first layer.
- `iy` the last integer y location before this layer, this is 0 for the first layer.
- `global_index` the global index starting from 0
- `global_index1` the global index starting from 1
- `index` same as `layer_index`
- `index1` same as `layer_index1`
- `layer_index` the current layer index starting from 0
Expand All @@ -134,6 +138,8 @@ For example every element accepts the value of `index`. You would encode that in
- `y` the last_y position before this line, this is 0 for the first line of the first layer
- `ix` the last integer x location before this layer, this is 0 for the first layer.
- `iy` the last integer y location before this layer, this is 0 for the first layer.
- `global_index` the global index starting from 0
- `global_index1` the global index starting from 1
- `index` same as `lines_index`
- `index1` same as `lines_index1`
- `lines_index` the current line index within this layer starting from 0
Expand All @@ -157,6 +163,8 @@ For example every element accepts the value of `index`. You would encode that in
* `iy` absolute position y in integer number. eg `{iy:d}`
* `idx` relative position x in integer number. eg `{idx:d}`
* `idy` relative position y in integer number. eg `{idy:d}`
* `global_index` the global index starting from 0
* `global_index1` the global index starting from 1
* `index` same as `segment_index`
* `index1` same as `segment_index1`
* `segment_index` the current segment within this line starting from 0
Expand Down Expand Up @@ -216,6 +224,50 @@ Alternatively, default values for string-based variable may be provided using th
$ vpype [...] gwrite -p summary --default vp_color undefined output.txt
```

Another example: Some CNC controllers require gcode with unique line numbers (`N10`, `N20` etc.).
This can be accomplished by using the `global_index` (or `global_index1`) property.
In below example it is used to create 4 digit line numbers with leading zeroes.
By adding the trailing zero we create a step size of 10, which leaves space for inserting any additional lines.

```toml
[gwrite.line_numbers]
document_start = """
N{global_index1:03d}0 (document_start)
N{global_index1:03d}1 (document_start, inserted line by the config)
"""
layer_join = "N{global_index1:03d}0 (layer_join)\n"
segment_first = "N{global_index1:03d}0 (segment_first)\n"
segment = "N{global_index1:03d}0 (segment)\n"
segment_last = "N{global_index1:03d}0 (segment_last)\n"
document_end = "N{global_index1:03d}0 (document_end)\n"
```
Example output:

```
N0010 (document_start)
N0011 (document_start, inserted line by the config)
N0020 (segment_first)
N0030 (segment)
N0040 (segment)
N0050 (segment)
N0060 (segment_last)
N0080 (segment_first)
N0090 (segment)
N0100 (segment)
N0110 (segment)
N0120 (segment_last)
N0140 (segment_first)
N0150 (segment_last)
N0170 (layer_join)
N0170 (segment_first)
N0180 (segment)
N0190 (segment)
N0200 (segment)
N0210 (segment_last)
N0230 (segment_first)
N0240 (segment_last)
N0260 (document_end)
```

### Information Control
- `info`: prints text to the console after file is written to inform the user of said information
Expand Down
35 changes: 33 additions & 2 deletions vpype_gcode/gwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def gwrite(
output most known non-gcode ascii text files.
"""
gwrite_config = vp.config_manager.config["gwrite"]
global_index = 0 # for creating a global line index across layers

# If no profile was provided, try to use a default
if not profile:
Expand Down Expand Up @@ -172,12 +173,18 @@ def write_template(template: str | None, **context_vars: typing.Any):

# process file
filename = output.name
write_template(document_start, filename=filename)
write_template(
document_start,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

last_x = 0
last_y = 0
xx = 0
yy = 0
global_index += 1
lastlayer_index = len(document.layers.values()) - 1

for layer_index, (layer_id, layer) in enumerate(sorted(document.layers.items())):
Expand All @@ -195,6 +202,8 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)
lastlines_index = len(layer) - 1
for lines_index, line in enumerate(layer):
Expand All @@ -212,6 +221,8 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

segment_last_index = len(line) - 1
Expand Down Expand Up @@ -255,11 +266,15 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

last_x = x
last_y = y

global_index += 1

write_template(
line_end,
x=last_x,
Expand All @@ -274,6 +289,8 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

if lines_index != lastlines_index:
Expand All @@ -291,7 +308,12 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

global_index += 1

write_template(
layer_end,
x=last_x,
Expand All @@ -304,6 +326,8 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)
if layer_index != lastlayer_index:
write_template(
Expand All @@ -318,10 +342,17 @@ def write_template(template: str | None, **context_vars: typing.Any):
layer_index1=layer_index + 1,
layer_id=layer_id,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)
current_layer = None

write_template(document_end, filename=filename)
write_template(
document_end,
filename=filename,
global_index=global_index,
global_index1=global_index + 1,
)

# handle info string
info = config.get("info", None)
Expand Down