TclCommandNregions.py 3.8 KB

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