ToolImage.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # ########################################################## ##
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. # ########################################################## ##
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.GUIElements import RadioSet, FCComboBox, IntEntry
  10. from PyQt5 import QtGui, QtWidgets
  11. import gettext
  12. import FlatCAMTranslation as fcTranslate
  13. import builtins
  14. fcTranslate.apply_language('strings')
  15. if '_' not in builtins.__dict__:
  16. _ = gettext.gettext
  17. class ToolImage(FlatCAMTool):
  18. toolName = _("Image as Object")
  19. def __init__(self, app):
  20. FlatCAMTool.__init__(self, app)
  21. # Title
  22. title_label = QtWidgets.QLabel("%s" % _('Image to PCB'))
  23. title_label.setStyleSheet("""
  24. QLabel
  25. {
  26. font-size: 16px;
  27. font-weight: bold;
  28. }
  29. """)
  30. self.layout.addWidget(title_label)
  31. # Form Layout
  32. ti_form_layout = QtWidgets.QFormLayout()
  33. self.layout.addLayout(ti_form_layout)
  34. # Type of object to create for the image
  35. self.tf_type_obj_combo = FCComboBox()
  36. self.tf_type_obj_combo.addItem("Gerber")
  37. self.tf_type_obj_combo.addItem("Geometry")
  38. self.tf_type_obj_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  39. self.tf_type_obj_combo.setItemIcon(1, QtGui.QIcon("share/geometry16.png"))
  40. self.tf_type_obj_combo_label = QtWidgets.QLabel('%s:' % _("Object Type"))
  41. self.tf_type_obj_combo_label.setToolTip(
  42. _("Specify the type of object to create from the image.\n"
  43. "It can be of type: Gerber or Geometry.")
  44. )
  45. ti_form_layout.addRow(self.tf_type_obj_combo_label, self.tf_type_obj_combo)
  46. # DPI value of the imported image
  47. self.dpi_entry = IntEntry()
  48. self.dpi_label = QtWidgets.QLabel('%s:' % _("DPI value"))
  49. self.dpi_label.setToolTip(
  50. _("Specify a DPI value for the image.")
  51. )
  52. ti_form_layout.addRow(self.dpi_label, self.dpi_entry)
  53. self.emty_lbl = QtWidgets.QLabel("")
  54. self.layout.addWidget(self.emty_lbl)
  55. self.detail_label = QtWidgets.QLabel("<font size=4><b>%s:</b></font>" % _('Level of detail'))
  56. self.layout.addWidget(self.detail_label)
  57. ti2_form_layout = QtWidgets.QFormLayout()
  58. self.layout.addLayout(ti2_form_layout)
  59. # Type of image interpretation
  60. self.image_type = RadioSet([{'label': 'B/W', 'value': 'black'},
  61. {'label': 'Color', 'value': 'color'}])
  62. self.image_type_label = QtWidgets.QLabel("<b>%s:</b>" % _('Image type'))
  63. self.image_type_label.setToolTip(
  64. _("Choose a method for the image interpretation.\n"
  65. "B/W means a black & white image. Color means a colored image.")
  66. )
  67. ti2_form_layout.addRow(self.image_type_label, self.image_type)
  68. # Mask value of the imported image when image monochrome
  69. self.mask_bw_entry = IntEntry()
  70. self.mask_bw_label = QtWidgets.QLabel("%s <b>B/W</b>:" % _('Mask value'))
  71. self.mask_bw_label.setToolTip(
  72. _("Mask for monochrome image.\n"
  73. "Takes values between [0 ... 255].\n"
  74. "Decides the level of details to include\n"
  75. "in the resulting geometry.\n"
  76. "0 means no detail and 255 means everything \n"
  77. "(which is totally black).")
  78. )
  79. ti2_form_layout.addRow(self.mask_bw_label, self.mask_bw_entry)
  80. # Mask value of the imported image for RED color when image color
  81. self.mask_r_entry = IntEntry()
  82. self.mask_r_label = QtWidgets.QLabel("%s <b>R:</b>" % _('Mask value'))
  83. self.mask_r_label.setToolTip(
  84. _("Mask for RED color.\n"
  85. "Takes values between [0 ... 255].\n"
  86. "Decides the level of details to include\n"
  87. "in the resulting geometry.")
  88. )
  89. ti2_form_layout.addRow(self.mask_r_label, self.mask_r_entry)
  90. # Mask value of the imported image for GREEN color when image color
  91. self.mask_g_entry = IntEntry()
  92. self.mask_g_label = QtWidgets.QLabel("%s <b>G:</b>" % _('Mask value'))
  93. self.mask_g_label.setToolTip(
  94. _("Mask for GREEN color.\n"
  95. "Takes values between [0 ... 255].\n"
  96. "Decides the level of details to include\n"
  97. "in the resulting geometry.")
  98. )
  99. ti2_form_layout.addRow(self.mask_g_label, self.mask_g_entry)
  100. # Mask value of the imported image for BLUE color when image color
  101. self.mask_b_entry = IntEntry()
  102. self.mask_b_label = QtWidgets.QLabel("%s <b>B:</b>" % _('Mask value'))
  103. self.mask_b_label.setToolTip(
  104. _("Mask for BLUE color.\n"
  105. "Takes values between [0 ... 255].\n"
  106. "Decides the level of details to include\n"
  107. "in the resulting geometry.")
  108. )
  109. ti2_form_layout.addRow(self.mask_b_label, self.mask_b_entry)
  110. # Buttons
  111. hlay = QtWidgets.QHBoxLayout()
  112. self.layout.addLayout(hlay)
  113. hlay.addStretch()
  114. self.import_button = QtWidgets.QPushButton(_("Import image"))
  115. self.import_button.setToolTip(
  116. _("Open a image of raster type and then import it in FlatCAM.")
  117. )
  118. hlay.addWidget(self.import_button)
  119. self.layout.addStretch()
  120. # ## Signals
  121. self.import_button.clicked.connect(self.on_file_importimage)
  122. def run(self, toggle=True):
  123. self.app.report_usage("ToolImage()")
  124. if toggle:
  125. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  126. if self.app.ui.splitter.sizes()[0] == 0:
  127. self.app.ui.splitter.setSizes([1, 1])
  128. else:
  129. try:
  130. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  131. # if tab is populated with the tool but it does not have the focus, focus on it
  132. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  133. # focus on Tool Tab
  134. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  135. else:
  136. self.app.ui.splitter.setSizes([0, 1])
  137. except AttributeError:
  138. pass
  139. else:
  140. if self.app.ui.splitter.sizes()[0] == 0:
  141. self.app.ui.splitter.setSizes([1, 1])
  142. FlatCAMTool.run(self)
  143. self.set_tool_ui()
  144. self.app.ui.notebook.setTabText(2, _("Image Tool"))
  145. def install(self, icon=None, separator=None, **kwargs):
  146. FlatCAMTool.install(self, icon, separator, **kwargs)
  147. def set_tool_ui(self):
  148. # ## Initialize form
  149. self.dpi_entry.set_value(96)
  150. self.image_type.set_value('black')
  151. self.mask_bw_entry.set_value(250)
  152. self.mask_r_entry.set_value(250)
  153. self.mask_g_entry.set_value(250)
  154. self.mask_b_entry.set_value(250)
  155. def on_file_importimage(self):
  156. """
  157. Callback for menu item File->Import IMAGE.
  158. :param type_of_obj: to import the IMAGE as Geometry or as Gerber
  159. :type type_of_obj: str
  160. :return: None
  161. """
  162. mask = []
  163. self.app.log.debug("on_file_importimage()")
  164. _filter = "Image Files(*.BMP *.PNG *.JPG *.JPEG);;" \
  165. "Bitmap File (*.BMP);;" \
  166. "PNG File (*.PNG);;" \
  167. "Jpeg File (*.JPG);;" \
  168. "All Files (*.*)"
  169. try:
  170. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"),
  171. directory=self.app.get_last_folder(), filter=_filter)
  172. except TypeError:
  173. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"), filter=filter)
  174. filename = str(filename)
  175. type_obj = self.tf_type_obj_combo.get_value().lower()
  176. dpi = self.dpi_entry.get_value()
  177. mode = self.image_type.get_value()
  178. mask = [self.mask_bw_entry.get_value(), self.mask_r_entry.get_value(), self.mask_g_entry.get_value(),
  179. self.mask_b_entry.get_value()]
  180. if filename == "":
  181. self.app.inform.emit(_("Open cancelled."))
  182. else:
  183. self.app.worker_task.emit({'fcn': self.app.import_image,
  184. 'params': [filename, type_obj, dpi, mode, mask]})
  185. # self.import_svg(filename, "geometry")