- made the 'M2' Gcode command footer optional, default is False (can be set using the TclCommand: set_sys cncjob_footer True)

- added a setting in Preferences to force the GCode output to have the Windows line-endings even for non-Windows OS's
This commit is contained in:
Marius Stanciu
2019-11-07 17:07:45 +02:00
parent 26d2831ded
commit 892c9130f2
4 changed files with 56 additions and 18 deletions

View File

@@ -6071,13 +6071,24 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob):
self.app.inform.emit('[success] %s...' %
_('Loaded Machine Code into Code Editor'))
def gcode_header(self):
def gcode_header(self, comment_start_symbol=None, comment_stop_symbol=None):
"""
Will create a header to be added to all GCode files generated by FlatCAM
:param comment_start_symbol: a symbol to be used as the first symbol in a comment
:param comment_stop_symbol: a symbol to be used as the last symbol in a comment
:return: a string with a GCode header
"""
log.debug("FlatCAMCNCJob.gcode_header()")
time_str = "{:%A, %d %B %Y at %H:%M}".format(datetime.now())
marlin = False
hpgl = False
probe_pp = False
start_comment = comment_start_symbol if comment_start_symbol is not None else '('
stop_comment = comment_stop_symbol if comment_stop_symbol is not None else ')'
try:
for key in self.cnc_tools:
ppg = self.cnc_tools[key]['data']['ppname_g']
@@ -6151,17 +6162,17 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob):
gcode += '(Units: ' + self.units.upper() + ')\n' + "\n"
gcode += '(Created on ' + time_str + ')\n' + '\n'
else:
gcode = '(G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s)\n' % \
(str(self.app.version), str(self.app.version_date)) + '\n'
gcode = '%sG-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s%s\n' % \
(start_comment, str(self.app.version), str(self.app.version_date), stop_comment) + '\n'
gcode += '(Name: ' + str(self.options['name']) + ')\n'
gcode += '(Type: ' + "G-code from " + str(self.options['type']) + ')\n'
gcode += '%sName: ' % start_comment + str(self.options['name']) + '%s\n' % stop_comment
gcode += '%sType: ' % start_comment + "G-code from " + str(self.options['type']) + '%s\n' % stop_comment
# if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
# gcode += '(Tools in use: ' + str(p['options']['Tools_in_use']) + ')\n'
gcode += '(Units: ' + self.units.upper() + ')\n' + "\n"
gcode += '(Created on ' + time_str + ')\n' + '\n'
gcode += '%sUnits: ' % start_comment + self.units.upper() + '%s\n' % stop_comment + "\n"
gcode += '%sCreated on ' % start_comment + time_str + '%s\n' % stop_comment + '\n'
return gcode
@@ -6177,6 +6188,15 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob):
return 'M02'
def export_gcode(self, filename=None, preamble='', postamble='', to_file=False):
"""
This will save the GCode from the Gcode object to a file on the OS filesystem
:param filename: filename for the GCode file
:param preamble: a custom Gcode block to be added at the beginning of the Gcode file
:param postamble: a custom Gcode block to be added at the end of the Gcode file
:param to_file: if False then no actual file is saved but the app will know that a file was created
:return: None
"""
gcode = ''
roland = False
hpgl = False
@@ -6241,7 +6261,9 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob):
_("G-code does not have a units code: either G20 or G21"))
return
g = gcode[:g_idx] + preamble + '\n' + gcode[g_idx:] + postamble + self.gcode_footer()
footer = self.app.defaults['cncjob_footer']
end_gcode = self.gcode_footer() if footer is True else ''
g = gcode[:g_idx] + preamble + '\n' + gcode[g_idx:] + postamble + end_gcode
# if toolchange custom is used, replace M6 code with the code from the Toolchange Custom Text box
if self.ui.toolchange_cb.get_value() is True:
@@ -6258,15 +6280,20 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob):
self.app.inform.emit('[success] %s' %
_("Toolchange G-code was replaced by a custom code."))
# lines = StringIO(self.gcode)
lines = StringIO(g)
# Write
if filename is not None:
try:
with open(filename, 'w') as f:
for line in lines:
f.write(line)
force_windows_line_endings = self.app.defaults['cncjob_line_ending']
if force_windows_line_endings and sys.platform != 'win32':
with open(filename, 'w', newline='\r\n') as f:
for line in lines:
f.write(line)
else:
with open(filename, 'w') as f:
for line in lines:
f.write(line)
except FileNotFoundError:
self.app.inform.emit('[WARNING_NOTCL] %s' %
_("No such file or directory"))