Skip to content

Commit

Permalink
Connect update_tool() to fluent API and add tool change tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giannissc committed May 6, 2023
1 parent d85ab46 commit 67b22c8
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/cq_cam/fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ def update_tool(self, tool: Optional[Tool] = None) -> Job:
commands = [StartSequence(speed, self.coolant)]
self = self._add_operation("Speed Change", commands)

# for operation in self.operations:
# print(operation.name)
# print("=========================")

# Update Job attributes
self.tool_number = tool_number
self.tool_diameter = tool_diameter
Expand Down Expand Up @@ -169,6 +165,7 @@ def profile(
raise ValueError('Set at least one of "outer_offset" or "inner_offset"')
outer_wires, inner_wires = extract_wires(shape)

self = self.update_tool(tool)
# Prefer inner wires first
commands = []
if inner_wires and inner_offset is not None:
Expand Down Expand Up @@ -201,6 +198,7 @@ def wire_profile(
tabs=None,
tool: Optional[Tool] = None,
):
self = self.update_tool(tool)
if isinstance(wires, cq.Wire):
wires = [wires]

Expand All @@ -227,6 +225,7 @@ def pocket(
stepdown: Optional[float] = None,
tool: Optional[Tool] = None,
) -> Job:
self = self.update_tool(tool)
if isinstance(op_areas, cq.Workplane):
op_areas = op_areas.objects
elif isinstance(op_areas, cq.Face):
Expand All @@ -253,6 +252,7 @@ def pocket(
def drill(self, op_areas, tool: Optional[Tool] = None, **kwargs) -> Job:
from cq_cam.operations.drill import Drill

self = self.update_tool(tool)
drill = Drill(self, o=op_areas, **kwargs)
return self._add_operation("Drill", drill.commands)

Expand Down
7 changes: 6 additions & 1 deletion src/cq_cam/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

@pytest.fixture
def box():
return cq.Workplane().rect(5, 5).extrude(2)
return cq.Workplane("XY").rect(5, 5).extrude(2)


@pytest.fixture
def big_box():
return cq.Workplane("XY").box(10, 10, 5)


@pytest.fixture
Expand Down
119 changes: 119 additions & 0 deletions src/cq_cam/tests/test_drill.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cq_cam.fluent import Job
from cq_cam.operations.tabs import EdgeTabs
from cq_cam.tool import Tool
from cq_cam.utils.circle_bug_workaround import circle_bug_workaround


Expand All @@ -21,3 +22,121 @@ def test_drill_superficially(box, job):
"G90 G54 G64 G50 G17 G94\nG49 G40 G80\nG21\nG30\n"
"M5"
)


tool_change_label = "(Job - Tool Change)\n"


def test_same_tool_number(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.center(2, 2)
.hole(2)
.faces(">Z")
.workplane()
.center(-3, -3)
.hole(3)
)

tool_1 = Tool(tool_diameter=2, tool_number=1)

small_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][1]
big_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][0]

job = job.drill(small_hole, tool=tool_1, depth=1)
job = job.drill(big_hole, tool=tool_1, depth=1)

assert job.operations[0].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)

assert job.operations[2].to_gcode() != (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)


def test_different_tool_number_(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.center(2, 2)
.hole(2)
.faces(">Z")
.workplane()
.center(-3, -3)
.hole(3)
)

tool_1 = Tool(tool_diameter=2, tool_number=1)
tool_2 = Tool(tool_diameter=3, tool_number=2)

small_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][1]
big_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][0]

job = job.drill(small_hole, tool=tool_1, depth=1)
job = job.drill(big_hole, tool=tool_2, depth=1)

assert job.operations[0].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)

assert job.operations[2].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T2 G43 H2 M6\n" "M3"
)


def test_speed_change(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.center(2, 2)
.hole(2)
.faces(">Z")
.workplane()
.center(-3, -3)
.hole(3)
)

tool_1 = Tool(speed=1000)
tool_2 = Tool(speed=2000)

small_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][1]
big_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][0]

job = job.drill(small_hole, tool=tool_1, depth=1)
job = job.drill(big_hole, tool=tool_2, depth=1)

assert job.operations[0].to_gcode() == ("(Job - Speed Change)\n" "M3 S1000")

assert job.operations[2].to_gcode() == ("(Job - Speed Change)\n" "M3 S2000")


def test_different_tool_number_and_speed(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.center(2, 2)
.hole(2)
.faces(">Z")
.workplane()
.center(-3, -3)
.hole(3)
)

tool_1 = Tool(tool_diameter=2, tool_number=1, speed=1000)
tool_2 = Tool(tool_diameter=3, tool_number=2, speed=2000)

small_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][1]
big_hole = [w for w in wp.wires(">Z").objects if len(w.Edges()) == 1][0]

job = job.drill(small_hole, tool=tool_1, depth=1)
job = job.drill(big_hole, tool=tool_2, depth=1)

assert job.operations[0].to_gcode() == (
"(Job - Tool Change)\n" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3 S1000"
)

assert job.operations[2].to_gcode() == (
"(Job - Tool Change)\n" "M5\n" "G30\n" "M1\n" "T2 G43 H2 M6\n" "M3 S2000"
)
156 changes: 154 additions & 2 deletions src/cq_cam/tests/test_pocket.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import cadquery as cq
import pytest

from cq_cam.fluent import Job
from cq_cam.operations.pocket import apply_stepdown, determine_stepdown_start_depth
from cq_cam.tool import Tool
from cq_cam.utils.geometry_op import PathFace, offset_face
from cq_cam.utils.tests.conftest import round_array
from cq_cam.utils.utils import break_compound_to_faces
Expand Down Expand Up @@ -61,8 +63,8 @@ def test_make_from_wire_with_bigger_inner_than_outer():

def test_stepdown(job, box):
wp = box.faces(">Z").workplane().rect(2, 2).cutBlind(-1)
pocket_face = wp.faces(">Z[1]")
job = job.pocket(pocket_face, stepdown=0.5)
face = wp.faces(">Z[1]")
job = job.pocket(face, stepdown=0.5)
assert job.operations[0].to_gcode() == (
"(Job - Pocket)\n"
"G0Z10\n"
Expand Down Expand Up @@ -95,3 +97,153 @@ def test_determine_stepdown_start_depth():
assert determine_stepdown_start_depth(bottom, []) is None
assert determine_stepdown_start_depth(bottom, [upper_container]) == -3
assert determine_stepdown_start_depth(bottom, [upper_non_container]) is None


tool_change_label = "(Job - Tool Change)\n"


def test_same_tool_diameter(job: Job, big_box):
wp = big_box.faces(">Z").workplane().rect(8, 8).cutBlind(-1)

tool_1 = Tool(tool_diameter=5, tool_number=1)
tool_2 = Tool(tool_diameter=5, tool_number=2)

face = wp.faces(">Z[1]")

job = job.pocket(face, tool=tool_1)
job = job.pocket(face, tool=tool_2)

assert job.operations[1].to_gcode() == job.operations[3].to_gcode()


def test_different_tool_diameter(job: Job, big_box):
wp = big_box.faces(">Z").workplane().rect(8, 8).cutBlind(-1)

tool_1 = Tool(tool_diameter=5, tool_number=1)
tool_2 = Tool(tool_diameter=1, tool_number=2)

face = wp.faces(">Z[1]")

job = job.pocket(face, tool=tool_1)
job = job.pocket(face, tool=tool_2)

assert job.operations[1].to_gcode() != job.operations[3].to_gcode()


def test_same_tool_number(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

tool_1 = Tool(tool_diameter=5, tool_number=1)

face_1 = wp.faces(">Z[1]", tag="operation-1")
face_2 = wp.faces(">Z[1]")

job = job.pocket(face_1, tool=tool_1)
job = job.pocket(face_2, tool=tool_1)

assert job.operations[0].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)

assert job.operations[2].to_gcode() != (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)


def test_different_tool_number_(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

tool_1 = Tool(tool_diameter=3, tool_number=1)
tool_2 = Tool(tool_diameter=3, tool_number=2)

face_1 = wp.faces(">Z[1]", tag="operation-1")
face_2 = wp.faces(">Z[1]")

job = job.pocket(face_1, tool=tool_1)
job = job.pocket(face_2, tool=tool_2)

assert job.operations[0].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3"
)

assert job.operations[2].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T2 G43 H2 M6\n" "M3"
)


def test_speed_change(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

tool_1 = Tool(speed=1000)
tool_2 = Tool(speed=2000)

face_1 = wp.faces(">Z[1]", tag="operation-1")
face_2 = wp.faces(">Z[1]")

job = job.pocket(face_1, tool=tool_1)
job = job.pocket(face_2, tool=tool_2)

assert job.operations[0].to_gcode() == ("(Job - Speed Change)\n" "M3 S1000")

assert job.operations[2].to_gcode() == ("(Job - Speed Change)\n" "M3 S2000")


def test_different_tool_number_and_speed(job: Job, big_box):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

tool_1 = Tool(tool_diameter=3, tool_number=1, speed=1000)
tool_2 = Tool(tool_diameter=3, tool_number=2, speed=2000)

face_1 = wp.faces(">Z[1]", tag="operation-1")
face_2 = wp.faces(">Z[1]")

job = job.pocket(face_1, tool=tool_1)
job = job.pocket(face_2, tool=tool_2)

assert job.operations[0].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T1 G43 H1 M6\n" "M3 S1000"
)

assert job.operations[2].to_gcode() == (
f"{tool_change_label}" "M5\n" "G30\n" "M1\n" "T2 G43 H2 M6\n" "M3 S2000"
)
Loading

0 comments on commit 67b22c8

Please sign in to comment.