FlatCAMGerber.py 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # ##########################################################
  8. # ##########################################################
  9. # File modified by: Marius Stanciu #
  10. # ##########################################################
  11. from shapely.geometry import Point, Polygon, MultiPolygon, MultiLineString, LineString, LinearRing
  12. from shapely.ops import cascaded_union
  13. from AppParsers.ParseGerber import Gerber
  14. from AppObjects.FlatCAMObj import *
  15. import math
  16. import numpy as np
  17. from copy import deepcopy
  18. import gettext
  19. import AppTranslation as fcTranslate
  20. import builtins
  21. fcTranslate.apply_language('strings')
  22. if '_' not in builtins.__dict__:
  23. _ = gettext.gettext
  24. class GerberObject(FlatCAMObj, Gerber):
  25. """
  26. Represents Gerber code.
  27. """
  28. optionChanged = QtCore.pyqtSignal(str)
  29. replotApertures = QtCore.pyqtSignal()
  30. ui_type = GerberObjectUI
  31. @staticmethod
  32. def merge(grb_list, grb_final):
  33. """
  34. Merges the geometry of objects in geo_list into
  35. the geometry of geo_final.
  36. :param grb_list: List of GerberObject Objects to join.
  37. :param grb_final: Destination GeometryObject object.
  38. :return: None
  39. """
  40. if grb_final.solid_geometry is None:
  41. grb_final.solid_geometry = []
  42. grb_final.follow_geometry = []
  43. if not grb_final.apertures:
  44. grb_final.apertures = {}
  45. if type(grb_final.solid_geometry) is not list:
  46. grb_final.solid_geometry = [grb_final.solid_geometry]
  47. grb_final.follow_geometry = [grb_final.follow_geometry]
  48. for grb in grb_list:
  49. # Expand lists
  50. if type(grb) is list:
  51. GerberObject.merge(grb_list=grb, grb_final=grb_final)
  52. else: # If not list, just append
  53. for option in grb.options:
  54. if option != 'name':
  55. try:
  56. grb_final.options[option] = grb.options[option]
  57. except KeyError:
  58. log.warning("Failed to copy option.", option)
  59. try:
  60. for geos in grb.solid_geometry:
  61. grb_final.solid_geometry.append(geos)
  62. grb_final.follow_geometry.append(geos)
  63. except TypeError:
  64. grb_final.solid_geometry.append(grb.solid_geometry)
  65. grb_final.follow_geometry.append(grb.solid_geometry)
  66. for ap in grb.apertures:
  67. if ap not in grb_final.apertures:
  68. grb_final.apertures[ap] = grb.apertures[ap]
  69. else:
  70. # create a list of integers out of the grb.apertures keys and find the max of that value
  71. # then, the aperture duplicate is assigned an id value incremented with 1,
  72. # and finally made string because the apertures dict keys are strings
  73. max_ap = str(max([int(k) for k in grb_final.apertures.keys()]) + 1)
  74. grb_final.apertures[max_ap] = {}
  75. grb_final.apertures[max_ap]['geometry'] = []
  76. for k, v in grb.apertures[ap].items():
  77. grb_final.apertures[max_ap][k] = deepcopy(v)
  78. grb_final.solid_geometry = MultiPolygon(grb_final.solid_geometry)
  79. grb_final.follow_geometry = MultiPolygon(grb_final.follow_geometry)
  80. def __init__(self, name):
  81. self.decimals = self.app.decimals
  82. self.circle_steps = int(self.app.defaults["gerber_circle_steps"])
  83. Gerber.__init__(self, steps_per_circle=self.circle_steps)
  84. FlatCAMObj.__init__(self, name)
  85. self.kind = "gerber"
  86. # The 'name' is already in self.options from FlatCAMObj
  87. # Automatically updates the UI
  88. self.options.update({
  89. "plot": True,
  90. "multicolored": False,
  91. "solid": False,
  92. "noncoppermargin": 0.0,
  93. "noncopperrounded": False,
  94. "bboxmargin": 0.0,
  95. "bboxrounded": False,
  96. "aperture_display": False,
  97. "follow": False,
  98. "milling_type": 'cl',
  99. })
  100. # type of isolation: 0 = exteriors, 1 = interiors, 2 = complete isolation (both interiors and exteriors)
  101. self.iso_type = 2
  102. self.multigeo = False
  103. self.follow = False
  104. self.apertures_row = 0
  105. # store the source file here
  106. self.source_file = ""
  107. # list of rows with apertures plotted
  108. self.marked_rows = []
  109. # Mouse events
  110. self.mr = None
  111. self.mm = None
  112. self.mp = None
  113. # dict to store the polygons selected for isolation; key is the shape added to be plotted and value is the poly
  114. self.poly_dict = {}
  115. # store the status of grid snapping
  116. self.grid_status_memory = None
  117. self.units_found = self.app.defaults['units']
  118. self.fill_color = self.app.defaults['gerber_plot_fill']
  119. self.outline_color = self.app.defaults['gerber_plot_line']
  120. self.alpha_level = 'bf'
  121. # keep track if the UI is built so we don't have to build it every time
  122. self.ui_build = False
  123. # build only once the aperture storage (takes time)
  124. self.build_aperture_storage = False
  125. # Attributes to be included in serialization
  126. # Always append to it because it carries contents
  127. # from predecessors.
  128. self.ser_attrs += ['options', 'kind', 'fill_color', 'outline_color', 'alpha_level']
  129. def set_ui(self, ui):
  130. """
  131. Maps options with GUI inputs.
  132. Connects GUI events to methods.
  133. :param ui: GUI object.
  134. :type ui: GerberObjectUI
  135. :return: None
  136. """
  137. FlatCAMObj.set_ui(self, ui)
  138. log.debug("GerberObject.set_ui()")
  139. self.units = self.app.defaults['units'].upper()
  140. self.replotApertures.connect(self.on_mark_cb_click_table)
  141. self.form_fields.update({
  142. "plot": self.ui.plot_cb,
  143. "multicolored": self.ui.multicolored_cb,
  144. "solid": self.ui.solid_cb,
  145. "noncoppermargin": self.ui.noncopper_margin_entry,
  146. "noncopperrounded": self.ui.noncopper_rounded_cb,
  147. "bboxmargin": self.ui.bbmargin_entry,
  148. "bboxrounded": self.ui.bbrounded_cb,
  149. "aperture_display": self.ui.aperture_table_visibility_cb,
  150. "follow": self.ui.follow_cb
  151. })
  152. # Fill form fields only on object create
  153. self.to_form()
  154. assert isinstance(self.ui, GerberObjectUI)
  155. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  156. self.ui.solid_cb.stateChanged.connect(self.on_solid_cb_click)
  157. self.ui.multicolored_cb.stateChanged.connect(self.on_multicolored_cb_click)
  158. # Tools
  159. self.ui.iso_button.clicked.connect(self.app.isolation_tool.run)
  160. self.ui.generate_ncc_button.clicked.connect(self.app.ncclear_tool.run)
  161. self.ui.generate_cutout_button.clicked.connect(self.app.cutout_tool.run)
  162. self.ui.generate_bb_button.clicked.connect(self.on_generatebb_button_click)
  163. self.ui.generate_noncopper_button.clicked.connect(self.on_generatenoncopper_button_click)
  164. self.ui.aperture_table_visibility_cb.stateChanged.connect(self.on_aperture_table_visibility_change)
  165. self.ui.follow_cb.stateChanged.connect(self.on_follow_cb_click)
  166. # Show/Hide Advanced Options
  167. if self.app.defaults["global_app_level"] == 'b':
  168. self.ui.level.setText('<span style="color:green;"><b>%s</b></span>' % _('Basic'))
  169. self.ui.apertures_table_label.hide()
  170. self.ui.aperture_table_visibility_cb.hide()
  171. self.ui.follow_cb.hide()
  172. else:
  173. self.ui.level.setText('<span style="color:red;"><b>%s</b></span>' % _('Advanced'))
  174. if self.app.defaults["gerber_buffering"] == 'no':
  175. self.ui.create_buffer_button.show()
  176. try:
  177. self.ui.create_buffer_button.clicked.disconnect(self.on_generate_buffer)
  178. except TypeError:
  179. pass
  180. self.ui.create_buffer_button.clicked.connect(self.on_generate_buffer)
  181. else:
  182. self.ui.create_buffer_button.hide()
  183. # set initial state of the aperture table and associated widgets
  184. self.on_aperture_table_visibility_change()
  185. self.build_ui()
  186. self.units_found = self.app.defaults['units']
  187. def build_ui(self):
  188. FlatCAMObj.build_ui(self)
  189. if self.ui.aperture_table_visibility_cb.get_value() and self.ui_build is False:
  190. self.ui_build = True
  191. try:
  192. # if connected, disconnect the signal from the slot on item_changed as it creates issues
  193. self.ui.apertures_table.itemChanged.disconnect()
  194. except (TypeError, AttributeError):
  195. pass
  196. self.apertures_row = 0
  197. aper_no = self.apertures_row + 1
  198. sort = []
  199. for k, v in list(self.apertures.items()):
  200. sort.append(int(k))
  201. sorted_apertures = sorted(sort)
  202. n = len(sorted_apertures)
  203. self.ui.apertures_table.setRowCount(n)
  204. for ap_code in sorted_apertures:
  205. ap_code = str(ap_code)
  206. ap_id_item = QtWidgets.QTableWidgetItem('%d' % int(self.apertures_row + 1))
  207. ap_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  208. self.ui.apertures_table.setItem(self.apertures_row, 0, ap_id_item) # Tool name/id
  209. ap_code_item = QtWidgets.QTableWidgetItem(ap_code)
  210. ap_code_item.setFlags(QtCore.Qt.ItemIsEnabled)
  211. ap_type_item = QtWidgets.QTableWidgetItem(str(self.apertures[ap_code]['type']))
  212. ap_type_item.setFlags(QtCore.Qt.ItemIsEnabled)
  213. if str(self.apertures[ap_code]['type']) == 'R' or str(self.apertures[ap_code]['type']) == 'O':
  214. ap_dim_item = QtWidgets.QTableWidgetItem(
  215. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['width'],
  216. self.decimals, self.apertures[ap_code]['height']
  217. )
  218. )
  219. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  220. elif str(self.apertures[ap_code]['type']) == 'P':
  221. ap_dim_item = QtWidgets.QTableWidgetItem(
  222. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['diam'],
  223. self.decimals, self.apertures[ap_code]['nVertices'])
  224. )
  225. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  226. else:
  227. ap_dim_item = QtWidgets.QTableWidgetItem('')
  228. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  229. try:
  230. if self.apertures[ap_code]['size'] is not None:
  231. ap_size_item = QtWidgets.QTableWidgetItem(
  232. '%.*f' % (self.decimals, float(self.apertures[ap_code]['size'])))
  233. else:
  234. ap_size_item = QtWidgets.QTableWidgetItem('')
  235. except KeyError:
  236. ap_size_item = QtWidgets.QTableWidgetItem('')
  237. ap_size_item.setFlags(QtCore.Qt.ItemIsEnabled)
  238. mark_item = FCCheckBox()
  239. mark_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  240. # if self.ui.aperture_table_visibility_cb.isChecked():
  241. # mark_item.setChecked(True)
  242. self.ui.apertures_table.setItem(self.apertures_row, 1, ap_code_item) # Aperture Code
  243. self.ui.apertures_table.setItem(self.apertures_row, 2, ap_type_item) # Aperture Type
  244. self.ui.apertures_table.setItem(self.apertures_row, 3, ap_size_item) # Aperture Dimensions
  245. self.ui.apertures_table.setItem(self.apertures_row, 4, ap_dim_item) # Aperture Dimensions
  246. empty_plot_item = QtWidgets.QTableWidgetItem('')
  247. empty_plot_item.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  248. self.ui.apertures_table.setItem(self.apertures_row, 5, empty_plot_item)
  249. self.ui.apertures_table.setCellWidget(self.apertures_row, 5, mark_item)
  250. self.apertures_row += 1
  251. self.ui.apertures_table.selectColumn(0)
  252. self.ui.apertures_table.resizeColumnsToContents()
  253. self.ui.apertures_table.resizeRowsToContents()
  254. vertical_header = self.ui.apertures_table.verticalHeader()
  255. # vertical_header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  256. vertical_header.hide()
  257. self.ui.apertures_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  258. horizontal_header = self.ui.apertures_table.horizontalHeader()
  259. horizontal_header.setMinimumSectionSize(10)
  260. horizontal_header.setDefaultSectionSize(70)
  261. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  262. horizontal_header.resizeSection(0, 27)
  263. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  264. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  265. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  266. horizontal_header.setSectionResizeMode(4, QtWidgets.QHeaderView.Stretch)
  267. horizontal_header.setSectionResizeMode(5, QtWidgets.QHeaderView.Fixed)
  268. horizontal_header.resizeSection(5, 17)
  269. self.ui.apertures_table.setColumnWidth(5, 17)
  270. self.ui.apertures_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  271. self.ui.apertures_table.setSortingEnabled(False)
  272. self.ui.apertures_table.setMinimumHeight(self.ui.apertures_table.getHeight())
  273. self.ui.apertures_table.setMaximumHeight(self.ui.apertures_table.getHeight())
  274. # update the 'mark' checkboxes state according with what is stored in the self.marked_rows list
  275. if self.marked_rows:
  276. for row in range(self.ui.apertures_table.rowCount()):
  277. try:
  278. self.ui.apertures_table.cellWidget(row, 5).set_value(self.marked_rows[row])
  279. except IndexError:
  280. pass
  281. self.ui_connect()
  282. def ui_connect(self):
  283. for row in range(self.ui.apertures_table.rowCount()):
  284. try:
  285. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect(self.on_mark_cb_click_table)
  286. except (TypeError, AttributeError):
  287. pass
  288. self.ui.apertures_table.cellWidget(row, 5).clicked.connect(self.on_mark_cb_click_table)
  289. try:
  290. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  291. except (TypeError, AttributeError):
  292. pass
  293. self.ui.mark_all_cb.clicked.connect(self.on_mark_all_click)
  294. def ui_disconnect(self):
  295. for row in range(self.ui.apertures_table.rowCount()):
  296. try:
  297. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect()
  298. except (TypeError, AttributeError):
  299. pass
  300. try:
  301. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  302. except (TypeError, AttributeError):
  303. pass
  304. def on_generate_buffer(self):
  305. self.app.inform.emit('[WARNING_NOTCL] %s...' % _("Buffering solid geometry"))
  306. def buffer_task():
  307. with self.app.proc_container.new('%s...' % _("Buffering")):
  308. if isinstance(self.solid_geometry, list):
  309. self.solid_geometry = MultiPolygon(self.solid_geometry)
  310. self.solid_geometry = self.solid_geometry.buffer(0.0000001)
  311. self.solid_geometry = self.solid_geometry.buffer(-0.0000001)
  312. self.app.inform.emit('[success] %s.' % _("Done"))
  313. self.plot_single_object.emit()
  314. self.app.worker_task.emit({'fcn': buffer_task, 'params': []})
  315. def on_generatenoncopper_button_click(self, *args):
  316. self.app.defaults.report_usage("gerber_on_generatenoncopper_button")
  317. self.read_form()
  318. name = self.options["name"] + "_noncopper"
  319. def geo_init(geo_obj, app_obj):
  320. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  321. if isinstance(self.solid_geometry, list):
  322. try:
  323. self.solid_geometry = MultiPolygon(self.solid_geometry)
  324. except Exception:
  325. self.solid_geometry = cascaded_union(self.solid_geometry)
  326. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["noncoppermargin"]))
  327. if not self.options["noncopperrounded"]:
  328. bounding_box = bounding_box.envelope
  329. non_copper = bounding_box.difference(self.solid_geometry)
  330. if non_copper is None or non_copper.is_empty:
  331. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  332. return "fail"
  333. geo_obj.solid_geometry = non_copper
  334. self.app.app_obj.new_object("geometry", name, geo_init)
  335. def on_generatebb_button_click(self, *args):
  336. self.app.defaults.report_usage("gerber_on_generatebb_button")
  337. self.read_form()
  338. name = self.options["name"] + "_bbox"
  339. def geo_init(geo_obj, app_obj):
  340. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  341. if isinstance(self.solid_geometry, list):
  342. try:
  343. self.solid_geometry = MultiPolygon(self.solid_geometry)
  344. except Exception:
  345. self.solid_geometry = cascaded_union(self.solid_geometry)
  346. # Bounding box with rounded corners
  347. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["bboxmargin"]))
  348. if not self.options["bboxrounded"]: # Remove rounded corners
  349. bounding_box = bounding_box.envelope
  350. if bounding_box is None or bounding_box.is_empty:
  351. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  352. return "fail"
  353. geo_obj.solid_geometry = bounding_box
  354. self.app.app_obj.new_object("geometry", name, geo_init)
  355. def isolate(self, iso_type=None, geometry=None, dia=None, passes=None, overlap=None, outname=None, combine=None,
  356. milling_type=None, follow=None, plot=True):
  357. """
  358. Creates an isolation routing geometry object in the project.
  359. :param iso_type: type of isolation to be done: 0 = exteriors, 1 = interiors and 2 = both
  360. :param geometry: specific geometry to isolate
  361. :param dia: Tool diameter
  362. :param passes: Number of tool widths to cut
  363. :param overlap: Overlap between passes in fraction of tool diameter
  364. :param outname: Base name of the output object
  365. :param combine: Boolean: if to combine passes in one resulting object in case of multiple passes
  366. :param milling_type: type of milling: conventional or climbing
  367. :param follow: Boolean: if to generate a 'follow' geometry
  368. :param plot: Boolean: if to plot the resulting geometry object
  369. :return: None
  370. """
  371. if geometry is None:
  372. work_geo = self.follow_geometry if follow is True else self.solid_geometry
  373. else:
  374. work_geo = geometry
  375. if dia is None:
  376. dia = float(self.app.defaults["tools_iso_tooldia"])
  377. if passes is None:
  378. passes = int(self.app.defaults["tools_iso_passes"])
  379. if overlap is None:
  380. overlap = float(self.app.defaults["tools_iso_overlap"])
  381. overlap /= 100.0
  382. combine = self.app.defaults["tools_iso_combine_passes"] if combine is None else bool(combine)
  383. if milling_type is None:
  384. milling_type = self.app.defaults["tools_iso_milling_type"]
  385. if iso_type is None:
  386. iso_t = 2
  387. else:
  388. iso_t = iso_type
  389. base_name = self.options["name"]
  390. if combine:
  391. if outname is None:
  392. if self.iso_type == 0:
  393. iso_name = base_name + "_ext_iso"
  394. elif self.iso_type == 1:
  395. iso_name = base_name + "_int_iso"
  396. else:
  397. iso_name = base_name + "_iso"
  398. else:
  399. iso_name = outname
  400. def iso_init(geo_obj, app_obj):
  401. # Propagate options
  402. geo_obj.options["cnctooldia"] = str(dia)
  403. geo_obj.tool_type = self.app.defaults["tools_iso_tool_type"]
  404. geo_obj.solid_geometry = []
  405. # transfer the Cut Z and Vtip and VAngle values in case that we use the V-Shape tool in Gerber UI
  406. if geo_obj.tool_type.lower() == 'v':
  407. new_cutz = self.app.defaults["tools_iso_tool_cutz"]
  408. new_vtipdia = self.app.defaults["tools_iso_tool_vtipdia"]
  409. new_vtipangle = self.app.defaults["tools_iso_tool_vtipangle"]
  410. tool_type = 'V'
  411. else:
  412. new_cutz = self.app.defaults['geometry_cutz']
  413. new_vtipdia = self.app.defaults['geometry_vtipdia']
  414. new_vtipangle = self.app.defaults['geometry_vtipangle']
  415. tool_type = 'C1'
  416. # store here the default data for Geometry Data
  417. default_data = {}
  418. default_data.update({
  419. "name": iso_name,
  420. "plot": self.app.defaults['geometry_plot'],
  421. "cutz": new_cutz,
  422. "vtipdia": new_vtipdia,
  423. "vtipangle": new_vtipangle,
  424. "travelz": self.app.defaults['geometry_travelz'],
  425. "feedrate": self.app.defaults['geometry_feedrate'],
  426. "feedrate_z": self.app.defaults['geometry_feedrate_z'],
  427. "feedrate_rapid": self.app.defaults['geometry_feedrate_rapid'],
  428. "dwell": self.app.defaults['geometry_dwell'],
  429. "dwelltime": self.app.defaults['geometry_dwelltime'],
  430. "multidepth": self.app.defaults['geometry_multidepth'],
  431. "ppname_g": self.app.defaults['geometry_ppname_g'],
  432. "depthperpass": self.app.defaults['geometry_depthperpass'],
  433. "extracut": self.app.defaults['geometry_extracut'],
  434. "extracut_length": self.app.defaults['geometry_extracut_length'],
  435. "toolchange": self.app.defaults['geometry_toolchange'],
  436. "toolchangez": self.app.defaults['geometry_toolchangez'],
  437. "endz": self.app.defaults['geometry_endz'],
  438. "spindlespeed": self.app.defaults['geometry_spindlespeed'],
  439. "toolchangexy": self.app.defaults['geometry_toolchangexy'],
  440. "startz": self.app.defaults['geometry_startz']
  441. })
  442. geo_obj.tools = {}
  443. geo_obj.tools['1'] = {}
  444. geo_obj.tools.update({
  445. '1': {
  446. 'tooldia': dia,
  447. 'offset': 'Path',
  448. 'offset_value': 0.0,
  449. 'type': _('Rough'),
  450. 'tool_type': tool_type,
  451. 'data': default_data,
  452. 'solid_geometry': geo_obj.solid_geometry
  453. }
  454. })
  455. for nr_pass in range(passes):
  456. iso_offset = dia * ((2 * nr_pass + 1) / 2.0) - (nr_pass * overlap * dia)
  457. # if milling type is climb then the move is counter-clockwise around features
  458. mill_dir = 1 if milling_type == 'cl' else 0
  459. geom = self.generate_envelope(iso_offset, mill_dir, geometry=work_geo, env_iso_type=iso_t,
  460. follow=follow, nr_passes=nr_pass)
  461. if geom == 'fail':
  462. app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Isolation geometry could not be generated."))
  463. return 'fail'
  464. geo_obj.solid_geometry.append(geom)
  465. # update the geometry in the tools
  466. geo_obj.tools['1']['solid_geometry'] = geo_obj.solid_geometry
  467. # detect if solid_geometry is empty and this require list flattening which is "heavy"
  468. # or just looking in the lists (they are one level depth) and if any is not empty
  469. # proceed with object creation, if there are empty and the number of them is the length
  470. # of the list then we have an empty solid_geometry which should raise a Custom Exception
  471. empty_cnt = 0
  472. if not isinstance(geo_obj.solid_geometry, list) and \
  473. not isinstance(geo_obj.solid_geometry, MultiPolygon):
  474. geo_obj.solid_geometry = [geo_obj.solid_geometry]
  475. for g in geo_obj.solid_geometry:
  476. if g:
  477. break
  478. else:
  479. empty_cnt += 1
  480. if empty_cnt == len(geo_obj.solid_geometry):
  481. raise ValidationError("Empty Geometry", None)
  482. else:
  483. app_obj.inform.emit('[success] %s" %s' % (_("Isolation geometry created"), geo_obj.options["name"]))
  484. # even if combine is checked, one pass is still single-geo
  485. geo_obj.multigeo = True if passes > 1 else False
  486. # ############################################################
  487. # ########## AREA SUBTRACTION ################################
  488. # ############################################################
  489. # if self.app.defaults["tools_iso_except"]:
  490. # self.app.proc_container.update_view_text(' %s' % _("Subtracting Geo"))
  491. # geo_obj.solid_geometry = self.area_subtraction(geo_obj.solid_geometry)
  492. self.app.app_obj.new_object("geometry", iso_name, iso_init, plot=plot)
  493. else:
  494. for i in range(passes):
  495. offset = dia * ((2 * i + 1) / 2.0) - (i * overlap * dia)
  496. if passes > 1:
  497. if outname is None:
  498. if self.iso_type == 0:
  499. iso_name = base_name + "_ext_iso" + str(i + 1)
  500. elif self.iso_type == 1:
  501. iso_name = base_name + "_int_iso" + str(i + 1)
  502. else:
  503. iso_name = base_name + "_iso" + str(i + 1)
  504. else:
  505. iso_name = outname
  506. else:
  507. if outname is None:
  508. if self.iso_type == 0:
  509. iso_name = base_name + "_ext_iso"
  510. elif self.iso_type == 1:
  511. iso_name = base_name + "_int_iso"
  512. else:
  513. iso_name = base_name + "_iso"
  514. else:
  515. iso_name = outname
  516. def iso_init(geo_obj, app_obj):
  517. # Propagate options
  518. geo_obj.options["cnctooldia"] = str(dia)
  519. geo_obj.tool_type = self.app.defaults["tools_iso_tool_type"]
  520. # if milling type is climb then the move is counter-clockwise around features
  521. mill_dir = 1 if milling_type == 'cl' else 0
  522. geom = self.generate_envelope(offset, mill_dir, geometry=work_geo, env_iso_type=iso_t,
  523. follow=follow, nr_passes=i)
  524. if geom == 'fail':
  525. app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Isolation geometry could not be generated."))
  526. return 'fail'
  527. geo_obj.solid_geometry = geom
  528. # transfer the Cut Z and Vtip and VAngle values in case that we use the V-Shape tool in Gerber UI
  529. # even if the resulting geometry is not multigeo we add the tools dict which will hold the data
  530. # required to be transfered to the Geometry object
  531. if self.app.defaults["tools_iso_tool_type"].lower() == 'v':
  532. new_cutz = self.app.defaults["tools_iso_tool_cutz"]
  533. new_vtipdia = self.app.defaults["tools_iso_tool_vtipdia"]
  534. new_vtipangle = self.app.defaults["tools_iso_tool_vtipangle"]
  535. tool_type = 'V'
  536. else:
  537. new_cutz = self.app.defaults['geometry_cutz']
  538. new_vtipdia = self.app.defaults['geometry_vtipdia']
  539. new_vtipangle = self.app.defaults['geometry_vtipangle']
  540. tool_type = 'C1'
  541. # store here the default data for Geometry Data
  542. default_data = {}
  543. default_data.update({
  544. "name": iso_name,
  545. "plot": self.app.defaults['geometry_plot'],
  546. "cutz": new_cutz,
  547. "vtipdia": new_vtipdia,
  548. "vtipangle": new_vtipangle,
  549. "travelz": self.app.defaults['geometry_travelz'],
  550. "feedrate": self.app.defaults['geometry_feedrate'],
  551. "feedrate_z": self.app.defaults['geometry_feedrate_z'],
  552. "feedrate_rapid": self.app.defaults['geometry_feedrate_rapid'],
  553. "dwell": self.app.defaults['geometry_dwell'],
  554. "dwelltime": self.app.defaults['geometry_dwelltime'],
  555. "multidepth": self.app.defaults['geometry_multidepth'],
  556. "ppname_g": self.app.defaults['geometry_ppname_g'],
  557. "depthperpass": self.app.defaults['geometry_depthperpass'],
  558. "extracut": self.app.defaults['geometry_extracut'],
  559. "extracut_length": self.app.defaults['geometry_extracut_length'],
  560. "toolchange": self.app.defaults['geometry_toolchange'],
  561. "toolchangez": self.app.defaults['geometry_toolchangez'],
  562. "endz": self.app.defaults['geometry_endz'],
  563. "spindlespeed": self.app.defaults['geometry_spindlespeed'],
  564. "toolchangexy": self.app.defaults['geometry_toolchangexy'],
  565. "startz": self.app.defaults['geometry_startz']
  566. })
  567. geo_obj.tools = {}
  568. geo_obj.tools['1'] = {}
  569. geo_obj.tools.update({
  570. '1': {
  571. 'tooldia': dia,
  572. 'offset': 'Path',
  573. 'offset_value': 0.0,
  574. 'type': _('Rough'),
  575. 'tool_type': tool_type,
  576. 'data': default_data,
  577. 'solid_geometry': geo_obj.solid_geometry
  578. }
  579. })
  580. # detect if solid_geometry is empty and this require list flattening which is "heavy"
  581. # or just looking in the lists (they are one level depth) and if any is not empty
  582. # proceed with object creation, if there are empty and the number of them is the length
  583. # of the list then we have an empty solid_geometry which should raise a Custom Exception
  584. empty_cnt = 0
  585. if not isinstance(geo_obj.solid_geometry, list):
  586. geo_obj.solid_geometry = [geo_obj.solid_geometry]
  587. for g in geo_obj.solid_geometry:
  588. if g:
  589. break
  590. else:
  591. empty_cnt += 1
  592. if empty_cnt == len(geo_obj.solid_geometry):
  593. raise ValidationError("Empty Geometry", None)
  594. else:
  595. app_obj.inform.emit('[success] %s: %s' %
  596. (_("Isolation geometry created"), geo_obj.options["name"]))
  597. geo_obj.multigeo = False
  598. # ############################################################
  599. # ########## AREA SUBTRACTION ################################
  600. # ############################################################
  601. # if self.app.defaults["tools_iso_except"]:
  602. # self.app.proc_container.update_view_text(' %s' % _("Subtracting Geo"))
  603. # geo_obj.solid_geometry = self.area_subtraction(geo_obj.solid_geometry)
  604. self.app.app_obj.new_object("geometry", iso_name, iso_init, plot=plot)
  605. def generate_envelope(self, offset, invert, geometry=None, env_iso_type=2, follow=None, nr_passes=0):
  606. # isolation_geometry produces an envelope that is going on the left of the geometry
  607. # (the copper features). To leave the least amount of burrs on the features
  608. # the tool needs to travel on the right side of the features (this is called conventional milling)
  609. # the first pass is the one cutting all of the features, so it needs to be reversed
  610. # the other passes overlap preceding ones and cut the left over copper. It is better for them
  611. # to cut on the right side of the left over copper i.e on the left side of the features.
  612. if follow:
  613. geom = self.isolation_geometry(offset, geometry=geometry, follow=follow)
  614. else:
  615. try:
  616. geom = self.isolation_geometry(offset, geometry=geometry, iso_type=env_iso_type, passes=nr_passes)
  617. except Exception as e:
  618. log.debug('GerberObject.isolate().generate_envelope() --> %s' % str(e))
  619. return 'fail'
  620. if invert:
  621. try:
  622. pl = []
  623. for p in geom:
  624. if p is not None:
  625. if isinstance(p, Polygon):
  626. pl.append(Polygon(p.exterior.coords[::-1], p.interiors))
  627. elif isinstance(p, LinearRing):
  628. pl.append(Polygon(p.coords[::-1]))
  629. geom = MultiPolygon(pl)
  630. except TypeError:
  631. if isinstance(geom, Polygon) and geom is not None:
  632. geom = Polygon(geom.exterior.coords[::-1], geom.interiors)
  633. elif isinstance(geom, LinearRing) and geom is not None:
  634. geom = Polygon(geom.coords[::-1])
  635. else:
  636. log.debug("GerberObject.isolate().generate_envelope() Error --> Unexpected Geometry %s" %
  637. type(geom))
  638. except Exception as e:
  639. log.debug("GerberObject.isolate().generate_envelope() Error --> %s" % str(e))
  640. return 'fail'
  641. return geom
  642. def follow_geo(self, outname=None):
  643. """
  644. Creates a geometry object "following" the gerber paths.
  645. :return: None
  646. """
  647. if outname is None:
  648. follow_name = self.options["name"] + "_follow"
  649. else:
  650. follow_name = outname
  651. def follow_init(follow_obj, app):
  652. # Propagate options
  653. follow_obj.options["cnctooldia"] = str(self.app.defaults["tools_iso_tooldia"])
  654. follow_obj.solid_geometry = self.follow_geometry
  655. # TODO: Do something if this is None. Offer changing name?
  656. try:
  657. self.app.app_obj.new_object("geometry", follow_name, follow_init)
  658. except Exception as e:
  659. return "Operation failed: %s" % str(e)
  660. def on_plot_cb_click(self, *args):
  661. if self.muted_ui:
  662. return
  663. self.read_form_item('plot')
  664. self.plot()
  665. def on_solid_cb_click(self, *args):
  666. if self.muted_ui:
  667. return
  668. self.read_form_item('solid')
  669. self.plot()
  670. def on_multicolored_cb_click(self, *args):
  671. if self.muted_ui:
  672. return
  673. self.read_form_item('multicolored')
  674. self.plot()
  675. def on_follow_cb_click(self):
  676. if self.muted_ui:
  677. return
  678. self.plot()
  679. def on_aperture_table_visibility_change(self):
  680. if self.ui.aperture_table_visibility_cb.isChecked():
  681. # add the shapes storage for marking apertures
  682. if self.build_aperture_storage is False:
  683. self.build_aperture_storage = True
  684. if self.app.is_legacy is False:
  685. for ap_code in self.apertures:
  686. self.mark_shapes[ap_code] = self.app.plotcanvas.new_shape_collection(layers=1)
  687. else:
  688. for ap_code in self.apertures:
  689. self.mark_shapes[ap_code] = ShapeCollectionLegacy(obj=self, app=self.app,
  690. name=self.options['name'] + str(ap_code))
  691. self.ui.apertures_table.setVisible(True)
  692. for ap in self.mark_shapes:
  693. self.mark_shapes[ap].enabled = True
  694. self.ui.mark_all_cb.setVisible(True)
  695. self.ui.mark_all_cb.setChecked(False)
  696. self.build_ui()
  697. else:
  698. self.ui.apertures_table.setVisible(False)
  699. self.ui.mark_all_cb.setVisible(False)
  700. # on hide disable all mark plots
  701. try:
  702. for row in range(self.ui.apertures_table.rowCount()):
  703. self.ui.apertures_table.cellWidget(row, 5).set_value(False)
  704. self.clear_plot_apertures()
  705. # for ap in list(self.mark_shapes.keys()):
  706. # # self.mark_shapes[ap].enabled = False
  707. # del self.mark_shapes[ap]
  708. except Exception as e:
  709. log.debug(" GerberObject.on_aperture_visibility_changed() --> %s" % str(e))
  710. def convert_units(self, units):
  711. """
  712. Converts the units of the object by scaling dimensions in all geometry
  713. and options.
  714. :param units: Units to which to convert the object: "IN" or "MM".
  715. :type units: str
  716. :return: None
  717. :rtype: None
  718. """
  719. # units conversion to get a conversion should be done only once even if we found multiple
  720. # units declaration inside a Gerber file (it can happen to find also the obsolete declaration)
  721. if self.conversion_done is True:
  722. log.debug("Gerber units conversion cancelled. Already done.")
  723. return
  724. log.debug("FlatCAMObj.GerberObject.convert_units()")
  725. factor = Gerber.convert_units(self, units)
  726. # self.options['isotooldia'] = float(self.options['isotooldia']) * factor
  727. # self.options['bboxmargin'] = float(self.options['bboxmargin']) * factor
  728. def plot(self, kind=None, **kwargs):
  729. """
  730. :param kind: Not used, for compatibility with the plot method for other objects
  731. :param kwargs: Color and face_color, visible
  732. :return:
  733. """
  734. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot()")
  735. # Does all the required setup and returns False
  736. # if the 'ptint' option is set to False.
  737. if not FlatCAMObj.plot(self):
  738. return
  739. if 'color' in kwargs:
  740. color = kwargs['color']
  741. else:
  742. color = self.outline_color
  743. if 'face_color' in kwargs:
  744. face_color = kwargs['face_color']
  745. else:
  746. face_color = self.fill_color
  747. if 'visible' not in kwargs:
  748. visible = self.options['plot']
  749. else:
  750. visible = kwargs['visible']
  751. # if the Follow Geometry checkbox is checked then plot only the follow geometry
  752. if self.ui.follow_cb.get_value():
  753. geometry = self.follow_geometry
  754. else:
  755. geometry = self.solid_geometry
  756. # Make sure geometry is iterable.
  757. try:
  758. __ = iter(geometry)
  759. except TypeError:
  760. geometry = [geometry]
  761. if self.app.is_legacy is False:
  762. def random_color():
  763. r_color = np.random.rand(4)
  764. r_color[3] = 1
  765. return r_color
  766. else:
  767. def random_color():
  768. while True:
  769. r_color = np.random.rand(4)
  770. r_color[3] = 1
  771. new_color = '#'
  772. for idx in range(len(r_color)):
  773. new_color += '%x' % int(r_color[idx] * 255)
  774. # do it until a valid color is generated
  775. # a valid color has the # symbol, another 6 chars for the color and the last 2 chars for alpha
  776. # for a total of 9 chars
  777. if len(new_color) == 9:
  778. break
  779. return new_color
  780. try:
  781. if self.options["solid"]:
  782. for g in geometry:
  783. if type(g) == Polygon or type(g) == LineString:
  784. self.add_shape(shape=g, color=color,
  785. face_color=random_color() if self.options['multicolored']
  786. else face_color, visible=visible)
  787. elif type(g) == Point:
  788. pass
  789. else:
  790. try:
  791. for el in g:
  792. self.add_shape(shape=el, color=color,
  793. face_color=random_color() if self.options['multicolored']
  794. else face_color, visible=visible)
  795. except TypeError:
  796. self.add_shape(shape=g, color=color,
  797. face_color=random_color() if self.options['multicolored']
  798. else face_color, visible=visible)
  799. else:
  800. for g in geometry:
  801. if type(g) == Polygon or type(g) == LineString:
  802. self.add_shape(shape=g, color=random_color() if self.options['multicolored'] else 'black',
  803. visible=visible)
  804. elif type(g) == Point:
  805. pass
  806. else:
  807. for el in g:
  808. self.add_shape(shape=el, color=random_color() if self.options['multicolored'] else 'black',
  809. visible=visible)
  810. self.shapes.redraw(
  811. # update_colors=(self.fill_color, self.outline_color),
  812. # indexes=self.app.plotcanvas.shape_collection.data.keys()
  813. )
  814. except (ObjectDeleted, AttributeError):
  815. self.shapes.clear(update=True)
  816. except Exception as e:
  817. log.debug("GerberObject.plot() --> %s" % str(e))
  818. # experimental plot() when the solid_geometry is stored in the self.apertures
  819. def plot_aperture(self, run_thread=True, **kwargs):
  820. """
  821. :param run_thread: if True run the aperture plot as a thread in a worker
  822. :param kwargs: color and face_color
  823. :return:
  824. """
  825. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot_aperture()")
  826. # Does all the required setup and returns False
  827. # if the 'ptint' option is set to False.
  828. # if not FlatCAMObj.plot(self):
  829. # return
  830. # for marking apertures, line color and fill color are the same
  831. if 'color' in kwargs:
  832. color = kwargs['color']
  833. else:
  834. color = self.app.defaults['gerber_plot_fill']
  835. if 'marked_aperture' not in kwargs:
  836. return
  837. else:
  838. aperture_to_plot_mark = kwargs['marked_aperture']
  839. if aperture_to_plot_mark is None:
  840. return
  841. if 'visible' not in kwargs:
  842. visibility = True
  843. else:
  844. visibility = kwargs['visible']
  845. with self.app.proc_container.new(_("Plotting Apertures")):
  846. def job_thread(app_obj):
  847. try:
  848. if aperture_to_plot_mark in self.apertures:
  849. for elem in self.apertures[aperture_to_plot_mark]['geometry']:
  850. if 'solid' in elem:
  851. geo = elem['solid']
  852. if type(geo) == Polygon or type(geo) == LineString:
  853. self.add_mark_shape(apid=aperture_to_plot_mark, shape=geo, color=color,
  854. face_color=color, visible=visibility)
  855. else:
  856. for el in geo:
  857. self.add_mark_shape(apid=aperture_to_plot_mark, shape=el, color=color,
  858. face_color=color, visible=visibility)
  859. self.mark_shapes[aperture_to_plot_mark].redraw()
  860. except (ObjectDeleted, AttributeError):
  861. self.clear_plot_apertures()
  862. except Exception as e:
  863. log.debug("GerberObject.plot_aperture() --> %s" % str(e))
  864. if run_thread:
  865. self.app.worker_task.emit({'fcn': job_thread, 'params': [self]})
  866. else:
  867. job_thread(self)
  868. def clear_plot_apertures(self, aperture='all'):
  869. """
  870. :param aperture: string; aperture for which to clear the mark shapes
  871. :return:
  872. """
  873. if self.mark_shapes:
  874. if aperture == 'all':
  875. for apid in list(self.apertures.keys()):
  876. try:
  877. if self.app.is_legacy is True:
  878. self.mark_shapes[apid].clear(update=False)
  879. else:
  880. self.mark_shapes[apid].clear(update=True)
  881. except Exception as e:
  882. log.debug("GerberObject.clear_plot_apertures() 'all' --> %s" % str(e))
  883. else:
  884. try:
  885. if self.app.is_legacy is True:
  886. self.mark_shapes[aperture].clear(update=False)
  887. else:
  888. self.mark_shapes[aperture].clear(update=True)
  889. except Exception as e:
  890. log.debug("GerberObject.clear_plot_apertures() 'aperture' --> %s" % str(e))
  891. def clear_mark_all(self):
  892. self.ui.mark_all_cb.set_value(False)
  893. self.marked_rows[:] = []
  894. def on_mark_cb_click_table(self):
  895. """
  896. Will mark aperture geometries on canvas or delete the markings depending on the checkbox state
  897. :return:
  898. """
  899. self.ui_disconnect()
  900. try:
  901. cw = self.sender()
  902. cw_index = self.ui.apertures_table.indexAt(cw.pos())
  903. cw_row = cw_index.row()
  904. except AttributeError:
  905. cw_row = 0
  906. except TypeError:
  907. return
  908. self.marked_rows[:] = []
  909. try:
  910. aperture = self.ui.apertures_table.item(cw_row, 1).text()
  911. except AttributeError:
  912. return
  913. if self.ui.apertures_table.cellWidget(cw_row, 5).isChecked():
  914. self.marked_rows.append(True)
  915. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  916. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  917. marked_aperture=aperture, visible=True, run_thread=True)
  918. # self.mark_shapes[aperture].redraw()
  919. else:
  920. self.marked_rows.append(False)
  921. self.clear_plot_apertures(aperture=aperture)
  922. # make sure that the Mark All is disabled if one of the row mark's are disabled and
  923. # if all the row mark's are enabled also enable the Mark All checkbox
  924. cb_cnt = 0
  925. total_row = self.ui.apertures_table.rowCount()
  926. for row in range(total_row):
  927. if self.ui.apertures_table.cellWidget(row, 5).isChecked():
  928. cb_cnt += 1
  929. else:
  930. cb_cnt -= 1
  931. if cb_cnt < total_row:
  932. self.ui.mark_all_cb.setChecked(False)
  933. else:
  934. self.ui.mark_all_cb.setChecked(True)
  935. self.ui_connect()
  936. def on_mark_all_click(self):
  937. self.ui_disconnect()
  938. mark_all = self.ui.mark_all_cb.isChecked()
  939. for row in range(self.ui.apertures_table.rowCount()):
  940. # update the mark_rows list
  941. if mark_all:
  942. self.marked_rows.append(True)
  943. else:
  944. self.marked_rows[:] = []
  945. mark_cb = self.ui.apertures_table.cellWidget(row, 5)
  946. mark_cb.setChecked(mark_all)
  947. if mark_all:
  948. for aperture in self.apertures:
  949. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  950. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  951. marked_aperture=aperture, visible=True)
  952. # HACK: enable/disable the grid for a better look
  953. self.app.ui.grid_snap_btn.trigger()
  954. self.app.ui.grid_snap_btn.trigger()
  955. else:
  956. self.clear_plot_apertures()
  957. self.marked_rows[:] = []
  958. self.ui_connect()
  959. def export_gerber(self, whole, fract, g_zeros='L', factor=1):
  960. """
  961. Creates a Gerber file content to be exported to a file.
  962. :param whole: how many digits in the whole part of coordinates
  963. :param fract: how many decimals in coordinates
  964. :param g_zeros: type of the zero suppression used: LZ or TZ; string
  965. :param factor: factor to be applied onto the Gerber coordinates
  966. :return: Gerber_code
  967. """
  968. log.debug("GerberObject.export_gerber() --> Generating the Gerber code from the selected Gerber file")
  969. def tz_format(x, y, fac):
  970. x_c = x * fac
  971. y_c = y * fac
  972. x_form = "{:.{dec}f}".format(x_c, dec=fract)
  973. y_form = "{:.{dec}f}".format(y_c, dec=fract)
  974. # extract whole part and decimal part
  975. x_form = x_form.partition('.')
  976. y_form = y_form.partition('.')
  977. # left padd the 'whole' part with zeros
  978. x_whole = x_form[0].rjust(whole, '0')
  979. y_whole = y_form[0].rjust(whole, '0')
  980. # restore the coordinate padded in the left with 0 and added the decimal part
  981. # without the decinal dot
  982. x_form = x_whole + x_form[2]
  983. y_form = y_whole + y_form[2]
  984. return x_form, y_form
  985. def lz_format(x, y, fac):
  986. x_c = x * fac
  987. y_c = y * fac
  988. x_form = "{:.{dec}f}".format(x_c, dec=fract).replace('.', '')
  989. y_form = "{:.{dec}f}".format(y_c, dec=fract).replace('.', '')
  990. # pad with rear zeros
  991. x_form.ljust(length, '0')
  992. y_form.ljust(length, '0')
  993. return x_form, y_form
  994. # Gerber code is stored here
  995. gerber_code = ''
  996. # apertures processing
  997. try:
  998. length = whole + fract
  999. if '0' in self.apertures:
  1000. if 'geometry' in self.apertures['0']:
  1001. for geo_elem in self.apertures['0']['geometry']:
  1002. if 'solid' in geo_elem:
  1003. geo = geo_elem['solid']
  1004. if not geo.is_empty:
  1005. gerber_code += 'G36*\n'
  1006. geo_coords = list(geo.exterior.coords)
  1007. # first command is a move with pen-up D02 at the beginning of the geo
  1008. if g_zeros == 'T':
  1009. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1010. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1011. yform=y_formatted)
  1012. else:
  1013. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1014. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1015. yform=y_formatted)
  1016. for coord in geo_coords[1:]:
  1017. if g_zeros == 'T':
  1018. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1019. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1020. yform=y_formatted)
  1021. else:
  1022. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1023. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1024. yform=y_formatted)
  1025. gerber_code += 'D02*\n'
  1026. gerber_code += 'G37*\n'
  1027. clear_list = list(geo.interiors)
  1028. if clear_list:
  1029. gerber_code += '%LPC*%\n'
  1030. for clear_geo in clear_list:
  1031. gerber_code += 'G36*\n'
  1032. geo_coords = list(clear_geo.coords)
  1033. # first command is a move with pen-up D02 at the beginning of the geo
  1034. if g_zeros == 'T':
  1035. x_formatted, y_formatted = tz_format(
  1036. geo_coords[0][0], geo_coords[0][1], factor)
  1037. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1038. yform=y_formatted)
  1039. else:
  1040. x_formatted, y_formatted = lz_format(
  1041. geo_coords[0][0], geo_coords[0][1], factor)
  1042. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1043. yform=y_formatted)
  1044. prev_coord = geo_coords[0]
  1045. for coord in geo_coords[1:]:
  1046. if coord != prev_coord:
  1047. if g_zeros == 'T':
  1048. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1049. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1050. yform=y_formatted)
  1051. else:
  1052. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1053. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1054. yform=y_formatted)
  1055. prev_coord = coord
  1056. gerber_code += 'D02*\n'
  1057. gerber_code += 'G37*\n'
  1058. gerber_code += '%LPD*%\n'
  1059. if 'clear' in geo_elem:
  1060. geo = geo_elem['clear']
  1061. if not geo.is_empty:
  1062. gerber_code += '%LPC*%\n'
  1063. gerber_code += 'G36*\n'
  1064. geo_coords = list(geo.exterior.coords)
  1065. # first command is a move with pen-up D02 at the beginning of the geo
  1066. if g_zeros == 'T':
  1067. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1068. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1069. yform=y_formatted)
  1070. else:
  1071. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1072. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1073. yform=y_formatted)
  1074. prev_coord = geo_coords[0]
  1075. for coord in geo_coords[1:]:
  1076. if coord != prev_coord:
  1077. if g_zeros == 'T':
  1078. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1079. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1080. yform=y_formatted)
  1081. else:
  1082. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1083. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1084. yform=y_formatted)
  1085. prev_coord = coord
  1086. gerber_code += 'D02*\n'
  1087. gerber_code += 'G37*\n'
  1088. gerber_code += '%LPD*%\n'
  1089. except Exception as e:
  1090. log.debug("FlatCAMObj.GerberObject.export_gerber() '0' aperture --> %s" % str(e))
  1091. for apid in self.apertures:
  1092. if apid == '0':
  1093. continue
  1094. else:
  1095. gerber_code += 'D%s*\n' % str(apid)
  1096. if 'geometry' in self.apertures[apid]:
  1097. for geo_elem in self.apertures[apid]['geometry']:
  1098. try:
  1099. if 'follow' in geo_elem:
  1100. geo = geo_elem['follow']
  1101. if not geo.is_empty:
  1102. if isinstance(geo, Point):
  1103. if g_zeros == 'T':
  1104. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  1105. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1106. yform=y_formatted)
  1107. else:
  1108. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  1109. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1110. yform=y_formatted)
  1111. else:
  1112. geo_coords = list(geo.coords)
  1113. # first command is a move with pen-up D02 at the beginning of the geo
  1114. if g_zeros == 'T':
  1115. x_formatted, y_formatted = tz_format(
  1116. geo_coords[0][0], geo_coords[0][1], factor)
  1117. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1118. yform=y_formatted)
  1119. else:
  1120. x_formatted, y_formatted = lz_format(
  1121. geo_coords[0][0], geo_coords[0][1], factor)
  1122. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1123. yform=y_formatted)
  1124. prev_coord = geo_coords[0]
  1125. for coord in geo_coords[1:]:
  1126. if coord != prev_coord:
  1127. if g_zeros == 'T':
  1128. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1129. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1130. yform=y_formatted)
  1131. else:
  1132. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1133. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1134. yform=y_formatted)
  1135. prev_coord = coord
  1136. # gerber_code += "D02*\n"
  1137. except Exception as e:
  1138. log.debug("FlatCAMObj.GerberObject.export_gerber() 'follow' --> %s" % str(e))
  1139. try:
  1140. if 'clear' in geo_elem:
  1141. gerber_code += '%LPC*%\n'
  1142. geo = geo_elem['clear']
  1143. if not geo.is_empty:
  1144. if isinstance(geo, Point):
  1145. if g_zeros == 'T':
  1146. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  1147. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1148. yform=y_formatted)
  1149. else:
  1150. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  1151. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1152. yform=y_formatted)
  1153. elif isinstance(geo, Polygon):
  1154. geo_coords = list(geo.exterior.coords)
  1155. # first command is a move with pen-up D02 at the beginning of the geo
  1156. if g_zeros == 'T':
  1157. x_formatted, y_formatted = tz_format(
  1158. geo_coords[0][0], geo_coords[0][1], factor)
  1159. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1160. yform=y_formatted)
  1161. else:
  1162. x_formatted, y_formatted = lz_format(
  1163. geo_coords[0][0], geo_coords[0][1], factor)
  1164. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1165. yform=y_formatted)
  1166. prev_coord = geo_coords[0]
  1167. for coord in geo_coords[1:]:
  1168. if coord != prev_coord:
  1169. if g_zeros == 'T':
  1170. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1171. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1172. yform=y_formatted)
  1173. else:
  1174. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1175. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1176. yform=y_formatted)
  1177. prev_coord = coord
  1178. for geo_int in geo.interiors:
  1179. geo_coords = list(geo_int.coords)
  1180. # first command is a move with pen-up D02 at the beginning of the geo
  1181. if g_zeros == 'T':
  1182. x_formatted, y_formatted = tz_format(
  1183. geo_coords[0][0], geo_coords[0][1], factor)
  1184. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1185. yform=y_formatted)
  1186. else:
  1187. x_formatted, y_formatted = lz_format(
  1188. geo_coords[0][0], geo_coords[0][1], factor)
  1189. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1190. yform=y_formatted)
  1191. prev_coord = geo_coords[0]
  1192. for coord in geo_coords[1:]:
  1193. if coord != prev_coord:
  1194. if g_zeros == 'T':
  1195. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1196. gerber_code += "X{xform}Y{yform}D01*\n".format(
  1197. xform=x_formatted,
  1198. yform=y_formatted)
  1199. else:
  1200. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1201. gerber_code += "X{xform}Y{yform}D01*\n".format(
  1202. xform=x_formatted,
  1203. yform=y_formatted)
  1204. prev_coord = coord
  1205. else:
  1206. geo_coords = list(geo.coords)
  1207. # first command is a move with pen-up D02 at the beginning of the geo
  1208. if g_zeros == 'T':
  1209. x_formatted, y_formatted = tz_format(
  1210. geo_coords[0][0], geo_coords[0][1], factor)
  1211. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1212. yform=y_formatted)
  1213. else:
  1214. x_formatted, y_formatted = lz_format(
  1215. geo_coords[0][0], geo_coords[0][1], factor)
  1216. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1217. yform=y_formatted)
  1218. prev_coord = geo_coords[0]
  1219. for coord in geo_coords[1:]:
  1220. if coord != prev_coord:
  1221. if g_zeros == 'T':
  1222. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1223. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1224. yform=y_formatted)
  1225. else:
  1226. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1227. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1228. yform=y_formatted)
  1229. prev_coord = coord
  1230. # gerber_code += "D02*\n"
  1231. gerber_code += '%LPD*%\n'
  1232. except Exception as e:
  1233. log.debug("FlatCAMObj.GerberObject.export_gerber() 'clear' --> %s" % str(e))
  1234. if not self.apertures:
  1235. log.debug("FlatCAMObj.GerberObject.export_gerber() --> Gerber Object is empty: no apertures.")
  1236. return 'fail'
  1237. return gerber_code
  1238. def mirror(self, axis, point):
  1239. Gerber.mirror(self, axis=axis, point=point)
  1240. self.replotApertures.emit()
  1241. def offset(self, vect):
  1242. Gerber.offset(self, vect=vect)
  1243. self.replotApertures.emit()
  1244. def rotate(self, angle, point):
  1245. Gerber.rotate(self, angle=angle, point=point)
  1246. self.replotApertures.emit()
  1247. def scale(self, xfactor, yfactor=None, point=None):
  1248. Gerber.scale(self, xfactor=xfactor, yfactor=yfactor, point=point)
  1249. self.replotApertures.emit()
  1250. def skew(self, angle_x, angle_y, point):
  1251. Gerber.skew(self, angle_x=angle_x, angle_y=angle_y, point=point)
  1252. self.replotApertures.emit()
  1253. def buffer(self, distance, join, factor=None):
  1254. Gerber.buffer(self, distance=distance, join=join, factor=factor)
  1255. self.replotApertures.emit()
  1256. def serialize(self):
  1257. return {
  1258. "options": self.options,
  1259. "kind": self.kind
  1260. }