From 27b7a7dce61f8f04c5250ca91a2d4e77a337335a Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Mon, 23 Sep 2019 04:07:24 +0300 Subject: [PATCH] - added a new TclCommand named "bounds" which will return a list of bounds values from a supplied list of objects names. For use in Tcl Scripts. --- README.md | 3 +- tclCommands/TclCommandBounds.py | 81 ++++++++++++++++++++++++++++ tclCommands/TclCommandCopperClear.py | 1 + tclCommands/TclCommandSetOrigin.py | 3 +- tclCommands/__init__.py | 1 + 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 tclCommands/TclCommandBounds.py diff --git a/README.md b/README.md index 02ea4bf9..4f411d70 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ CAD program, and create G-Code for Isolation routing. - in legacy graphic engine, fixed bug that made the old object disappear when a new object was loaded - in legacy graphic engine, fixed bug that crashed the app when creating a new project - in legacy graphic engine, fixed a bug that when deleting an object all objects where deleted -- added a new TclCommand named set_origin which will set the origin for all loaded objects to zero if the -auto True argument is used and to a certain x,y location if the format is: set_origin 5,7 +- added a new TclCommand named "set_origin" which will set the origin for all loaded objects to zero if the -auto True argument is used and to a certain x,y location if the format is: set_origin 5,7 +- added a new TclCommand named "bounds" which will return a list of bounds values from a supplied list of objects names. For use in Tcl Scripts 22.09.2019 diff --git a/tclCommands/TclCommandBounds.py b/tclCommands/TclCommandBounds.py new file mode 100644 index 00000000..9f50f52c --- /dev/null +++ b/tclCommands/TclCommandBounds.py @@ -0,0 +1,81 @@ +from tclCommands.TclCommand import TclCommand +from ObjectCollection import * + +from camlib import get_bounds + +import gettext +import FlatCAMTranslation as fcTranslate +import builtins + +fcTranslate.apply_language('strings') +if '_' not in builtins.__dict__: + _ = gettext.gettext + + +class TclCommandBounds(TclCommand): + """ + Tcl shell command to return the bounds values for a supplied list of objects (identified by their names). + example: + + """ + + # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon) + aliases = ['get_bounds', 'bounds'] + + # Dictionary of types from Tcl command, needs to be ordered + arg_names = collections.OrderedDict([ + ('objects', str) + ]) + + # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value + option_types = collections.OrderedDict([ + ]) + + # array of mandatory options for current Tcl command: required = {'name','outname'} + required = [] + + # structured help for current command, args needs to be ordered + help = { + 'main': "Will return a list of bounds values, each set of bound values is " + "a list itself: [xmin, ymin, xmax, ymax].", + 'args': collections.OrderedDict([ + ('objects', 'A list of object names separated by comma without spaces.'), + ]), + 'examples': ['bounds a_obj.GTL,b_obj.DRL'] + } + + def execute(self, args, unnamed_args): + """ + + :param args: + :param unnamed_args: + :return: + """ + + obj_list = list() + if 'objects' in args: + try: + obj_list = [str(obj_name) for obj_name in str(args['objects']).split(",") if obj_name != ''] + except AttributeError as e: + log.debug("TclCommandBounds.execute --> %s" % str(e)) + + if not obj_list: + self.raise_tcl_error('%s: %s:' % ( + _("Expected a list of objects names separated by comma. Got"), str(args['objects']))) + return 'fail' + else: + self.raise_tcl_error('%s: %s:' % ( + _("Expected a list of objects names separated by comma. Got"), str(args['objects']))) + return 'fail' + + result_list = list() + for name in obj_list: + obj = self.app.collection.get_by_name(name) + + xmin, ymin, xmax, ymax = obj.bounds() + result_list.append([xmin, ymin, xmax, ymax]) + + self.app.inform.emit('[success] %s ...' % + _('TclCommand Bounds done.')) + + return result_list diff --git a/tclCommands/TclCommandCopperClear.py b/tclCommands/TclCommandCopperClear.py index cc22a4f2..0f93169e 100644 --- a/tclCommands/TclCommandCopperClear.py +++ b/tclCommands/TclCommandCopperClear.py @@ -136,6 +136,7 @@ class TclCommandCopperClear(TclCommand): tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != ''] except AttributeError: tools = [float(tooldia)] + # store here the default data for Geometry Data default_data = {} default_data.update({ diff --git a/tclCommands/TclCommandSetOrigin.py b/tclCommands/TclCommandSetOrigin.py index 01cfe944..2415d6a6 100644 --- a/tclCommands/TclCommandSetOrigin.py +++ b/tclCommands/TclCommandSetOrigin.py @@ -14,7 +14,7 @@ if '_' not in builtins.__dict__: class TclCommandSetOrigin(TclCommand): """ - Tcl shell command to clear the text in the Tcl Shell browser. + Tcl shell command to set the origin to zero or to a specified location for all loaded objects in FlatCAM. example: @@ -55,7 +55,6 @@ class TclCommandSetOrigin(TclCommand): :return: """ - print(args) loc = list() if 'auto' in args: if args['auto'] == 1: diff --git a/tclCommands/__init__.py b/tclCommands/__init__.py index 9899e8b4..8295926f 100644 --- a/tclCommands/__init__.py +++ b/tclCommands/__init__.py @@ -10,6 +10,7 @@ import tclCommands.TclCommandAddRectangle import tclCommands.TclCommandAlignDrill import tclCommands.TclCommandAlignDrillGrid import tclCommands.TclCommandBbox +import tclCommands.TclCommandBounds import tclCommands.TclCommandClearShell import tclCommands.TclCommandCncjob import tclCommands.TclCommandCopperClear