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 8, 2023
1 parent d85ab46 commit 1c3c53e
Show file tree
Hide file tree
Showing 6 changed files with 158 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
5 changes: 5 additions & 0 deletions src/cq_cam/tests/test_drill.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import cadquery as cq

from cq_cam.command import StartSequence, ToolChange
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

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


def test_drill_superficially(box, job):
box = box.faces(">Z").workplane().circle(1).cutThruAll()
Expand Down
10 changes: 8 additions & 2 deletions src/cq_cam/tests/test_pocket.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import cadquery as cq
import pytest

from cq_cam.command import StartSequence, ToolChange
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

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


def test_offset_face():
wp = (
Expand Down Expand Up @@ -61,8 +67,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
5 changes: 5 additions & 0 deletions src/cq_cam/tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import cadquery as cq

from cq_cam.command import StartSequence, ToolChange
from cq_cam.fluent import Job
from cq_cam.operations.tabs import EdgeTabs, WireTabs
from cq_cam.tool import Tool
from cq_cam.utils.circle_bug_workaround import circle_bug_workaround

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


def test_circle_bug_workaround():
wp = cq.Workplane("XY")
Expand Down
130 changes: 130 additions & 0 deletions src/cq_cam/tests/test_tool_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import pytest

from cq_cam.command import StartSequence, ToolChange
from cq_cam.fluent import Job
from cq_cam.tool import Tool

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


@pytest.mark.parametrize("operation", [Job.pocket, Job.profile])
def test_same_tool_diameter(job: Job, big_box, operation):
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]").val()
print(operation.__name__)

job = operation(job, face, tool=tool_1)
job = operation(job, face, tool=tool_2)

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


@pytest.mark.parametrize("operation", [Job.pocket, Job.profile])
def test_different_tool_diameter(job: Job, big_box, operation):
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]").val()

job = operation(job, face, tool=tool_1)
job = operation(job, face, tool=tool_2)

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


@pytest.mark.parametrize("operation", [Job.pocket, Job.profile, Job.drill])
def test_same_tool_number(job: Job, big_box, operation):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

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

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

if operation.__name__ == "drill":
job = operation(job, face_1, tool=tool_1, depth=1)
job = operation(job, face_2, tool=tool_1, depth=1)
else:
job = operation(job, face_1, tool=tool_1)
job = operation(job, face_2, tool=tool_1)

gcode_str, _ = output_1.to_gcode()
assert job.operations[0].to_gcode() == f"{tool_change_label}{gcode_str}"
assert job.operations[2].to_gcode() != f"{tool_change_label}{gcode_str}"


@pytest.mark.parametrize("operation", [Job.pocket, Job.profile, Job.drill])
@pytest.mark.parametrize(
"output_1, output_2, tool_1, tool_2, label",
[
(
ToolChange(tool_number=1),
ToolChange(tool_number=2),
Tool(tool_diameter=3, tool_number=1),
Tool(tool_diameter=3, tool_number=2),
tool_change_label,
),
(
StartSequence(spindle=1000),
StartSequence(spindle=2000),
Tool(speed=1000),
Tool(speed=2000),
speed_change_label,
),
(
ToolChange(tool_number=1, spindle=1000),
ToolChange(tool_number=2, spindle=2000),
Tool(tool_diameter=3, tool_number=1, speed=1000),
Tool(tool_diameter=3, tool_number=2, speed=2000),
tool_change_label,
),
],
)
def test_different_tool_number_(
job: Job, big_box, operation, output_1, output_2, tool_1, tool_2, label
):
wp = (
big_box.faces(">Z")
.workplane()
.rect(8, 8)
.cutBlind(-1)
.tag("operation-1")
.faces(">Z[1]")
.workplane()
.rect(6, 6)
.cutBlind(-1)
)

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

if operation.__name__ == "drill":
job = operation(job, face_1, tool=tool_1, depth=1)
job = operation(job, face_2, tool=tool_2, depth=1)
else:
job = operation(job, face_1, tool=tool_1)
job = operation(job, face_2, tool=tool_2)

gcode_str_1, _ = output_1.to_gcode()
gcode_str_2, _ = output_2.to_gcode()

assert job.operations[0].to_gcode() == f"{label}{gcode_str_1}"
assert job.operations[2].to_gcode() == f"{label}{gcode_str_2}"

0 comments on commit 1c3c53e

Please sign in to comment.