From 642550b1bca05d719d29363b0a54b6b0bf26a126 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Tue, 1 Feb 2022 13:59:54 +0200 Subject: [PATCH] - upgraded the `mirror` Tcl command to work on a selection of objects and also fixed a plot issue --- CHANGELOG.md | 1 + tclCommands/TclCommandMirror.py | 159 ++++++++++++++++++++------------ 2 files changed, 103 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7c56a15..92a045c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ CHANGELOG for FlatCAM beta - minor changes in `cncjob` Tcl command - updated the `offset` and `scale` Tcl commands to work on a selection of objects - minor changed in camlib.py regarding the self.check_zcut() method setting the self.z_cut to 'fail' +- upgraded the `mirror` Tcl command to work on a selection of objects and also fixed a plot issue 31.02.2022 diff --git a/tclCommands/TclCommandMirror.py b/tclCommands/TclCommandMirror.py index 190c2857..0aaa9248 100644 --- a/tclCommands/TclCommandMirror.py +++ b/tclCommands/TclCommandMirror.py @@ -17,7 +17,7 @@ class TclCommandMirror(TclCommandSignaled): # Dictionary of types from Tcl command, needs to be ordered. # For positional arguments arg_names = collections.OrderedDict([ - ('name', str) + ]) # Dictionary of types from Tcl command, needs to be ordered. @@ -29,21 +29,26 @@ class TclCommandMirror(TclCommandSignaled): ]) # array of mandatory options for current Tcl command: required = {'name','outname'} - required = ['name'] + required = [] # structured help for current command, args needs to be ordered help = { - 'main': "Will mirror the geometry of a named object. Does not create a new object.", + 'main': "Will mirror the geometry of a named object. Does not create a new object.\n" + "The names of the objects to be scaled will be entered after the command,\n" + "separated by spaces. See the example below.\n" + "WARNING: if the name of an object has spaces, enclose the name with quotes.", 'args': collections.OrderedDict([ - ('name', 'Name of the object (Gerber, Geometry or Excellon) to be mirrored. Required.'), ('axis', 'Mirror axis parallel to the X or Y axis.'), ('box', 'Name of object which act as box (cutout for example.)'), ('origin', 'Reference point . It is used only if the box is not used. Format (x,y).\n' - 'Comma will separate the X and Y coordinates.\n' - 'WARNING: no spaces are allowed. If uncertain enclose the two values inside parenthesis.\n' - 'See the example.') + 'The reference point can be:\n' + '- "origin" which means point (0, 0)\n' + '- "min_bounds" which means the lower left point of the bounding box made for all objects\n' + '- "center" which means the center point of the bounding box made for all objects.\n' + '- a point in format (x,y) with the X and Y coordinates separated by a comma. NO SPACES ALLOWED') ]), - 'examples': ['mirror obj_name -box box_geo -axis X -origin 3.2,4.7'] + 'examples': ['mirror obj_name -box box_geo -axis X', + 'mirror obj_name -axis X -origin 3.2,4.7'] } def execute(self, args, unnamed_args): @@ -56,62 +61,102 @@ class TclCommandMirror(TclCommandSignaled): :return: None or exception """ - name = args['name'] + obj_names = unnamed_args + if not obj_names: + self.app.log.error("Missing objects to be offset. Exiting.") + return "fail" - # Get source object. - try: + # calculate the bounds + minx_lst = [] + miny_lst = [] + maxx_lst = [] + maxy_lst = [] + for name in obj_names: obj = self.app.collection.get_by_name(str(name)) - except Exception: - return "Could not retrieve object: %s" % name + if obj is None or obj == '': + self.app.log.error("Object not found: %s" % name) + return "fail" + a, b, c, d = obj.bounds() + minx_lst.append(a) + miny_lst.append(b) + maxx_lst.append(c) + maxy_lst.append(d) + xmin = min(minx_lst) + ymin = min(miny_lst) + xmax = max(maxx_lst) + ymax = max(maxy_lst) - if obj is None: - return "Object not found: %s" % name - - if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon': - return "ERROR: Only Gerber, Excellon and Geometry objects can be mirrored." - - # Axis - if 'axis' in args: + for name in obj_names: + # Get source object. try: - axis = args['axis'].upper() - except KeyError: - axis = 'Y' - else: - axis = 'Y' - - # Box - if 'box' in args: - try: - box = self.app.collection.get_by_name(args['box']) + obj = self.app.collection.get_by_name(str(name)) except Exception: - return "Could not retrieve object: %s" % args['box'] + self.app.log.error("Could not retrieve object: %s" % name) + return "fail" - if box is None: - return "Object box not found: %s" % args['box'] + if obj is None: + self.app.log.error("Object not found: %s" % name) + return "fail" - try: - xmin, ymin, xmax, ymax = box.bounds() - px = 0.5 * (xmin + xmax) - py = 0.5 * (ymin + ymax) + if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon': + self.app.log.error("ERROR: Only Gerber, Excellon and Geometry objects can be mirrored.") + return "fail" - obj.mirror(axis, [px, py]) - obj.plot() - return - except Exception as e: - return "Operation failed: %s" % str(e) + # Axis + if 'axis' in args: + try: + axis = args['axis'].upper() + except KeyError: + axis = 'Y' + else: + axis = 'Y' - # Origin - if 'origin' in args: - try: - origin_val = eval(args['origin']) - x = float(origin_val[0]) - y = float(origin_val[1]) - except KeyError: - x, y = (0, 0) - except ValueError: - return "Invalid distance: %s" % str(args['origin']) + # Box + if 'box' in args: + try: + box = self.app.collection.get_by_name(args['box']) + except Exception: + self.app.log.error("Could not retrieve object: %s" % args['box']) + return "fail" - try: - obj.mirror(axis, [x, y]) - except Exception as e: - return "Operation failed: %s" % str(e) + if box is None: + self.app.log.error("Object box not found: %s" % args['box']) + return "fail" + + try: + xmin_b, ymin_b, xmax_b, ymax_b = box.bounds() + px = 0.5 * (xmin_b + xmax_b) + py = 0.5 * (ymin_b + ymax_b) + + obj.mirror(axis, [px, py]) + continue + except Exception as e: + self.app.log.error("Operation failed: %s" % str(e)) + return "fail" + + # Origin + if 'origin' in args: + if args['origin'] == 'origin': + x, y = (0, 0) + elif args['origin'] == 'min_bounds': + x, y = (xmin, ymin) + elif args['origin'] == 'center': + c_x = xmin + (xmax - xmin) / 2 + c_y = ymin + (ymax - ymin) / 2 + x, y = (c_x, c_y) + else: + try: + origin_val = eval(args['origin']) + x = float(origin_val[0]) + y = float(origin_val[1]) + except KeyError: + x, y = (0, 0) + except ValueError: + self.app.log.error("Invalid distance: %s" % str(args['origin'])) + return "fail" + + try: + obj.mirror(axis, [x, y]) + except Exception as e: + self.app.log.error("Operation failed: %s" % str(e)) + return "fail"