Skip to content

Commit 4f3eb71

Browse files
author
usmfi
committed
cleanup and tests
1 parent 3276cc4 commit 4f3eb71

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

pyGCodeDecode/state_generator.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
default_virtual_machine = {
8282
"absolute_position": True,
8383
"absolute_extrusion": True,
84+
"extrusion_volumetric": False,
8485
"units": "SI (mm)",
8586
"initial_position": None,
8687
# general properties
@@ -208,21 +209,21 @@ def dict_list_traveler(line_dict_list: List[dict], initial_machine_setup: dict)
208209
209210
"""
210211

211-
def apply_extrusion(line_dict: dict, virtual_machine: dict, command: str, initial_machine_setup: dict) -> dict:
212+
def apply_extrusion(line_dict: dict, virtual_machine: dict, command: str) -> dict:
212213
import math
213214

214215
e_value = line_dict[command]["E"]
215216

216217
# volumetric to length conversion
217218
# (1) V = (d/2)^2 * pi * E
218219
# (2) E = V / ((d/2)^2 * pi)
219-
if initial_machine_setup.get("extrusion_volumetric", False):
220+
if virtual_machine.get("extrusion_volumetric", False):
220221
# volumetric extrusion
221-
e_value = e_value / (math.pi * (initial_machine_setup["filament_diam"] / 2) ** 2)
222+
e_value = e_value / (math.pi * (virtual_machine["filament_diam"] / 2) ** 2)
222223

223-
if virtual_machine["absolute_extrusion"] is True:
224+
if virtual_machine["absolute_extrusion"]:
224225
virtual_machine["E"] = e_value
225-
if virtual_machine["absolute_extrusion"] is False: # redundant
226+
else:
226227
virtual_machine["E"] = virtual_machine["E"] + e_value
227228

228229
return virtual_machine
@@ -245,17 +246,15 @@ def apply_extrusion(line_dict: dict, virtual_machine: dict, command: str, initia
245246
layer_counter = 0
246247

247248
# overwrite default values from initial machine setup
248-
"""TODO: depending on the setting the user should be informed that a default value is used.
249-
I prepared a warning below.
250-
Are all these settings necessary?"""
251249
for key in default_virtual_machine:
252250
if initial_machine_setup is not None and key in initial_machine_setup:
253251
virtual_machine[key] = initial_machine_setup[key]
254252
else:
255-
"""print(
253+
custom_print(
256254
f"The parameter '{key}' was not specified in your machine presets. "
257-
f"Using the the default value of '{default_virtual_machine[key]}' to continue."
258-
)"""
255+
f"Using the the default value of '{default_virtual_machine[key]}' to continue.",
256+
lvl=3,
257+
)
259258
virtual_machine[key] = default_virtual_machine[key]
260259

261260
# initial state creation
@@ -318,7 +317,7 @@ def apply_extrusion(line_dict: dict, virtual_machine: dict, command: str, initia
318317

319318
# look for extrusion commands and apply abs/rel
320319
if "E" in line_dict[command]:
321-
virtual_machine = apply_extrusion(line_dict, virtual_machine, command, initial_machine_setup)
320+
virtual_machine = apply_extrusion(line_dict, virtual_machine, command)
322321

323322
# feed rates in unit/min to unit/sec
324323
if "F" in line_dict[command]:

tests/test_end_to_end.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pathlib
44

5+
import numpy as np
6+
57

68
def test_end_to_end_compact():
79
"""Testing the simulation functionality with automatic setup, similarly to the brace example."""
@@ -13,6 +15,38 @@ def test_end_to_end_compact():
1315
)
1416

1517

18+
def test_end_to_end_volumetr():
19+
"""Testing the simulation functionality with automatic setup, using volumetric or distance based extrusion."""
20+
from pyGCodeDecode.gcode_interpreter import setup, simulation
21+
22+
preset = setup(pathlib.Path("./tests/data/test_printer_setups.yaml"), "test")
23+
preset.set_property({"extrusion_volumetric": False})
24+
25+
sim = simulation(
26+
gcode_path=pathlib.Path("./tests/data/test_state_generator.gcode"),
27+
initial_machine_setup=preset,
28+
)
29+
30+
end_extrusion = sim.blocklist[-1].segments[-1].pos_end.e
31+
expected_extrusion = 14.0
32+
assert end_extrusion == expected_extrusion, f"Expected {expected_extrusion}, but got {end_extrusion}"
33+
34+
preset = setup(pathlib.Path("./tests/data/test_printer_setups.yaml"), "test")
35+
preset.set_property({"extrusion_volumetric": True})
36+
37+
sim = simulation(
38+
gcode_path=pathlib.Path("./tests/data/test_state_generator.gcode"),
39+
initial_machine_setup=preset,
40+
)
41+
42+
end_extrusion = sim.blocklist[-1].segments[-1].pos_end.e
43+
expected_extrusion = 14.0 / ((1.75 / 2) ** 2 * np.pi)
44+
45+
assert np.isclose(
46+
end_extrusion, expected_extrusion, rtol=1e-9
47+
), f"Expected {expected_extrusion}, but got {end_extrusion}"
48+
49+
1650
def test_end_to_end_extensive():
1751
"""Testing the simulation functionality as well as the various outputs, similarly to the benchy example."""
1852
from pyGCodeDecode.abaqus_file_generator import generate_abaqus_event_series

0 commit comments

Comments
 (0)