From e7268991675b376425d58743e1ee2e3150e06a10 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Mon, 4 Jan 2021 00:44:57 +0200 Subject: [PATCH] - added a new parameter to the New Tcl command named -reset. If set False (or 0) the TCL instance will not be recreated, allowing to keep the TCL variable content on the new project. - included the modifications from PR #336 from Andre Spahlinger --- CHANGELOG.md | 2 ++ appObjects/FlatCAMScript.py | 27 +++++++++++++++++++++++++-- app_Main.py | 10 ++++++++-- tclCommands/TclCommandNew.py | 18 ++++++++++++++---- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c867fbf..bea9decd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ CHANGELOG for FlatCAM beta - fixed the Tcl Commands: new_geometry, new_gerber and new_excellon to create correct objects. New Geometry object created with the new_geometry Tcl command accept now the usage of add_circle/polygon/polyline/rectangle Tcl commands - updated the autocompleter list for the Tcl commands with missing Tcl commands names - added three new Tcl Commands: add_aperture (adds an aperture to a Gerber object), add_drill and add_slot (add a drill/slot to an Excellon object) +- added a new parameter to the New Tcl command named -reset. If set False (or 0) the TCL instance will not be recreated, allowing to keep the TCL variable content on the new project. +- included the modifications from PR #336 from Andre Spahlinger 2.01.2021 diff --git a/appObjects/FlatCAMScript.py b/appObjects/FlatCAMScript.py index 52644f0f..86aa87ea 100644 --- a/appObjects/FlatCAMScript.py +++ b/appObjects/FlatCAMScript.py @@ -54,6 +54,7 @@ class ScriptObject(FlatCAMObj): self.ser_attrs = ['options', 'kind', 'source_file'] self.source_file = '' self.script_code = '' + self.script_filename = '' self.units_found = self.app.defaults['units'] @@ -168,6 +169,7 @@ class ScriptObject(FlatCAMObj): script_content = ''.join(script_content) self.source_file = script_content + self.script_filename = filename def handle_run_code(self): # trying to run a Tcl command without having the Shell open will create some warnings because the Tcl Shell @@ -184,10 +186,31 @@ class ScriptObject(FlatCAMObj): self.script_code = self.script_editor_tab.code_editor.toPlainText() old_line = '' - for tcl_command_line in self.script_code.splitlines(): + # set tcl info script to actual scriptfile + + set_tcl_script_name = '''proc procExists p {{ + return uplevel 1 [expr {{[llength [info command $p]] > 0}}] + }} + + if {{[procExists "info_original"]==0}} {{ + rename info info_original + }} + + proc info args {{ + switch [lindex $args 0] {{ + script {{ + return "{0}" + }} + default {{ + return [uplevel info_original $args] + }} + }} + }}'''.format(self.script_filename) + + for tcl_command_line in set_tcl_script_name.splitlines()+self.script_code.splitlines(): # do not process lines starting with '#' = comment and empty lines if not tcl_command_line.startswith('#') and tcl_command_line != '': - # id FlatCAM is run in Windows then replace all the slashes with + # if FlatCAM is run in Windows then replace all the slashes with # the UNIX style slash that TCL understands if sys.platform == 'win32': if "open" in tcl_command_line: diff --git a/app_Main.py b/app_Main.py index d1bbc5b2..92af243d 100644 --- a/app_Main.py +++ b/app_Main.py @@ -1842,6 +1842,7 @@ class App(QtCore.QObject): if init_tcl: # shell tool has to be initialized always first because other tools print messages in the Shell Dock self.shell = FCShell(app=self, version=self.version) + self.log.debug("TCL was re-instantiated. TCL variables are reset.") self.distance_tool = Distance(self) self.distance_tool.install(icon=QtGui.QIcon(self.resource_location + '/distance16.png'), pos=self.ui.menuedit, @@ -9396,11 +9397,13 @@ class MenuFileHandlers(QtCore.QObject): else: self.on_file_new_project(threaded=True) - def on_file_new_project(self, cli=None, threaded=None): + def on_file_new_project(self, cli=None, reset_tcl=True, threaded=None): """ Returns the application to its startup state. This method is thread-safe. :param cli: Boolean. If True this method was run from command line + :param reset_tcl: Boolean. If False, on new project creation the Tcl instance is not recreated, therefore it + will remember all the previous variables. If True then the Tcl is re-instantiated. :param threaded: Bool. If True some part of the initialization are done threaded :return: None """ @@ -9468,7 +9471,10 @@ class MenuFileHandlers(QtCore.QObject): self.app.clear_pool() # Init FlatCAMTools - self.app.init_tools(init_tcl=True) + if reset_tcl is True: + self.app.init_tools(init_tcl=True) + else: + self.app.init_tools(init_tcl=False) # tcl needs to be reinitialized, otherwise old shell variables etc remains # self.app.shell.init_tcl() diff --git a/tclCommands/TclCommandNew.py b/tclCommands/TclCommandNew.py index 7e4ba68a..cf390ff8 100644 --- a/tclCommands/TclCommandNew.py +++ b/tclCommands/TclCommandNew.py @@ -17,7 +17,9 @@ class TclCommandNew(TclCommand): arg_names = collections.OrderedDict() # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value - option_types = collections.OrderedDict() + option_types = collections.OrderedDict([ + ('reset', str), + ]) # array of mandatory options for current Tcl command: required = {'name','outname'} required = [] @@ -25,8 +27,11 @@ class TclCommandNew(TclCommand): # structured help for current command, args needs to be ordered help = { 'main': "Starts a new project. Clears objects from memory.", - 'args': collections.OrderedDict(), - 'examples': ['new'] + 'args': collections.OrderedDict([ + ('reset', 'If True/0 or missing (default value) or no value provided then the Tcl is re-instantiated ' + 'thus resetting all its variables.'), + ]), + 'examples': ['new', 'new -reset False'] } def execute(self, args, unnamed_args): @@ -39,4 +44,9 @@ class TclCommandNew(TclCommand): :return: None or exception """ - self.app.f_handlers.on_file_new_project(cli=True) + reset_tcl = True + if 'reset' in args: + if args['reset'] and (args['reset'] == '0' or args['reset'].lower() == 'false'): + reset_tcl = False + + self.app.f_handlers.on_file_new_project(cli=True, reset_tcl=reset_tcl)