ToolFilm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from FlatCAMTool import FlatCAMTool
  2. from GUIElements import RadioSet, FCEntry
  3. from PyQt5 import QtGui, QtCore, QtWidgets
  4. class Film(FlatCAMTool):
  5. toolName = "Film PCB"
  6. def __init__(self, app):
  7. FlatCAMTool.__init__(self, app)
  8. # Title
  9. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font>" % self.toolName)
  10. self.layout.addWidget(title_label)
  11. # Form Layout
  12. tf_form_layout = QtWidgets.QFormLayout()
  13. self.layout.addLayout(tf_form_layout)
  14. # Type of object for which to create the film
  15. self.tf_type_obj_combo = QtWidgets.QComboBox()
  16. self.tf_type_obj_combo.addItem("Gerber")
  17. self.tf_type_obj_combo.addItem("Excellon")
  18. self.tf_type_obj_combo.addItem("Geometry")
  19. # we get rid of item1 ("Excellon") as it is not suitable for creating film
  20. self.tf_type_obj_combo.view().setRowHidden(1, True)
  21. self.tf_type_obj_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  22. self.tf_type_obj_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  23. self.tf_type_obj_combo_label = QtWidgets.QLabel("Object Type:")
  24. self.tf_type_obj_combo_label.setToolTip(
  25. "Specify the type of object for which to create the film.\n"
  26. "The object can be of type: Gerber or Geometry.\n"
  27. "The selection here decide the type of objects that will be\n"
  28. "in the Film Object combobox."
  29. )
  30. tf_form_layout.addRow(self.tf_type_obj_combo_label, self.tf_type_obj_combo)
  31. # List of objects for which we can create the film
  32. self.tf_object_combo = QtWidgets.QComboBox()
  33. self.tf_object_combo.setModel(self.app.collection)
  34. self.tf_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  35. self.tf_object_combo.setCurrentIndex(1)
  36. self.tf_object_label = QtWidgets.QLabel("Film Object:")
  37. self.tf_object_label.setToolTip(
  38. "Object for which to create the film."
  39. )
  40. tf_form_layout.addRow(self.tf_object_label, self.tf_object_combo)
  41. # Type of Box Object to be used as an envelope for film creation
  42. # Within this we can create negative
  43. self.tf_type_box_combo = QtWidgets.QComboBox()
  44. self.tf_type_box_combo.addItem("Gerber")
  45. self.tf_type_box_combo.addItem("Excellon")
  46. self.tf_type_box_combo.addItem("Geometry")
  47. # we get rid of item1 ("Excellon") as it is not suitable for box when creating film
  48. self.tf_type_box_combo.view().setRowHidden(1, True)
  49. self.tf_type_box_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  50. self.tf_type_box_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  51. self.tf_type_box_combo_label = QtWidgets.QLabel("Box Type:")
  52. self.tf_type_box_combo_label.setToolTip(
  53. "Specify the type of object to be used as an container for\n"
  54. "film creation. It can be: Gerber or Geometry type."
  55. "The selection here decide the type of objects that will be\n"
  56. "in the Box Object combobox."
  57. )
  58. tf_form_layout.addRow(self.tf_type_box_combo_label, self.tf_type_box_combo)
  59. # Box
  60. self.tf_box_combo = QtWidgets.QComboBox()
  61. self.tf_box_combo.setModel(self.app.collection)
  62. self.tf_box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  63. self.tf_box_combo.setCurrentIndex(1)
  64. self.tf_box_combo_label = QtWidgets.QLabel("Box Object:")
  65. self.tf_box_combo_label.setToolTip(
  66. "The actual object that is used a container for the\n "
  67. "selected object for which we create the film.\n"
  68. "Usually it is the PCB outline but it can be also the\n"
  69. "same object for which the film is created.")
  70. tf_form_layout.addRow(self.tf_box_combo_label, self.tf_box_combo)
  71. # Film Type
  72. self.film_type = RadioSet([{'label': 'Positive', 'value': 'pos'},
  73. {'label': 'Negative', 'value': 'neg'}])
  74. self.film_type_label = QtWidgets.QLabel("Film Type:")
  75. self.film_type_label.setToolTip(
  76. "Generate a Positive black film or a Negative film.\n"
  77. "Positive means that it will print the features\n"
  78. "with black on a white canvas.\n"
  79. "Negative means that it will print the features\n"
  80. "with white on a black canvas.\n"
  81. "The Film format is SVG."
  82. )
  83. tf_form_layout.addRow(self.film_type_label, self.film_type)
  84. # Boundary for negative film generation
  85. self.boundary_entry = FCEntry()
  86. self.boundary_label = QtWidgets.QLabel("Border:")
  87. self.boundary_label.setToolTip(
  88. "Specify a border around the object.\n"
  89. "Only for negative film.\n"
  90. "It helps if we use as a Box Object the same \n"
  91. "object as in Film Object. It will create a thick\n"
  92. "black bar around the actual print allowing for a\n"
  93. "better delimitation of the outline features which are of\n"
  94. "white color like the rest and which may confound with the\n"
  95. "surroundings if not for this border."
  96. )
  97. tf_form_layout.addRow(self.boundary_label, self.boundary_entry)
  98. self.film_scale_entry = FCEntry()
  99. self.film_scale_label = QtWidgets.QLabel("Scale Stroke:")
  100. self.film_scale_label.setToolTip(
  101. "Scale the line stroke thickness of each feature in the SVG file.\n"
  102. "It means that the line that envelope each SVG feature will be thicker or thinner,\n"
  103. "therefore the fine features may be more affected by this parameter."
  104. )
  105. tf_form_layout.addRow(self.film_scale_label, self.film_scale_entry)
  106. # Buttons
  107. hlay = QtWidgets.QHBoxLayout()
  108. self.layout.addLayout(hlay)
  109. hlay.addStretch()
  110. self.film_object_button = QtWidgets.QPushButton("Save Film")
  111. self.film_object_button.setToolTip(
  112. "Create a Film for the selected object, within\n"
  113. "the specified box. Does not create a new \n "
  114. "FlatCAM object, but directly save it in SVG format\n"
  115. "which can be opened with Inkscape."
  116. )
  117. hlay.addWidget(self.film_object_button)
  118. self.layout.addStretch()
  119. ## Signals
  120. self.film_object_button.clicked.connect(self.on_film_creation)
  121. self.tf_type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  122. self.tf_type_box_combo.currentIndexChanged.connect(self.on_type_box_index_changed)
  123. def on_type_obj_index_changed(self, index):
  124. obj_type = self.tf_type_obj_combo.currentIndex()
  125. self.tf_object_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  126. self.tf_object_combo.setCurrentIndex(0)
  127. def on_type_box_index_changed(self, index):
  128. obj_type = self.tf_type_box_combo.currentIndex()
  129. self.tf_box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  130. self.tf_box_combo.setCurrentIndex(0)
  131. def run(self):
  132. self.app.report_usage("ToolFilm()")
  133. FlatCAMTool.run(self)
  134. self.set_tool_ui()
  135. # if the splitter us hidden, display it
  136. if self.app.ui.splitter.sizes()[0] == 0:
  137. self.app.ui.splitter.setSizes([1, 1])
  138. self.app.ui.notebook.setTabText(2, "Film Tool")
  139. def install(self, icon=None, separator=None, **kwargs):
  140. FlatCAMTool.install(self, icon, separator, shortcut='ALT+L', **kwargs)
  141. def set_tool_ui(self):
  142. self.reset_fields()
  143. f_type = self.app.defaults["tools_film_type"] if self.app.defaults["tools_film_type"] else 'neg'
  144. self.film_type.set_value(str(f_type))
  145. b_entry = self.app.defaults[ "tools_film_boundary"] if self.app.defaults[ "tools_film_boundary"] else 0.0
  146. self.boundary_entry.set_value(float(b_entry))
  147. scale_stroke_width = self.app.defaults["tools_film_scale"] if self.app.defaults["tools_film_scale"] else 0.0
  148. self.film_scale_entry.set_value(int(scale_stroke_width))
  149. def on_film_creation(self):
  150. try:
  151. name = self.tf_object_combo.currentText()
  152. except:
  153. self.app.inform.emit("[ERROR_NOTCL] No FlatCAM object selected. Load an object for Film and retry.")
  154. return
  155. try:
  156. boxname = self.tf_box_combo.currentText()
  157. except:
  158. self.app.inform.emit("[ERROR_NOTCL] No FlatCAM object selected. Load an object for Box and retry.")
  159. return
  160. try:
  161. border = float(self.boundary_entry.get_value())
  162. except ValueError:
  163. # try to convert comma to decimal point. if it's still not working error message and return
  164. try:
  165. border = float(self.boundary_entry.get_value().replace(',', '.'))
  166. except ValueError:
  167. self.app.inform.emit("[ERROR_NOTCL]Wrong value format entered, "
  168. "use a number.")
  169. return
  170. try:
  171. scale_stroke_width = int(self.film_scale_entry.get_value())
  172. except ValueError:
  173. self.app.inform.emit("[ERROR_NOTCL]Wrong value format entered, "
  174. "use a number.")
  175. return
  176. if border is None:
  177. border = 0
  178. self.app.inform.emit("Generating Film ...")
  179. if self.film_type.get_value() == "pos":
  180. try:
  181. filename, _ = QtWidgets.QFileDialog.getSaveFileName(
  182. caption="Export SVG positive",
  183. directory=self.app.get_last_save_folder() + '/' + name,
  184. filter="*.svg")
  185. except TypeError:
  186. filename, _ = QtWidgets.QFileDialog.getSaveFileName(caption="Export SVG positive")
  187. filename = str(filename)
  188. if str(filename) == "":
  189. self.app.inform.emit("[WARNING_NOTCL]Export SVG positive cancelled.")
  190. return
  191. else:
  192. self.app.export_svg_black(name, boxname, filename, scale_factor=scale_stroke_width)
  193. else:
  194. try:
  195. filename, _ = QtWidgets.QFileDialog.getSaveFileName(
  196. caption="Export SVG negative",
  197. directory=self.app.get_last_save_folder() + '/' + name,
  198. filter="*.svg")
  199. except TypeError:
  200. filename, _ = QtWidgets.QFileDialog.getSaveFileName(caption="Export SVG negative")
  201. filename = str(filename)
  202. if str(filename) == "":
  203. self.app.inform.emit("[WARNING_NOTCL]Export SVG negative cancelled.")
  204. return
  205. else:
  206. self.app.export_svg_negative(name, boxname, filename, border, scale_factor=scale_stroke_width)
  207. def reset_fields(self):
  208. self.tf_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  209. self.tf_box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))