- added a new Tcl command: buffer which will buffer the geometry of an object or will scale individually each geometry sub element

- fixed the buffer() method for the Excellon objects (the resulting tool diameters were calculated less than the what was expected)
This commit is contained in:
Marius Stanciu
2022-01-21 02:37:34 +02:00
committed by Marius
parent ba8b16e54f
commit b0db73029d
8 changed files with 176 additions and 77 deletions

View File

@@ -0,0 +1,112 @@
from tclCommands.TclCommand import TclCommand
import collections
import logging
import gettext
import appTranslation as fcTranslate
import builtins
fcTranslate.apply_language('strings')
if '_' not in builtins.__dict__:
_ = gettext.gettext
log = logging.getLogger('base')
class TclCommandBuffer(TclCommand):
"""
Tcl shell command to buffer the object by a distance or to scale each geometric element using the center of
its individual bounding box as a reference.
example:
# buffer each geometric element at the distance 4.2 in the my_geo Geometry obj
buffer my_geo -distance 4.2
# scale each geo element by a factor of 4.2 in the my_geo Geometry obj and the join is 2 (square)
buffer my_geo -distance 4.2 -factor True -join 2
"""
# List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
aliases = ['buffer']
description = '%s %s' % ("--", "Will buffer the geometry of a named object. Does not create a new object.")
# Dictionary of types from Tcl command, needs to be ordered
arg_names = collections.OrderedDict([
('name', str)
])
# Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
option_types = collections.OrderedDict([
('dist', float),
('join', str),
('factor', str)
])
# array of mandatory options for current Tcl command: required = {'name','outname'}
required = ['name', 'distance']
# structured help for current command, args needs to be ordered
help = {
'main': "Works only on Geometry objects.\n"
"Buffer the object by a distance or to scale each geometric element using \n"
"the center of its individual bounding box as a reference.\n"
"If 'factor' is True(1) then 'dist' is the scale factor for each geometric element.",
'args': collections.OrderedDict([
('name', 'Name of the Geometry object to be buffered. Required.'),
('dist', 'Distance to which to buffer each geometric element.'),
('join', 'How two lines join and make a corner: round (1), square (2) or bevel (3). Default is: round'),
('factor', "If 'factor' is True(1) then 'distance' parameter\n"
"is the scale factor for each geometric element")
]),
'examples': [
'# buffer each geometric element at the distance 4.2 in the my_geo Geometry obj'
'buffer my_geo -dist 4.2 ',
'# scale each geo element by a factor of 4.2 in the my_geo Geometry obj',
'buffer my_geo -dist 4.2 -factor True',
'buffer my_geo -dist 4.2 -factor 1',
'# scale each geo element by a factor of 4.2 in the my_geo Geometry obj and the join is 2 (square)',
'buffer my_geo -distance 4.2 -factor True -join 2'
]
}
def execute(self, args, unnamed_args):
"""
:param args:
:param unnamed_args:
:return:
"""
name = args['name']
try:
obj_to_buff = self.app.collection.get_by_name(name)
except Exception as e:
self.app.log.error("TclCommandCopperBuffer.execute() --> %s" % str(e))
self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
return "Could not retrieve object: %s" % name
if obj_to_buff.kind not in ['geometry', 'excellon', 'gerber']:
self.app.log.error("%s: %s %s" % ("The object", name, "can be only of type Geometry, Gerber or Excellon."))
self.raise_tcl_error(
"%s: %s %s" % ("The object", name, "can be only of type Geometry, Gerber or Excellon."))
return "%s: %s %s" % ("The object", name, "can be only of type Geometry, Gerber or Excellon.")
if 'dist' not in args:
self.raise_tcl_error('%s' % _("Expected -dist <value>"))
return 'fail'
distance = args['dist']
if 'join' not in args:
join = 1
else:
if args['join'] in ['square', 'Square', '2', 2]:
join = 2
elif args['join'] in ['bevel', 'Bevel', '3', 3]:
join = 3
else:
join = 1
factor = bool(eval(str(args['factor']).capitalize())) if 'factor' in args else None
obj_to_buff.buffer(distance, join, factor, only_exterior=True)

View File

@@ -77,7 +77,7 @@ class TclCommandScale(TclCommand):
try:
obj_to_scale = self.app.collection.get_by_name(name)
except Exception as e:
log.error("TclCommandCopperClear.execute() --> %s" % str(e))
self.app.log.error("TclCommandCopperScale.execute() --> %s" % str(e))
self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
return "Could not retrieve object: %s" % name

View File

@@ -13,6 +13,7 @@ import tclCommands.TclCommandAlignDrill
import tclCommands.TclCommandAlignDrillGrid
import tclCommands.TclCommandBbox
import tclCommands.TclCommandBounds
import tclCommands.TclCommandBuffer
import tclCommands.TclCommandClearShell
import tclCommands.TclCommandCncjob
import tclCommands.TclCommandCopperClear