- 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,62 +61,102 @@ 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"
# Get source object. # calculate the bounds
try: minx_lst = []
miny_lst = []
maxx_lst = []
maxy_lst = []
for name in obj_names:
obj = self.app.collection.get_by_name(str(name)) obj = self.app.collection.get_by_name(str(name))
except Exception: if obj is None or obj == '':
return "Could not retrieve object: %s" % name 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: for name in obj_names:
return "Object not found: %s" % name # Get source object.
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:
try: try:
axis = args['axis'].upper() obj = self.app.collection.get_by_name(str(name))
except KeyError:
axis = 'Y'
else:
axis = 'Y'
# Box
if 'box' in args:
try:
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" % name)
return "fail"
if box is None: if obj is None:
return "Object box not found: %s" % args['box'] self.app.log.error("Object not found: %s" % name)
return "fail"
try: if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon':
xmin, ymin, xmax, ymax = box.bounds() self.app.log.error("ERROR: Only Gerber, Excellon and Geometry objects can be mirrored.")
px = 0.5 * (xmin + xmax) return "fail"
py = 0.5 * (ymin + ymax)
obj.mirror(axis, [px, py]) # Axis
obj.plot() if 'axis' in args:
return try:
except Exception as e: axis = args['axis'].upper()
return "Operation failed: %s" % str(e) except KeyError:
axis = 'Y'
else:
axis = 'Y'
# Origin # Box
if 'origin' in args: if 'box' in args:
try: try:
origin_val = eval(args['origin']) box = self.app.collection.get_by_name(args['box'])
x = float(origin_val[0]) except Exception:
y = float(origin_val[1]) self.app.log.error("Could not retrieve object: %s" % args['box'])
except KeyError: return "fail"
x, y = (0, 0)
except ValueError:
return "Invalid distance: %s" % str(args['origin'])
try: if box is None:
obj.mirror(axis, [x, y]) self.app.log.error("Object box not found: %s" % args['box'])
except Exception as e: return "fail"
return "Operation failed: %s" % str(e)
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"