ToolFilm.py 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtCore, QtWidgets, QtGui
  8. from appTool import AppTool
  9. from appGUI.GUIElements import RadioSet, FCDoubleSpinner, FCCheckBox, \
  10. OptionalHideInputSection, FCComboBox, FCFileSaveDialog, FCButton, FCLabel, FCSpinner
  11. from copy import deepcopy
  12. import logging
  13. from shapely.geometry import Polygon, MultiPolygon, Point
  14. import shapely.affinity as affinity
  15. from shapely.ops import unary_union
  16. from reportlab.graphics import renderPDF
  17. from reportlab.pdfgen import canvas
  18. from reportlab.graphics import renderPM
  19. from reportlab.lib.units import inch, mm
  20. from reportlab.lib.pagesizes import landscape, portrait
  21. from svglib.svglib import svg2rlg
  22. from xml.dom.minidom import parseString as parse_xml_string
  23. from lxml import etree as ET
  24. from io import StringIO
  25. import gettext
  26. import appTranslation as fcTranslate
  27. import builtins
  28. fcTranslate.apply_language('strings')
  29. if '_' not in builtins.__dict__:
  30. _ = gettext.gettext
  31. log = logging.getLogger('base')
  32. class Film(AppTool):
  33. def __init__(self, app):
  34. AppTool.__init__(self, app)
  35. self.decimals = self.app.decimals
  36. self.units = self.app.defaults['units']
  37. # #############################################################################
  38. # ######################### Tool GUI ##########################################
  39. # #############################################################################
  40. self.ui = FilmUI(layout=self.layout, app=self.app)
  41. self.toolName = self.ui.toolName
  42. # ## Signals
  43. self.ui.film_object_button.clicked.connect(self.on_film_creation)
  44. self.ui.tf_type_obj_combo.activated_custom.connect(self.on_type_obj_index_changed)
  45. self.ui.tf_type_box_combo.activated_custom.connect(self.on_type_box_index_changed)
  46. self.ui.film_type.activated_custom.connect(self.ui.on_film_type)
  47. self.ui.source_punch.activated_custom.connect(self.ui.on_punch_source)
  48. self.ui.file_type_radio.activated_custom.connect(self.ui.on_file_type)
  49. self.ui.reset_button.clicked.connect(self.set_tool_ui)
  50. self.screen_dpi = 96
  51. def on_type_obj_index_changed(self, val):
  52. obj_type = 2 if val == 'geo' else 0
  53. self.ui.tf_object_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  54. self.ui.tf_object_combo.setCurrentIndex(0)
  55. self.ui.tf_object_combo.obj_type = {
  56. "grb": "gerber", "geo": "geometry"
  57. }[self.ui.tf_type_obj_combo.get_value()]
  58. def on_type_box_index_changed(self, val):
  59. obj_type = 2 if val == 'geo' else 0
  60. self.ui.tf_box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  61. self.ui.tf_box_combo.setCurrentIndex(0)
  62. self.ui.tf_box_combo.obj_type = {
  63. "grb": "gerber", "geo": "geometry"
  64. }[self.ui.tf_type_obj_combo.get_value()]
  65. def run(self, toggle=True):
  66. self.app.defaults.report_usage("ToolFilm()")
  67. if toggle:
  68. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  69. if self.app.ui.splitter.sizes()[0] == 0:
  70. self.app.ui.splitter.setSizes([1, 1])
  71. else:
  72. try:
  73. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  74. # if tab is populated with the tool but it does not have the focus, focus on it
  75. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  76. # focus on Tool Tab
  77. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  78. else:
  79. self.app.ui.splitter.setSizes([0, 1])
  80. except AttributeError:
  81. pass
  82. else:
  83. if self.app.ui.splitter.sizes()[0] == 0:
  84. self.app.ui.splitter.setSizes([1, 1])
  85. AppTool.run(self)
  86. self.set_tool_ui()
  87. self.app.ui.notebook.setTabText(2, _("Film Tool"))
  88. def install(self, icon=None, separator=None, **kwargs):
  89. AppTool.install(self, icon, separator, shortcut='Alt+L', **kwargs)
  90. def set_tool_ui(self):
  91. self.reset_fields()
  92. f_type = self.app.defaults["tools_film_type"] if self.app.defaults["tools_film_type"] else 'neg'
  93. self.ui.film_type.set_value(str(f_type))
  94. self.ui.on_film_type(val=f_type)
  95. b_entry = self.app.defaults["tools_film_boundary"] if self.app.defaults["tools_film_boundary"] else 0.0
  96. self.ui.boundary_entry.set_value(float(b_entry))
  97. scale_stroke_width = self.app.defaults["tools_film_scale_stroke"] if \
  98. self.app.defaults["tools_film_scale_stroke"] else 0.0
  99. self.ui.film_scale_stroke_entry.set_value(int(scale_stroke_width))
  100. self.ui.punch_cb.set_value(False)
  101. self.ui.source_punch.set_value('exc')
  102. self.ui.film_scale_cb.set_value(self.app.defaults["tools_film_scale_cb"])
  103. self.ui.film_scalex_entry.set_value(float(self.app.defaults["tools_film_scale_x_entry"]))
  104. self.ui.film_scaley_entry.set_value(float(self.app.defaults["tools_film_scale_y_entry"]))
  105. self.ui.film_skew_cb.set_value(self.app.defaults["tools_film_skew_cb"])
  106. self.ui.film_skewx_entry.set_value(float(self.app.defaults["tools_film_skew_x_entry"]))
  107. self.ui.film_skewy_entry.set_value(float(self.app.defaults["tools_film_skew_y_entry"]))
  108. self.ui.film_skew_reference.set_value(self.app.defaults["tools_film_skew_ref_radio"])
  109. self.ui.film_mirror_cb.set_value(self.app.defaults["tools_film_mirror_cb"])
  110. self.ui.film_mirror_axis.set_value(self.app.defaults["tools_film_mirror_axis_radio"])
  111. self.ui.file_type_radio.set_value(self.app.defaults["tools_film_file_type_radio"])
  112. self.ui.orientation_radio.set_value(self.app.defaults["tools_film_orientation"])
  113. self.ui.pagesize_combo.set_value(self.app.defaults["tools_film_pagesize"])
  114. self.ui.png_dpi_spinner.set_value(self.app.defaults["tools_film_png_dpi"])
  115. self.ui.tf_type_obj_combo.set_value('grb')
  116. self.ui.tf_type_box_combo.set_value('grb')
  117. # run once to update the obj_type attribute in the FCCombobox so the last object is showed in cb
  118. self.on_type_obj_index_changed(val='grb')
  119. self.on_type_box_index_changed(val='grb')
  120. def on_film_creation(self):
  121. log.debug("ToolFilm.Film.on_film_creation() started ...")
  122. try:
  123. name = self.ui.tf_object_combo.currentText()
  124. except Exception:
  125. self.app.inform.emit('[ERROR_NOTCL] %s' %
  126. _("No FlatCAM object selected. Load an object for Film and retry."))
  127. return
  128. try:
  129. boxname = self.ui.tf_box_combo.currentText()
  130. except Exception:
  131. self.app.inform.emit('[ERROR_NOTCL] %s' %
  132. _("No FlatCAM object selected. Load an object for Box and retry."))
  133. return
  134. if name == '' or boxname == '':
  135. self.app.inform.emit('[ERROR_NOTCL] %s' % _("No FlatCAM object selected."))
  136. return
  137. scale_stroke_width = float(self.ui.film_scale_stroke_entry.get_value())
  138. source = self.ui.source_punch.get_value()
  139. file_type = self.ui.file_type_radio.get_value()
  140. # #################################################################
  141. # ################ STARTING THE JOB ###############################
  142. # #################################################################
  143. self.app.inform.emit(_("Generating Film ..."))
  144. if self.ui.film_type.get_value() == "pos":
  145. if self.ui.punch_cb.get_value() is False:
  146. self.generate_positive_normal_film(name, boxname, factor=scale_stroke_width, ftype=file_type)
  147. else:
  148. self.generate_positive_punched_film(name, boxname, source, factor=scale_stroke_width, ftype=file_type)
  149. else:
  150. self.generate_negative_film(name, boxname, factor=scale_stroke_width, ftype=file_type)
  151. def generate_positive_normal_film(self, name, boxname, factor, ftype='svg'):
  152. log.debug("ToolFilm.Film.generate_positive_normal_film() started ...")
  153. scale_factor_x = 1
  154. scale_factor_y = 1
  155. skew_factor_x = None
  156. skew_factor_y = None
  157. mirror = None
  158. skew_reference = 'center'
  159. if self.ui.film_scale_cb.get_value():
  160. if self.ui.film_scalex_entry.get_value() != 1.0:
  161. scale_factor_x = self.ui.film_scalex_entry.get_value()
  162. if self.ui.film_scaley_entry.get_value() != 1.0:
  163. scale_factor_y = self.ui.film_scaley_entry.get_value()
  164. if self.ui.film_skew_cb.get_value():
  165. if self.ui.film_skewx_entry.get_value() != 0.0:
  166. skew_factor_x = self.ui.film_skewx_entry.get_value()
  167. if self.ui.film_skewy_entry.get_value() != 0.0:
  168. skew_factor_y = self.ui.film_skewy_entry.get_value()
  169. skew_reference = self.ui.film_skew_reference.get_value()
  170. if self.ui.film_mirror_cb.get_value():
  171. if self.ui.film_mirror_axis.get_value() != 'none':
  172. mirror = self.ui.film_mirror_axis.get_value()
  173. if ftype == 'svg':
  174. filter_ext = "SVG Files (*.SVG);;"\
  175. "All Files (*.*)"
  176. elif ftype == 'png':
  177. filter_ext = "PNG Files (*.PNG);;" \
  178. "All Files (*.*)"
  179. else:
  180. filter_ext = "PDF Files (*.PDF);;" \
  181. "All Files (*.*)"
  182. try:
  183. filename, _f = FCFileSaveDialog.get_saved_filename(
  184. caption=_("Export positive film"),
  185. directory=self.app.get_last_save_folder() + '/' + name + '_film',
  186. ext_filter=filter_ext)
  187. except TypeError:
  188. filename, _f = FCFileSaveDialog.get_saved_filename(
  189. caption=_("Export positive film"),
  190. ext_filter=filter_ext)
  191. filename = str(filename)
  192. if str(filename) == "":
  193. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
  194. return
  195. else:
  196. pagesize = self.ui.pagesize_combo.get_value()
  197. orientation = self.ui.orientation_radio.get_value()
  198. color = self.app.defaults['tools_film_color']
  199. self.export_positive(name, boxname, filename,
  200. scale_stroke_factor=factor,
  201. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
  202. skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
  203. skew_reference=skew_reference,
  204. mirror=mirror,
  205. pagesize_val=pagesize, orientation_val=orientation, color_val=color, opacity_val=1.0,
  206. ftype=ftype
  207. )
  208. def generate_positive_punched_film(self, name, boxname, source, factor, ftype='svg'):
  209. film_obj = self.app.collection.get_by_name(name)
  210. if source == 'exc':
  211. log.debug("ToolFilm.Film.generate_positive_punched_film() with Excellon source started ...")
  212. try:
  213. exc_name = self.ui.exc_combo.currentText()
  214. except Exception:
  215. self.app.inform.emit('[ERROR_NOTCL] %s' %
  216. _("No Excellon object selected. Load an object for punching reference and retry."))
  217. return
  218. exc_obj = self.app.collection.get_by_name(exc_name)
  219. exc_solid_geometry = MultiPolygon(exc_obj.solid_geometry)
  220. punched_solid_geometry = MultiPolygon(film_obj.solid_geometry).difference(exc_solid_geometry)
  221. def init_func(new_obj, app_obj):
  222. new_obj.solid_geometry = deepcopy(punched_solid_geometry)
  223. outname = name + "_punched"
  224. self.app.app_obj.new_object('gerber', outname, init_func)
  225. self.generate_positive_normal_film(outname, boxname, factor=factor, ftype=ftype)
  226. else:
  227. log.debug("ToolFilm.Film.generate_positive_punched_film() with Pad center source started ...")
  228. punch_size = float(self.ui.punch_size_spinner.get_value())
  229. punching_geo = []
  230. for apid in film_obj.apertures:
  231. if film_obj.apertures[apid]['type'] == 'C':
  232. if punch_size >= float(film_obj.apertures[apid]['size']):
  233. self.app.inform.emit('[ERROR_NOTCL] %s' %
  234. _(" Could not generate punched hole film because the punch hole size"
  235. "is bigger than some of the apertures in the Gerber object."))
  236. return 'fail'
  237. else:
  238. for elem in film_obj.apertures[apid]['geometry']:
  239. if 'follow' in elem:
  240. if isinstance(elem['follow'], Point):
  241. punching_geo.append(elem['follow'].buffer(punch_size / 2))
  242. else:
  243. if punch_size >= float(film_obj.apertures[apid]['width']) or \
  244. punch_size >= float(film_obj.apertures[apid]['height']):
  245. self.app.inform.emit('[ERROR_NOTCL] %s' %
  246. _("Could not generate punched hole film because the punch hole size"
  247. "is bigger than some of the apertures in the Gerber object."))
  248. return 'fail'
  249. else:
  250. for elem in film_obj.apertures[apid]['geometry']:
  251. if 'follow' in elem:
  252. if isinstance(elem['follow'], Point):
  253. punching_geo.append(elem['follow'].buffer(punch_size / 2))
  254. punching_geo = MultiPolygon(punching_geo)
  255. if not isinstance(film_obj.solid_geometry, Polygon):
  256. temp_solid_geometry = MultiPolygon(film_obj.solid_geometry)
  257. else:
  258. temp_solid_geometry = film_obj.solid_geometry
  259. punched_solid_geometry = temp_solid_geometry.difference(punching_geo)
  260. if punched_solid_geometry == temp_solid_geometry:
  261. self.app.inform.emit('[WARNING_NOTCL] %s' %
  262. _("Could not generate punched hole film because the newly created object geometry "
  263. "is the same as the one in the source object geometry..."))
  264. return 'fail'
  265. def init_func(new_obj, app_obj):
  266. new_obj.solid_geometry = deepcopy(punched_solid_geometry)
  267. outname = name + "_punched"
  268. self.app.app_obj.new_object('gerber', outname, init_func)
  269. self.generate_positive_normal_film(outname, boxname, factor=factor, ftype=ftype)
  270. def generate_negative_film(self, name, boxname, factor, ftype='svg'):
  271. log.debug("ToolFilm.Film.generate_negative_film() started ...")
  272. scale_factor_x = 1
  273. scale_factor_y = 1
  274. skew_factor_x = None
  275. skew_factor_y = None
  276. mirror = None
  277. skew_reference = 'center'
  278. if self.ui.film_scale_cb.get_value():
  279. if self.ui.film_scalex_entry.get_value() != 1.0:
  280. scale_factor_x = self.ui.film_scalex_entry.get_value()
  281. if self.ui.film_scaley_entry.get_value() != 1.0:
  282. scale_factor_y = self.ui.film_scaley_entry.get_value()
  283. if self.ui.film_skew_cb.get_value():
  284. if self.ui.film_skewx_entry.get_value() != 0.0:
  285. skew_factor_x = self.ui.film_skewx_entry.get_value()
  286. if self.ui.film_skewy_entry.get_value() != 0.0:
  287. skew_factor_y = self.ui.film_skewy_entry.get_value()
  288. skew_reference = self.ui.film_skew_reference.get_value()
  289. if self.ui.film_mirror_cb.get_value():
  290. if self.ui.film_mirror_axis.get_value() != 'none':
  291. mirror = self.ui.film_mirror_axis.get_value()
  292. border = self.ui.boundary_entry.get_value()
  293. if border is None:
  294. border = 0
  295. if ftype == 'svg':
  296. filter_ext = "SVG Files (*.SVG);;"\
  297. "All Files (*.*)"
  298. elif ftype == 'png':
  299. filter_ext = "PNG Files (*.PNG);;" \
  300. "All Files (*.*)"
  301. else:
  302. filter_ext = "PDF Files (*.PDF);;" \
  303. "All Files (*.*)"
  304. try:
  305. filename, _f = FCFileSaveDialog.get_saved_filename(
  306. caption=_("Export negative film"),
  307. directory=self.app.get_last_save_folder() + '/' + name + '_film',
  308. ext_filter=filter_ext)
  309. except TypeError:
  310. filename, _f = FCFileSaveDialog.get_saved_filename(
  311. caption=_("Export negative film"),
  312. ext_filter=filter_ext)
  313. filename = str(filename)
  314. if str(filename) == "":
  315. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
  316. return
  317. else:
  318. self.export_negative(name, boxname, filename, border,
  319. scale_stroke_factor=factor,
  320. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
  321. skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
  322. skew_reference=skew_reference,
  323. mirror=mirror, ftype=ftype
  324. )
  325. def export_negative(self, obj_name, box_name, filename, boundary,
  326. scale_stroke_factor=0.00,
  327. scale_factor_x=1, scale_factor_y=1,
  328. skew_factor_x=None, skew_factor_y=None, skew_reference='center',
  329. mirror=None,
  330. use_thread=True, ftype='svg'):
  331. """
  332. Exports a Geometry Object to an SVG file in negative.
  333. :param obj_name: the name of the FlatCAM object to be saved as SVG
  334. :param box_name: the name of the FlatCAM object to be used as delimitation of the content to be saved
  335. :param filename: Path to the SVG file to save to.
  336. :param boundary: thickness of a black border to surround all the features
  337. :param scale_stroke_factor: factor by which to change/scale the thickness of the features
  338. :param scale_factor_x: factor to scale the svg geometry on the X axis
  339. :param scale_factor_y: factor to scale the svg geometry on the Y axis
  340. :param skew_factor_x: factor to skew the svg geometry on the X axis
  341. :param skew_factor_y: factor to skew the svg geometry on the Y axis
  342. :param skew_reference: reference to use for skew. Can be 'bottomleft', 'bottomright', 'topleft', 'topright' and
  343. those are the 4 points of the bounding box of the geometry to be skewed.
  344. :param mirror: can be 'x' or 'y' or 'both'. Axis on which to mirror the svg geometry
  345. :param use_thread: if to be run in a separate thread; boolean
  346. :param ftype: the type of file for saving the film: 'svg', 'png' or 'pdf'
  347. :return:
  348. """
  349. self.app.defaults.report_usage("export_negative()")
  350. if filename is None:
  351. filename = self.app.defaults["global_last_save_folder"]
  352. self.app.log.debug("export_svg() negative")
  353. try:
  354. obj = self.app.collection.get_by_name(str(obj_name))
  355. except Exception:
  356. return "Could not retrieve object: %s" % obj_name
  357. try:
  358. box = self.app.collection.get_by_name(str(box_name))
  359. except Exception:
  360. return "Could not retrieve object: %s" % box_name
  361. if box is None:
  362. self.app.inform.emit('[WARNING_NOTCL] %s: %s' % (_("No object Box. Using instead"), obj))
  363. box = obj
  364. scale_factor_x = scale_factor_x
  365. scale_factor_y = scale_factor_y
  366. def make_negative_film(scale_factor_x, scale_factor_y):
  367. log.debug("FilmTool.export_negative().make_negative_film()")
  368. scale_reference = 'center'
  369. self.screen_dpi = self.app.qapp.screens()[0].logicalDotsPerInch()
  370. new_png_dpi = self.ui.png_dpi_spinner.get_value()
  371. dpi_rate = new_png_dpi / self.screen_dpi
  372. # Determine bounding area for svg export
  373. bounds = box.bounds()
  374. tr_scale_reference = (bounds[0], bounds[1])
  375. if dpi_rate != 1 and ftype == 'png':
  376. scale_factor_x += dpi_rate
  377. scale_factor_y += dpi_rate
  378. scale_reference = (bounds[0], bounds[1])
  379. if box.kind.lower() == 'geometry':
  380. flat_geo = []
  381. if box.multigeo:
  382. for tool in box.tools:
  383. flat_geo += box.flatten(box.tools[tool]['solid_geometry'])
  384. box_geo = unary_union(flat_geo)
  385. else:
  386. box_geo = unary_union(box.flatten())
  387. else:
  388. box_geo = unary_union(box.flatten())
  389. skew_ref = 'center'
  390. if skew_reference != 'center':
  391. xmin, ymin, xmax, ymax = box_geo.bounds
  392. if skew_reference == 'topleft':
  393. skew_ref = (xmin, ymax)
  394. elif skew_reference == 'bottomleft':
  395. skew_ref = (xmin, ymin)
  396. elif skew_reference == 'topright':
  397. skew_ref = (xmax, ymax)
  398. elif skew_reference == 'bottomright':
  399. skew_ref = (xmax, ymin)
  400. transformed_box_geo = box_geo
  401. if scale_factor_x and not scale_factor_y:
  402. transformed_box_geo = affinity.scale(transformed_box_geo, scale_factor_x, 1.0,
  403. origin=tr_scale_reference)
  404. elif not scale_factor_x and scale_factor_y:
  405. transformed_box_geo = affinity.scale(transformed_box_geo, 1.0, scale_factor_y,
  406. origin=tr_scale_reference)
  407. elif scale_factor_x and scale_factor_y:
  408. transformed_box_geo = affinity.scale(transformed_box_geo, scale_factor_x, scale_factor_y,
  409. origin=tr_scale_reference)
  410. if skew_factor_x and not skew_factor_y:
  411. transformed_box_geo = affinity.skew(transformed_box_geo, skew_factor_x, 0.0, origin=skew_ref)
  412. elif not skew_factor_x and skew_factor_y:
  413. transformed_box_geo = affinity.skew(transformed_box_geo, 0.0, skew_factor_y, origin=skew_ref)
  414. elif skew_factor_x and skew_factor_y:
  415. transformed_box_geo = affinity.skew(transformed_box_geo, skew_factor_x, skew_factor_y, origin=skew_ref)
  416. if mirror:
  417. if mirror == 'x':
  418. transformed_box_geo = affinity.scale(transformed_box_geo, 1.0, -1.0)
  419. if mirror == 'y':
  420. transformed_box_geo = affinity.scale(transformed_box_geo, -1.0, 1.0)
  421. if mirror == 'both':
  422. transformed_box_geo = affinity.scale(transformed_box_geo, -1.0, -1.0)
  423. bounds = transformed_box_geo.bounds
  424. size = bounds[2] - bounds[0], bounds[3] - bounds[1]
  425. exported_svg = obj.export_svg(scale_stroke_factor=scale_stroke_factor,
  426. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
  427. skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
  428. mirror=mirror,
  429. scale_reference=scale_reference, skew_reference=skew_reference
  430. )
  431. uom = obj.units.lower()
  432. # Convert everything to strings for use in the xml doc
  433. svgwidth = str(size[0] + (2 * boundary))
  434. svgheight = str(size[1] + (2 * boundary))
  435. minx = str(bounds[0] - boundary)
  436. miny = str(bounds[1] + boundary + size[1])
  437. miny_rect = str(bounds[1] - boundary)
  438. # Add a SVG Header and footer to the svg output from shapely
  439. # The transform flips the Y Axis so that everything renders
  440. # properly within svg apps such as inkscape
  441. svg_header = '<svg xmlns="http://www.w3.org/2000/svg" ' \
  442. 'version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" '
  443. svg_header += 'width="' + svgwidth + uom + '" '
  444. svg_header += 'height="' + svgheight + uom + '" '
  445. svg_header += 'viewBox="' + minx + ' -' + miny + ' ' + svgwidth + ' ' + svgheight + '" '
  446. svg_header += '>'
  447. svg_header += '<g transform="scale(1,-1)">'
  448. svg_footer = '</g> </svg>'
  449. # Change the attributes of the exported SVG
  450. # We don't need stroke-width - wrong, we do when we have lines with certain width
  451. # We set opacity to maximum
  452. # We set the color to WHITE
  453. root = ET.fromstring(exported_svg)
  454. for child in root:
  455. child.set('fill', '#FFFFFF')
  456. child.set('opacity', '1.0')
  457. child.set('stroke', '#FFFFFF')
  458. # first_svg_elem = 'rect x="' + minx + '" ' + 'y="' + miny_rect + '" '
  459. # first_svg_elem += 'width="' + svgwidth + '" ' + 'height="' + svgheight + '" '
  460. # first_svg_elem += 'fill="#000000" opacity="1.0" stroke-width="0.0"'
  461. first_svg_elem_tag = 'rect'
  462. first_svg_elem_attribs = {
  463. 'x': minx,
  464. 'y': miny_rect,
  465. 'width': svgwidth,
  466. 'height': svgheight,
  467. 'id': 'neg_rect',
  468. 'style': 'fill:#000000;opacity:1.0;stroke-width:0.0'
  469. }
  470. root.insert(0, ET.Element(first_svg_elem_tag, first_svg_elem_attribs))
  471. exported_svg = ET.tostring(root)
  472. svg_elem = svg_header + str(exported_svg) + svg_footer
  473. # Parse the xml through a xml parser just to add line feeds
  474. # and to make it look more pretty for the output
  475. doc = parse_xml_string(svg_elem)
  476. doc_final = doc.toprettyxml()
  477. if ftype == 'svg':
  478. try:
  479. with open(filename, 'w') as fp:
  480. fp.write(doc_final)
  481. except PermissionError:
  482. self.app.inform.emit('[WARNING] %s' %
  483. _("Permission denied, saving not possible.\n"
  484. "Most likely another app is holding the file open and not accessible."))
  485. return 'fail'
  486. elif ftype == 'png':
  487. try:
  488. doc_final = StringIO(doc_final)
  489. drawing = svg2rlg(doc_final)
  490. renderPM.drawToFile(drawing, filename, 'PNG')
  491. # if new_png_dpi == default_dpi:
  492. # renderPM.drawToFile(drawing, filename, 'PNG')
  493. # else:
  494. # renderPM.drawToFile(drawing, filename, 'PNG', dpi=new_png_dpi)
  495. except Exception as e:
  496. log.debug("FilmTool.export_negative() --> PNG output --> %s" % str(e))
  497. return 'fail'
  498. else:
  499. try:
  500. if self.units == 'INCH':
  501. unit = inch
  502. else:
  503. unit = mm
  504. doc_final = StringIO(doc_final)
  505. drawing = svg2rlg(doc_final)
  506. p_size = self.ui.pagesize_combo.get_value()
  507. if p_size == 'Bounds':
  508. renderPDF.drawToFile(drawing, filename)
  509. else:
  510. if self.ui.orientation_radio.get_value() == 'p':
  511. page_size = portrait(self.ui.pagesize[p_size])
  512. else:
  513. page_size = landscape(self.ui.pagesize[p_size])
  514. my_canvas = canvas.Canvas(filename, pagesize=page_size)
  515. my_canvas.translate(bounds[0] * unit, bounds[1] * unit)
  516. renderPDF.draw(drawing, my_canvas, 0, 0)
  517. my_canvas.save()
  518. except Exception as e:
  519. log.debug("FilmTool.export_negative() --> PDF output --> %s" % str(e))
  520. return 'fail'
  521. if self.app.defaults["global_open_style"] is False:
  522. self.app.file_opened.emit("SVG", filename)
  523. self.app.file_saved.emit("SVG", filename)
  524. self.app.inform.emit('[success] %s: %s' % (_("Film file exported to"), filename))
  525. if use_thread is True:
  526. def job_thread_film():
  527. with self.app.proc_container.new(_("Working...")):
  528. try:
  529. make_negative_film(scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
  530. except Exception as e:
  531. log.debug("export_negative() process -> %s" % str(e))
  532. return
  533. self.app.worker_task.emit({'fcn': job_thread_film, 'params': []})
  534. else:
  535. make_negative_film(scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
  536. def export_positive(self, obj_name, box_name, filename,
  537. scale_stroke_factor=0.00,
  538. scale_factor_x=1, scale_factor_y=1,
  539. skew_factor_x=None, skew_factor_y=None, skew_reference='center',
  540. mirror=None, orientation_val='p', pagesize_val='A4', color_val='black', opacity_val=1.0,
  541. use_thread=True, ftype='svg'):
  542. """
  543. Exports a Geometry Object to an SVG file in positive black.
  544. :param obj_name: the name of the FlatCAM object to be saved
  545. :param box_name: the name of the FlatCAM object to be used as delimitation of the content to be saved
  546. :param filename: Path to the file to save to.
  547. :param scale_stroke_factor: factor by which to change/scale the thickness of the features
  548. :param scale_factor_x: factor to scale the geometry on the X axis
  549. :param scale_factor_y: factor to scale the geometry on the Y axis
  550. :param skew_factor_x: factor to skew the geometry on the X axis
  551. :param skew_factor_y: factor to skew the geometry on the Y axis
  552. :param skew_reference: reference to use for skew. Can be 'bottomleft', 'bottomright', 'topleft',
  553. 'topright' and those are the 4 points of the bounding box of the geometry to be skewed.
  554. :param mirror: can be 'x' or 'y' or 'both'. Axis on which to mirror the svg geometry
  555. :param orientation_val:
  556. :param pagesize_val:
  557. :param color_val:
  558. :param opacity_val:
  559. :param use_thread: if to be run in a separate thread; boolean
  560. :param ftype: the type of file for saving the film: 'svg', 'png' or 'pdf'
  561. :return:
  562. """
  563. self.app.defaults.report_usage("export_positive()")
  564. if filename is None:
  565. filename = self.app.defaults["global_last_save_folder"]
  566. self.app.log.debug("export_svg() black")
  567. try:
  568. obj = self.app.collection.get_by_name(str(obj_name))
  569. except Exception:
  570. return "Could not retrieve object: %s" % obj_name
  571. try:
  572. box = self.app.collection.get_by_name(str(box_name))
  573. except Exception:
  574. return "Could not retrieve object: %s" % box_name
  575. if box is None:
  576. self.inform.emit('[WARNING_NOTCL] %s: %s' % (_("No object Box. Using instead"), obj))
  577. box = obj
  578. scale_factor_x = scale_factor_x
  579. scale_factor_y = scale_factor_y
  580. p_size = pagesize_val
  581. orientation = orientation_val
  582. color = color_val
  583. transparency_level = opacity_val
  584. def make_positive_film(p_size, orientation, color, transparency_level, scale_factor_x, scale_factor_y):
  585. log.debug("FilmTool.export_positive().make_positive_film()")
  586. scale_reference = 'center'
  587. self.screen_dpi = self.app.qapp.screens()[0].logicalDotsPerInch()
  588. new_png_dpi = self.ui.png_dpi_spinner.get_value()
  589. dpi_rate = new_png_dpi / self.screen_dpi
  590. # Determine bounding area for svg export
  591. bounds = box.bounds()
  592. tr_scale_reference = (bounds[0], bounds[1])
  593. if dpi_rate != 1 and ftype == 'png':
  594. scale_factor_x += dpi_rate
  595. scale_factor_y += dpi_rate
  596. scale_reference = (bounds[0], bounds[1])
  597. if box.kind.lower() == 'geometry':
  598. flat_geo = []
  599. if box.multigeo:
  600. for tool in box.tools:
  601. flat_geo += box.flatten(box.tools[tool]['solid_geometry'])
  602. box_geo = unary_union(flat_geo)
  603. else:
  604. box_geo = unary_union(box.flatten())
  605. else:
  606. box_geo = unary_union(box.flatten())
  607. skew_ref = 'center'
  608. if skew_reference != 'center':
  609. xmin, ymin, xmax, ymax = box_geo.bounds
  610. if skew_reference == 'topleft':
  611. skew_ref = (xmin, ymax)
  612. elif skew_reference == 'bottomleft':
  613. skew_ref = (xmin, ymin)
  614. elif skew_reference == 'topright':
  615. skew_ref = (xmax, ymax)
  616. elif skew_reference == 'bottomright':
  617. skew_ref = (xmax, ymin)
  618. transformed_box_geo = box_geo
  619. if scale_factor_x and not scale_factor_y:
  620. transformed_box_geo = affinity.scale(transformed_box_geo, scale_factor_x, 1.0,
  621. origin=tr_scale_reference)
  622. elif not scale_factor_x and scale_factor_y:
  623. transformed_box_geo = affinity.scale(transformed_box_geo, 1.0, scale_factor_y,
  624. origin=tr_scale_reference)
  625. elif scale_factor_x and scale_factor_y:
  626. transformed_box_geo = affinity.scale(transformed_box_geo, scale_factor_x, scale_factor_y,
  627. origin=tr_scale_reference)
  628. if skew_factor_x and not skew_factor_y:
  629. transformed_box_geo = affinity.skew(transformed_box_geo, skew_factor_x, 0.0, origin=skew_ref)
  630. elif not skew_factor_x and skew_factor_y:
  631. transformed_box_geo = affinity.skew(transformed_box_geo, 0.0, skew_factor_y, origin=skew_ref)
  632. elif skew_factor_x and skew_factor_y:
  633. transformed_box_geo = affinity.skew(transformed_box_geo, skew_factor_x, skew_factor_y, origin=skew_ref)
  634. if mirror:
  635. if mirror == 'x':
  636. transformed_box_geo = affinity.scale(transformed_box_geo, 1.0, -1.0)
  637. if mirror == 'y':
  638. transformed_box_geo = affinity.scale(transformed_box_geo, -1.0, 1.0)
  639. if mirror == 'both':
  640. transformed_box_geo = affinity.scale(transformed_box_geo, -1.0, -1.0)
  641. bounds = transformed_box_geo.bounds
  642. size = bounds[2] - bounds[0], bounds[3] - bounds[1]
  643. exported_svg = obj.export_svg(scale_stroke_factor=scale_stroke_factor,
  644. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y,
  645. skew_factor_x=skew_factor_x, skew_factor_y=skew_factor_y,
  646. mirror=mirror,
  647. scale_reference=scale_reference, skew_reference=skew_reference
  648. )
  649. # Change the attributes of the exported SVG
  650. # We don't need stroke-width
  651. # We set opacity to maximum
  652. # We set the colour to WHITE
  653. root = ET.fromstring(exported_svg)
  654. for child in root:
  655. child.set('fill', str(color))
  656. child.set('opacity', str(transparency_level))
  657. child.set('stroke', str(color))
  658. exported_svg = ET.tostring(root)
  659. # This contain the measure units
  660. uom = obj.units.lower()
  661. # Define a boundary around SVG of about 1.0mm (~39mils)
  662. if uom in "mm":
  663. boundary = 1.0
  664. else:
  665. boundary = 0.0393701
  666. # Convert everything to strings for use in the xml doc
  667. svgwidth = str(size[0] + (2 * boundary))
  668. svgheight = str(size[1] + (2 * boundary))
  669. minx = str(bounds[0] - boundary)
  670. miny = str(bounds[1] + boundary + size[1])
  671. # Add a SVG Header and footer to the svg output from shapely
  672. # The transform flips the Y Axis so that everything renders
  673. # properly within svg apps such as inkscape
  674. svg_header = '<svg xmlns="http://www.w3.org/2000/svg" ' \
  675. 'version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" '
  676. svg_header += 'width="' + svgwidth + uom + '" '
  677. svg_header += 'height="' + svgheight + uom + '" '
  678. svg_header += 'viewBox="' + minx + ' -' + miny + ' ' + svgwidth + ' ' + svgheight + '" '
  679. svg_header += '>'
  680. svg_header += '<g transform="scale(1,-1)">'
  681. svg_footer = '</g> </svg>'
  682. svg_elem = str(svg_header) + str(exported_svg) + str(svg_footer)
  683. # Parse the xml through a xml parser just to add line feeds
  684. # and to make it look more pretty for the output
  685. doc = parse_xml_string(svg_elem)
  686. doc_final = doc.toprettyxml()
  687. if ftype == 'svg':
  688. try:
  689. with open(filename, 'w') as fp:
  690. fp.write(doc_final)
  691. except PermissionError:
  692. self.app.inform.emit('[WARNING] %s' %
  693. _("Permission denied, saving not possible.\n"
  694. "Most likely another app is holding the file open and not accessible."))
  695. return 'fail'
  696. elif ftype == 'png':
  697. try:
  698. doc_final = StringIO(doc_final)
  699. drawing = svg2rlg(doc_final)
  700. renderPM.drawToFile(drawing, filename, 'PNG')
  701. except Exception as e:
  702. log.debug("FilmTool.export_positive() --> PNG output --> %s" % str(e))
  703. return 'fail'
  704. else:
  705. try:
  706. if self.units == 'IN':
  707. unit = inch
  708. else:
  709. unit = mm
  710. doc_final = StringIO(doc_final)
  711. drawing = svg2rlg(doc_final)
  712. if p_size == 'Bounds':
  713. renderPDF.drawToFile(drawing, filename)
  714. else:
  715. if orientation == 'p':
  716. page_size = portrait(self.ui.pagesize[p_size])
  717. else:
  718. page_size = landscape(self.ui.pagesize[p_size])
  719. my_canvas = canvas.Canvas(filename, pagesize=page_size)
  720. my_canvas.translate(bounds[0] * unit, bounds[1] * unit)
  721. renderPDF.draw(drawing, my_canvas, 0, 0)
  722. my_canvas.save()
  723. except Exception as e:
  724. log.debug("FilmTool.export_positive() --> PDF output --> %s" % str(e))
  725. return 'fail'
  726. if self.app.defaults["global_open_style"] is False:
  727. self.app.file_opened.emit("SVG", filename)
  728. self.app.file_saved.emit("SVG", filename)
  729. self.app.inform.emit('[success] %s: %s' % (_("Film file exported to"), filename))
  730. if use_thread is True:
  731. def job_thread_film():
  732. with self.app.proc_container.new(_("Working...")):
  733. try:
  734. make_positive_film(p_size=p_size, orientation=orientation, color=color,
  735. transparency_level=transparency_level,
  736. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
  737. except Exception as e:
  738. log.debug("export_positive() process -> %s" % str(e))
  739. return
  740. self.app.worker_task.emit({'fcn': job_thread_film, 'params': []})
  741. else:
  742. make_positive_film(p_size=p_size, orientation=orientation, color=color,
  743. transparency_level=transparency_level,
  744. scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y)
  745. def reset_fields(self):
  746. self.ui.tf_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  747. self.ui.tf_box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  748. class FilmUI:
  749. toolName = _("Film PCB")
  750. def __init__(self, layout, app):
  751. self.app = app
  752. self.decimals = self.app.decimals
  753. self.layout = layout
  754. # ## Title
  755. title_label = FCLabel("%s" % self.toolName)
  756. title_label.setStyleSheet("""
  757. QLabel
  758. {
  759. font-size: 16px;
  760. font-weight: bold;
  761. }
  762. """)
  763. self.layout.addWidget(title_label)
  764. self.layout.addWidget(FCLabel(""))
  765. # Form Layout
  766. grid0 = QtWidgets.QGridLayout()
  767. self.layout.addLayout(grid0)
  768. grid0.setColumnStretch(0, 0)
  769. grid0.setColumnStretch(1, 1)
  770. # Type of object for which to create the film
  771. self.tf_type_obj_combo = RadioSet([{'label': _('Gerber'), 'value': 'grb'},
  772. {'label': _('Geometry'), 'value': 'geo'}])
  773. self.tf_type_obj_combo_label = FCLabel('<b>%s</b>:' % _("Object"))
  774. self.tf_type_obj_combo_label.setToolTip(
  775. _("Specify the type of object for which to create the film.\n"
  776. "The object can be of type: Gerber or Geometry.\n"
  777. "The selection here decide the type of objects that will be\n"
  778. "in the Film Object combobox.")
  779. )
  780. grid0.addWidget(self.tf_type_obj_combo_label, 0, 0)
  781. grid0.addWidget(self.tf_type_obj_combo, 0, 1)
  782. # List of objects for which we can create the film
  783. self.tf_object_combo = FCComboBox()
  784. self.tf_object_combo.setModel(self.app.collection)
  785. self.tf_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  786. self.tf_object_combo.is_last = True
  787. grid0.addWidget(self.tf_object_combo, 1, 0, 1, 2)
  788. # Type of Box Object to be used as an envelope for film creation
  789. # Within this we can create negative
  790. self.tf_type_box_combo = RadioSet([{'label': _('Gerber'), 'value': 'grb'},
  791. {'label': _('Geometry'), 'value': 'geo'}])
  792. self.tf_type_box_combo_label = FCLabel(_("Box Type:"))
  793. self.tf_type_box_combo_label.setToolTip(
  794. _("Specify the type of object to be used as an container for\n"
  795. "film creation. It can be: Gerber or Geometry type."
  796. "The selection here decide the type of objects that will be\n"
  797. "in the Box Object combobox.")
  798. )
  799. grid0.addWidget(self.tf_type_box_combo_label, 2, 0)
  800. grid0.addWidget(self.tf_type_box_combo, 2, 1)
  801. # Box
  802. self.tf_box_combo = FCComboBox()
  803. self.tf_box_combo.setModel(self.app.collection)
  804. self.tf_box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  805. self.tf_box_combo.is_last = True
  806. grid0.addWidget(self.tf_box_combo, 3, 0, 1, 2)
  807. separator_line = QtWidgets.QFrame()
  808. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  809. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  810. grid0.addWidget(separator_line, 4, 0, 1, 2)
  811. self.film_adj_label = FCLabel('<b>%s</b>' % _("Film Adjustments"))
  812. self.film_adj_label.setToolTip(
  813. _("Sometime the printers will distort the print shape, especially the Laser types.\n"
  814. "This section provide the tools to compensate for the print distortions.")
  815. )
  816. grid0.addWidget(self.film_adj_label, 5, 0, 1, 2)
  817. # Scale Geometry
  818. self.film_scale_cb = FCCheckBox('%s' % _("Scale Film geometry"))
  819. self.film_scale_cb.setToolTip(
  820. _("A value greater than 1 will stretch the film\n"
  821. "while a value less than 1 will jolt it.")
  822. )
  823. self.film_scale_cb.setStyleSheet(
  824. """
  825. QCheckBox {font-weight: bold; color: black}
  826. """
  827. )
  828. grid0.addWidget(self.film_scale_cb, 6, 0, 1, 2)
  829. self.film_scalex_label = FCLabel('%s:' % _("X factor"))
  830. self.film_scalex_entry = FCDoubleSpinner(callback=self.confirmation_message)
  831. self.film_scalex_entry.set_range(-999.9999, 999.9999)
  832. self.film_scalex_entry.set_precision(self.decimals)
  833. self.film_scalex_entry.setSingleStep(0.01)
  834. grid0.addWidget(self.film_scalex_label, 7, 0)
  835. grid0.addWidget(self.film_scalex_entry, 7, 1)
  836. self.film_scaley_label = FCLabel('%s:' % _("Y factor"))
  837. self.film_scaley_entry = FCDoubleSpinner(callback=self.confirmation_message)
  838. self.film_scaley_entry.set_range(-999.9999, 999.9999)
  839. self.film_scaley_entry.set_precision(self.decimals)
  840. self.film_scaley_entry.setSingleStep(0.01)
  841. grid0.addWidget(self.film_scaley_label, 8, 0)
  842. grid0.addWidget(self.film_scaley_entry, 8, 1)
  843. self.ois_scale = OptionalHideInputSection(self.film_scale_cb,
  844. [
  845. self.film_scalex_label,
  846. self.film_scalex_entry,
  847. self.film_scaley_label,
  848. self.film_scaley_entry
  849. ])
  850. separator_line = QtWidgets.QFrame()
  851. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  852. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  853. grid0.addWidget(separator_line, 9, 0, 1, 2)
  854. # Skew Geometry
  855. self.film_skew_cb = FCCheckBox('%s' % _("Skew Film geometry"))
  856. self.film_skew_cb.setToolTip(
  857. _("Positive values will skew to the right\n"
  858. "while negative values will skew to the left.")
  859. )
  860. self.film_skew_cb.setStyleSheet(
  861. """
  862. QCheckBox {font-weight: bold; color: black}
  863. """
  864. )
  865. grid0.addWidget(self.film_skew_cb, 10, 0, 1, 2)
  866. self.film_skewx_label = FCLabel('%s:' % _("X angle"))
  867. self.film_skewx_entry = FCDoubleSpinner(callback=self.confirmation_message)
  868. self.film_skewx_entry.set_range(-999.9999, 999.9999)
  869. self.film_skewx_entry.set_precision(self.decimals)
  870. self.film_skewx_entry.setSingleStep(0.01)
  871. grid0.addWidget(self.film_skewx_label, 11, 0)
  872. grid0.addWidget(self.film_skewx_entry, 11, 1)
  873. self.film_skewy_label = FCLabel('%s:' % _("Y angle"))
  874. self.film_skewy_entry = FCDoubleSpinner(callback=self.confirmation_message)
  875. self.film_skewy_entry.set_range(-999.9999, 999.9999)
  876. self.film_skewy_entry.set_precision(self.decimals)
  877. self.film_skewy_entry.setSingleStep(0.01)
  878. grid0.addWidget(self.film_skewy_label, 12, 0)
  879. grid0.addWidget(self.film_skewy_entry, 12, 1)
  880. self.film_skew_ref_label = FCLabel('%s:' % _("Reference"))
  881. self.film_skew_ref_label.setToolTip(
  882. _("The reference point to be used as origin for the skew.\n"
  883. "It can be one of the four points of the geometry bounding box.")
  884. )
  885. self.film_skew_reference = RadioSet([{'label': _('Bottom Left'), 'value': 'bottomleft'},
  886. {'label': _('Top Left'), 'value': 'topleft'},
  887. {'label': _('Bottom Right'), 'value': 'bottomright'},
  888. {'label': _('Top right'), 'value': 'topright'}],
  889. orientation='vertical',
  890. stretch=False)
  891. grid0.addWidget(self.film_skew_ref_label, 13, 0)
  892. grid0.addWidget(self.film_skew_reference, 13, 1)
  893. self.ois_skew = OptionalHideInputSection(self.film_skew_cb,
  894. [
  895. self.film_skewx_label,
  896. self.film_skewx_entry,
  897. self.film_skewy_label,
  898. self.film_skewy_entry,
  899. self.film_skew_ref_label,
  900. self.film_skew_reference
  901. ])
  902. separator_line1 = QtWidgets.QFrame()
  903. separator_line1.setFrameShape(QtWidgets.QFrame.HLine)
  904. separator_line1.setFrameShadow(QtWidgets.QFrame.Sunken)
  905. grid0.addWidget(separator_line1, 14, 0, 1, 2)
  906. # Mirror Geometry
  907. self.film_mirror_cb = FCCheckBox('%s' % _("Mirror Film geometry"))
  908. self.film_mirror_cb.setToolTip(
  909. _("Mirror the film geometry on the selected axis or on both.")
  910. )
  911. self.film_mirror_cb.setStyleSheet(
  912. """
  913. QCheckBox {font-weight: bold; color: black}
  914. """
  915. )
  916. grid0.addWidget(self.film_mirror_cb, 15, 0, 1, 2)
  917. self.film_mirror_axis = RadioSet([{'label': _('None'), 'value': 'none'},
  918. {'label': _('X'), 'value': 'x'},
  919. {'label': _('Y'), 'value': 'y'},
  920. {'label': _('Both'), 'value': 'both'}],
  921. stretch=False)
  922. self.film_mirror_axis_label = FCLabel('%s:' % _("Mirror axis"))
  923. grid0.addWidget(self.film_mirror_axis_label, 16, 0)
  924. grid0.addWidget(self.film_mirror_axis, 16, 1)
  925. self.ois_mirror = OptionalHideInputSection(self.film_mirror_cb,
  926. [
  927. self.film_mirror_axis_label,
  928. self.film_mirror_axis
  929. ])
  930. separator_line2 = QtWidgets.QFrame()
  931. separator_line2.setFrameShape(QtWidgets.QFrame.HLine)
  932. separator_line2.setFrameShadow(QtWidgets.QFrame.Sunken)
  933. grid0.addWidget(separator_line2, 17, 0, 1, 2)
  934. self.film_param_label = FCLabel('<b>%s</b>' % _("Film Parameters"))
  935. grid0.addWidget(self.film_param_label, 18, 0, 1, 2)
  936. # Scale Stroke size
  937. self.film_scale_stroke_entry = FCDoubleSpinner(callback=self.confirmation_message)
  938. self.film_scale_stroke_entry.set_range(-999.9999, 999.9999)
  939. self.film_scale_stroke_entry.setSingleStep(0.01)
  940. self.film_scale_stroke_entry.set_precision(self.decimals)
  941. self.film_scale_stroke_label = FCLabel('%s:' % _("Scale Stroke"))
  942. self.film_scale_stroke_label.setToolTip(
  943. _("Scale the line stroke thickness of each feature in the SVG file.\n"
  944. "It means that the line that envelope each SVG feature will be thicker or thinner,\n"
  945. "therefore the fine features may be more affected by this parameter.")
  946. )
  947. grid0.addWidget(self.film_scale_stroke_label, 19, 0)
  948. grid0.addWidget(self.film_scale_stroke_entry, 19, 1)
  949. # Film Type
  950. self.film_type = RadioSet([{'label': _('Positive'), 'value': 'pos'},
  951. {'label': _('Negative'), 'value': 'neg'}],
  952. stretch=False)
  953. self.film_type_label = FCLabel(_("Film Type:"))
  954. self.film_type_label.setToolTip(
  955. _("Generate a Positive black film or a Negative film.\n"
  956. "Positive means that it will print the features\n"
  957. "with black on a white canvas.\n"
  958. "Negative means that it will print the features\n"
  959. "with white on a black canvas.\n"
  960. "The Film format is SVG.")
  961. )
  962. grid0.addWidget(self.film_type_label, 21, 0)
  963. grid0.addWidget(self.film_type, 21, 1)
  964. # Boundary for negative film generation
  965. self.boundary_entry = FCDoubleSpinner(callback=self.confirmation_message)
  966. self.boundary_entry.set_range(-999.9999, 999.9999)
  967. self.boundary_entry.setSingleStep(0.01)
  968. self.boundary_entry.set_precision(self.decimals)
  969. self.boundary_label = FCLabel('%s:' % _("Border"))
  970. self.boundary_label.setToolTip(
  971. _("Specify a border around the object.\n"
  972. "Only for negative film.\n"
  973. "It helps if we use as a Box Object the same \n"
  974. "object as in Film Object. It will create a thick\n"
  975. "black bar around the actual print allowing for a\n"
  976. "better delimitation of the outline features which are of\n"
  977. "white color like the rest and which may confound with the\n"
  978. "surroundings if not for this border.")
  979. )
  980. grid0.addWidget(self.boundary_label, 22, 0)
  981. grid0.addWidget(self.boundary_entry, 22, 1)
  982. self.boundary_label.hide()
  983. self.boundary_entry.hide()
  984. # Punch Drill holes
  985. self.punch_cb = FCCheckBox(_("Punch drill holes"))
  986. self.punch_cb.setToolTip(_("When checked the generated film will have holes in pads when\n"
  987. "the generated film is positive. This is done to help drilling,\n"
  988. "when done manually."))
  989. grid0.addWidget(self.punch_cb, 23, 0, 1, 2)
  990. # this way I can hide/show the frame
  991. self.punch_frame = QtWidgets.QFrame()
  992. self.punch_frame.setContentsMargins(0, 0, 0, 0)
  993. self.layout.addWidget(self.punch_frame)
  994. punch_grid = QtWidgets.QGridLayout()
  995. punch_grid.setContentsMargins(0, 0, 0, 0)
  996. self.punch_frame.setLayout(punch_grid)
  997. punch_grid.setColumnStretch(0, 0)
  998. punch_grid.setColumnStretch(1, 1)
  999. self.ois_p = OptionalHideInputSection(self.punch_cb, [self.punch_frame])
  1000. self.source_label = FCLabel('%s:' % _("Source"))
  1001. self.source_label.setToolTip(
  1002. _("The punch hole source can be:\n"
  1003. "- Excellon -> an Excellon holes center will serve as reference.\n"
  1004. "- Pad Center -> will try to use the pads center as reference.")
  1005. )
  1006. self.source_punch = RadioSet([{'label': _('Excellon'), 'value': 'exc'},
  1007. {'label': _('Pad center'), 'value': 'pad'}],
  1008. stretch=False)
  1009. punch_grid.addWidget(self.source_label, 0, 0)
  1010. punch_grid.addWidget(self.source_punch, 0, 1)
  1011. self.exc_label = FCLabel('%s:' % _("Excellon Obj"))
  1012. self.exc_label.setToolTip(
  1013. _("Remove the geometry of Excellon from the Film to create the holes in pads.")
  1014. )
  1015. self.exc_combo = FCComboBox()
  1016. self.exc_combo.setModel(self.app.collection)
  1017. self.exc_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  1018. self.exc_combo.is_last = True
  1019. self.exc_combo.obj_type = "Excellon"
  1020. punch_grid.addWidget(self.exc_label, 1, 0)
  1021. punch_grid.addWidget(self.exc_combo, 1, 1)
  1022. self.exc_label.hide()
  1023. self.exc_combo.hide()
  1024. self.punch_size_label = FCLabel('%s:' % _("Punch Size"))
  1025. self.punch_size_label.setToolTip(_("The value here will control how big is the punch hole in the pads."))
  1026. self.punch_size_spinner = FCDoubleSpinner(callback=self.confirmation_message)
  1027. self.punch_size_spinner.set_range(0, 999.9999)
  1028. self.punch_size_spinner.setSingleStep(0.1)
  1029. self.punch_size_spinner.set_precision(self.decimals)
  1030. punch_grid.addWidget(self.punch_size_label, 2, 0)
  1031. punch_grid.addWidget(self.punch_size_spinner, 2, 1)
  1032. self.punch_size_label.hide()
  1033. self.punch_size_spinner.hide()
  1034. grid1 = QtWidgets.QGridLayout()
  1035. self.layout.addLayout(grid1)
  1036. grid1.setColumnStretch(0, 0)
  1037. grid1.setColumnStretch(1, 1)
  1038. separator_line3 = QtWidgets.QFrame()
  1039. separator_line3.setFrameShape(QtWidgets.QFrame.HLine)
  1040. separator_line3.setFrameShadow(QtWidgets.QFrame.Sunken)
  1041. grid1.addWidget(separator_line3, 0, 0, 1, 2)
  1042. # File type
  1043. self.file_type_radio = RadioSet([{'label': _('SVG'), 'value': 'svg'},
  1044. {'label': _('PNG'), 'value': 'png'},
  1045. {'label': _('PDF'), 'value': 'pdf'}
  1046. ], stretch=False)
  1047. self.file_type_label = FCLabel(_("Film Type:"))
  1048. self.file_type_label.setToolTip(
  1049. _("The file type of the saved film. Can be:\n"
  1050. "- 'SVG' -> open-source vectorial format\n"
  1051. "- 'PNG' -> raster image\n"
  1052. "- 'PDF' -> portable document format")
  1053. )
  1054. grid1.addWidget(self.file_type_label, 1, 0)
  1055. grid1.addWidget(self.file_type_radio, 1, 1)
  1056. # Page orientation
  1057. self.orientation_label = FCLabel('%s:' % _("Page Orientation"))
  1058. self.orientation_label.setToolTip(_("Can be:\n"
  1059. "- Portrait\n"
  1060. "- Landscape"))
  1061. self.orientation_radio = RadioSet([{'label': _('Portrait'), 'value': 'p'},
  1062. {'label': _('Landscape'), 'value': 'l'},
  1063. ], stretch=False)
  1064. grid1.addWidget(self.orientation_label, 2, 0)
  1065. grid1.addWidget(self.orientation_radio, 2, 1)
  1066. # Page Size
  1067. self.pagesize_label = FCLabel('%s:' % _("Page Size"))
  1068. self.pagesize_label.setToolTip(_("A selection of standard ISO 216 page sizes."))
  1069. self.pagesize_combo = FCComboBox()
  1070. self.pagesize = {}
  1071. self.pagesize.update(
  1072. {
  1073. 'Bounds': None,
  1074. 'A0': (841 * mm, 1189 * mm),
  1075. 'A1': (594 * mm, 841 * mm),
  1076. 'A2': (420 * mm, 594 * mm),
  1077. 'A3': (297 * mm, 420 * mm),
  1078. 'A4': (210 * mm, 297 * mm),
  1079. 'A5': (148 * mm, 210 * mm),
  1080. 'A6': (105 * mm, 148 * mm),
  1081. 'A7': (74 * mm, 105 * mm),
  1082. 'A8': (52 * mm, 74 * mm),
  1083. 'A9': (37 * mm, 52 * mm),
  1084. 'A10': (26 * mm, 37 * mm),
  1085. 'B0': (1000 * mm, 1414 * mm),
  1086. 'B1': (707 * mm, 1000 * mm),
  1087. 'B2': (500 * mm, 707 * mm),
  1088. 'B3': (353 * mm, 500 * mm),
  1089. 'B4': (250 * mm, 353 * mm),
  1090. 'B5': (176 * mm, 250 * mm),
  1091. 'B6': (125 * mm, 176 * mm),
  1092. 'B7': (88 * mm, 125 * mm),
  1093. 'B8': (62 * mm, 88 * mm),
  1094. 'B9': (44 * mm, 62 * mm),
  1095. 'B10': (31 * mm, 44 * mm),
  1096. 'C0': (917 * mm, 1297 * mm),
  1097. 'C1': (648 * mm, 917 * mm),
  1098. 'C2': (458 * mm, 648 * mm),
  1099. 'C3': (324 * mm, 458 * mm),
  1100. 'C4': (229 * mm, 324 * mm),
  1101. 'C5': (162 * mm, 229 * mm),
  1102. 'C6': (114 * mm, 162 * mm),
  1103. 'C7': (81 * mm, 114 * mm),
  1104. 'C8': (57 * mm, 81 * mm),
  1105. 'C9': (40 * mm, 57 * mm),
  1106. 'C10': (28 * mm, 40 * mm),
  1107. # American paper sizes
  1108. 'LETTER': (8.5 * inch, 11 * inch),
  1109. 'LEGAL': (8.5 * inch, 14 * inch),
  1110. 'ELEVENSEVENTEEN': (11 * inch, 17 * inch),
  1111. # From https://en.wikipedia.org/wiki/Paper_size
  1112. 'JUNIOR_LEGAL': (5 * inch, 8 * inch),
  1113. 'HALF_LETTER': (5.5 * inch, 8 * inch),
  1114. 'GOV_LETTER': (8 * inch, 10.5 * inch),
  1115. 'GOV_LEGAL': (8.5 * inch, 13 * inch),
  1116. 'LEDGER': (17 * inch, 11 * inch),
  1117. }
  1118. )
  1119. page_size_list = list(self.pagesize.keys())
  1120. self.pagesize_combo.addItems(page_size_list)
  1121. grid1.addWidget(self.pagesize_label, 3, 0)
  1122. grid1.addWidget(self.pagesize_combo, 3, 1)
  1123. self.on_film_type(val='hide')
  1124. # PNG DPI
  1125. self.png_dpi_label = FCLabel('%s:' % "PNG DPI")
  1126. self.png_dpi_label.setToolTip(
  1127. _("Default value is 96 DPI. Change this value to scale the PNG file.")
  1128. )
  1129. self.png_dpi_spinner = FCSpinner(callback=self.confirmation_message_int)
  1130. self.png_dpi_spinner.set_range(0, 100000)
  1131. grid1.addWidget(self.png_dpi_label, 4, 0)
  1132. grid1.addWidget(self.png_dpi_spinner, 4, 1)
  1133. self.png_dpi_label.hide()
  1134. self.png_dpi_spinner.hide()
  1135. # Buttons
  1136. self.film_object_button = FCButton(_("Save Film"))
  1137. self.film_object_button.setIcon(QtGui.QIcon(self.app.resource_location + '/save_as.png'))
  1138. self.film_object_button.setToolTip(
  1139. _("Create a Film for the selected object, within\n"
  1140. "the specified box. Does not create a new \n "
  1141. "FlatCAM object, but directly save it in the\n"
  1142. "selected format.")
  1143. )
  1144. self.film_object_button.setStyleSheet("""
  1145. QPushButton
  1146. {
  1147. font-weight: bold;
  1148. }
  1149. """)
  1150. grid1.addWidget(self.film_object_button, 6, 0, 1, 2)
  1151. self.layout.addStretch()
  1152. # ## Reset Tool
  1153. self.reset_button = FCButton(_("Reset Tool"))
  1154. self.reset_button.setIcon(QtGui.QIcon(self.app.resource_location + '/reset32.png'))
  1155. self.reset_button.setToolTip(
  1156. _("Will reset the tool parameters.")
  1157. )
  1158. self.reset_button.setStyleSheet("""
  1159. QPushButton
  1160. {
  1161. font-weight: bold;
  1162. }
  1163. """)
  1164. self.layout.addWidget(self.reset_button)
  1165. # #################################### FINSIHED GUI ###########################
  1166. # #############################################################################
  1167. def on_film_type(self, val):
  1168. type_of_film = val
  1169. if type_of_film == 'neg':
  1170. self.boundary_label.show()
  1171. self.boundary_entry.show()
  1172. self.punch_cb.set_value(False) # required so the self.punch_frame it's hidden also by the signal emitted
  1173. self.punch_cb.hide()
  1174. else:
  1175. self.boundary_label.hide()
  1176. self.boundary_entry.hide()
  1177. self.punch_cb.show()
  1178. def on_file_type(self, val):
  1179. if val == 'pdf':
  1180. self.orientation_label.show()
  1181. self.orientation_radio.show()
  1182. self.pagesize_label.show()
  1183. self.pagesize_combo.show()
  1184. self.png_dpi_label.hide()
  1185. self.png_dpi_spinner.hide()
  1186. elif val == 'png':
  1187. self.png_dpi_label.show()
  1188. self.png_dpi_spinner.show()
  1189. self.orientation_label.hide()
  1190. self.orientation_radio.hide()
  1191. self.pagesize_label.hide()
  1192. self.pagesize_combo.hide()
  1193. else:
  1194. self.orientation_label.hide()
  1195. self.orientation_radio.hide()
  1196. self.pagesize_label.hide()
  1197. self.pagesize_combo.hide()
  1198. self.png_dpi_label.hide()
  1199. self.png_dpi_spinner.hide()
  1200. def on_punch_source(self, val):
  1201. if val == 'pad' and self.punch_cb.get_value():
  1202. self.punch_size_label.show()
  1203. self.punch_size_spinner.show()
  1204. self.exc_label.hide()
  1205. self.exc_combo.hide()
  1206. else:
  1207. self.punch_size_label.hide()
  1208. self.punch_size_spinner.hide()
  1209. self.exc_label.show()
  1210. self.exc_combo.show()
  1211. if val == 'pad' and self.tf_type_obj_combo.get_value() == 'geo':
  1212. self.source_punch.set_value('exc')
  1213. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Using the Pad center does not work on Geometry objects. "
  1214. "Only a Gerber object has pads."))
  1215. def confirmation_message(self, accepted, minval, maxval):
  1216. if accepted is False:
  1217. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%.*f, %.*f]' % (_("Edited value is out of range"),
  1218. self.decimals,
  1219. minval,
  1220. self.decimals,
  1221. maxval), False)
  1222. else:
  1223. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
  1224. def confirmation_message_int(self, accepted, minval, maxval):
  1225. if accepted is False:
  1226. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%d, %d]' %
  1227. (_("Edited value is out of range"), minval, maxval), False)
  1228. else:
  1229. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)