Merge pull request #25273 from Thom-de-Jong/main

CAM: Exporting G-code can be canceled from the editor dialog
This commit is contained in:
sliptonic
2025-11-30 14:04:22 -06:00
committed by GitHub
2 changed files with 55 additions and 16 deletions

View File

@@ -211,7 +211,7 @@ class GCodeHighlighter(QtGui.QSyntaxHighlighter):
class GCodeEditorDialog(QtGui.QDialog):
def __init__(self, parent=None):
def __init__(self, text="", parent=None, refactored=False):
if parent is None:
parent = FreeCADGui.getMainWindow()
QtGui.QDialog.__init__(self, parent)
@@ -227,15 +227,35 @@ class GCodeEditorDialog(QtGui.QDialog):
font.setFixedPitch(True)
font.setPointSize(p.GetInt("FontSize", 10))
self.editor.setFont(font)
self.editor.setPlainText("G01 X55 Y4.5 F300.0")
self.editor.setPlainText(text)
layout.addWidget(self.editor)
# OK and Cancel buttons
self.buttons = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal,
self,
)
# buttons depending on the post processor used
if refactored:
self.buttons = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok
| QtGui.QDialogButtonBox.Discard
| QtGui.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal,
self,
)
# Swap the button text as to not change the old cancel behaviour for the user
self.buttons.button(QtGui.QDialogButtonBox.Discard).setIcon(
self.buttons.button(QtGui.QDialogButtonBox.Cancel).icon()
)
self.buttons.button(QtGui.QDialogButtonBox.Discard).setText(
self.buttons.button(QtGui.QDialogButtonBox.Cancel).text()
)
self.buttons.button(QtGui.QDialogButtonBox.Cancel).setIcon(QtGui.QIcon())
self.buttons.button(QtGui.QDialogButtonBox.Cancel).setText("Abort")
else:
self.buttons = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal,
self,
)
self.buttons.button(QtGui.QDialogButtonBox.Ok).setDisabled(True)
layout.addWidget(self.buttons)
# restore placement and size
@@ -250,8 +270,21 @@ class GCodeEditorDialog(QtGui.QDialog):
if width > 0 and height > 0:
self.resize(width, height)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
# connect signals
self.editor.textChanged.connect(self.text_changed)
self.buttons.clicked.connect(self.clicked)
def text_changed(self):
self.buttons.button(QtGui.QDialogButtonBox.Ok).setDisabled(False)
def clicked(self, button):
match self.buttons.buttonRole(button):
case QtGui.QDialogButtonBox.RejectRole:
self.done(0)
case QtGui.QDialogButtonBox.ApplyRole | QtGui.QDialogButtonBox.AcceptRole:
self.done(1)
case QtGui.QDialogButtonBox.DestructiveRole:
self.done(2)
def done(self, *args, **kwargs):
params = FreeCAD.ParamGet(self.paramKey)
@@ -308,6 +341,7 @@ def editor(gcode):
dia = GCodeEditorDialog()
dia.editor.setText(gcode)
dia.buttons.button(QtGui.QDialogButtonBox.Ok).setDisabled(True)
gcodeSize = len(dia.editor.toPlainText())
if gcodeSize <= mhs:
# because of poor performance, syntax highlighting is

View File

@@ -275,6 +275,7 @@ def export_common(values: Values, objectslist, filename: str) -> str:
final: str
final_for_editor: str
gcode: Gcode = []
editor_result: int = 1
for obj in objectslist:
if not hasattr(obj, "Path"):
@@ -335,16 +336,17 @@ def export_common(values: Values, objectslist, filename: str) -> str:
if len(final) > 100000:
print("Skipping editor since output is greater than 100kb")
else:
dia = PostUtils.GCodeEditorDialog()
# the editor expects lines to end in "\n", and returns lines ending in "\n"
if values["END_OF_LINE_CHARACTERS"] == "\n":
dia.editor.setPlainText(final)
if dia.exec_():
dia = PostUtils.GCodeEditorDialog(final, refactored=True)
editor_result = dia.exec_()
if editor_result == 1:
final = dia.editor.toPlainText()
else:
final_for_editor = "\n".join(gcode)
dia.editor.setPlainText(final_for_editor)
if dia.exec_():
dia = PostUtils.GCodeEditorDialog(final_for_editor, refactored=True)
editor_result = dia.exec_()
if editor_result == 1:
final_for_editor = dia.editor.toPlainText()
# convert all "\n" to the appropriate end-of-line characters
if values["END_OF_LINE_CHARACTERS"] == "\n\n":
@@ -358,7 +360,9 @@ def export_common(values: Values, objectslist, filename: str) -> str:
# "\r\n" means "use \r\n"
final = final_for_editor.replace("\n", values["END_OF_LINE_CHARACTERS"])
print("done postprocessing.")
if editor_result == 0:
print("aborted postprocessing.")
return None
if not filename == "-":
if final[0:2] == "\n\n":
@@ -377,4 +381,5 @@ def export_common(values: Values, objectslist, filename: str) -> str:
# that is running the postprocessor uses".
gfile.write(final)
print("done postprocessing.")
return final