2019-10-13 15:13:39 +00:00
|
|
|
# ##########################################################
|
|
|
|
# FlatCAM: 2D Post-processing for Manufacturing #
|
|
|
|
# File Author: Marius Adrian Stanciu (c) #
|
|
|
|
# Date: 8/17/2019 #
|
|
|
|
# MIT Licence #
|
|
|
|
# ##########################################################
|
|
|
|
|
2019-09-23 00:35:32 +00:00
|
|
|
from tclCommands.TclCommand import TclCommand
|
2019-10-24 23:10:52 +00:00
|
|
|
|
|
|
|
import collections
|
2019-09-23 00:35:32 +00:00
|
|
|
|
|
|
|
from camlib import get_bounds
|
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
import logging
|
2019-09-23 00:35:32 +00:00
|
|
|
import gettext
|
|
|
|
import FlatCAMTranslation as fcTranslate
|
|
|
|
import builtins
|
|
|
|
|
|
|
|
fcTranslate.apply_language('strings')
|
|
|
|
if '_' not in builtins.__dict__:
|
|
|
|
_ = gettext.gettext
|
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
log = logging.getLogger('base')
|
|
|
|
|
2019-09-23 00:35:32 +00:00
|
|
|
|
|
|
|
class TclCommandSetOrigin(TclCommand):
|
|
|
|
"""
|
2019-09-23 01:07:24 +00:00
|
|
|
Tcl shell command to set the origin to zero or to a specified location for all loaded objects in FlatCAM.
|
2019-09-23 00:35:32 +00:00
|
|
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
|
|
|
|
aliases = ['set_origin', 'origin']
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Set the origin at the specified x,y location.")
|
|
|
|
|
2019-09-23 00:35:32 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered
|
|
|
|
arg_names = collections.OrderedDict([
|
|
|
|
('loc', str)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
|
|
|
|
option_types = collections.OrderedDict([
|
2020-04-13 16:15:20 +00:00
|
|
|
('auto', str)
|
2019-09-23 00:35:32 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# array of mandatory options for current Tcl command: required = {'name','outname'}
|
|
|
|
required = []
|
|
|
|
|
|
|
|
# structured help for current command, args needs to be ordered
|
|
|
|
help = {
|
2020-04-09 01:13:04 +00:00
|
|
|
'main': "Will set the origin at the specified x,y location.\n"
|
|
|
|
"If it is called without arguments it will set origin at (0, 0)",
|
2019-09-23 00:35:32 +00:00
|
|
|
'args': collections.OrderedDict([
|
2020-04-09 01:13:04 +00:00
|
|
|
('loc', 'Location to offset all the selected objects. NO SPACES ALLOWED in X and Y pair.\n'
|
|
|
|
'Use like this: 2,3'),
|
2019-12-08 20:11:39 +00:00
|
|
|
('auto', 'If set to True it will set the origin to the minimum x, y of the object selection bounding box.'
|
2020-04-13 16:15:20 +00:00
|
|
|
'-auto=True is not correct but -auto 1 or -auto True is correct. True (1) or False (0).')
|
2019-09-23 00:35:32 +00:00
|
|
|
]),
|
2020-04-09 01:13:04 +00:00
|
|
|
'examples': ['set_origin 3,2', 'set_origin -auto 1', 'origin']
|
2019-09-23 00:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
|
|
|
|
:param args:
|
|
|
|
:param unnamed_args:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2020-02-29 22:52:24 +00:00
|
|
|
loc = []
|
2019-09-23 00:35:32 +00:00
|
|
|
if 'auto' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
if bool(args['auto']) is True:
|
2019-09-23 00:35:32 +00:00
|
|
|
objs = self.app.collection.get_list()
|
|
|
|
minx, miny, __, ___ = get_bounds(objs)
|
|
|
|
|
|
|
|
loc.append(0 - minx)
|
|
|
|
loc.append(0 - miny)
|
|
|
|
else:
|
|
|
|
loc = [0, 0]
|
|
|
|
elif 'loc' in args:
|
|
|
|
try:
|
|
|
|
location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
|
|
|
|
except AttributeError as e:
|
|
|
|
log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
|
|
|
|
location = (0, 0)
|
|
|
|
|
|
|
|
loc.append(location[0])
|
|
|
|
loc.append(location[1])
|
|
|
|
|
|
|
|
if len(location) != 2:
|
|
|
|
self.raise_tcl_error('%s: %s' % (
|
|
|
|
_("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
|
|
|
|
return 'fail'
|
|
|
|
else:
|
|
|
|
loc = [0, 0]
|
|
|
|
|
|
|
|
self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
|
|
|
|
self.app.inform.emit('[success] Tcl %s: %s' %
|
|
|
|
(_('Origin set by offsetting all loaded objects with '),
|
|
|
|
'{0:.4f}, {0:.4f}'.format(loc[0], loc[1])))
|