set rules for TCL shell commands
implement TCL shell rules for: drillcncjob, millholes(renamed from drillcncjobgeometry), exteriors, interiors, isolate
This commit is contained in:
parent
8855a3ffdb
commit
4f2f989bdf
170
FlatCAMApp.py
170
FlatCAMApp.py
|
@ -2494,8 +2494,12 @@ class App(QtCore.QObject):
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
def drillcncjob(name=None, *args):
|
def drillcncjob(name=None, *args):
|
||||||
# name should not be none, but we set it to default, because TCL return error without reason if argument is missing
|
'''
|
||||||
# we should check it inside shell commamnd instead
|
TCL shell command - see help section
|
||||||
|
:param name: name of object
|
||||||
|
:param args: array of arguments
|
||||||
|
:return: "Ok" if completed without errors
|
||||||
|
'''
|
||||||
a, kwa = h(*args)
|
a, kwa = h(*args)
|
||||||
types = {'tools': str,
|
types = {'tools': str,
|
||||||
'outname': str,
|
'outname': str,
|
||||||
|
@ -2512,7 +2516,10 @@ class App(QtCore.QObject):
|
||||||
for key in kwa:
|
for key in kwa:
|
||||||
if key not in types:
|
if key not in types:
|
||||||
self.raiseTclError('Unknown parameter: %s' % key)
|
self.raiseTclError('Unknown parameter: %s' % key)
|
||||||
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
kwa[key] = types[key](kwa[key])
|
||||||
|
except Exception, e:
|
||||||
|
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, str(types[key])))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = self.collection.get_by_name(str(name))
|
obj = self.collection.get_by_name(str(name))
|
||||||
|
@ -2523,27 +2530,21 @@ class App(QtCore.QObject):
|
||||||
self.raiseTclError('Object not found: %s' % name)
|
self.raiseTclError('Object not found: %s' % name)
|
||||||
|
|
||||||
if not isinstance(obj, FlatCAMExcellon):
|
if not isinstance(obj, FlatCAMExcellon):
|
||||||
self.raiseTclError('Only Excellon objects can be drilled: %s' % name)
|
self.raiseTclError('Only Excellon objects can be drilled, got %s %s.' % (name, type(obj)))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# Get the tools from the list
|
# Get the tools from the list
|
||||||
job_name = kwa["outname"]
|
job_name = kwa["outname"]
|
||||||
|
|
||||||
# Object initialization function for app.new_object()
|
# Object initialization function for app.new_object()
|
||||||
def job_init(job_obj, app_obj):
|
def job_init(job_obj, app_obj):
|
||||||
assert isinstance(job_obj, FlatCAMCNCjob), \
|
|
||||||
"Initializer expected FlatCAMCNCjob, got %s" % type(job_obj)
|
|
||||||
|
|
||||||
job_obj.z_cut = kwa["drillz"]
|
job_obj.z_cut = kwa["drillz"]
|
||||||
job_obj.z_move = kwa["travelz"]
|
job_obj.z_move = kwa["travelz"]
|
||||||
job_obj.feedrate = kwa["feedrate"]
|
job_obj.feedrate = kwa["feedrate"]
|
||||||
job_obj.spindlespeed = kwa["spindlespeed"] if "spindlespeed" in kwa else None
|
job_obj.spindlespeed = kwa["spindlespeed"] if "spindlespeed" in kwa else None
|
||||||
toolchange = True if "toolchange" in kwa and kwa["toolchange"] == 1 else False
|
toolchange = True if "toolchange" in kwa and kwa["toolchange"] == 1 else False
|
||||||
job_obj.generate_from_excellon_by_tool(obj, kwa["tools"], toolchange)
|
job_obj.generate_from_excellon_by_tool(obj, kwa["tools"], toolchange)
|
||||||
|
|
||||||
job_obj.gcode_parse()
|
job_obj.gcode_parse()
|
||||||
|
|
||||||
job_obj.create_geometry()
|
job_obj.create_geometry()
|
||||||
|
|
||||||
obj.app.new_object("cncjob", job_name, job_init)
|
obj.app.new_object("cncjob", job_name, job_init)
|
||||||
|
@ -2553,65 +2554,87 @@ class App(QtCore.QObject):
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
def drillmillgeometry(name, *args):
|
def millholes(name=None, *args):
|
||||||
|
'''
|
||||||
|
TCL shell command - see help section
|
||||||
|
:param name: name of object
|
||||||
|
:param args: array of arguments
|
||||||
|
:return: "Ok" if completed without errors
|
||||||
|
'''
|
||||||
a, kwa = h(*args)
|
a, kwa = h(*args)
|
||||||
types = {'tooldia': float,
|
types = {'tooldia': float,
|
||||||
'tools': str,
|
'tools': str,
|
||||||
'outname': str}
|
'outname': str}
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
self.raiseTclError('Argument name is missing.')
|
||||||
|
|
||||||
for key in kwa:
|
for key in kwa:
|
||||||
if key not in types:
|
if key not in types:
|
||||||
return 'Unknown parameter: %s' % key
|
self.raiseTclError('Unknown parameter: %s' % key)
|
||||||
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
kwa[key] = types[key](kwa[key])
|
||||||
|
except Exception, e:
|
||||||
|
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if 'tools' in kwa:
|
if 'tools' in kwa:
|
||||||
kwa['tools'] = [x.strip() for x in kwa['tools'].split(",")]
|
kwa['tools'] = [x.strip() for x in kwa['tools'].split(",")]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "Bad tools: %s" % str(e)
|
self.raiseTclError("Bad tools: %s" % str(e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = self.collection.get_by_name(str(name))
|
obj = self.collection.get_by_name(str(name))
|
||||||
except:
|
except:
|
||||||
return "Could not retrieve object: %s" % name
|
self.raiseTclError("Could not retrieve object: %s" % name)
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return "Object not found: %s" % name
|
self.raiseTclError("Object not found: %s" % name)
|
||||||
|
|
||||||
assert isinstance(obj, FlatCAMExcellon), \
|
if not isinstance(obj, FlatCAMExcellon):
|
||||||
"Expected a FlatCAMExcellon object, got %s" % type(obj)
|
self.raiseTclError('Only Excellon objects can be mill drilled, got %s %s.' % (name, type(obj)))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
success, msg = obj.generate_milling(**kwa)
|
success, msg = obj.generate_milling(**kwa)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "Operation failed: %s" % str(e)
|
self.raiseTclError("Operation failed: %s" % str(e))
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
return msg
|
self.raiseTclError(msg)
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
def exteriors(obj_name, *args):
|
def exteriors(name=None, *args):
|
||||||
|
'''
|
||||||
|
TCL shell command - see help section
|
||||||
|
:param name: name of object
|
||||||
|
:param args: array of arguments
|
||||||
|
:return: "Ok" if completed without errors
|
||||||
|
'''
|
||||||
a, kwa = h(*args)
|
a, kwa = h(*args)
|
||||||
types = {'outname': str}
|
types = {'outname': str}
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
self.raiseTclError('Argument name is missing.')
|
||||||
|
|
||||||
for key in kwa:
|
for key in kwa:
|
||||||
if key not in types:
|
if key not in types:
|
||||||
return 'Unknown parameter: %s' % key
|
self.raiseTclError('Unknown parameter: %s' % key)
|
||||||
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
kwa[key] = types[key](kwa[key])
|
||||||
|
except Exception, e:
|
||||||
|
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = self.collection.get_by_name(str(obj_name))
|
obj = self.collection.get_by_name(str(name))
|
||||||
except:
|
except:
|
||||||
return "Could not retrieve object: %s" % obj_name
|
self.raiseTclError("Could not retrieve object: %s" % name)
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return "Object not found: %s" % obj_name
|
self.raiseTclError("Object not found: %s" % name)
|
||||||
|
|
||||||
assert isinstance(obj, Geometry), \
|
if not isinstance(obj, Geometry):
|
||||||
"Expected a Geometry, got %s" % type(obj)
|
self.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
|
||||||
|
|
||||||
obj_exteriors = obj.get_exteriors()
|
|
||||||
|
|
||||||
def geo_init(geo_obj, app_obj):
|
def geo_init(geo_obj, app_obj):
|
||||||
geo_obj.solid_geometry = obj_exteriors
|
geo_obj.solid_geometry = obj_exteriors
|
||||||
|
@ -2619,36 +2642,47 @@ class App(QtCore.QObject):
|
||||||
if 'outname' in kwa:
|
if 'outname' in kwa:
|
||||||
outname = kwa['outname']
|
outname = kwa['outname']
|
||||||
else:
|
else:
|
||||||
outname = obj_name + ".exteriors"
|
outname = name + ".exteriors"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
obj_exteriors = obj.get_exteriors()
|
||||||
self.new_object('geometry', outname, geo_init)
|
self.new_object('geometry', outname, geo_init)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "Failed: %s" % str(e)
|
self.raiseTclError("Failed: %s" % str(e))
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
def interiors(obj_name, *args):
|
def interiors(name=None, *args):
|
||||||
|
'''
|
||||||
|
TCL shell command - see help section
|
||||||
|
:param name: name of object
|
||||||
|
:param args: array of arguments
|
||||||
|
:return: "Ok" if completed without errors
|
||||||
|
'''
|
||||||
a, kwa = h(*args)
|
a, kwa = h(*args)
|
||||||
types = {}
|
types = {'outname': str}
|
||||||
|
|
||||||
for key in kwa:
|
for key in kwa:
|
||||||
if key not in types:
|
if key not in types:
|
||||||
return 'Unknown parameter: %s' % key
|
self.raiseTclError('Unknown parameter: %s' % key)
|
||||||
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
kwa[key] = types[key](kwa[key])
|
||||||
|
except Exception, e:
|
||||||
|
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
self.raiseTclError('Argument name is missing.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj = self.collection.get_by_name(str(obj_name))
|
obj = self.collection.get_by_name(str(name))
|
||||||
except:
|
except:
|
||||||
return "Could not retrieve object: %s" % obj_name
|
self.raiseTclError("Could not retrieve object: %s" % name)
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return "Object not found: %s" % obj_name
|
self.raiseTclError("Object not found: %s" % name)
|
||||||
|
|
||||||
assert isinstance(obj, Geometry), \
|
if not isinstance(obj, Geometry):
|
||||||
"Expected a Geometry, got %s" % type(obj)
|
self.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
|
||||||
|
|
||||||
obj_interiors = obj.get_interiors()
|
|
||||||
|
|
||||||
def geo_init(geo_obj, app_obj):
|
def geo_init(geo_obj, app_obj):
|
||||||
geo_obj.solid_geometry = obj_interiors
|
geo_obj.solid_geometry = obj_interiors
|
||||||
|
@ -2656,16 +2690,23 @@ class App(QtCore.QObject):
|
||||||
if 'outname' in kwa:
|
if 'outname' in kwa:
|
||||||
outname = kwa['outname']
|
outname = kwa['outname']
|
||||||
else:
|
else:
|
||||||
outname = obj_name + ".interiors"
|
outname = name + ".interiors"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
obj_interiors = obj.get_interiors()
|
||||||
self.new_object('geometry', outname, geo_init)
|
self.new_object('geometry', outname, geo_init)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return "Failed: %s" % str(e)
|
self.raiseTclError("Failed: %s" % str(e))
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
def isolate(name, *args):
|
def isolate(name=None, *args):
|
||||||
|
'''
|
||||||
|
TCL shell command - see help section
|
||||||
|
:param name: name of object
|
||||||
|
:param args: array of arguments
|
||||||
|
:return: "Ok" if completed without errors
|
||||||
|
'''
|
||||||
a, kwa = h(*args)
|
a, kwa = h(*args)
|
||||||
types = {'dia': float,
|
types = {'dia': float,
|
||||||
'passes': int,
|
'passes': int,
|
||||||
|
@ -2675,24 +2716,29 @@ class App(QtCore.QObject):
|
||||||
|
|
||||||
for key in kwa:
|
for key in kwa:
|
||||||
if key not in types:
|
if key not in types:
|
||||||
return 'Unknown parameter: %s' % key
|
self.raiseTclError('Unknown parameter: %s' % key)
|
||||||
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
kwa[key] = types[key](kwa[key])
|
||||||
|
except Exception, e:
|
||||||
|
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
|
||||||
try:
|
try:
|
||||||
obj = self.collection.get_by_name(str(name))
|
obj = self.collection.get_by_name(str(name))
|
||||||
except:
|
except:
|
||||||
return "Could not retrieve object: %s" % name
|
self.raiseTclError("Could not retrieve object: %s" % name)
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return "Object not found: %s" % name
|
self.raiseTclError("Object not found: %s" % name)
|
||||||
|
|
||||||
assert isinstance(obj, FlatCAMGerber), \
|
assert isinstance(obj, FlatCAMGerber), \
|
||||||
"Expected a FlatCAMGerber, got %s" % type(obj)
|
"Expected a FlatCAMGerber, got %s" % type(obj)
|
||||||
|
|
||||||
|
if not isinstance(obj, FlatCAMGerber):
|
||||||
|
self.raiseTclError('Expected FlatCAMGerber, got %s %s.' % (name, type(obj)))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
obj.isolate(**kwa)
|
obj.isolate(**kwa)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
return "Operation failed: %s" % str(e)
|
self.raiseTclError("Operation failed: %s" % str(e))
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
|
@ -3071,6 +3117,34 @@ class App(QtCore.QObject):
|
||||||
|
|
||||||
return "ERROR: No such system parameter."
|
return "ERROR: No such system parameter."
|
||||||
|
|
||||||
|
'''
|
||||||
|
Howto implement TCL shell commands:
|
||||||
|
|
||||||
|
All parameters passed to command should be possible to set as None and test it afterwards.
|
||||||
|
This is because we need to see error caused in tcl,
|
||||||
|
if None value as default parameter is not allowed TCL will return empty error.
|
||||||
|
Use:
|
||||||
|
def mycommand(name=None,...):
|
||||||
|
|
||||||
|
Test it like this:
|
||||||
|
if name is None:
|
||||||
|
|
||||||
|
self.raiseTclError('Argument name is missing.')
|
||||||
|
|
||||||
|
When error ocurre, always use raiseTclError, never return "sometext" on error,
|
||||||
|
otherwise we will miss it and processing will silently continue.
|
||||||
|
Method raiseTclError pass error into TCL interpreter, then raise python exception,
|
||||||
|
which is catched in exec_command and displayed in TCL shell console with red background.
|
||||||
|
Error in console is displayed with TCL trace.
|
||||||
|
|
||||||
|
This behavior works only within main thread,
|
||||||
|
errors with promissed tasks can be catched and detected only with log.
|
||||||
|
TODO: this problem have to be addressed somehow, maybe rewrite promissing to be blocking somehow for TCL shell.
|
||||||
|
|
||||||
|
Kamil's comment: I will rewrite existing TCL commands from time to time to follow this rules.
|
||||||
|
|
||||||
|
'''
|
||||||
|
|
||||||
commands = {
|
commands = {
|
||||||
'help': {
|
'help': {
|
||||||
'fcn': shelp,
|
'fcn': shelp,
|
||||||
|
@ -3248,7 +3322,7 @@ class App(QtCore.QObject):
|
||||||
" toolchange: Enable tool changes (example: 1)\n"
|
" toolchange: Enable tool changes (example: 1)\n"
|
||||||
},
|
},
|
||||||
'millholes': {
|
'millholes': {
|
||||||
'fcn': drillmillgeometry,
|
'fcn': millholes,
|
||||||
'help': "Create Geometry Object for milling holes from Excellon.\n" +
|
'help': "Create Geometry Object for milling holes from Excellon.\n" +
|
||||||
"> millholes <name> -tools <str> -tooldia <float> -outname <str> \n" +
|
"> millholes <name> -tools <str> -tooldia <float> -outname <str> \n" +
|
||||||
" name: Name of the Excellon Object\n" +
|
" name: Name of the Excellon Object\n" +
|
||||||
|
|
Loading…
Reference in New Issue