- upgraded the mirror Tcl command to work on a selection of objects and also fixed a plot issue

This commit is contained in:
Marius Stanciu
2022-02-01 13:59:54 +02:00
committed by Marius
parent a314115448
commit 642550b1bc
2 changed files with 103 additions and 57 deletions

View File

@@ -15,6 +15,7 @@ CHANGELOG for FlatCAM beta
- minor changes in `cncjob` Tcl command - minor changes in `cncjob` Tcl command
- updated the `offset` and `scale` Tcl commands to work on a selection of objects - 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' - 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 31.02.2022

View File

@@ -17,7 +17,7 @@ class TclCommandMirror(TclCommandSignaled):
# Dictionary of types from Tcl command, needs to be ordered. # Dictionary of types from Tcl command, needs to be ordered.
# For positional arguments # For positional arguments
arg_names = collections.OrderedDict([ arg_names = collections.OrderedDict([
('name', str)
]) ])
# Dictionary of types from Tcl command, needs to be ordered. # 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'} # array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name'] required = []
# structured help for current command, args needs to be ordered # structured help for current command, args needs to be ordered
help = { 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([ '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.'), ('axis', 'Mirror axis parallel to the X or Y axis.'),
('box', 'Name of object which act as box (cutout for example.)'), ('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' ('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' 'The reference point can be:\n'
'WARNING: no spaces are allowed. If uncertain enclose the two values inside parenthesis.\n' '- "origin" which means point (0, 0)\n'
'See the example.') '- "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): def execute(self, args, unnamed_args):
@@ -56,19 +61,46 @@ class TclCommandMirror(TclCommandSignaled):
:return: None or exception :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"
# 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))
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)
for name in obj_names:
# Get source object. # Get source object.
try: try:
obj = self.app.collection.get_by_name(str(name)) obj = self.app.collection.get_by_name(str(name))
except Exception: except Exception:
return "Could not retrieve object: %s" % name self.app.log.error("Could not retrieve object: %s" % name)
return "fail"
if obj is None: if obj is None:
return "Object not found: %s" % name self.app.log.error("Object not found: %s" % name)
return "fail"
if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon': if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon':
return "ERROR: Only Gerber, Excellon and Geometry objects can be mirrored." self.app.log.error("ERROR: Only Gerber, Excellon and Geometry objects can be mirrored.")
return "fail"
# Axis # Axis
if 'axis' in args: if 'axis' in args:
@@ -84,24 +116,35 @@ class TclCommandMirror(TclCommandSignaled):
try: try:
box = self.app.collection.get_by_name(args['box']) box = self.app.collection.get_by_name(args['box'])
except Exception: except Exception:
return "Could not retrieve object: %s" % args['box'] self.app.log.error("Could not retrieve object: %s" % args['box'])
return "fail"
if box is None: if box is None:
return "Object box not found: %s" % args['box'] self.app.log.error("Object box not found: %s" % args['box'])
return "fail"
try: try:
xmin, ymin, xmax, ymax = box.bounds() xmin_b, ymin_b, xmax_b, ymax_b = box.bounds()
px = 0.5 * (xmin + xmax) px = 0.5 * (xmin_b + xmax_b)
py = 0.5 * (ymin + ymax) py = 0.5 * (ymin_b + ymax_b)
obj.mirror(axis, [px, py]) obj.mirror(axis, [px, py])
obj.plot() continue
return
except Exception as e: except Exception as e:
return "Operation failed: %s" % str(e) self.app.log.error("Operation failed: %s" % str(e))
return "fail"
# Origin # Origin
if 'origin' in args: 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: try:
origin_val = eval(args['origin']) origin_val = eval(args['origin'])
x = float(origin_val[0]) x = float(origin_val[0])
@@ -109,9 +152,11 @@ class TclCommandMirror(TclCommandSignaled):
except KeyError: except KeyError:
x, y = (0, 0) x, y = (0, 0)
except ValueError: except ValueError:
return "Invalid distance: %s" % str(args['origin']) self.app.log.error("Invalid distance: %s" % str(args['origin']))
return "fail"
try: try:
obj.mirror(axis, [x, y]) obj.mirror(axis, [x, y])
except Exception as e: except Exception as e:
return "Operation failed: %s" % str(e) self.app.log.error("Operation failed: %s" % str(e))
return "fail"