-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcn5X_gcodeParser.py
134 lines (111 loc) · 4.52 KB
/
cn5X_gcodeParser.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
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: UTF-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Copyright 2018-2024 Gauthier Brière (gauthier.briere "at" gmail.com) '
' '
' This file: cn5X_gcodeParser.py, is part of cn5X++ '
' '
' cn5X++ is free software: you can redistribute it and/or modify it '
' under the terms of the GNU General Public License as published by '
' the Free Software Foundation, either version 3 of the License, or '
' (at your option) any later version. '
' '
' cn5X++ is distributed in the hope that it will be useful, but '
' WITHOUT ANY WARRANTY; without even the implied warranty of '
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the '
' GNU General Public License for more details. '
' '
' You should have received a copy of the GNU General Public License '
' along with this program. If not, see <http://www.gnu.org/licenses/>. '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
from PyQt6.QtCore import Qt, QObject, pyqtSignal, pyqtSlot
from cn5X_config import *
LINE_FLAG_OVERFLOW = 1
LINE_FLAG_COMMENT_PARENTHESES = 2
LINE_FLAG_COMMENT_SEMICOLON = 4
class gcodeParser(QObject):
'''
Analyse d'une ligne de GCode
'''
sig_log = pyqtSignal(int, str) # Message de fonctionnement du composant
def __init__(self):
super().__init__()
@pyqtSlot(str)
def noComment(self, gcodeLine: str):
'''
Suprimme le(s) commentaire(s) et les espaces de la ligne GCode.
Utilisation du même algorithme (protocol.c) que Grbl pour la
supression des commentaires et des espaces dans la ligne.
'''
line = ""
line_flags = 0
for c in gcodeLine:
if line_flags:
if (c == ')'):
# End of '()' comment. Resume line allowed.
if (line_flags & LINE_FLAG_COMMENT_PARENTHESES):
line_flags &= ~(LINE_FLAG_COMMENT_PARENTHESES)
else:
if (c <= ' '):
# Throw away whitepace and control characters
pass
elif (c == '/'):
# Block delete NOT SUPPORTED. Ignore character.
pass
elif (c == '('):
# Enable comments flag and ignore all characters until ')' or EOL.
# NOTE: This doesn't follow the NIST definition exactly, but is good enough for now.
line_flags |= LINE_FLAG_COMMENT_PARENTHESES;
elif (c == ';'):
# NOTE: ';' comment to EOL is a LinuxCNC definition. Not NIST.
line_flags |= LINE_FLAG_COMMENT_SEMICOLON;
else:
# Ajout du caractère à la ligne
line += c.upper();
return line
@pyqtSlot(str)
def wordDict(self, gcodeLine: str):
'''
Renvoi un dictionnaire des mots GCode contenus dans la ligne
Sous la forme {'mot': 'valeurs', 'mot': 'valeurs', ...}
'''
words = dict()
currentWord = ""
currentValue = ""
for c in self.noComment(gcodeLine):
if c in VALIDES_GCODE_WORDS:
if currentWord != "":
# Ajoute le mot courant au dictionnaire avant de passer au suivant
words[currentWord] = currentValue
currentValue = ""
# C'est le nouveau mot courant nouveau mot
currentWord = c
else:
currentValue += c
# Ajoute le dernier mot
words[currentWord] = currentValue
# Revoi le résultat
return words
@pyqtSlot(str)
def wordList(self, gcodeLine: str):
'''
Renvoi une liste des mots GCode contenus dans la ligne avec leurs valeurs
'''
liste = []
currentWord = ""
currentValue = ""
for c in self.noComment(gcodeLine):
if c in VALIDES_GCODE_WORDS:
if currentWord != "":
# Ajoute le mot courant au la liste avant de passer au suivant
liste.append(currentWord + currentValue)
currentValue = ""
# C'est le nouveau mot courant nouveau mot
currentWord = c
else:
currentValue += c
# Ajoute le dernier mot
liste.append(currentWord + currentValue)
# Revoi le résultat
return liste