TclCommandPanelize.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from ObjectCollection import *
  2. import TclCommand
  3. class TclCommandPanelize(TclCommand.TclCommand):
  4. """
  5. Tcl shell command to pannelize an object.
  6. example:
  7. """
  8. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['panelize']
  10. # Dictionary of types from Tcl command, needs to be ordered
  11. arg_names = collections.OrderedDict([
  12. ('name', str),
  13. ])
  14. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ('rows', int),
  17. ('columns', int),
  18. ('spacing_columns', float),
  19. ('spacing_rows', float),
  20. ('box', str),
  21. ('outname', str)
  22. ])
  23. # array of mandatory options for current Tcl command: required = {'name','outname'}
  24. required = ['name', 'rows', 'columns']
  25. # structured help for current command, args needs to be ordered
  26. help = {
  27. 'main': 'Rectangular panelizing.',
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the object to panelize.'),
  30. ('box', 'Name of object which acts as box (cutout for example.)'
  31. 'for cutout boundary. Object from name is used if not specified.'),
  32. ('spacing_columns', 'Spacing between columns.'),
  33. ('spacing_rows', 'Spacing between rows.'),
  34. ('columns', 'Number of columns.'),
  35. ('rows', 'Number of rows;'),
  36. ('outname', 'Name of the new geometry object.')
  37. ]),
  38. 'examples': []
  39. }
  40. def execute(self, args, unnamed_args):
  41. """
  42. :param args:
  43. :param unnamed_args:
  44. :return:
  45. """
  46. name = args['name']
  47. # Get source object.
  48. try:
  49. obj = self.app.collection.get_by_name(str(name))
  50. except:
  51. return "Could not retrieve object: %s" % name
  52. if obj is None:
  53. return "Object not found: %s" % name
  54. if 'box' in args:
  55. boxname = args['box']
  56. try:
  57. box = self.app.collection.get_by_name(boxname)
  58. except:
  59. return "Could not retrieve object: %s" % name
  60. else:
  61. box = obj
  62. if 'columns' not in args or 'rows' not in args:
  63. return "ERROR: Specify -columns and -rows"
  64. if 'outname' in args:
  65. outname = args['outname']
  66. else:
  67. outname = name + '_panelized'
  68. if 'spacing_columns' in args:
  69. spacing_columns = args['spacing_columns']
  70. else:
  71. spacing_columns = 5
  72. if 'spacing_rows' in args:
  73. spacing_rows = args['spacing_rows']
  74. else:
  75. spacing_rows = 5
  76. xmin, ymin, xmax, ymax = box.bounds()
  77. lenghtx = xmax - xmin + spacing_columns
  78. lenghty = ymax - ymin + spacing_rows
  79. currenty = 0
  80. def initialize_local(obj_init, app):
  81. obj_init.solid_geometry = obj.solid_geometry
  82. obj_init.offset([float(currentx), float(currenty)]),
  83. def initialize_local_excellon(obj_init, app):
  84. FlatCAMExcellon.merge(obj, obj_init)
  85. obj_init.offset([float(currentx), float(currenty)]),
  86. def initialize_geometry(obj_init, app):
  87. FlatCAMGeometry.merge(objs, obj_init)
  88. def initialize_excellon(obj_init, app):
  89. FlatCAMExcellon.merge(objs, obj_init)
  90. objs = []
  91. if obj is not None:
  92. for row in range(args['rows']):
  93. currentx = 0
  94. for col in range(args['columns']):
  95. local_outname = outname + ".tmp." + str(col) + "." + str(row)
  96. if isinstance(obj, FlatCAMExcellon):
  97. self.app.new_object("excellon", local_outname, initialize_local_excellon)
  98. else:
  99. self.app.new_object("geometry", local_outname, initialize_local)
  100. currentx += lenghtx
  101. currenty += lenghty
  102. if isinstance(obj, FlatCAMExcellon):
  103. self.app.new_object("excellon", outname, initialize_excellon)
  104. else:
  105. self.app.new_object("geometry", outname, initialize_geometry)
  106. # deselect all to avoid delete selected object when run delete from shell
  107. self.app.collection.set_all_inactive()
  108. for delobj in objs:
  109. self.app.collection.set_active(delobj.options['name'])
  110. self.app.on_delete()
  111. else:
  112. return "ERROR: obj is None"
  113. return "Ok"