FlatCAMGerber.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. })
  99. # type of isolation: 0 = exteriors, 1 = interiors, 2 = complete isolation (both interiors and exteriors)
  100. self.iso_type = 2
  101. self.multigeo = False
  102. self.follow = False
  103. self.apertures_row = 0
  104. # store the source file here
  105. self.source_file = ""
  106. # list of rows with apertures plotted
  107. self.marked_rows = []
  108. # Mouse events
  109. self.mr = None
  110. self.mm = None
  111. self.mp = None
  112. # dict to store the polygons selected for isolation; key is the shape added to be plotted and value is the poly
  113. self.poly_dict = {}
  114. # store the status of grid snapping
  115. self.grid_status_memory = None
  116. self.units_found = self.app.defaults['units']
  117. self.fill_color = self.app.defaults['gerber_plot_fill']
  118. self.outline_color = self.app.defaults['gerber_plot_line']
  119. self.alpha_level = 'bf'
  120. # keep track if the UI is built so we don't have to build it every time
  121. self.ui_build = False
  122. # build only once the aperture storage (takes time)
  123. self.build_aperture_storage = False
  124. # Attributes to be included in serialization
  125. # Always append to it because it carries contents
  126. # from predecessors.
  127. self.ser_attrs += ['options', 'kind', 'fill_color', 'outline_color', 'alpha_level']
  128. def set_ui(self, ui):
  129. """
  130. Maps options with GUI inputs.
  131. Connects GUI events to methods.
  132. :param ui: GUI object.
  133. :type ui: GerberObjectUI
  134. :return: None
  135. """
  136. FlatCAMObj.set_ui(self, ui)
  137. log.debug("GerberObject.set_ui()")
  138. self.units = self.app.defaults['units'].upper()
  139. self.replotApertures.connect(self.on_mark_cb_click_table)
  140. self.form_fields.update({
  141. "plot": self.ui.plot_cb,
  142. "multicolored": self.ui.multicolored_cb,
  143. "solid": self.ui.solid_cb,
  144. "noncoppermargin": self.ui.noncopper_margin_entry,
  145. "noncopperrounded": self.ui.noncopper_rounded_cb,
  146. "bboxmargin": self.ui.bbmargin_entry,
  147. "bboxrounded": self.ui.bbrounded_cb,
  148. "aperture_display": self.ui.aperture_table_visibility_cb,
  149. "follow": self.ui.follow_cb
  150. })
  151. # Fill form fields only on object create
  152. self.to_form()
  153. assert isinstance(self.ui, GerberObjectUI)
  154. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  155. self.ui.solid_cb.stateChanged.connect(self.on_solid_cb_click)
  156. self.ui.multicolored_cb.stateChanged.connect(self.on_multicolored_cb_click)
  157. # Tools
  158. self.ui.iso_button.clicked.connect(self.app.isolation_tool.run)
  159. self.ui.generate_ncc_button.clicked.connect(self.app.ncclear_tool.run)
  160. self.ui.generate_cutout_button.clicked.connect(self.app.cutout_tool.run)
  161. self.ui.generate_bb_button.clicked.connect(self.on_generatebb_button_click)
  162. self.ui.generate_noncopper_button.clicked.connect(self.on_generatenoncopper_button_click)
  163. self.ui.aperture_table_visibility_cb.stateChanged.connect(self.on_aperture_table_visibility_change)
  164. self.ui.follow_cb.stateChanged.connect(self.on_follow_cb_click)
  165. # Show/Hide Advanced Options
  166. if self.app.defaults["global_app_level"] == 'b':
  167. self.ui.level.setText('<span style="color:green;"><b>%s</b></span>' % _('Basic'))
  168. self.ui.apertures_table_label.hide()
  169. self.ui.aperture_table_visibility_cb.hide()
  170. self.ui.follow_cb.hide()
  171. else:
  172. self.ui.level.setText('<span style="color:red;"><b>%s</b></span>' % _('Advanced'))
  173. if self.app.defaults["gerber_buffering"] == 'no':
  174. self.ui.create_buffer_button.show()
  175. try:
  176. self.ui.create_buffer_button.clicked.disconnect(self.on_generate_buffer)
  177. except TypeError:
  178. pass
  179. self.ui.create_buffer_button.clicked.connect(self.on_generate_buffer)
  180. else:
  181. self.ui.create_buffer_button.hide()
  182. # set initial state of the aperture table and associated widgets
  183. self.on_aperture_table_visibility_change()
  184. self.build_ui()
  185. self.units_found = self.app.defaults['units']
  186. def build_ui(self):
  187. FlatCAMObj.build_ui(self)
  188. if self.ui.aperture_table_visibility_cb.get_value() and self.ui_build is False:
  189. self.ui_build = True
  190. try:
  191. # if connected, disconnect the signal from the slot on item_changed as it creates issues
  192. self.ui.apertures_table.itemChanged.disconnect()
  193. except (TypeError, AttributeError):
  194. pass
  195. self.apertures_row = 0
  196. aper_no = self.apertures_row + 1
  197. sort = []
  198. for k, v in list(self.apertures.items()):
  199. sort.append(int(k))
  200. sorted_apertures = sorted(sort)
  201. n = len(sorted_apertures)
  202. self.ui.apertures_table.setRowCount(n)
  203. for ap_code in sorted_apertures:
  204. ap_code = str(ap_code)
  205. ap_id_item = QtWidgets.QTableWidgetItem('%d' % int(self.apertures_row + 1))
  206. ap_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  207. self.ui.apertures_table.setItem(self.apertures_row, 0, ap_id_item) # Tool name/id
  208. ap_code_item = QtWidgets.QTableWidgetItem(ap_code)
  209. ap_code_item.setFlags(QtCore.Qt.ItemIsEnabled)
  210. ap_type_item = QtWidgets.QTableWidgetItem(str(self.apertures[ap_code]['type']))
  211. ap_type_item.setFlags(QtCore.Qt.ItemIsEnabled)
  212. if str(self.apertures[ap_code]['type']) == 'R' or str(self.apertures[ap_code]['type']) == 'O':
  213. ap_dim_item = QtWidgets.QTableWidgetItem(
  214. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['width'],
  215. self.decimals, self.apertures[ap_code]['height']
  216. )
  217. )
  218. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  219. elif str(self.apertures[ap_code]['type']) == 'P':
  220. ap_dim_item = QtWidgets.QTableWidgetItem(
  221. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['diam'],
  222. self.decimals, self.apertures[ap_code]['nVertices'])
  223. )
  224. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  225. else:
  226. ap_dim_item = QtWidgets.QTableWidgetItem('')
  227. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  228. try:
  229. if self.apertures[ap_code]['size'] is not None:
  230. ap_size_item = QtWidgets.QTableWidgetItem(
  231. '%.*f' % (self.decimals, float(self.apertures[ap_code]['size'])))
  232. else:
  233. ap_size_item = QtWidgets.QTableWidgetItem('')
  234. except KeyError:
  235. ap_size_item = QtWidgets.QTableWidgetItem('')
  236. ap_size_item.setFlags(QtCore.Qt.ItemIsEnabled)
  237. mark_item = FCCheckBox()
  238. mark_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  239. # if self.ui.aperture_table_visibility_cb.isChecked():
  240. # mark_item.setChecked(True)
  241. self.ui.apertures_table.setItem(self.apertures_row, 1, ap_code_item) # Aperture Code
  242. self.ui.apertures_table.setItem(self.apertures_row, 2, ap_type_item) # Aperture Type
  243. self.ui.apertures_table.setItem(self.apertures_row, 3, ap_size_item) # Aperture Dimensions
  244. self.ui.apertures_table.setItem(self.apertures_row, 4, ap_dim_item) # Aperture Dimensions
  245. empty_plot_item = QtWidgets.QTableWidgetItem('')
  246. empty_plot_item.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  247. self.ui.apertures_table.setItem(self.apertures_row, 5, empty_plot_item)
  248. self.ui.apertures_table.setCellWidget(self.apertures_row, 5, mark_item)
  249. self.apertures_row += 1
  250. self.ui.apertures_table.selectColumn(0)
  251. self.ui.apertures_table.resizeColumnsToContents()
  252. self.ui.apertures_table.resizeRowsToContents()
  253. vertical_header = self.ui.apertures_table.verticalHeader()
  254. # vertical_header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  255. vertical_header.hide()
  256. self.ui.apertures_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  257. horizontal_header = self.ui.apertures_table.horizontalHeader()
  258. horizontal_header.setMinimumSectionSize(10)
  259. horizontal_header.setDefaultSectionSize(70)
  260. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  261. horizontal_header.resizeSection(0, 27)
  262. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  263. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  264. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  265. horizontal_header.setSectionResizeMode(4, QtWidgets.QHeaderView.Stretch)
  266. horizontal_header.setSectionResizeMode(5, QtWidgets.QHeaderView.Fixed)
  267. horizontal_header.resizeSection(5, 17)
  268. self.ui.apertures_table.setColumnWidth(5, 17)
  269. self.ui.apertures_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  270. self.ui.apertures_table.setSortingEnabled(False)
  271. self.ui.apertures_table.setMinimumHeight(self.ui.apertures_table.getHeight())
  272. self.ui.apertures_table.setMaximumHeight(self.ui.apertures_table.getHeight())
  273. # update the 'mark' checkboxes state according with what is stored in the self.marked_rows list
  274. if self.marked_rows:
  275. for row in range(self.ui.apertures_table.rowCount()):
  276. try:
  277. self.ui.apertures_table.cellWidget(row, 5).set_value(self.marked_rows[row])
  278. except IndexError:
  279. pass
  280. self.ui_connect()
  281. def ui_connect(self):
  282. for row in range(self.ui.apertures_table.rowCount()):
  283. try:
  284. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect(self.on_mark_cb_click_table)
  285. except (TypeError, AttributeError):
  286. pass
  287. self.ui.apertures_table.cellWidget(row, 5).clicked.connect(self.on_mark_cb_click_table)
  288. try:
  289. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  290. except (TypeError, AttributeError):
  291. pass
  292. self.ui.mark_all_cb.clicked.connect(self.on_mark_all_click)
  293. def ui_disconnect(self):
  294. for row in range(self.ui.apertures_table.rowCount()):
  295. try:
  296. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect()
  297. except (TypeError, AttributeError):
  298. pass
  299. try:
  300. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  301. except (TypeError, AttributeError):
  302. pass
  303. def on_generate_buffer(self):
  304. self.app.inform.emit('[WARNING_NOTCL] %s...' % _("Buffering solid geometry"))
  305. def buffer_task():
  306. with self.app.proc_container.new('%s...' % _("Buffering")):
  307. if isinstance(self.solid_geometry, list):
  308. self.solid_geometry = MultiPolygon(self.solid_geometry)
  309. self.solid_geometry = self.solid_geometry.buffer(0.0000001)
  310. self.solid_geometry = self.solid_geometry.buffer(-0.0000001)
  311. self.app.inform.emit('[success] %s.' % _("Done"))
  312. self.plot_single_object.emit()
  313. self.app.worker_task.emit({'fcn': buffer_task, 'params': []})
  314. def on_generatenoncopper_button_click(self, *args):
  315. self.app.defaults.report_usage("gerber_on_generatenoncopper_button")
  316. self.read_form()
  317. name = self.options["name"] + "_noncopper"
  318. def geo_init(geo_obj, app_obj):
  319. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  320. if isinstance(self.solid_geometry, list):
  321. try:
  322. self.solid_geometry = MultiPolygon(self.solid_geometry)
  323. except Exception:
  324. self.solid_geometry = cascaded_union(self.solid_geometry)
  325. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["noncoppermargin"]))
  326. if not self.options["noncopperrounded"]:
  327. bounding_box = bounding_box.envelope
  328. non_copper = bounding_box.difference(self.solid_geometry)
  329. if non_copper is None or non_copper.is_empty:
  330. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  331. return "fail"
  332. geo_obj.solid_geometry = non_copper
  333. self.app.app_obj.new_object("geometry", name, geo_init)
  334. def on_generatebb_button_click(self, *args):
  335. self.app.defaults.report_usage("gerber_on_generatebb_button")
  336. self.read_form()
  337. name = self.options["name"] + "_bbox"
  338. def geo_init(geo_obj, app_obj):
  339. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  340. if isinstance(self.solid_geometry, list):
  341. try:
  342. self.solid_geometry = MultiPolygon(self.solid_geometry)
  343. except Exception:
  344. self.solid_geometry = cascaded_union(self.solid_geometry)
  345. # Bounding box with rounded corners
  346. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["bboxmargin"]))
  347. if not self.options["bboxrounded"]: # Remove rounded corners
  348. bounding_box = bounding_box.envelope
  349. if bounding_box is None or bounding_box.is_empty:
  350. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  351. return "fail"
  352. geo_obj.solid_geometry = bounding_box
  353. self.app.app_obj.new_object("geometry", name, geo_init)
  354. def generate_envelope(self, offset, invert, geometry=None, env_iso_type=2, follow=None, nr_passes=0):
  355. # isolation_geometry produces an envelope that is going on the left of the geometry
  356. # (the copper features). To leave the least amount of burrs on the features
  357. # the tool needs to travel on the right side of the features (this is called conventional milling)
  358. # the first pass is the one cutting all of the features, so it needs to be reversed
  359. # the other passes overlap preceding ones and cut the left over copper. It is better for them
  360. # to cut on the right side of the left over copper i.e on the left side of the features.
  361. if follow:
  362. geom = self.isolation_geometry(offset, geometry=geometry, follow=follow)
  363. else:
  364. try:
  365. geom = self.isolation_geometry(offset, geometry=geometry, iso_type=env_iso_type, passes=nr_passes)
  366. except Exception as e:
  367. log.debug('GerberObject.isolate().generate_envelope() --> %s' % str(e))
  368. return 'fail'
  369. if invert:
  370. try:
  371. pl = []
  372. for p in geom:
  373. if p is not None:
  374. if isinstance(p, Polygon):
  375. pl.append(Polygon(p.exterior.coords[::-1], p.interiors))
  376. elif isinstance(p, LinearRing):
  377. pl.append(Polygon(p.coords[::-1]))
  378. geom = MultiPolygon(pl)
  379. except TypeError:
  380. if isinstance(geom, Polygon) and geom is not None:
  381. geom = Polygon(geom.exterior.coords[::-1], geom.interiors)
  382. elif isinstance(geom, LinearRing) and geom is not None:
  383. geom = Polygon(geom.coords[::-1])
  384. else:
  385. log.debug("GerberObject.isolate().generate_envelope() Error --> Unexpected Geometry %s" %
  386. type(geom))
  387. except Exception as e:
  388. log.debug("GerberObject.isolate().generate_envelope() Error --> %s" % str(e))
  389. return 'fail'
  390. return geom
  391. def on_plot_cb_click(self, *args):
  392. if self.muted_ui:
  393. return
  394. self.read_form_item('plot')
  395. self.plot()
  396. def on_solid_cb_click(self, *args):
  397. if self.muted_ui:
  398. return
  399. self.read_form_item('solid')
  400. self.plot()
  401. def on_multicolored_cb_click(self, *args):
  402. if self.muted_ui:
  403. return
  404. self.read_form_item('multicolored')
  405. self.plot()
  406. def on_follow_cb_click(self):
  407. if self.muted_ui:
  408. return
  409. self.plot()
  410. def on_aperture_table_visibility_change(self):
  411. if self.ui.aperture_table_visibility_cb.isChecked():
  412. # add the shapes storage for marking apertures
  413. if self.build_aperture_storage is False:
  414. self.build_aperture_storage = True
  415. if self.app.is_legacy is False:
  416. for ap_code in self.apertures:
  417. self.mark_shapes[ap_code] = self.app.plotcanvas.new_shape_collection(layers=1)
  418. else:
  419. for ap_code in self.apertures:
  420. self.mark_shapes[ap_code] = ShapeCollectionLegacy(obj=self, app=self.app,
  421. name=self.options['name'] + str(ap_code))
  422. self.ui.apertures_table.setVisible(True)
  423. for ap in self.mark_shapes:
  424. self.mark_shapes[ap].enabled = True
  425. self.ui.mark_all_cb.setVisible(True)
  426. self.ui.mark_all_cb.setChecked(False)
  427. self.build_ui()
  428. else:
  429. self.ui.apertures_table.setVisible(False)
  430. self.ui.mark_all_cb.setVisible(False)
  431. # on hide disable all mark plots
  432. try:
  433. for row in range(self.ui.apertures_table.rowCount()):
  434. self.ui.apertures_table.cellWidget(row, 5).set_value(False)
  435. self.clear_plot_apertures()
  436. # for ap in list(self.mark_shapes.keys()):
  437. # # self.mark_shapes[ap].enabled = False
  438. # del self.mark_shapes[ap]
  439. except Exception as e:
  440. log.debug(" GerberObject.on_aperture_visibility_changed() --> %s" % str(e))
  441. def convert_units(self, units):
  442. """
  443. Converts the units of the object by scaling dimensions in all geometry
  444. and options.
  445. :param units: Units to which to convert the object: "IN" or "MM".
  446. :type units: str
  447. :return: None
  448. :rtype: None
  449. """
  450. # units conversion to get a conversion should be done only once even if we found multiple
  451. # units declaration inside a Gerber file (it can happen to find also the obsolete declaration)
  452. if self.conversion_done is True:
  453. log.debug("Gerber units conversion cancelled. Already done.")
  454. return
  455. log.debug("FlatCAMObj.GerberObject.convert_units()")
  456. factor = Gerber.convert_units(self, units)
  457. # self.options['isotooldia'] = float(self.options['isotooldia']) * factor
  458. # self.options['bboxmargin'] = float(self.options['bboxmargin']) * factor
  459. def plot(self, kind=None, **kwargs):
  460. """
  461. :param kind: Not used, for compatibility with the plot method for other objects
  462. :param kwargs: Color and face_color, visible
  463. :return:
  464. """
  465. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot()")
  466. # Does all the required setup and returns False
  467. # if the 'ptint' option is set to False.
  468. if not FlatCAMObj.plot(self):
  469. return
  470. if 'color' in kwargs:
  471. color = kwargs['color']
  472. else:
  473. color = self.outline_color
  474. if 'face_color' in kwargs:
  475. face_color = kwargs['face_color']
  476. else:
  477. face_color = self.fill_color
  478. if 'visible' not in kwargs:
  479. visible = self.options['plot']
  480. else:
  481. visible = kwargs['visible']
  482. # if the Follow Geometry checkbox is checked then plot only the follow geometry
  483. if self.ui.follow_cb.get_value():
  484. geometry = self.follow_geometry
  485. else:
  486. geometry = self.solid_geometry
  487. # Make sure geometry is iterable.
  488. try:
  489. __ = iter(geometry)
  490. except TypeError:
  491. geometry = [geometry]
  492. if self.app.is_legacy is False:
  493. def random_color():
  494. r_color = np.random.rand(4)
  495. r_color[3] = 1
  496. return r_color
  497. else:
  498. def random_color():
  499. while True:
  500. r_color = np.random.rand(4)
  501. r_color[3] = 1
  502. new_color = '#'
  503. for idx in range(len(r_color)):
  504. new_color += '%x' % int(r_color[idx] * 255)
  505. # do it until a valid color is generated
  506. # a valid color has the # symbol, another 6 chars for the color and the last 2 chars for alpha
  507. # for a total of 9 chars
  508. if len(new_color) == 9:
  509. break
  510. return new_color
  511. try:
  512. if self.options["solid"]:
  513. for g in geometry:
  514. if type(g) == Polygon or type(g) == LineString:
  515. self.add_shape(shape=g, color=color,
  516. face_color=random_color() if self.options['multicolored']
  517. else face_color, visible=visible)
  518. elif type(g) == Point:
  519. pass
  520. else:
  521. try:
  522. for el in g:
  523. self.add_shape(shape=el, color=color,
  524. face_color=random_color() if self.options['multicolored']
  525. else face_color, visible=visible)
  526. except TypeError:
  527. self.add_shape(shape=g, color=color,
  528. face_color=random_color() if self.options['multicolored']
  529. else face_color, visible=visible)
  530. else:
  531. for g in geometry:
  532. if type(g) == Polygon or type(g) == LineString:
  533. self.add_shape(shape=g, color=random_color() if self.options['multicolored'] else 'black',
  534. visible=visible)
  535. elif type(g) == Point:
  536. pass
  537. else:
  538. for el in g:
  539. self.add_shape(shape=el, color=random_color() if self.options['multicolored'] else 'black',
  540. visible=visible)
  541. self.shapes.redraw(
  542. # update_colors=(self.fill_color, self.outline_color),
  543. # indexes=self.app.plotcanvas.shape_collection.data.keys()
  544. )
  545. except (ObjectDeleted, AttributeError):
  546. self.shapes.clear(update=True)
  547. except Exception as e:
  548. log.debug("GerberObject.plot() --> %s" % str(e))
  549. # experimental plot() when the solid_geometry is stored in the self.apertures
  550. def plot_aperture(self, run_thread=True, **kwargs):
  551. """
  552. :param run_thread: if True run the aperture plot as a thread in a worker
  553. :param kwargs: color and face_color
  554. :return:
  555. """
  556. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot_aperture()")
  557. # Does all the required setup and returns False
  558. # if the 'ptint' option is set to False.
  559. # if not FlatCAMObj.plot(self):
  560. # return
  561. # for marking apertures, line color and fill color are the same
  562. if 'color' in kwargs:
  563. color = kwargs['color']
  564. else:
  565. color = self.app.defaults['gerber_plot_fill']
  566. if 'marked_aperture' not in kwargs:
  567. return
  568. else:
  569. aperture_to_plot_mark = kwargs['marked_aperture']
  570. if aperture_to_plot_mark is None:
  571. return
  572. if 'visible' not in kwargs:
  573. visibility = True
  574. else:
  575. visibility = kwargs['visible']
  576. with self.app.proc_container.new(_("Plotting Apertures")):
  577. def job_thread(app_obj):
  578. try:
  579. if aperture_to_plot_mark in self.apertures:
  580. for elem in self.apertures[aperture_to_plot_mark]['geometry']:
  581. if 'solid' in elem:
  582. geo = elem['solid']
  583. if type(geo) == Polygon or type(geo) == LineString:
  584. self.add_mark_shape(apid=aperture_to_plot_mark, shape=geo, color=color,
  585. face_color=color, visible=visibility)
  586. else:
  587. for el in geo:
  588. self.add_mark_shape(apid=aperture_to_plot_mark, shape=el, color=color,
  589. face_color=color, visible=visibility)
  590. self.mark_shapes[aperture_to_plot_mark].redraw()
  591. except (ObjectDeleted, AttributeError):
  592. self.clear_plot_apertures()
  593. except Exception as e:
  594. log.debug("GerberObject.plot_aperture() --> %s" % str(e))
  595. if run_thread:
  596. self.app.worker_task.emit({'fcn': job_thread, 'params': [self]})
  597. else:
  598. job_thread(self)
  599. def clear_plot_apertures(self, aperture='all'):
  600. """
  601. :param aperture: string; aperture for which to clear the mark shapes
  602. :return:
  603. """
  604. if self.mark_shapes:
  605. if aperture == 'all':
  606. for apid in list(self.apertures.keys()):
  607. try:
  608. if self.app.is_legacy is True:
  609. self.mark_shapes[apid].clear(update=False)
  610. else:
  611. self.mark_shapes[apid].clear(update=True)
  612. except Exception as e:
  613. log.debug("GerberObject.clear_plot_apertures() 'all' --> %s" % str(e))
  614. else:
  615. try:
  616. if self.app.is_legacy is True:
  617. self.mark_shapes[aperture].clear(update=False)
  618. else:
  619. self.mark_shapes[aperture].clear(update=True)
  620. except Exception as e:
  621. log.debug("GerberObject.clear_plot_apertures() 'aperture' --> %s" % str(e))
  622. def clear_mark_all(self):
  623. self.ui.mark_all_cb.set_value(False)
  624. self.marked_rows[:] = []
  625. def on_mark_cb_click_table(self):
  626. """
  627. Will mark aperture geometries on canvas or delete the markings depending on the checkbox state
  628. :return:
  629. """
  630. self.ui_disconnect()
  631. try:
  632. cw = self.sender()
  633. cw_index = self.ui.apertures_table.indexAt(cw.pos())
  634. cw_row = cw_index.row()
  635. except AttributeError:
  636. cw_row = 0
  637. except TypeError:
  638. return
  639. self.marked_rows[:] = []
  640. try:
  641. aperture = self.ui.apertures_table.item(cw_row, 1).text()
  642. except AttributeError:
  643. return
  644. if self.ui.apertures_table.cellWidget(cw_row, 5).isChecked():
  645. self.marked_rows.append(True)
  646. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  647. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  648. marked_aperture=aperture, visible=True, run_thread=True)
  649. # self.mark_shapes[aperture].redraw()
  650. else:
  651. self.marked_rows.append(False)
  652. self.clear_plot_apertures(aperture=aperture)
  653. # make sure that the Mark All is disabled if one of the row mark's are disabled and
  654. # if all the row mark's are enabled also enable the Mark All checkbox
  655. cb_cnt = 0
  656. total_row = self.ui.apertures_table.rowCount()
  657. for row in range(total_row):
  658. if self.ui.apertures_table.cellWidget(row, 5).isChecked():
  659. cb_cnt += 1
  660. else:
  661. cb_cnt -= 1
  662. if cb_cnt < total_row:
  663. self.ui.mark_all_cb.setChecked(False)
  664. else:
  665. self.ui.mark_all_cb.setChecked(True)
  666. self.ui_connect()
  667. def on_mark_all_click(self):
  668. self.ui_disconnect()
  669. mark_all = self.ui.mark_all_cb.isChecked()
  670. for row in range(self.ui.apertures_table.rowCount()):
  671. # update the mark_rows list
  672. if mark_all:
  673. self.marked_rows.append(True)
  674. else:
  675. self.marked_rows[:] = []
  676. mark_cb = self.ui.apertures_table.cellWidget(row, 5)
  677. mark_cb.setChecked(mark_all)
  678. if mark_all:
  679. for aperture in self.apertures:
  680. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  681. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  682. marked_aperture=aperture, visible=True)
  683. # HACK: enable/disable the grid for a better look
  684. self.app.ui.grid_snap_btn.trigger()
  685. self.app.ui.grid_snap_btn.trigger()
  686. else:
  687. self.clear_plot_apertures()
  688. self.marked_rows[:] = []
  689. self.ui_connect()
  690. def export_gerber(self, whole, fract, g_zeros='L', factor=1):
  691. """
  692. Creates a Gerber file content to be exported to a file.
  693. :param whole: how many digits in the whole part of coordinates
  694. :param fract: how many decimals in coordinates
  695. :param g_zeros: type of the zero suppression used: LZ or TZ; string
  696. :param factor: factor to be applied onto the Gerber coordinates
  697. :return: Gerber_code
  698. """
  699. log.debug("GerberObject.export_gerber() --> Generating the Gerber code from the selected Gerber file")
  700. def tz_format(x, y, fac):
  701. x_c = x * fac
  702. y_c = y * fac
  703. x_form = "{:.{dec}f}".format(x_c, dec=fract)
  704. y_form = "{:.{dec}f}".format(y_c, dec=fract)
  705. # extract whole part and decimal part
  706. x_form = x_form.partition('.')
  707. y_form = y_form.partition('.')
  708. # left padd the 'whole' part with zeros
  709. x_whole = x_form[0].rjust(whole, '0')
  710. y_whole = y_form[0].rjust(whole, '0')
  711. # restore the coordinate padded in the left with 0 and added the decimal part
  712. # without the decinal dot
  713. x_form = x_whole + x_form[2]
  714. y_form = y_whole + y_form[2]
  715. return x_form, y_form
  716. def lz_format(x, y, fac):
  717. x_c = x * fac
  718. y_c = y * fac
  719. x_form = "{:.{dec}f}".format(x_c, dec=fract).replace('.', '')
  720. y_form = "{:.{dec}f}".format(y_c, dec=fract).replace('.', '')
  721. # pad with rear zeros
  722. x_form.ljust(length, '0')
  723. y_form.ljust(length, '0')
  724. return x_form, y_form
  725. # Gerber code is stored here
  726. gerber_code = ''
  727. # apertures processing
  728. try:
  729. length = whole + fract
  730. if '0' in self.apertures:
  731. if 'geometry' in self.apertures['0']:
  732. for geo_elem in self.apertures['0']['geometry']:
  733. if 'solid' in geo_elem:
  734. geo = geo_elem['solid']
  735. if not geo.is_empty:
  736. gerber_code += 'G36*\n'
  737. geo_coords = list(geo.exterior.coords)
  738. # first command is a move with pen-up D02 at the beginning of the geo
  739. if g_zeros == 'T':
  740. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  741. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  742. yform=y_formatted)
  743. else:
  744. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  745. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  746. yform=y_formatted)
  747. for coord in geo_coords[1:]:
  748. if g_zeros == 'T':
  749. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  750. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  751. yform=y_formatted)
  752. else:
  753. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  754. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  755. yform=y_formatted)
  756. gerber_code += 'D02*\n'
  757. gerber_code += 'G37*\n'
  758. clear_list = list(geo.interiors)
  759. if clear_list:
  760. gerber_code += '%LPC*%\n'
  761. for clear_geo in clear_list:
  762. gerber_code += 'G36*\n'
  763. geo_coords = list(clear_geo.coords)
  764. # first command is a move with pen-up D02 at the beginning of the geo
  765. if g_zeros == 'T':
  766. x_formatted, y_formatted = tz_format(
  767. geo_coords[0][0], geo_coords[0][1], factor)
  768. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  769. yform=y_formatted)
  770. else:
  771. x_formatted, y_formatted = lz_format(
  772. geo_coords[0][0], geo_coords[0][1], factor)
  773. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  774. yform=y_formatted)
  775. prev_coord = geo_coords[0]
  776. for coord in geo_coords[1:]:
  777. if coord != prev_coord:
  778. if g_zeros == 'T':
  779. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  780. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  781. yform=y_formatted)
  782. else:
  783. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  784. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  785. yform=y_formatted)
  786. prev_coord = coord
  787. gerber_code += 'D02*\n'
  788. gerber_code += 'G37*\n'
  789. gerber_code += '%LPD*%\n'
  790. if 'clear' in geo_elem:
  791. geo = geo_elem['clear']
  792. if not geo.is_empty:
  793. gerber_code += '%LPC*%\n'
  794. gerber_code += 'G36*\n'
  795. geo_coords = list(geo.exterior.coords)
  796. # first command is a move with pen-up D02 at the beginning of the geo
  797. if g_zeros == 'T':
  798. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  799. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  800. yform=y_formatted)
  801. else:
  802. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  803. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  804. yform=y_formatted)
  805. prev_coord = geo_coords[0]
  806. for coord in geo_coords[1:]:
  807. if coord != prev_coord:
  808. if g_zeros == 'T':
  809. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  810. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  811. yform=y_formatted)
  812. else:
  813. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  814. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  815. yform=y_formatted)
  816. prev_coord = coord
  817. gerber_code += 'D02*\n'
  818. gerber_code += 'G37*\n'
  819. gerber_code += '%LPD*%\n'
  820. except Exception as e:
  821. log.debug("FlatCAMObj.GerberObject.export_gerber() '0' aperture --> %s" % str(e))
  822. for apid in self.apertures:
  823. if apid == '0':
  824. continue
  825. else:
  826. gerber_code += 'D%s*\n' % str(apid)
  827. if 'geometry' in self.apertures[apid]:
  828. for geo_elem in self.apertures[apid]['geometry']:
  829. try:
  830. if 'follow' in geo_elem:
  831. geo = geo_elem['follow']
  832. if not geo.is_empty:
  833. if isinstance(geo, Point):
  834. if g_zeros == 'T':
  835. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  836. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  837. yform=y_formatted)
  838. else:
  839. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  840. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  841. yform=y_formatted)
  842. else:
  843. geo_coords = list(geo.coords)
  844. # first command is a move with pen-up D02 at the beginning of the geo
  845. if g_zeros == 'T':
  846. x_formatted, y_formatted = tz_format(
  847. geo_coords[0][0], geo_coords[0][1], factor)
  848. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  849. yform=y_formatted)
  850. else:
  851. x_formatted, y_formatted = lz_format(
  852. geo_coords[0][0], geo_coords[0][1], factor)
  853. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  854. yform=y_formatted)
  855. prev_coord = geo_coords[0]
  856. for coord in geo_coords[1:]:
  857. if coord != prev_coord:
  858. if g_zeros == 'T':
  859. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  860. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  861. yform=y_formatted)
  862. else:
  863. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  864. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  865. yform=y_formatted)
  866. prev_coord = coord
  867. # gerber_code += "D02*\n"
  868. except Exception as e:
  869. log.debug("FlatCAMObj.GerberObject.export_gerber() 'follow' --> %s" % str(e))
  870. try:
  871. if 'clear' in geo_elem:
  872. gerber_code += '%LPC*%\n'
  873. geo = geo_elem['clear']
  874. if not geo.is_empty:
  875. if isinstance(geo, Point):
  876. if g_zeros == 'T':
  877. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  878. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  879. yform=y_formatted)
  880. else:
  881. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  882. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  883. yform=y_formatted)
  884. elif isinstance(geo, Polygon):
  885. geo_coords = list(geo.exterior.coords)
  886. # first command is a move with pen-up D02 at the beginning of the geo
  887. if g_zeros == 'T':
  888. x_formatted, y_formatted = tz_format(
  889. geo_coords[0][0], geo_coords[0][1], factor)
  890. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  891. yform=y_formatted)
  892. else:
  893. x_formatted, y_formatted = lz_format(
  894. geo_coords[0][0], geo_coords[0][1], factor)
  895. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  896. yform=y_formatted)
  897. prev_coord = geo_coords[0]
  898. for coord in geo_coords[1:]:
  899. if coord != prev_coord:
  900. if g_zeros == 'T':
  901. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  902. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  903. yform=y_formatted)
  904. else:
  905. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  906. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  907. yform=y_formatted)
  908. prev_coord = coord
  909. for geo_int in geo.interiors:
  910. geo_coords = list(geo_int.coords)
  911. # first command is a move with pen-up D02 at the beginning of the geo
  912. if g_zeros == 'T':
  913. x_formatted, y_formatted = tz_format(
  914. geo_coords[0][0], geo_coords[0][1], factor)
  915. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  916. yform=y_formatted)
  917. else:
  918. x_formatted, y_formatted = lz_format(
  919. geo_coords[0][0], geo_coords[0][1], factor)
  920. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  921. yform=y_formatted)
  922. prev_coord = geo_coords[0]
  923. for coord in geo_coords[1:]:
  924. if coord != prev_coord:
  925. if g_zeros == 'T':
  926. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  927. gerber_code += "X{xform}Y{yform}D01*\n".format(
  928. xform=x_formatted,
  929. yform=y_formatted)
  930. else:
  931. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  932. gerber_code += "X{xform}Y{yform}D01*\n".format(
  933. xform=x_formatted,
  934. yform=y_formatted)
  935. prev_coord = coord
  936. else:
  937. geo_coords = list(geo.coords)
  938. # first command is a move with pen-up D02 at the beginning of the geo
  939. if g_zeros == 'T':
  940. x_formatted, y_formatted = tz_format(
  941. geo_coords[0][0], geo_coords[0][1], factor)
  942. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  943. yform=y_formatted)
  944. else:
  945. x_formatted, y_formatted = lz_format(
  946. geo_coords[0][0], geo_coords[0][1], factor)
  947. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  948. yform=y_formatted)
  949. prev_coord = geo_coords[0]
  950. for coord in geo_coords[1:]:
  951. if coord != prev_coord:
  952. if g_zeros == 'T':
  953. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  954. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  955. yform=y_formatted)
  956. else:
  957. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  958. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  959. yform=y_formatted)
  960. prev_coord = coord
  961. # gerber_code += "D02*\n"
  962. gerber_code += '%LPD*%\n'
  963. except Exception as e:
  964. log.debug("FlatCAMObj.GerberObject.export_gerber() 'clear' --> %s" % str(e))
  965. if not self.apertures:
  966. log.debug("FlatCAMObj.GerberObject.export_gerber() --> Gerber Object is empty: no apertures.")
  967. return 'fail'
  968. return gerber_code
  969. def mirror(self, axis, point):
  970. Gerber.mirror(self, axis=axis, point=point)
  971. self.replotApertures.emit()
  972. def offset(self, vect):
  973. Gerber.offset(self, vect=vect)
  974. self.replotApertures.emit()
  975. def rotate(self, angle, point):
  976. Gerber.rotate(self, angle=angle, point=point)
  977. self.replotApertures.emit()
  978. def scale(self, xfactor, yfactor=None, point=None):
  979. Gerber.scale(self, xfactor=xfactor, yfactor=yfactor, point=point)
  980. self.replotApertures.emit()
  981. def skew(self, angle_x, angle_y, point):
  982. Gerber.skew(self, angle_x=angle_x, angle_y=angle_y, point=point)
  983. self.replotApertures.emit()
  984. def buffer(self, distance, join, factor=None):
  985. Gerber.buffer(self, distance=distance, join=join, factor=factor)
  986. self.replotApertures.emit()
  987. def serialize(self):
  988. return {
  989. "options": self.options,
  990. "kind": self.kind
  991. }