Skip to content

Commit

Permalink
Change G-Codes for upcoming klipper merge
Browse files Browse the repository at this point in the history
DEFINE_OBJECT        -> EXCLUDE_OBJECT_DEFINE
START_CURRENT_OBJECT -> EXCLUDE_OBJECT_START
END_CURRENT_OBJECT   -> EXCLUDE_OBJECT_END

TODO: Add a "convert" step so passed in old-style codes get rewritten
      to the new style.

Update the github build action to remove `-latest` from the filenames
  • Loading branch information
kageurufu committed Mar 1, 2022
1 parent a70bb16 commit 8dc6b59
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 215 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu, windows, macos]
defaults:
run:
shell: bash
runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
Expand Down Expand Up @@ -60,4 +60,4 @@ jobs:
uses: softprops/action-gh-release@v1
with:
files: |
preprocess_cancellation-*
*/preprocess_cancellation-*.zip
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Then, all generated gcode should be automatically processed and rewritten to sup
There are 3 gcodes inserted in the files automatically, and 4 more used to control the
object cancellation.

`DEFINE_OBJECT NAME=<object name> [CENTER=x,y] [POLYGON=[[x,y],...]]`
`EXCLUDE_OBJECT_DEFINE NAME=<object name> [CENTER=x,y] [POLYGON=[[x,y],...]]`

The NAME must be unique and consistent throughout the file. CENTER is the center location
for the mesh, used to show on interfaces where and object being canceled is on the bed.
POLYGON is a series of points, used to represent the bounds of the object. It can be just
a bounding box, a simplified outline, or another useful shape.

`START_CURRENT_OBJECT NAME=<object name>` and `END_CURRENT_OBJECT [NAME=<object name>]`
`EXCLUDE_OBJECT_START NAME=<object name>` and `EXCLUDE_OBJECT_END [NAME=<object name>]`

The beginning and end markers for the gcode for a single object. When an object is excluded,
anything between these markers is ignored.
Expand All @@ -60,6 +60,6 @@ so canceling a mesh cancels it's support as well.
This looks for known markers inside the GCode, specific to each slicer. It uses those
to figure out the printing object's name, and track's all extrusion moves within it's
print movements. Those are used to calculate a minimal bounding box for each mesh.
A series of `DEFINE_OBJECT` gcodes are placed in a header, including the bounding boxes
and objects centers. Then, these markers are used to place `START_CURRENT_OBJECT` and
`END_CURRENT_OBJECT` gcodes in the file surrounding each set of extrusions for that object.
A series of `EXCLUDE_OBJECT_DEFINE` gcodes are placed in a header, including the bounding boxes
and objects centers. Then, these markers are used to place `EXCLUDE_OBJECT_START` and
`EXCLUDE_OBJECT_END` gcodes in the file surrounding each set of extrusions for that object.
8 changes: 4 additions & 4 deletions preprocess_cancellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def define_object(
polygon: Optional[Point] = None,
region: Optional[List[Point]] = None,
):
yield f"DEFINE_OBJECT NAME={name}"
yield f"EXCLUDE_OBJECT_DEFINE NAME={name}"
if center:
yield f" CENTER={_dump_coords(center)}"
if polygon:
Expand All @@ -181,11 +181,11 @@ def define_object(


def object_start_marker(object_name):
yield f"START_CURRENT_OBJECT NAME={object_name}\n"
yield f"EXCLUDE_OBJECT_START NAME={object_name}\n"


def object_end_marker(object_name):
yield f"END_CURRENT_OBJECT NAME={object_name}\n"
yield f"EXCLUDE_OBJECT_END NAME={object_name}\n"


def preprocess_pipe(infile):
Expand Down Expand Up @@ -443,7 +443,7 @@ def preprocessor(infile, outfile):
found_m486 = False
processor = None
for line in infile:
if line.startswith("DEFINE_OBJECT"):
if line.startswith("EXCLUDE_OBJECT_DEFINE") or line.startswith("DEFINE_OBJECT"):
logger.info("GCode already supports cancellation")
infile.seek(0)
outfile.write(infile.read())
Expand Down
158 changes: 78 additions & 80 deletions test_preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import re
import pathlib
import re
import subprocess
import sys


from preprocess_cancellation import preprocess_cura, preprocess_ideamaker, preprocess_m486, preprocess_slicer


gcode_path = pathlib.Path("./GCode")


def collect_definitions(lines):
definitions = set()
for line in lines:
if line.startswith("DEFINE_OBJECT"):
if line.startswith("EXCLUDE_OBJECT_DEFINE"):
definitions.add(line)
# Add just the `DEFINE_OBJECT NAME=...` as well, for quick checking without caring about coordinates
definitions.add(re.sub(r"^(DEFINE_OBJECT).*(NAME=\S+).*$", r"\1 \2", line))
# Add just the `EXCLUDE_OBJECT_DEFINE NAME=...` as well, for quick checking without caring about coordinates
definitions.add(re.sub(r"^(EXCLUDE_OBJECT_DEFINE).*(NAME=\S+).*$", r"\1 \2", line))
return definitions


Expand Down Expand Up @@ -47,22 +45,22 @@ def test_m486():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=0" in definitions
assert "DEFINE_OBJECT NAME=1" in definitions
assert "DEFINE_OBJECT NAME=2" in definitions
assert "DEFINE_OBJECT NAME=3" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=2" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=3" in definitions

assert results.count(f"START_CURRENT_OBJECT NAME=0") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=0") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=0") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=0") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=1") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=1") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=1") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=1") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=2") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=2") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=2") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=2") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=3") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=3") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=3") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=3") == 25


def test_superslicer():
Expand All @@ -71,10 +69,10 @@ def test_superslicer():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_1" in definitions
assert "DEFINE_OBJECT NAME=union_3_id_2_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cylinder_2_id_1_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=union_3_id_2_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cylinder_2_id_1_copy_0" in definitions


def test_superslicer():
Expand All @@ -83,10 +81,10 @@ def test_superslicer():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_1" in definitions
assert "DEFINE_OBJECT NAME=union_3_id_2_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cylinder_2_id_1_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=union_3_id_2_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cylinder_2_id_1_copy_0" in definitions


def test_prusaslicer():
Expand All @@ -95,22 +93,22 @@ def test_prusaslicer():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=cylinder_2_id_1_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cube_1_id_0_copy_1" in definitions
assert "DEFINE_OBJECT NAME=union_3_id_2_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cylinder_2_id_1_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_id_0_copy_1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=union_3_id_2_copy_0" in definitions

assert results.count(f"START_CURRENT_OBJECT NAME=cylinder_2_id_1_copy_0") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cylinder_2_id_1_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cylinder_2_id_1_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cylinder_2_id_1_copy_0") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_id_0_copy_0") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_id_0_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_id_0_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_id_0_copy_0") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_id_0_copy_1") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_id_0_copy_1") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_id_0_copy_1") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_id_0_copy_1") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=union_3_id_2_copy_0") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=union_3_id_2_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=union_3_id_2_copy_0") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=union_3_id_2_copy_0") == 25


def test_slic3r():
Expand All @@ -119,22 +117,22 @@ def test_slic3r():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=cube_1_stl_id_0_copy_0" in definitions
assert "DEFINE_OBJECT NAME=cube_1_stl_id_0_copy_1" in definitions
assert "DEFINE_OBJECT NAME=cylinder_2_stl_id_1_copy_0" in definitions
assert "DEFINE_OBJECT NAME=union_3_stl_id_2_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_stl_id_0_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_stl_id_0_copy_1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cylinder_2_stl_id_1_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=union_3_stl_id_2_copy_0" in definitions

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_stl_id_0_copy_0") == 16
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_stl_id_0_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_stl_id_0_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_stl_id_0_copy_0") == 16

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_stl_id_0_copy_1") == 16
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_stl_id_0_copy_1") == 16
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_stl_id_0_copy_1") == 16
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_stl_id_0_copy_1") == 16

assert results.count(f"START_CURRENT_OBJECT NAME=cylinder_2_stl_id_1_copy_0") == 16
assert results.count(f"END_CURRENT_OBJECT NAME=cylinder_2_stl_id_1_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_START NAME=cylinder_2_stl_id_1_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_END NAME=cylinder_2_stl_id_1_copy_0") == 16

assert results.count(f"START_CURRENT_OBJECT NAME=union_3_stl_id_2_copy_0") == 16
assert results.count(f"END_CURRENT_OBJECT NAME=union_3_stl_id_2_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_START NAME=union_3_stl_id_2_copy_0") == 16
assert results.count(f"EXCLUDE_OBJECT_END NAME=union_3_stl_id_2_copy_0") == 16


def test_cura():
Expand All @@ -143,22 +141,22 @@ def test_cura():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=cylinder_2_stl" in definitions
assert "DEFINE_OBJECT NAME=cube_1_stl" in definitions
assert "DEFINE_OBJECT NAME=union_3_stl" in definitions
assert "DEFINE_OBJECT NAME=cube_1_stl_1" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cylinder_2_stl" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_stl" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=union_3_stl" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=cube_1_stl_1" in definitions

assert results.count(f"START_CURRENT_OBJECT NAME=cylinder_2_stl") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cylinder_2_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cylinder_2_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cylinder_2_stl") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_stl") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_stl") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=union_3_stl") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=union_3_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=union_3_stl") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=union_3_stl") == 25

assert results.count(f"START_CURRENT_OBJECT NAME=cube_1_stl_1") == 25
assert results.count(f"END_CURRENT_OBJECT NAME=cube_1_stl_1") == 25
assert results.count(f"EXCLUDE_OBJECT_START NAME=cube_1_stl_1") == 25
assert results.count(f"EXCLUDE_OBJECT_END NAME=cube_1_stl_1") == 25


def test_ideamaker():
Expand All @@ -167,22 +165,22 @@ def test_ideamaker():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=test_bed_part1_3mf" in definitions
assert "DEFINE_OBJECT NAME=test_bed_part2_3mf" in definitions
assert "DEFINE_OBJECT NAME=test_bed_part0_3mf" in definitions
assert "DEFINE_OBJECT NAME=test_bed_part0_1_3mf" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=test_bed_part1_3mf" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=test_bed_part2_3mf" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=test_bed_part0_3mf" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=test_bed_part0_1_3mf" in definitions

assert results.count("START_CURRENT_OBJECT NAME=test_bed_part1_3mf") == 32
assert results.count("END_CURRENT_OBJECT NAME=test_bed_part1_3mf") == 32
assert results.count("EXCLUDE_OBJECT_START NAME=test_bed_part1_3mf") == 32
assert results.count("EXCLUDE_OBJECT_END NAME=test_bed_part1_3mf") == 32

assert results.count("START_CURRENT_OBJECT NAME=test_bed_part2_3mf") == 32
assert results.count("END_CURRENT_OBJECT NAME=test_bed_part2_3mf") == 32
assert results.count("EXCLUDE_OBJECT_START NAME=test_bed_part2_3mf") == 32
assert results.count("EXCLUDE_OBJECT_END NAME=test_bed_part2_3mf") == 32

assert results.count("START_CURRENT_OBJECT NAME=test_bed_part0_3mf") == 33
assert results.count("END_CURRENT_OBJECT NAME=test_bed_part0_3mf") == 33
assert results.count("EXCLUDE_OBJECT_START NAME=test_bed_part0_3mf") == 33
assert results.count("EXCLUDE_OBJECT_END NAME=test_bed_part0_3mf") == 33

assert results.count("START_CURRENT_OBJECT NAME=test_bed_part0_1_3mf") == 33
assert results.count("END_CURRENT_OBJECT NAME=test_bed_part0_1_3mf") == 33
assert results.count("EXCLUDE_OBJECT_START NAME=test_bed_part0_1_3mf") == 33
assert results.count("EXCLUDE_OBJECT_END NAME=test_bed_part0_1_3mf") == 33


def test_issue_1_prusaslicer_point_collection():
Expand All @@ -191,11 +189,11 @@ def test_issue_1_prusaslicer_point_collection():

definitions = collect_definitions(results)

assert "DEFINE_OBJECT NAME=Shape_Cylinder_id_1_copy_0" in definitions
assert "DEFINE_OBJECT NAME=Shape_Box_id_0_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=Shape_Cylinder_id_1_copy_0" in definitions
assert "EXCLUDE_OBJECT_DEFINE NAME=Shape_Box_id_0_copy_0" in definitions

assert results.count(f"START_CURRENT_OBJECT NAME=Shape_Cylinder_id_1_copy_0") == 125
assert results.count(f"END_CURRENT_OBJECT NAME=Shape_Cylinder_id_1_copy_0") == 125
assert results.count(f"EXCLUDE_OBJECT_START NAME=Shape_Cylinder_id_1_copy_0") == 125
assert results.count(f"EXCLUDE_OBJECT_END NAME=Shape_Cylinder_id_1_copy_0") == 125

assert results.count(f"START_CURRENT_OBJECT NAME=Shape_Box_id_0_copy_0") == 125
assert results.count(f"END_CURRENT_OBJECT NAME=Shape_Box_id_0_copy_0") == 125
assert results.count(f"EXCLUDE_OBJECT_START NAME=Shape_Box_id_0_copy_0") == 125
assert results.count(f"EXCLUDE_OBJECT_END NAME=Shape_Box_id_0_copy_0") == 125
Loading

0 comments on commit 8dc6b59

Please sign in to comment.