-
Notifications
You must be signed in to change notification settings - Fork 4
/
para_star.py
113 lines (92 loc) · 2.51 KB
/
para_star.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#Parametric GCode Star
#
#Generates a six pointed star in GCode.
#Goal is to generate snowflakes algorithmically.
#
#This code is derived from Allan Ecker's
#source: http://www.thingiverse.com/thing:849
#
#License: CC-BY-SA
#Import the math library
import math
#Rounding function
# Input: num=int, float, or string; r=int
# Output: string
def myRound(num, r=0):
if type(num) is str:
num = float(num)
num += 0.5/(10**r)
if r == 0:
return str(int(num))
else:
num = str(num)
return num[:num.find('.')+r+1]
#G1 Code Object
class G1Code:
def __init__(self, X=0, Y=0, Z=0, F=0):
self.X =X
self.Y =Y
self.Z = Z
self.F = F
def __str__(self):
#Added rounding
string = "G1 X" + myRound(self.X,2) + " Y" + myRound(self.Y,2) + " Z" + myRound(self.Z,2) + " F" + str(self.F)
return string
#Rotate the XY point of the GCode
def rotate(self, Theta):
OldX = self.X
OldY = self.Y
self.X = OldX * math.cos(Theta) - OldY * math.sin(Theta)
self.Y = OldX * math.sin(Theta) + OldY * math.cos(Theta)
#Add relative moves
def relative_move(self, XMove, YMove):
OldX = self.X
OldY = self.Y
self.X = OldX + XMove
self.Y = OldY + YMove
#Clone Method
def Clone(self):
CloneCode = G1Code(self.X, self.Y, self.Z, self.F)
return CloneCode
filename = "test.gcode"
ThisGCode = G1Code(X=0, Y=0, Z=0, F=700)
Zsteps = 0
TotalHeight = 10 # in layers
#Open the output file and paste on the "headers"
FILE = open(filename,"w")
FILE.writelines("G21\n")
FILE.writelines("G90\n")
FILE.writelines("M103\n")
FILE.writelines("M105\n")
FILE.writelines("M104 S220.0\n")
FILE.writelines("M101\n")
#Step through the Z range of the object.
for Zsteps in range(TotalHeight):
ThisGCode.Z = 0.4 * Zsteps
for a in range(6):
angle = math.radians(60*a)
ThisGCode.X = 0.5
ThisGCode.Y = 0.5
ThisGCode.rotate(angle)
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = 0.5
ThisGCode.Y = 10
ThisGCode.rotate(angle)
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = 0
ThisGCode.Y = 10.5
ThisGCode.rotate(angle)
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = -0.5
ThisGCode.Y = 10
ThisGCode.rotate(angle)
FILE.writelines(str(ThisGCode)+ "\n")
ThisGCode.X = -0.5
ThisGCode.Y = 0.5
ThisGCode.rotate(angle)
FILE.writelines(str(ThisGCode)+ "\n")
FILE.writelines("M103\n")
ThisGCode.Z = ThisGCode.Z + 10
FILE.writelines(str(ThisGCode)+ "\n")
FILE.writelines("M104 S0\n")
FILE.close