1
1
from typing import Callable , Tuple , Union
2
+ from . import util
2
3
3
- # flow calculations
4
+ # circumference calcs for arcs
4
5
# minimize feedrate output
6
+ # draw absolute
5
7
class Gcode :
6
8
def __init__ (
7
9
self ,
@@ -10,13 +12,17 @@ def __init__(
10
12
line_width : float = 0.4 ,
11
13
filament_width : float = 1.75 ,
12
14
home_position : Tuple [float , float , float ] = (0 , 0 , 0 ),
15
+ extrusion_length_calculator : Callable [
16
+ [float , float , float , float ], float
17
+ ] = util .calculate_extrusion_length ,
13
18
) -> None :
14
19
self .filename = filename
15
20
self .file = open (filename , "w" )
16
21
self .layer_height = layer_height
17
22
self .line_width = line_width
18
23
self .filament_width = filament_width
19
24
self .home_pos = home_position
25
+ self .extrusion_length_calculator = extrusion_length_calculator
20
26
self .pos = [None ] * 3
21
27
self .e = None
22
28
@@ -39,6 +45,15 @@ def get_z(self) -> float:
39
45
def get_e (self ) -> float :
40
46
return self .e
41
47
48
+ def set_layer_height (self , layer_height : float ):
49
+ self .layer_height = layer_height
50
+
51
+ def set_line_width (self , line_width : float ):
52
+ self .line_width = line_width
53
+
54
+ def set_filament_width (self , filament_width : float ):
55
+ self .filament_width = filament_width
56
+
42
57
def zero_extruder (self ):
43
58
self .file .write ("G92 E0\n " )
44
59
self .e = 0
@@ -114,7 +129,12 @@ def draw(
114
129
self , delta : Tuple [float , float , float ], e : float = None , feedrate : int = 2400
115
130
):
116
131
if e is None :
117
- e = 0 # REPLACE
132
+ e = self .extrusion_length_calculator (
133
+ util .dist (delta ),
134
+ self .line_width ,
135
+ self .layer_height ,
136
+ self .filament_width ,
137
+ )
118
138
self .pos [0 ] += delta [0 ]
119
139
self .pos [1 ] += delta [1 ]
120
140
self .pos [2 ] += delta [2 ]
@@ -133,7 +153,12 @@ def draw_arc(
133
153
feedrate : int = 2400 ,
134
154
):
135
155
if e is None :
136
- e = 0 # REPLACE
156
+ e = self .extrusion_length_calculator (
157
+ util .dist (delta ), # REPLACE
158
+ self .line_width ,
159
+ self .layer_height ,
160
+ self .filament_width ,
161
+ )
137
162
out = "G2" if clockwise else "G3"
138
163
out += f" F{ feedrate } "
139
164
self .pos [0 ] += delta [0 ]
@@ -158,7 +183,12 @@ def draw_arc_r(
158
183
feedrate : int = 2400 ,
159
184
):
160
185
if e is None :
161
- e = 0 # REPLACE
186
+ e = self .extrusion_length_calculator (
187
+ util .dist (delta ), # REPLACE
188
+ self .line_width ,
189
+ self .layer_height ,
190
+ self .filament_width ,
191
+ )
162
192
if delta [0 ] == 0.0 and delta [1 ] == 0.0 :
163
193
raise ValueError ("Both x and y cannot be 0. Make sure to set at least one!" )
164
194
out = "G2" if clockwise else "G3"
0 commit comments