TclCommandNregions.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from tclCommands.TclCommand import TclCommand
  2. from shapely.ops import unary_union
  3. import collections
  4. import gettext
  5. import appTranslation as fcTranslate
  6. import builtins
  7. fcTranslate.apply_language('strings')
  8. if '_' not in builtins.__dict__:
  9. _ = gettext.gettext
  10. class TclCommandNregions(TclCommand):
  11. """
  12. Tcl shell command to follow a Gerber file
  13. """
  14. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  15. aliases = ['non_copper_regions', 'ncr']
  16. description = '%s %s' % ("--", "Creates a Geometry object with the non-copper regions.")
  17. # dictionary of types from Tcl command, needs to be ordered
  18. arg_names = collections.OrderedDict([
  19. ('name', str)
  20. ])
  21. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  22. option_types = collections.OrderedDict([
  23. ('outname', str),
  24. ('margin', float),
  25. ('rounded', str)
  26. ])
  27. # array of mandatory options for current Tcl command: required = {'name','outname'}
  28. required = ['name']
  29. # structured help for current command, args needs to be ordered
  30. help = {
  31. 'main': "Creates a Geometry object with the non-copper regions.",
  32. 'args': collections.OrderedDict([
  33. ('name', 'Object name for which to create non-copper regions. String. Required.'),
  34. ('outname', 'Name of the resulting Geometry object. String.'),
  35. ('margin', "Specify the edge of the PCB by drawing a box around all objects with this minimum distance. "
  36. "Float number."),
  37. ('rounded', "Resulting geometry will have rounded corners. True (1) or False (0).")
  38. ]),
  39. 'examples': ['ncr name -margin 0.1 -rounded True -outname name_ncr']
  40. }
  41. def execute(self, args, unnamed_args):
  42. """
  43. execute current TCL shell command
  44. :param args: array of known named arguments and options
  45. :param unnamed_args: array of other values which were passed into command
  46. without -somename and we do not have them in known arg_names
  47. :return: None or exception
  48. """
  49. name = args['name']
  50. if 'outname' not in args:
  51. args['outname'] = name + "_noncopper"
  52. obj = self.app.collection.get_by_name(name)
  53. if obj is None:
  54. self.raise_tcl_error("%s: %s" % (_("Object not found"), name))
  55. if obj.kind != 'gerber' and obj.kind != 'geometry':
  56. self.raise_tcl_error('%s %s: %s.' % (_("Expected GerberObject or GeometryObject, got"), name, type(obj)))
  57. if 'margin' not in args:
  58. args['margin'] = float(self.app.defaults["gerber_noncoppermargin"])
  59. margin = float(args['margin'])
  60. if 'rounded' in args:
  61. try:
  62. par = args['rounded'].capitalize()
  63. except AttributeError:
  64. par = args['rounded']
  65. rounded = bool(eval(par))
  66. else:
  67. rounded = bool(eval(self.app.defaults["gerber_noncopperrounded"]))
  68. del args['name']
  69. try:
  70. def geo_init(geo_obj, app_obj):
  71. assert geo_obj.kind == 'geometry'
  72. geo = unary_union(obj.solid_geometry)
  73. bounding_box = geo.envelope.buffer(float(margin))
  74. if not rounded:
  75. bounding_box = bounding_box.envelope
  76. non_copper = bounding_box.difference(geo)
  77. geo_obj.solid_geometry = non_copper
  78. self.app.app_obj.new_object("geometry", args['outname'], geo_init, plot=False)
  79. except Exception as e:
  80. return "Operation failed: %s" % str(e)
  81. # in the end toggle the visibility of the origin object so we can see the generated Geometry
  82. self.app.collection.get_by_name(name).ui.plot_cb.toggle()