FlatCAMGerber.py 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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 flatcamParsers.ParseGerber import Gerber
  14. from flatcamObjects.FlatCAMObj import *
  15. import math
  16. import numpy as np
  17. from copy import deepcopy
  18. import gettext
  19. import FlatCAMTranslation 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. def merge(self, grb_list, grb_final):
  32. """
  33. Merges the geometry of objects in geo_list into
  34. the geometry of geo_final.
  35. :param grb_list: List of GerberObject Objects to join.
  36. :param grb_final: Destination GeometryObject object.
  37. :return: None
  38. """
  39. if grb_final.solid_geometry is None:
  40. grb_final.solid_geometry = []
  41. grb_final.follow_geometry = []
  42. if not grb_final.apertures:
  43. grb_final.apertures = {}
  44. if type(grb_final.solid_geometry) is not list:
  45. grb_final.solid_geometry = [grb_final.solid_geometry]
  46. grb_final.follow_geometry = [grb_final.follow_geometry]
  47. for grb in grb_list:
  48. # Expand lists
  49. if type(grb) is list:
  50. GerberObject.merge(grb, grb_final)
  51. else: # If not list, just append
  52. for option in grb.options:
  53. if option != 'name':
  54. try:
  55. grb_final.options[option] = grb.options[option]
  56. except KeyError:
  57. log.warning("Failed to copy option.", option)
  58. try:
  59. for geos in grb.solid_geometry:
  60. grb_final.solid_geometry.append(geos)
  61. grb_final.follow_geometry.append(geos)
  62. except TypeError:
  63. grb_final.solid_geometry.append(grb.solid_geometry)
  64. grb_final.follow_geometry.append(grb.solid_geometry)
  65. for ap in grb.apertures:
  66. if ap not in grb_final.apertures:
  67. grb_final.apertures[ap] = grb.apertures[ap]
  68. else:
  69. # create a list of integers out of the grb.apertures keys and find the max of that value
  70. # then, the aperture duplicate is assigned an id value incremented with 1,
  71. # and finally made string because the apertures dict keys are strings
  72. max_ap = str(max([int(k) for k in grb_final.apertures.keys()]) + 1)
  73. grb_final.apertures[max_ap] = {}
  74. grb_final.apertures[max_ap]['geometry'] = []
  75. for k, v in grb.apertures[ap].items():
  76. grb_final.apertures[max_ap][k] = deepcopy(v)
  77. grb_final.solid_geometry = MultiPolygon(grb_final.solid_geometry)
  78. grb_final.follow_geometry = MultiPolygon(grb_final.follow_geometry)
  79. def __init__(self, name):
  80. self.decimals = self.app.decimals
  81. self.circle_steps = int(self.app.defaults["gerber_circle_steps"])
  82. Gerber.__init__(self, steps_per_circle=self.circle_steps)
  83. FlatCAMObj.__init__(self, name)
  84. self.kind = "gerber"
  85. # The 'name' is already in self.options from FlatCAMObj
  86. # Automatically updates the UI
  87. self.options.update({
  88. "plot": True,
  89. "multicolored": False,
  90. "solid": False,
  91. "tool_type": 'circular',
  92. "vtipdia": 0.1,
  93. "vtipangle": 30,
  94. "vcutz": -0.05,
  95. "isotooldia": 0.016,
  96. "isopasses": 1,
  97. "isooverlap": 15,
  98. "milling_type": "cl",
  99. "combine_passes": True,
  100. "noncoppermargin": 0.0,
  101. "noncopperrounded": False,
  102. "bboxmargin": 0.0,
  103. "bboxrounded": False,
  104. "aperture_display": False,
  105. "follow": False,
  106. "iso_scope": 'all',
  107. "iso_type": 'full'
  108. })
  109. # type of isolation: 0 = exteriors, 1 = interiors, 2 = complete isolation (both interiors and exteriors)
  110. self.iso_type = 2
  111. self.multigeo = False
  112. self.follow = False
  113. self.apertures_row = 0
  114. # store the source file here
  115. self.source_file = ""
  116. # list of rows with apertures plotted
  117. self.marked_rows = []
  118. # Mouse events
  119. self.mr = None
  120. self.mm = None
  121. self.mp = None
  122. # dict to store the polygons selected for isolation; key is the shape added to be plotted and value is the poly
  123. self.poly_dict = {}
  124. # store the status of grid snapping
  125. self.grid_status_memory = None
  126. self.units_found = self.app.defaults['units']
  127. self.fill_color = self.app.defaults['gerber_plot_fill']
  128. self.outline_color = self.app.defaults['gerber_plot_line']
  129. self.alpha_level = 'bf'
  130. # keep track if the UI is built so we don't have to build it every time
  131. self.ui_build = False
  132. # build only once the aperture storage (takes time)
  133. self.build_aperture_storage = False
  134. # Attributes to be included in serialization
  135. # Always append to it because it carries contents
  136. # from predecessors.
  137. self.ser_attrs += ['options', 'kind', 'fill_color', 'outline_color', 'alpha_level']
  138. def set_ui(self, ui):
  139. """
  140. Maps options with GUI inputs.
  141. Connects GUI events to methods.
  142. :param ui: GUI object.
  143. :type ui: GerberObjectUI
  144. :return: None
  145. """
  146. FlatCAMObj.set_ui(self, ui)
  147. log.debug("GerberObject.set_ui()")
  148. self.units = self.app.defaults['units'].upper()
  149. self.replotApertures.connect(self.on_mark_cb_click_table)
  150. self.form_fields.update({
  151. "plot": self.ui.plot_cb,
  152. "multicolored": self.ui.multicolored_cb,
  153. "solid": self.ui.solid_cb,
  154. "tool_type": self.ui.tool_type_radio,
  155. "vtipdia": self.ui.tipdia_spinner,
  156. "vtipangle": self.ui.tipangle_spinner,
  157. "vcutz": self.ui.cutz_spinner,
  158. "isotooldia": self.ui.iso_tool_dia_entry,
  159. "isopasses": self.ui.iso_width_entry,
  160. "isooverlap": self.ui.iso_overlap_entry,
  161. "milling_type": self.ui.milling_type_radio,
  162. "combine_passes": self.ui.combine_passes_cb,
  163. "noncoppermargin": self.ui.noncopper_margin_entry,
  164. "noncopperrounded": self.ui.noncopper_rounded_cb,
  165. "bboxmargin": self.ui.bbmargin_entry,
  166. "bboxrounded": self.ui.bbrounded_cb,
  167. "aperture_display": self.ui.aperture_table_visibility_cb,
  168. "follow": self.ui.follow_cb,
  169. "iso_scope": self.ui.iso_scope_radio,
  170. "iso_type": self.ui.iso_type_radio
  171. })
  172. # Fill form fields only on object create
  173. self.to_form()
  174. assert isinstance(self.ui, GerberObjectUI)
  175. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  176. self.ui.solid_cb.stateChanged.connect(self.on_solid_cb_click)
  177. self.ui.multicolored_cb.stateChanged.connect(self.on_multicolored_cb_click)
  178. self.ui.generate_iso_button.clicked.connect(self.on_iso_button_click)
  179. self.ui.generate_ncc_button.clicked.connect(self.app.ncclear_tool.run)
  180. self.ui.generate_cutout_button.clicked.connect(self.app.cutout_tool.run)
  181. self.ui.generate_bb_button.clicked.connect(self.on_generatebb_button_click)
  182. self.ui.generate_noncopper_button.clicked.connect(self.on_generatenoncopper_button_click)
  183. self.ui.aperture_table_visibility_cb.stateChanged.connect(self.on_aperture_table_visibility_change)
  184. self.ui.follow_cb.stateChanged.connect(self.on_follow_cb_click)
  185. # set the model for the Area Exception comboboxes
  186. self.ui.obj_combo.setModel(self.app.collection)
  187. self.ui.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  188. self.ui.obj_combo.is_last = True
  189. self.ui.obj_combo.obj_type = {
  190. _("Gerber"): "Gerber", _("Geometry"): "Geometry"
  191. }[self.ui.type_obj_combo.get_value()]
  192. self.on_type_obj_index_changed()
  193. self.ui.type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  194. self.ui.tool_type_radio.activated_custom.connect(self.on_tool_type_change)
  195. # establish visibility for the GUI elements found in the slot function
  196. self.ui.tool_type_radio.activated_custom.emit(self.options['tool_type'])
  197. # Show/Hide Advanced Options
  198. if self.app.defaults["global_app_level"] == 'b':
  199. self.ui.level.setText('<span style="color:green;"><b>%s</b></span>' % _('Basic'))
  200. self.options['tool_type'] = 'circular'
  201. self.ui.tool_type_label.hide()
  202. self.ui.tool_type_radio.hide()
  203. # override the Preferences Value; in Basic mode the Tool Type is always Circular ('C1')
  204. self.ui.tool_type_radio.set_value('circular')
  205. self.ui.tipdialabel.hide()
  206. self.ui.tipdia_spinner.hide()
  207. self.ui.tipanglelabel.hide()
  208. self.ui.tipangle_spinner.hide()
  209. self.ui.cutzlabel.hide()
  210. self.ui.cutz_spinner.hide()
  211. self.ui.apertures_table_label.hide()
  212. self.ui.aperture_table_visibility_cb.hide()
  213. self.ui.milling_type_label.hide()
  214. self.ui.milling_type_radio.hide()
  215. self.ui.iso_type_label.hide()
  216. self.ui.iso_type_radio.hide()
  217. self.ui.follow_cb.hide()
  218. self.ui.except_cb.setChecked(False)
  219. self.ui.except_cb.hide()
  220. else:
  221. self.ui.level.setText('<span style="color:red;"><b>%s</b></span>' % _('Advanced'))
  222. self.ui.tipdia_spinner.valueChanged.connect(self.on_calculate_tooldia)
  223. self.ui.tipangle_spinner.valueChanged.connect(self.on_calculate_tooldia)
  224. self.ui.cutz_spinner.valueChanged.connect(self.on_calculate_tooldia)
  225. if self.app.defaults["gerber_buffering"] == 'no':
  226. self.ui.create_buffer_button.show()
  227. try:
  228. self.ui.create_buffer_button.clicked.disconnect(self.on_generate_buffer)
  229. except TypeError:
  230. pass
  231. self.ui.create_buffer_button.clicked.connect(self.on_generate_buffer)
  232. else:
  233. self.ui.create_buffer_button.hide()
  234. # set initial state of the aperture table and associated widgets
  235. self.on_aperture_table_visibility_change()
  236. self.build_ui()
  237. self.units_found = self.app.defaults['units']
  238. def on_calculate_tooldia(self):
  239. try:
  240. tdia = float(self.ui.tipdia_spinner.get_value())
  241. except Exception:
  242. return
  243. try:
  244. dang = float(self.ui.tipangle_spinner.get_value())
  245. except Exception:
  246. return
  247. try:
  248. cutz = float(self.ui.cutz_spinner.get_value())
  249. except Exception:
  250. return
  251. cutz *= -1
  252. if cutz < 0:
  253. cutz *= -1
  254. half_tip_angle = dang / 2
  255. tool_diameter = tdia + (2 * cutz * math.tan(math.radians(half_tip_angle)))
  256. self.ui.iso_tool_dia_entry.set_value(tool_diameter)
  257. def on_type_obj_index_changed(self):
  258. val = self.ui.type_obj_combo.get_value()
  259. obj_type = {"Gerber": 0, "Geometry": 2}[val]
  260. self.ui.obj_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  261. self.ui.obj_combo.setCurrentIndex(0)
  262. self.ui.obj_combo.obj_type = {_("Gerber"): "Gerber", _("Geometry"): "Geometry"}[val]
  263. def on_tool_type_change(self, state):
  264. if state == 'circular':
  265. self.ui.tipdialabel.hide()
  266. self.ui.tipdia_spinner.hide()
  267. self.ui.tipanglelabel.hide()
  268. self.ui.tipangle_spinner.hide()
  269. self.ui.cutzlabel.hide()
  270. self.ui.cutz_spinner.hide()
  271. self.ui.iso_tool_dia_entry.setDisabled(False)
  272. # update the value in the self.iso_tool_dia_entry once this is selected
  273. self.ui.iso_tool_dia_entry.set_value(self.options['isotooldia'])
  274. else:
  275. self.ui.tipdialabel.show()
  276. self.ui.tipdia_spinner.show()
  277. self.ui.tipanglelabel.show()
  278. self.ui.tipangle_spinner.show()
  279. self.ui.cutzlabel.show()
  280. self.ui.cutz_spinner.show()
  281. self.ui.iso_tool_dia_entry.setDisabled(True)
  282. # update the value in the self.iso_tool_dia_entry once this is selected
  283. self.on_calculate_tooldia()
  284. def build_ui(self):
  285. FlatCAMObj.build_ui(self)
  286. if self.ui.aperture_table_visibility_cb.get_value() and self.ui_build is False:
  287. self.ui_build = True
  288. try:
  289. # if connected, disconnect the signal from the slot on item_changed as it creates issues
  290. self.ui.apertures_table.itemChanged.disconnect()
  291. except (TypeError, AttributeError):
  292. pass
  293. self.apertures_row = 0
  294. aper_no = self.apertures_row + 1
  295. sort = []
  296. for k, v in list(self.apertures.items()):
  297. sort.append(int(k))
  298. sorted_apertures = sorted(sort)
  299. n = len(sorted_apertures)
  300. self.ui.apertures_table.setRowCount(n)
  301. for ap_code in sorted_apertures:
  302. ap_code = str(ap_code)
  303. ap_id_item = QtWidgets.QTableWidgetItem('%d' % int(self.apertures_row + 1))
  304. ap_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  305. self.ui.apertures_table.setItem(self.apertures_row, 0, ap_id_item) # Tool name/id
  306. ap_code_item = QtWidgets.QTableWidgetItem(ap_code)
  307. ap_code_item.setFlags(QtCore.Qt.ItemIsEnabled)
  308. ap_type_item = QtWidgets.QTableWidgetItem(str(self.apertures[ap_code]['type']))
  309. ap_type_item.setFlags(QtCore.Qt.ItemIsEnabled)
  310. if str(self.apertures[ap_code]['type']) == 'R' or str(self.apertures[ap_code]['type']) == 'O':
  311. ap_dim_item = QtWidgets.QTableWidgetItem(
  312. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['width'],
  313. self.decimals, self.apertures[ap_code]['height']
  314. )
  315. )
  316. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  317. elif str(self.apertures[ap_code]['type']) == 'P':
  318. ap_dim_item = QtWidgets.QTableWidgetItem(
  319. '%.*f, %.*f' % (self.decimals, self.apertures[ap_code]['diam'],
  320. self.decimals, self.apertures[ap_code]['nVertices'])
  321. )
  322. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  323. else:
  324. ap_dim_item = QtWidgets.QTableWidgetItem('')
  325. ap_dim_item.setFlags(QtCore.Qt.ItemIsEnabled)
  326. try:
  327. if self.apertures[ap_code]['size'] is not None:
  328. ap_size_item = QtWidgets.QTableWidgetItem(
  329. '%.*f' % (self.decimals, float(self.apertures[ap_code]['size'])))
  330. else:
  331. ap_size_item = QtWidgets.QTableWidgetItem('')
  332. except KeyError:
  333. ap_size_item = QtWidgets.QTableWidgetItem('')
  334. ap_size_item.setFlags(QtCore.Qt.ItemIsEnabled)
  335. mark_item = FCCheckBox()
  336. mark_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  337. # if self.ui.aperture_table_visibility_cb.isChecked():
  338. # mark_item.setChecked(True)
  339. self.ui.apertures_table.setItem(self.apertures_row, 1, ap_code_item) # Aperture Code
  340. self.ui.apertures_table.setItem(self.apertures_row, 2, ap_type_item) # Aperture Type
  341. self.ui.apertures_table.setItem(self.apertures_row, 3, ap_size_item) # Aperture Dimensions
  342. self.ui.apertures_table.setItem(self.apertures_row, 4, ap_dim_item) # Aperture Dimensions
  343. empty_plot_item = QtWidgets.QTableWidgetItem('')
  344. empty_plot_item.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  345. self.ui.apertures_table.setItem(self.apertures_row, 5, empty_plot_item)
  346. self.ui.apertures_table.setCellWidget(self.apertures_row, 5, mark_item)
  347. self.apertures_row += 1
  348. self.ui.apertures_table.selectColumn(0)
  349. self.ui.apertures_table.resizeColumnsToContents()
  350. self.ui.apertures_table.resizeRowsToContents()
  351. vertical_header = self.ui.apertures_table.verticalHeader()
  352. # vertical_header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  353. vertical_header.hide()
  354. self.ui.apertures_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  355. horizontal_header = self.ui.apertures_table.horizontalHeader()
  356. horizontal_header.setMinimumSectionSize(10)
  357. horizontal_header.setDefaultSectionSize(70)
  358. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  359. horizontal_header.resizeSection(0, 27)
  360. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  361. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  362. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  363. horizontal_header.setSectionResizeMode(4, QtWidgets.QHeaderView.Stretch)
  364. horizontal_header.setSectionResizeMode(5, QtWidgets.QHeaderView.Fixed)
  365. horizontal_header.resizeSection(5, 17)
  366. self.ui.apertures_table.setColumnWidth(5, 17)
  367. self.ui.apertures_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  368. self.ui.apertures_table.setSortingEnabled(False)
  369. self.ui.apertures_table.setMinimumHeight(self.ui.apertures_table.getHeight())
  370. self.ui.apertures_table.setMaximumHeight(self.ui.apertures_table.getHeight())
  371. # update the 'mark' checkboxes state according with what is stored in the self.marked_rows list
  372. if self.marked_rows:
  373. for row in range(self.ui.apertures_table.rowCount()):
  374. try:
  375. self.ui.apertures_table.cellWidget(row, 5).set_value(self.marked_rows[row])
  376. except IndexError:
  377. pass
  378. self.ui_connect()
  379. def ui_connect(self):
  380. for row in range(self.ui.apertures_table.rowCount()):
  381. try:
  382. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect(self.on_mark_cb_click_table)
  383. except (TypeError, AttributeError):
  384. pass
  385. self.ui.apertures_table.cellWidget(row, 5).clicked.connect(self.on_mark_cb_click_table)
  386. try:
  387. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  388. except (TypeError, AttributeError):
  389. pass
  390. self.ui.mark_all_cb.clicked.connect(self.on_mark_all_click)
  391. def ui_disconnect(self):
  392. for row in range(self.ui.apertures_table.rowCount()):
  393. try:
  394. self.ui.apertures_table.cellWidget(row, 5).clicked.disconnect()
  395. except (TypeError, AttributeError):
  396. pass
  397. try:
  398. self.ui.mark_all_cb.clicked.disconnect(self.on_mark_all_click)
  399. except (TypeError, AttributeError):
  400. pass
  401. def on_generate_buffer(self):
  402. self.app.inform.emit('[WARNING_NOTCL] %s...' % _("Buffering solid geometry"))
  403. def buffer_task():
  404. with self.app.proc_container.new('%s...' % _("Buffering")):
  405. if isinstance(self.solid_geometry, list):
  406. self.solid_geometry = MultiPolygon(self.solid_geometry)
  407. self.solid_geometry = self.solid_geometry.buffer(0.0000001)
  408. self.solid_geometry = self.solid_geometry.buffer(-0.0000001)
  409. self.app.inform.emit('[success] %s.' % _("Done"))
  410. self.plot_single_object.emit()
  411. self.app.worker_task.emit({'fcn': buffer_task, 'params': []})
  412. def on_generatenoncopper_button_click(self, *args):
  413. self.app.report_usage("gerber_on_generatenoncopper_button")
  414. self.read_form()
  415. name = self.options["name"] + "_noncopper"
  416. def geo_init(geo_obj, app_obj):
  417. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  418. if isinstance(self.solid_geometry, list):
  419. try:
  420. self.solid_geometry = MultiPolygon(self.solid_geometry)
  421. except Exception:
  422. self.solid_geometry = cascaded_union(self.solid_geometry)
  423. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["noncoppermargin"]))
  424. if not self.options["noncopperrounded"]:
  425. bounding_box = bounding_box.envelope
  426. non_copper = bounding_box.difference(self.solid_geometry)
  427. if non_copper is None or non_copper.is_empty:
  428. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  429. return "fail"
  430. geo_obj.solid_geometry = non_copper
  431. self.app.new_object("geometry", name, geo_init)
  432. def on_generatebb_button_click(self, *args):
  433. self.app.report_usage("gerber_on_generatebb_button")
  434. self.read_form()
  435. name = self.options["name"] + "_bbox"
  436. def geo_init(geo_obj, app_obj):
  437. assert geo_obj.kind == 'geometry', "Expected a Geometry object got %s" % type(geo_obj)
  438. if isinstance(self.solid_geometry, list):
  439. try:
  440. self.solid_geometry = MultiPolygon(self.solid_geometry)
  441. except Exception:
  442. self.solid_geometry = cascaded_union(self.solid_geometry)
  443. # Bounding box with rounded corners
  444. bounding_box = self.solid_geometry.envelope.buffer(float(self.options["bboxmargin"]))
  445. if not self.options["bboxrounded"]: # Remove rounded corners
  446. bounding_box = bounding_box.envelope
  447. if bounding_box is None or bounding_box.is_empty:
  448. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Operation could not be done."))
  449. return "fail"
  450. geo_obj.solid_geometry = bounding_box
  451. self.app.new_object("geometry", name, geo_init)
  452. def on_iso_button_click(self, *args):
  453. obj = self.app.collection.get_active()
  454. self.iso_type = 2
  455. if self.ui.iso_type_radio.get_value() == 'ext':
  456. self.iso_type = 0
  457. if self.ui.iso_type_radio.get_value() == 'int':
  458. self.iso_type = 1
  459. def worker_task(iso_obj, app_obj):
  460. with self.app.proc_container.new(_("Isolating...")):
  461. if self.ui.follow_cb.get_value() is True:
  462. iso_obj.follow_geo()
  463. # in the end toggle the visibility of the origin object so we can see the generated Geometry
  464. iso_obj.ui.plot_cb.toggle()
  465. else:
  466. app_obj.report_usage("gerber_on_iso_button")
  467. self.read_form()
  468. iso_scope = 'all' if self.ui.iso_scope_radio.get_value() == 'all' else 'single'
  469. self.isolate_handler(iso_type=self.iso_type, iso_scope=iso_scope)
  470. self.app.worker_task.emit({'fcn': worker_task, 'params': [obj, self.app]})
  471. def follow_geo(self, outname=None):
  472. """
  473. Creates a geometry object "following" the gerber paths.
  474. :return: None
  475. """
  476. # default_name = self.options["name"] + "_follow"
  477. # follow_name = outname or default_name
  478. if outname is None:
  479. follow_name = self.options["name"] + "_follow"
  480. else:
  481. follow_name = outname
  482. def follow_init(follow_obj, app):
  483. # Propagate options
  484. follow_obj.options["cnctooldia"] = str(self.options["isotooldia"])
  485. follow_obj.solid_geometry = self.follow_geometry
  486. # TODO: Do something if this is None. Offer changing name?
  487. try:
  488. self.app.new_object("geometry", follow_name, follow_init)
  489. except Exception as e:
  490. return "Operation failed: %s" % str(e)
  491. def isolate_handler(self, iso_type, iso_scope):
  492. if iso_scope == 'all':
  493. self.isolate(iso_type=iso_type)
  494. else:
  495. # disengage the grid snapping since it may be hard to click on polygons with grid snapping on
  496. if self.app.ui.grid_snap_btn.isChecked():
  497. self.grid_status_memory = True
  498. self.app.ui.grid_snap_btn.trigger()
  499. else:
  500. self.grid_status_memory = False
  501. self.mr = self.app.plotcanvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  502. if self.app.is_legacy is False:
  503. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  504. else:
  505. self.app.plotcanvas.graph_event_disconnect(self.app.mr)
  506. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Click on a polygon to isolate it."))
  507. def on_mouse_click_release(self, event):
  508. if self.app.is_legacy is False:
  509. event_pos = event.pos
  510. right_button = 2
  511. self.app.event_is_dragging = self.app.event_is_dragging
  512. else:
  513. event_pos = (event.xdata, event.ydata)
  514. right_button = 3
  515. self.app.event_is_dragging = self.app.ui.popMenu.mouse_is_panning
  516. try:
  517. x = float(event_pos[0])
  518. y = float(event_pos[1])
  519. except TypeError:
  520. return
  521. event_pos = (x, y)
  522. curr_pos = self.app.plotcanvas.translate_coords(event_pos)
  523. if self.app.grid_status():
  524. curr_pos = self.app.geo_editor.snap(curr_pos[0], curr_pos[1])
  525. else:
  526. curr_pos = (curr_pos[0], curr_pos[1])
  527. if event.button == 1:
  528. clicked_poly = self.find_polygon(point=(curr_pos[0], curr_pos[1]))
  529. if self.app.selection_type is not None:
  530. self.selection_area_handler(self.app.pos, curr_pos, self.app.selection_type)
  531. self.app.selection_type = None
  532. elif clicked_poly:
  533. if clicked_poly not in self.poly_dict.values():
  534. shape_id = self.app.tool_shapes.add(tolerance=self.drawing_tolerance, layer=0, shape=clicked_poly,
  535. color=self.app.defaults['global_sel_draw_color'] + 'AF',
  536. face_color=self.app.defaults['global_sel_draw_color'] + 'AF',
  537. visible=True)
  538. self.poly_dict[shape_id] = clicked_poly
  539. self.app.inform.emit(
  540. '%s: %d. %s' % (_("Added polygon"), int(len(self.poly_dict)),
  541. _("Click to add next polygon or right click to start isolation."))
  542. )
  543. else:
  544. try:
  545. for k, v in list(self.poly_dict.items()):
  546. if v == clicked_poly:
  547. self.app.tool_shapes.remove(k)
  548. self.poly_dict.pop(k)
  549. break
  550. except TypeError:
  551. return
  552. self.app.inform.emit(
  553. '%s. %s' % (_("Removed polygon"),
  554. _("Click to add/remove next polygon or right click to start isolation."))
  555. )
  556. self.app.tool_shapes.redraw()
  557. else:
  558. self.app.inform.emit(_("No polygon detected under click position."))
  559. elif event.button == right_button and self.app.event_is_dragging is False:
  560. # restore the Grid snapping if it was active before
  561. if self.grid_status_memory is True:
  562. self.app.ui.grid_snap_btn.trigger()
  563. if self.app.is_legacy is False:
  564. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  565. else:
  566. self.app.plotcanvas.graph_event_disconnect(self.mr)
  567. self.app.mr = self.app.plotcanvas.graph_event_connect('mouse_release',
  568. self.app.on_mouse_click_release_over_plot)
  569. self.app.tool_shapes.clear(update=True)
  570. if self.poly_dict:
  571. poly_list = deepcopy(list(self.poly_dict.values()))
  572. self.isolate(iso_type=self.iso_type, geometry=poly_list)
  573. self.poly_dict.clear()
  574. else:
  575. self.app.inform.emit('[ERROR_NOTCL] %s' % _("List of single polygons is empty. Aborting."))
  576. def selection_area_handler(self, start_pos, end_pos, sel_type):
  577. """
  578. :param start_pos: mouse position when the selection LMB click was done
  579. :param end_pos: mouse position when the left mouse button is released
  580. :param sel_type: if True it's a left to right selection (enclosure), if False it's a 'touch' selection
  581. :return:
  582. """
  583. poly_selection = Polygon([start_pos, (end_pos[0], start_pos[1]), end_pos, (start_pos[0], end_pos[1])])
  584. # delete previous selection shape
  585. self.app.delete_selection_shape()
  586. added_poly_count = 0
  587. try:
  588. for geo in self.solid_geometry:
  589. if geo not in self.poly_dict.values():
  590. if sel_type is True:
  591. if geo.within(poly_selection):
  592. shape_id = self.app.tool_shapes.add(tolerance=self.drawing_tolerance, layer=0,
  593. shape=geo,
  594. color=self.app.defaults['global_sel_draw_color'] + 'AF',
  595. face_color=self.app.defaults[
  596. 'global_sel_draw_color'] + 'AF',
  597. visible=True)
  598. self.poly_dict[shape_id] = geo
  599. added_poly_count += 1
  600. else:
  601. if poly_selection.intersects(geo):
  602. shape_id = self.app.tool_shapes.add(tolerance=self.drawing_tolerance, layer=0,
  603. shape=geo,
  604. color=self.app.defaults['global_sel_draw_color'] + 'AF',
  605. face_color=self.app.defaults[
  606. 'global_sel_draw_color'] + 'AF',
  607. visible=True)
  608. self.poly_dict[shape_id] = geo
  609. added_poly_count += 1
  610. except TypeError:
  611. if self.solid_geometry not in self.poly_dict.values():
  612. if sel_type is True:
  613. if self.solid_geometry.within(poly_selection):
  614. shape_id = self.app.tool_shapes.add(tolerance=self.drawing_tolerance, layer=0,
  615. shape=self.solid_geometry,
  616. color=self.app.defaults['global_sel_draw_color'] + 'AF',
  617. face_color=self.app.defaults[
  618. 'global_sel_draw_color'] + 'AF',
  619. visible=True)
  620. self.poly_dict[shape_id] = self.solid_geometry
  621. added_poly_count += 1
  622. else:
  623. if poly_selection.intersects(self.solid_geometry):
  624. shape_id = self.app.tool_shapes.add(tolerance=self.drawing_tolerance, layer=0,
  625. shape=self.solid_geometry,
  626. color=self.app.defaults['global_sel_draw_color'] + 'AF',
  627. face_color=self.app.defaults[
  628. 'global_sel_draw_color'] + 'AF',
  629. visible=True)
  630. self.poly_dict[shape_id] = self.solid_geometry
  631. added_poly_count += 1
  632. if added_poly_count > 0:
  633. self.app.tool_shapes.redraw()
  634. self.app.inform.emit(
  635. '%s: %d. %s' % (_("Added polygon"),
  636. int(added_poly_count),
  637. _("Click to add next polygon or right click to start isolation."))
  638. )
  639. else:
  640. self.app.inform.emit(_("No polygon in selection."))
  641. def isolate(self, iso_type=None, geometry=None, dia=None, passes=None, overlap=None, outname=None, combine=None,
  642. milling_type=None, follow=None, plot=True):
  643. """
  644. Creates an isolation routing geometry object in the project.
  645. :param iso_type: type of isolation to be done: 0 = exteriors, 1 = interiors and 2 = both
  646. :param geometry: specific geometry to isolate
  647. :param dia: Tool diameter
  648. :param passes: Number of tool widths to cut
  649. :param overlap: Overlap between passes in fraction of tool diameter
  650. :param outname: Base name of the output object
  651. :param combine: Boolean: if to combine passes in one resulting object in case of multiple passes
  652. :param milling_type: type of milling: conventional or climbing
  653. :param follow: Boolean: if to generate a 'follow' geometry
  654. :param plot: Boolean: if to plot the resulting geometry object
  655. :return: None
  656. """
  657. if geometry is None:
  658. work_geo = self.follow_geometry if follow is True else self.solid_geometry
  659. else:
  660. work_geo = geometry
  661. if dia is None:
  662. dia = float(self.options["isotooldia"])
  663. if passes is None:
  664. passes = int(self.options["isopasses"])
  665. if overlap is None:
  666. overlap = float(self.options["isooverlap"])
  667. overlap /= 100.0
  668. combine = self.options["combine_passes"] if combine is None else bool(combine)
  669. if milling_type is None:
  670. milling_type = self.options["milling_type"]
  671. if iso_type is None:
  672. iso_t = 2
  673. else:
  674. iso_t = iso_type
  675. base_name = self.options["name"]
  676. if combine:
  677. if outname is None:
  678. if self.iso_type == 0:
  679. iso_name = base_name + "_ext_iso"
  680. elif self.iso_type == 1:
  681. iso_name = base_name + "_int_iso"
  682. else:
  683. iso_name = base_name + "_iso"
  684. else:
  685. iso_name = outname
  686. def iso_init(geo_obj, app_obj):
  687. # Propagate options
  688. geo_obj.options["cnctooldia"] = str(self.options["isotooldia"])
  689. geo_obj.tool_type = self.ui.tool_type_radio.get_value().upper()
  690. geo_obj.solid_geometry = []
  691. # transfer the Cut Z and Vtip and VAngle values in case that we use the V-Shape tool in Gerber UI
  692. if self.ui.tool_type_radio.get_value() == 'v':
  693. new_cutz = self.ui.cutz_spinner.get_value()
  694. new_vtipdia = self.ui.tipdia_spinner.get_value()
  695. new_vtipangle = self.ui.tipangle_spinner.get_value()
  696. tool_type = 'V'
  697. else:
  698. new_cutz = self.app.defaults['geometry_cutz']
  699. new_vtipdia = self.app.defaults['geometry_vtipdia']
  700. new_vtipangle = self.app.defaults['geometry_vtipangle']
  701. tool_type = 'C1'
  702. # store here the default data for Geometry Data
  703. default_data = {}
  704. default_data.update({
  705. "name": iso_name,
  706. "plot": self.app.defaults['geometry_plot'],
  707. "cutz": new_cutz,
  708. "vtipdia": new_vtipdia,
  709. "vtipangle": new_vtipangle,
  710. "travelz": self.app.defaults['geometry_travelz'],
  711. "feedrate": self.app.defaults['geometry_feedrate'],
  712. "feedrate_z": self.app.defaults['geometry_feedrate_z'],
  713. "feedrate_rapid": self.app.defaults['geometry_feedrate_rapid'],
  714. "dwell": self.app.defaults['geometry_dwell'],
  715. "dwelltime": self.app.defaults['geometry_dwelltime'],
  716. "multidepth": self.app.defaults['geometry_multidepth'],
  717. "ppname_g": self.app.defaults['geometry_ppname_g'],
  718. "depthperpass": self.app.defaults['geometry_depthperpass'],
  719. "extracut": self.app.defaults['geometry_extracut'],
  720. "extracut_length": self.app.defaults['geometry_extracut_length'],
  721. "toolchange": self.app.defaults['geometry_toolchange'],
  722. "toolchangez": self.app.defaults['geometry_toolchangez'],
  723. "endz": self.app.defaults['geometry_endz'],
  724. "spindlespeed": self.app.defaults['geometry_spindlespeed'],
  725. "toolchangexy": self.app.defaults['geometry_toolchangexy'],
  726. "startz": self.app.defaults['geometry_startz']
  727. })
  728. geo_obj.tools = {}
  729. geo_obj.tools['1'] = {}
  730. geo_obj.tools.update({
  731. '1': {
  732. 'tooldia': float(self.options["isotooldia"]),
  733. 'offset': 'Path',
  734. 'offset_value': 0.0,
  735. 'type': _('Rough'),
  736. 'tool_type': tool_type,
  737. 'data': default_data,
  738. 'solid_geometry': geo_obj.solid_geometry
  739. }
  740. })
  741. for nr_pass in range(passes):
  742. iso_offset = dia * ((2 * nr_pass + 1) / 2.0) - (nr_pass * overlap * dia)
  743. # if milling type is climb then the move is counter-clockwise around features
  744. mill_dir = 1 if milling_type == 'cl' else 0
  745. geom = self.generate_envelope(iso_offset, mill_dir, geometry=work_geo, env_iso_type=iso_t,
  746. follow=follow, nr_passes=nr_pass)
  747. if geom == 'fail':
  748. app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Isolation geometry could not be generated."))
  749. return 'fail'
  750. geo_obj.solid_geometry.append(geom)
  751. # update the geometry in the tools
  752. geo_obj.tools['1']['solid_geometry'] = geo_obj.solid_geometry
  753. # detect if solid_geometry is empty and this require list flattening which is "heavy"
  754. # or just looking in the lists (they are one level depth) and if any is not empty
  755. # proceed with object creation, if there are empty and the number of them is the length
  756. # of the list then we have an empty solid_geometry which should raise a Custom Exception
  757. empty_cnt = 0
  758. if not isinstance(geo_obj.solid_geometry, list) and \
  759. not isinstance(geo_obj.solid_geometry, MultiPolygon):
  760. geo_obj.solid_geometry = [geo_obj.solid_geometry]
  761. for g in geo_obj.solid_geometry:
  762. if g:
  763. break
  764. else:
  765. empty_cnt += 1
  766. if empty_cnt == len(geo_obj.solid_geometry):
  767. raise ValidationError("Empty Geometry", None)
  768. else:
  769. app_obj.inform.emit('[success] %s" %s' % (_("Isolation geometry created"), geo_obj.options["name"]))
  770. # even if combine is checked, one pass is still single-geo
  771. geo_obj.multigeo = True if passes > 1 else False
  772. # ############################################################
  773. # ########## AREA SUBTRACTION ################################
  774. # ############################################################
  775. if self.ui.except_cb.get_value():
  776. self.app.proc_container.update_view_text(' %s' % _("Subtracting Geo"))
  777. geo_obj.solid_geometry = self.area_subtraction(geo_obj.solid_geometry)
  778. # TODO: Do something if this is None. Offer changing name?
  779. self.app.new_object("geometry", iso_name, iso_init, plot=plot)
  780. else:
  781. for i in range(passes):
  782. offset = dia * ((2 * i + 1) / 2.0) - (i * overlap * dia)
  783. if passes > 1:
  784. if outname is None:
  785. if self.iso_type == 0:
  786. iso_name = base_name + "_ext_iso" + str(i + 1)
  787. elif self.iso_type == 1:
  788. iso_name = base_name + "_int_iso" + str(i + 1)
  789. else:
  790. iso_name = base_name + "_iso" + str(i + 1)
  791. else:
  792. iso_name = outname
  793. else:
  794. if outname is None:
  795. if self.iso_type == 0:
  796. iso_name = base_name + "_ext_iso"
  797. elif self.iso_type == 1:
  798. iso_name = base_name + "_int_iso"
  799. else:
  800. iso_name = base_name + "_iso"
  801. else:
  802. iso_name = outname
  803. def iso_init(geo_obj, app_obj):
  804. # Propagate options
  805. geo_obj.options["cnctooldia"] = str(self.options["isotooldia"])
  806. if self.ui.tool_type_radio.get_value() == 'v':
  807. geo_obj.tool_type = 'V'
  808. else:
  809. geo_obj.tool_type = 'C1'
  810. # if milling type is climb then the move is counter-clockwise around features
  811. mill_dir = 1 if milling_type == 'cl' else 0
  812. geom = self.generate_envelope(offset, mill_dir, geometry=work_geo, env_iso_type=iso_t,
  813. follow=follow,
  814. nr_passes=i)
  815. if geom == 'fail':
  816. app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Isolation geometry could not be generated."))
  817. return 'fail'
  818. geo_obj.solid_geometry = geom
  819. # transfer the Cut Z and Vtip and VAngle values in case that we use the V-Shape tool in Gerber UI
  820. # even if the resulting geometry is not multigeo we add the tools dict which will hold the data
  821. # required to be transfered to the Geometry object
  822. if self.ui.tool_type_radio.get_value() == 'v':
  823. new_cutz = self.ui.cutz_spinner.get_value()
  824. new_vtipdia = self.ui.tipdia_spinner.get_value()
  825. new_vtipangle = self.ui.tipangle_spinner.get_value()
  826. tool_type = 'V'
  827. else:
  828. new_cutz = self.app.defaults['geometry_cutz']
  829. new_vtipdia = self.app.defaults['geometry_vtipdia']
  830. new_vtipangle = self.app.defaults['geometry_vtipangle']
  831. tool_type = 'C1'
  832. # store here the default data for Geometry Data
  833. default_data = {}
  834. default_data.update({
  835. "name": iso_name,
  836. "plot": self.app.defaults['geometry_plot'],
  837. "cutz": new_cutz,
  838. "vtipdia": new_vtipdia,
  839. "vtipangle": new_vtipangle,
  840. "travelz": self.app.defaults['geometry_travelz'],
  841. "feedrate": self.app.defaults['geometry_feedrate'],
  842. "feedrate_z": self.app.defaults['geometry_feedrate_z'],
  843. "feedrate_rapid": self.app.defaults['geometry_feedrate_rapid'],
  844. "dwell": self.app.defaults['geometry_dwell'],
  845. "dwelltime": self.app.defaults['geometry_dwelltime'],
  846. "multidepth": self.app.defaults['geometry_multidepth'],
  847. "ppname_g": self.app.defaults['geometry_ppname_g'],
  848. "depthperpass": self.app.defaults['geometry_depthperpass'],
  849. "extracut": self.app.defaults['geometry_extracut'],
  850. "extracut_length": self.app.defaults['geometry_extracut_length'],
  851. "toolchange": self.app.defaults['geometry_toolchange'],
  852. "toolchangez": self.app.defaults['geometry_toolchangez'],
  853. "endz": self.app.defaults['geometry_endz'],
  854. "spindlespeed": self.app.defaults['geometry_spindlespeed'],
  855. "toolchangexy": self.app.defaults['geometry_toolchangexy'],
  856. "startz": self.app.defaults['geometry_startz']
  857. })
  858. geo_obj.tools = {}
  859. geo_obj.tools['1'] = {}
  860. geo_obj.tools.update({
  861. '1': {
  862. 'tooldia': float(self.options["isotooldia"]),
  863. 'offset': 'Path',
  864. 'offset_value': 0.0,
  865. 'type': _('Rough'),
  866. 'tool_type': tool_type,
  867. 'data': default_data,
  868. 'solid_geometry': geo_obj.solid_geometry
  869. }
  870. })
  871. # detect if solid_geometry is empty and this require list flattening which is "heavy"
  872. # or just looking in the lists (they are one level depth) and if any is not empty
  873. # proceed with object creation, if there are empty and the number of them is the length
  874. # of the list then we have an empty solid_geometry which should raise a Custom Exception
  875. empty_cnt = 0
  876. if not isinstance(geo_obj.solid_geometry, list):
  877. geo_obj.solid_geometry = [geo_obj.solid_geometry]
  878. for g in geo_obj.solid_geometry:
  879. if g:
  880. break
  881. else:
  882. empty_cnt += 1
  883. if empty_cnt == len(geo_obj.solid_geometry):
  884. raise ValidationError("Empty Geometry", None)
  885. else:
  886. app_obj.inform.emit('[success] %s: %s' %
  887. (_("Isolation geometry created"), geo_obj.options["name"]))
  888. geo_obj.multigeo = False
  889. # ############################################################
  890. # ########## AREA SUBTRACTION ################################
  891. # ############################################################
  892. if self.ui.except_cb.get_value():
  893. self.app.proc_container.update_view_text(' %s' % _("Subtracting Geo"))
  894. geo_obj.solid_geometry = self.area_subtraction(geo_obj.solid_geometry)
  895. # TODO: Do something if this is None. Offer changing name?
  896. self.app.new_object("geometry", iso_name, iso_init, plot=plot)
  897. def generate_envelope(self, offset, invert, geometry=None, env_iso_type=2, follow=None, nr_passes=0):
  898. # isolation_geometry produces an envelope that is going on the left of the geometry
  899. # (the copper features). To leave the least amount of burrs on the features
  900. # the tool needs to travel on the right side of the features (this is called conventional milling)
  901. # the first pass is the one cutting all of the features, so it needs to be reversed
  902. # the other passes overlap preceding ones and cut the left over copper. It is better for them
  903. # to cut on the right side of the left over copper i.e on the left side of the features.
  904. if follow:
  905. geom = self.isolation_geometry(offset, geometry=geometry, follow=follow)
  906. else:
  907. try:
  908. geom = self.isolation_geometry(offset, geometry=geometry, iso_type=env_iso_type, passes=nr_passes)
  909. except Exception as e:
  910. log.debug('GerberObject.isolate().generate_envelope() --> %s' % str(e))
  911. return 'fail'
  912. if invert:
  913. try:
  914. pl = []
  915. for p in geom:
  916. if p is not None:
  917. if isinstance(p, Polygon):
  918. pl.append(Polygon(p.exterior.coords[::-1], p.interiors))
  919. elif isinstance(p, LinearRing):
  920. pl.append(Polygon(p.coords[::-1]))
  921. geom = MultiPolygon(pl)
  922. except TypeError:
  923. if isinstance(geom, Polygon) and geom is not None:
  924. geom = Polygon(geom.exterior.coords[::-1], geom.interiors)
  925. elif isinstance(geom, LinearRing) and geom is not None:
  926. geom = Polygon(geom.coords[::-1])
  927. else:
  928. log.debug("GerberObject.isolate().generate_envelope() Error --> Unexpected Geometry %s" %
  929. type(geom))
  930. except Exception as e:
  931. log.debug("GerberObject.isolate().generate_envelope() Error --> %s" % str(e))
  932. return 'fail'
  933. return geom
  934. def area_subtraction(self, geo, subtractor_geo=None):
  935. """
  936. Subtracts the subtractor_geo (if present else self.solid_geometry) from the geo
  937. :param geo: target geometry from which to subtract
  938. :param subtractor_geo: geometry that acts as subtractor
  939. :return:
  940. """
  941. new_geometry = []
  942. target_geo = geo
  943. if subtractor_geo:
  944. sub_union = cascaded_union(subtractor_geo)
  945. else:
  946. name = self.ui.obj_combo.currentText()
  947. subtractor_obj = self.app.collection.get_by_name(name)
  948. sub_union = cascaded_union(subtractor_obj.solid_geometry)
  949. try:
  950. for geo_elem in target_geo:
  951. if isinstance(geo_elem, Polygon):
  952. for ring in self.poly2rings(geo_elem):
  953. new_geo = ring.difference(sub_union)
  954. if new_geo and not new_geo.is_empty:
  955. new_geometry.append(new_geo)
  956. elif isinstance(geo_elem, MultiPolygon):
  957. for poly in geo_elem:
  958. for ring in self.poly2rings(poly):
  959. new_geo = ring.difference(sub_union)
  960. if new_geo and not new_geo.is_empty:
  961. new_geometry.append(new_geo)
  962. elif isinstance(geo_elem, LineString):
  963. new_geo = geo_elem.difference(sub_union)
  964. if new_geo:
  965. if not new_geo.is_empty:
  966. new_geometry.append(new_geo)
  967. elif isinstance(geo_elem, MultiLineString):
  968. for line_elem in geo_elem:
  969. new_geo = line_elem.difference(sub_union)
  970. if new_geo and not new_geo.is_empty:
  971. new_geometry.append(new_geo)
  972. except TypeError:
  973. if isinstance(target_geo, Polygon):
  974. for ring in self.poly2rings(target_geo):
  975. new_geo = ring.difference(sub_union)
  976. if new_geo:
  977. if not new_geo.is_empty:
  978. new_geometry.append(new_geo)
  979. elif isinstance(target_geo, LineString):
  980. new_geo = target_geo.difference(sub_union)
  981. if new_geo and not new_geo.is_empty:
  982. new_geometry.append(new_geo)
  983. elif isinstance(target_geo, MultiLineString):
  984. for line_elem in target_geo:
  985. new_geo = line_elem.difference(sub_union)
  986. if new_geo and not new_geo.is_empty:
  987. new_geometry.append(new_geo)
  988. return new_geometry
  989. def on_plot_cb_click(self, *args):
  990. if self.muted_ui:
  991. return
  992. self.read_form_item('plot')
  993. self.plot()
  994. def on_solid_cb_click(self, *args):
  995. if self.muted_ui:
  996. return
  997. self.read_form_item('solid')
  998. self.plot()
  999. def on_multicolored_cb_click(self, *args):
  1000. if self.muted_ui:
  1001. return
  1002. self.read_form_item('multicolored')
  1003. self.plot()
  1004. def on_follow_cb_click(self):
  1005. if self.muted_ui:
  1006. return
  1007. self.plot()
  1008. def on_aperture_table_visibility_change(self):
  1009. if self.ui.aperture_table_visibility_cb.isChecked():
  1010. # add the shapes storage for marking apertures
  1011. if self.build_aperture_storage is False:
  1012. self.build_aperture_storage = True
  1013. if self.app.is_legacy is False:
  1014. for ap_code in self.apertures:
  1015. self.mark_shapes[ap_code] = self.app.plotcanvas.new_shape_collection(layers=1)
  1016. else:
  1017. for ap_code in self.apertures:
  1018. self.mark_shapes[ap_code] = ShapeCollectionLegacy(obj=self, app=self.app,
  1019. name=self.options['name'] + str(ap_code))
  1020. self.ui.apertures_table.setVisible(True)
  1021. for ap in self.mark_shapes:
  1022. self.mark_shapes[ap].enabled = True
  1023. self.ui.mark_all_cb.setVisible(True)
  1024. self.ui.mark_all_cb.setChecked(False)
  1025. self.build_ui()
  1026. else:
  1027. self.ui.apertures_table.setVisible(False)
  1028. self.ui.mark_all_cb.setVisible(False)
  1029. # on hide disable all mark plots
  1030. try:
  1031. for row in range(self.ui.apertures_table.rowCount()):
  1032. self.ui.apertures_table.cellWidget(row, 5).set_value(False)
  1033. self.clear_plot_apertures()
  1034. # for ap in list(self.mark_shapes.keys()):
  1035. # # self.mark_shapes[ap].enabled = False
  1036. # del self.mark_shapes[ap]
  1037. except Exception as e:
  1038. log.debug(" GerberObject.on_aperture_visibility_changed() --> %s" % str(e))
  1039. def convert_units(self, units):
  1040. """
  1041. Converts the units of the object by scaling dimensions in all geometry
  1042. and options.
  1043. :param units: Units to which to convert the object: "IN" or "MM".
  1044. :type units: str
  1045. :return: None
  1046. :rtype: None
  1047. """
  1048. # units conversion to get a conversion should be done only once even if we found multiple
  1049. # units declaration inside a Gerber file (it can happen to find also the obsolete declaration)
  1050. if self.conversion_done is True:
  1051. log.debug("Gerber units conversion cancelled. Already done.")
  1052. return
  1053. log.debug("FlatCAMObj.GerberObject.convert_units()")
  1054. factor = Gerber.convert_units(self, units)
  1055. # self.options['isotooldia'] = float(self.options['isotooldia']) * factor
  1056. # self.options['bboxmargin'] = float(self.options['bboxmargin']) * factor
  1057. def plot(self, kind=None, **kwargs):
  1058. """
  1059. :param kind: Not used, for compatibility with the plot method for other objects
  1060. :param kwargs: Color and face_color, visible
  1061. :return:
  1062. """
  1063. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot()")
  1064. # Does all the required setup and returns False
  1065. # if the 'ptint' option is set to False.
  1066. if not FlatCAMObj.plot(self):
  1067. return
  1068. if 'color' in kwargs:
  1069. color = kwargs['color']
  1070. else:
  1071. color = self.outline_color
  1072. if 'face_color' in kwargs:
  1073. face_color = kwargs['face_color']
  1074. else:
  1075. face_color = self.fill_color
  1076. if 'visible' not in kwargs:
  1077. visible = self.options['plot']
  1078. else:
  1079. visible = kwargs['visible']
  1080. # if the Follow Geometry checkbox is checked then plot only the follow geometry
  1081. if self.ui.follow_cb.get_value():
  1082. geometry = self.follow_geometry
  1083. else:
  1084. geometry = self.solid_geometry
  1085. # Make sure geometry is iterable.
  1086. try:
  1087. __ = iter(geometry)
  1088. except TypeError:
  1089. geometry = [geometry]
  1090. if self.app.is_legacy is False:
  1091. def random_color():
  1092. r_color = np.random.rand(4)
  1093. r_color[3] = 1
  1094. return r_color
  1095. else:
  1096. def random_color():
  1097. while True:
  1098. r_color = np.random.rand(4)
  1099. r_color[3] = 1
  1100. new_color = '#'
  1101. for idx in range(len(r_color)):
  1102. new_color += '%x' % int(r_color[idx] * 255)
  1103. # do it until a valid color is generated
  1104. # a valid color has the # symbol, another 6 chars for the color and the last 2 chars for alpha
  1105. # for a total of 9 chars
  1106. if len(new_color) == 9:
  1107. break
  1108. return new_color
  1109. try:
  1110. if self.options["solid"]:
  1111. for g in geometry:
  1112. if type(g) == Polygon or type(g) == LineString:
  1113. self.add_shape(shape=g, color=color,
  1114. face_color=random_color() if self.options['multicolored']
  1115. else face_color, visible=visible)
  1116. elif type(g) == Point:
  1117. pass
  1118. else:
  1119. try:
  1120. for el in g:
  1121. self.add_shape(shape=el, color=color,
  1122. face_color=random_color() if self.options['multicolored']
  1123. else face_color, visible=visible)
  1124. except TypeError:
  1125. self.add_shape(shape=g, color=color,
  1126. face_color=random_color() if self.options['multicolored']
  1127. else face_color, visible=visible)
  1128. else:
  1129. for g in geometry:
  1130. if type(g) == Polygon or type(g) == LineString:
  1131. self.add_shape(shape=g, color=random_color() if self.options['multicolored'] else 'black',
  1132. visible=visible)
  1133. elif type(g) == Point:
  1134. pass
  1135. else:
  1136. for el in g:
  1137. self.add_shape(shape=el, color=random_color() if self.options['multicolored'] else 'black',
  1138. visible=visible)
  1139. self.shapes.redraw(
  1140. # update_colors=(self.fill_color, self.outline_color),
  1141. # indexes=self.app.plotcanvas.shape_collection.data.keys()
  1142. )
  1143. except (ObjectDeleted, AttributeError):
  1144. self.shapes.clear(update=True)
  1145. except Exception as e:
  1146. log.debug("GerberObject.plot() --> %s" % str(e))
  1147. # experimental plot() when the solid_geometry is stored in the self.apertures
  1148. def plot_aperture(self, run_thread=True, **kwargs):
  1149. """
  1150. :param run_thread: if True run the aperture plot as a thread in a worker
  1151. :param kwargs: color and face_color
  1152. :return:
  1153. """
  1154. log.debug(str(inspect.stack()[1][3]) + " --> GerberObject.plot_aperture()")
  1155. # Does all the required setup and returns False
  1156. # if the 'ptint' option is set to False.
  1157. # if not FlatCAMObj.plot(self):
  1158. # return
  1159. # for marking apertures, line color and fill color are the same
  1160. if 'color' in kwargs:
  1161. color = kwargs['color']
  1162. else:
  1163. color = self.app.defaults['gerber_plot_fill']
  1164. if 'marked_aperture' not in kwargs:
  1165. return
  1166. else:
  1167. aperture_to_plot_mark = kwargs['marked_aperture']
  1168. if aperture_to_plot_mark is None:
  1169. return
  1170. if 'visible' not in kwargs:
  1171. visibility = True
  1172. else:
  1173. visibility = kwargs['visible']
  1174. with self.app.proc_container.new(_("Plotting Apertures")):
  1175. def job_thread(app_obj):
  1176. try:
  1177. if aperture_to_plot_mark in self.apertures:
  1178. for elem in self.apertures[aperture_to_plot_mark]['geometry']:
  1179. if 'solid' in elem:
  1180. geo = elem['solid']
  1181. if type(geo) == Polygon or type(geo) == LineString:
  1182. self.add_mark_shape(apid=aperture_to_plot_mark, shape=geo, color=color,
  1183. face_color=color, visible=visibility)
  1184. else:
  1185. for el in geo:
  1186. self.add_mark_shape(apid=aperture_to_plot_mark, shape=el, color=color,
  1187. face_color=color, visible=visibility)
  1188. self.mark_shapes[aperture_to_plot_mark].redraw()
  1189. except (ObjectDeleted, AttributeError):
  1190. self.clear_plot_apertures()
  1191. except Exception as e:
  1192. log.debug("GerberObject.plot_aperture() --> %s" % str(e))
  1193. if run_thread:
  1194. self.app.worker_task.emit({'fcn': job_thread, 'params': [self]})
  1195. else:
  1196. job_thread(self)
  1197. def clear_plot_apertures(self, aperture='all'):
  1198. """
  1199. :param aperture: string; aperture for which to clear the mark shapes
  1200. :return:
  1201. """
  1202. if self.mark_shapes:
  1203. if aperture == 'all':
  1204. for apid in list(self.apertures.keys()):
  1205. try:
  1206. if self.app.is_legacy is True:
  1207. self.mark_shapes[apid].clear(update=False)
  1208. else:
  1209. self.mark_shapes[apid].clear(update=True)
  1210. except Exception as e:
  1211. log.debug("GerberObject.clear_plot_apertures() 'all' --> %s" % str(e))
  1212. else:
  1213. try:
  1214. if self.app.is_legacy is True:
  1215. self.mark_shapes[aperture].clear(update=False)
  1216. else:
  1217. self.mark_shapes[aperture].clear(update=True)
  1218. except Exception as e:
  1219. log.debug("GerberObject.clear_plot_apertures() 'aperture' --> %s" % str(e))
  1220. def clear_mark_all(self):
  1221. self.ui.mark_all_cb.set_value(False)
  1222. self.marked_rows[:] = []
  1223. def on_mark_cb_click_table(self):
  1224. """
  1225. Will mark aperture geometries on canvas or delete the markings depending on the checkbox state
  1226. :return:
  1227. """
  1228. self.ui_disconnect()
  1229. try:
  1230. cw = self.sender()
  1231. assert isinstance(cw, FCCheckBox),\
  1232. "Expected a cellWidget but got %s" % type(cw)
  1233. cw_index = self.ui.apertures_table.indexAt(cw.pos())
  1234. cw_row = cw_index.row()
  1235. except AttributeError:
  1236. cw_row = 0
  1237. except TypeError:
  1238. return
  1239. self.marked_rows[:] = []
  1240. try:
  1241. aperture = self.ui.apertures_table.item(cw_row, 1).text()
  1242. except AttributeError:
  1243. return
  1244. if self.ui.apertures_table.cellWidget(cw_row, 5).isChecked():
  1245. self.marked_rows.append(True)
  1246. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  1247. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  1248. marked_aperture=aperture, visible=True, run_thread=True)
  1249. # self.mark_shapes[aperture].redraw()
  1250. else:
  1251. self.marked_rows.append(False)
  1252. self.clear_plot_apertures(aperture=aperture)
  1253. # make sure that the Mark All is disabled if one of the row mark's are disabled and
  1254. # if all the row mark's are enabled also enable the Mark All checkbox
  1255. cb_cnt = 0
  1256. total_row = self.ui.apertures_table.rowCount()
  1257. for row in range(total_row):
  1258. if self.ui.apertures_table.cellWidget(row, 5).isChecked():
  1259. cb_cnt += 1
  1260. else:
  1261. cb_cnt -= 1
  1262. if cb_cnt < total_row:
  1263. self.ui.mark_all_cb.setChecked(False)
  1264. else:
  1265. self.ui.mark_all_cb.setChecked(True)
  1266. self.ui_connect()
  1267. def on_mark_all_click(self):
  1268. self.ui_disconnect()
  1269. mark_all = self.ui.mark_all_cb.isChecked()
  1270. for row in range(self.ui.apertures_table.rowCount()):
  1271. # update the mark_rows list
  1272. if mark_all:
  1273. self.marked_rows.append(True)
  1274. else:
  1275. self.marked_rows[:] = []
  1276. mark_cb = self.ui.apertures_table.cellWidget(row, 5)
  1277. mark_cb.setChecked(mark_all)
  1278. if mark_all:
  1279. for aperture in self.apertures:
  1280. # self.plot_aperture(color='#2d4606bf', marked_aperture=aperture, visible=True)
  1281. self.plot_aperture(color=self.app.defaults['global_sel_draw_color'] + 'AF',
  1282. marked_aperture=aperture, visible=True)
  1283. # HACK: enable/disable the grid for a better look
  1284. self.app.ui.grid_snap_btn.trigger()
  1285. self.app.ui.grid_snap_btn.trigger()
  1286. else:
  1287. self.clear_plot_apertures()
  1288. self.marked_rows[:] = []
  1289. self.ui_connect()
  1290. def export_gerber(self, whole, fract, g_zeros='L', factor=1):
  1291. """
  1292. Creates a Gerber file content to be exported to a file.
  1293. :param whole: how many digits in the whole part of coordinates
  1294. :param fract: how many decimals in coordinates
  1295. :param g_zeros: type of the zero suppression used: LZ or TZ; string
  1296. :param factor: factor to be applied onto the Gerber coordinates
  1297. :return: Gerber_code
  1298. """
  1299. log.debug("GerberObject.export_gerber() --> Generating the Gerber code from the selected Gerber file")
  1300. def tz_format(x, y, fac):
  1301. x_c = x * fac
  1302. y_c = y * fac
  1303. x_form = "{:.{dec}f}".format(x_c, dec=fract)
  1304. y_form = "{:.{dec}f}".format(y_c, dec=fract)
  1305. # extract whole part and decimal part
  1306. x_form = x_form.partition('.')
  1307. y_form = y_form.partition('.')
  1308. # left padd the 'whole' part with zeros
  1309. x_whole = x_form[0].rjust(whole, '0')
  1310. y_whole = y_form[0].rjust(whole, '0')
  1311. # restore the coordinate padded in the left with 0 and added the decimal part
  1312. # without the decinal dot
  1313. x_form = x_whole + x_form[2]
  1314. y_form = y_whole + y_form[2]
  1315. return x_form, y_form
  1316. def lz_format(x, y, fac):
  1317. x_c = x * fac
  1318. y_c = y * fac
  1319. x_form = "{:.{dec}f}".format(x_c, dec=fract).replace('.', '')
  1320. y_form = "{:.{dec}f}".format(y_c, dec=fract).replace('.', '')
  1321. # pad with rear zeros
  1322. x_form.ljust(length, '0')
  1323. y_form.ljust(length, '0')
  1324. return x_form, y_form
  1325. # Gerber code is stored here
  1326. gerber_code = ''
  1327. # apertures processing
  1328. try:
  1329. length = whole + fract
  1330. if '0' in self.apertures:
  1331. if 'geometry' in self.apertures['0']:
  1332. for geo_elem in self.apertures['0']['geometry']:
  1333. if 'solid' in geo_elem:
  1334. geo = geo_elem['solid']
  1335. if not geo.is_empty:
  1336. gerber_code += 'G36*\n'
  1337. geo_coords = list(geo.exterior.coords)
  1338. # first command is a move with pen-up D02 at the beginning of the geo
  1339. if g_zeros == 'T':
  1340. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1341. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1342. yform=y_formatted)
  1343. else:
  1344. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1345. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1346. yform=y_formatted)
  1347. for coord in geo_coords[1:]:
  1348. if g_zeros == 'T':
  1349. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1350. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1351. yform=y_formatted)
  1352. else:
  1353. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1354. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1355. yform=y_formatted)
  1356. gerber_code += 'D02*\n'
  1357. gerber_code += 'G37*\n'
  1358. clear_list = list(geo.interiors)
  1359. if clear_list:
  1360. gerber_code += '%LPC*%\n'
  1361. for clear_geo in clear_list:
  1362. gerber_code += 'G36*\n'
  1363. geo_coords = list(clear_geo.coords)
  1364. # first command is a move with pen-up D02 at the beginning of the geo
  1365. if g_zeros == 'T':
  1366. x_formatted, y_formatted = tz_format(
  1367. geo_coords[0][0], geo_coords[0][1], factor)
  1368. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1369. yform=y_formatted)
  1370. else:
  1371. x_formatted, y_formatted = lz_format(
  1372. geo_coords[0][0], geo_coords[0][1], factor)
  1373. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1374. yform=y_formatted)
  1375. prev_coord = geo_coords[0]
  1376. for coord in geo_coords[1:]:
  1377. if coord != prev_coord:
  1378. if g_zeros == 'T':
  1379. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1380. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1381. yform=y_formatted)
  1382. else:
  1383. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1384. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1385. yform=y_formatted)
  1386. prev_coord = coord
  1387. gerber_code += 'D02*\n'
  1388. gerber_code += 'G37*\n'
  1389. gerber_code += '%LPD*%\n'
  1390. if 'clear' in geo_elem:
  1391. geo = geo_elem['clear']
  1392. if not geo.is_empty:
  1393. gerber_code += '%LPC*%\n'
  1394. gerber_code += 'G36*\n'
  1395. geo_coords = list(geo.exterior.coords)
  1396. # first command is a move with pen-up D02 at the beginning of the geo
  1397. if g_zeros == 'T':
  1398. x_formatted, y_formatted = tz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1399. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1400. yform=y_formatted)
  1401. else:
  1402. x_formatted, y_formatted = lz_format(geo_coords[0][0], geo_coords[0][1], factor)
  1403. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1404. yform=y_formatted)
  1405. prev_coord = geo_coords[0]
  1406. for coord in geo_coords[1:]:
  1407. if coord != prev_coord:
  1408. if g_zeros == 'T':
  1409. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1410. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1411. yform=y_formatted)
  1412. else:
  1413. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1414. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1415. yform=y_formatted)
  1416. prev_coord = coord
  1417. gerber_code += 'D02*\n'
  1418. gerber_code += 'G37*\n'
  1419. gerber_code += '%LPD*%\n'
  1420. except Exception as e:
  1421. log.debug("FlatCAMObj.GerberObject.export_gerber() '0' aperture --> %s" % str(e))
  1422. for apid in self.apertures:
  1423. if apid == '0':
  1424. continue
  1425. else:
  1426. gerber_code += 'D%s*\n' % str(apid)
  1427. if 'geometry' in self.apertures[apid]:
  1428. for geo_elem in self.apertures[apid]['geometry']:
  1429. try:
  1430. if 'follow' in geo_elem:
  1431. geo = geo_elem['follow']
  1432. if not geo.is_empty:
  1433. if isinstance(geo, Point):
  1434. if g_zeros == 'T':
  1435. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  1436. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1437. yform=y_formatted)
  1438. else:
  1439. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  1440. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1441. yform=y_formatted)
  1442. else:
  1443. geo_coords = list(geo.coords)
  1444. # first command is a move with pen-up D02 at the beginning of the geo
  1445. if g_zeros == 'T':
  1446. x_formatted, y_formatted = tz_format(
  1447. geo_coords[0][0], geo_coords[0][1], factor)
  1448. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1449. yform=y_formatted)
  1450. else:
  1451. x_formatted, y_formatted = lz_format(
  1452. geo_coords[0][0], geo_coords[0][1], factor)
  1453. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1454. yform=y_formatted)
  1455. prev_coord = geo_coords[0]
  1456. for coord in geo_coords[1:]:
  1457. if coord != prev_coord:
  1458. if g_zeros == 'T':
  1459. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1460. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1461. yform=y_formatted)
  1462. else:
  1463. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1464. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1465. yform=y_formatted)
  1466. prev_coord = coord
  1467. # gerber_code += "D02*\n"
  1468. except Exception as e:
  1469. log.debug("FlatCAMObj.GerberObject.export_gerber() 'follow' --> %s" % str(e))
  1470. try:
  1471. if 'clear' in geo_elem:
  1472. gerber_code += '%LPC*%\n'
  1473. geo = geo_elem['clear']
  1474. if not geo.is_empty:
  1475. if isinstance(geo, Point):
  1476. if g_zeros == 'T':
  1477. x_formatted, y_formatted = tz_format(geo.x, geo.y, factor)
  1478. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1479. yform=y_formatted)
  1480. else:
  1481. x_formatted, y_formatted = lz_format(geo.x, geo.y, factor)
  1482. gerber_code += "X{xform}Y{yform}D03*\n".format(xform=x_formatted,
  1483. yform=y_formatted)
  1484. elif isinstance(geo, Polygon):
  1485. geo_coords = list(geo.exterior.coords)
  1486. # first command is a move with pen-up D02 at the beginning of the geo
  1487. if g_zeros == 'T':
  1488. x_formatted, y_formatted = tz_format(
  1489. geo_coords[0][0], geo_coords[0][1], factor)
  1490. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1491. yform=y_formatted)
  1492. else:
  1493. x_formatted, y_formatted = lz_format(
  1494. geo_coords[0][0], geo_coords[0][1], factor)
  1495. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1496. yform=y_formatted)
  1497. prev_coord = geo_coords[0]
  1498. for coord in geo_coords[1:]:
  1499. if coord != prev_coord:
  1500. if g_zeros == 'T':
  1501. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1502. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1503. yform=y_formatted)
  1504. else:
  1505. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1506. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1507. yform=y_formatted)
  1508. prev_coord = coord
  1509. for geo_int in geo.interiors:
  1510. geo_coords = list(geo_int.coords)
  1511. # first command is a move with pen-up D02 at the beginning of the geo
  1512. if g_zeros == 'T':
  1513. x_formatted, y_formatted = tz_format(
  1514. geo_coords[0][0], geo_coords[0][1], factor)
  1515. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1516. yform=y_formatted)
  1517. else:
  1518. x_formatted, y_formatted = lz_format(
  1519. geo_coords[0][0], geo_coords[0][1], factor)
  1520. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1521. yform=y_formatted)
  1522. prev_coord = geo_coords[0]
  1523. for coord in geo_coords[1:]:
  1524. if coord != prev_coord:
  1525. if g_zeros == 'T':
  1526. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1527. gerber_code += "X{xform}Y{yform}D01*\n".format(
  1528. xform=x_formatted,
  1529. yform=y_formatted)
  1530. else:
  1531. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1532. gerber_code += "X{xform}Y{yform}D01*\n".format(
  1533. xform=x_formatted,
  1534. yform=y_formatted)
  1535. prev_coord = coord
  1536. else:
  1537. geo_coords = list(geo.coords)
  1538. # first command is a move with pen-up D02 at the beginning of the geo
  1539. if g_zeros == 'T':
  1540. x_formatted, y_formatted = tz_format(
  1541. geo_coords[0][0], geo_coords[0][1], factor)
  1542. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1543. yform=y_formatted)
  1544. else:
  1545. x_formatted, y_formatted = lz_format(
  1546. geo_coords[0][0], geo_coords[0][1], factor)
  1547. gerber_code += "X{xform}Y{yform}D02*\n".format(xform=x_formatted,
  1548. yform=y_formatted)
  1549. prev_coord = geo_coords[0]
  1550. for coord in geo_coords[1:]:
  1551. if coord != prev_coord:
  1552. if g_zeros == 'T':
  1553. x_formatted, y_formatted = tz_format(coord[0], coord[1], factor)
  1554. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1555. yform=y_formatted)
  1556. else:
  1557. x_formatted, y_formatted = lz_format(coord[0], coord[1], factor)
  1558. gerber_code += "X{xform}Y{yform}D01*\n".format(xform=x_formatted,
  1559. yform=y_formatted)
  1560. prev_coord = coord
  1561. # gerber_code += "D02*\n"
  1562. gerber_code += '%LPD*%\n'
  1563. except Exception as e:
  1564. log.debug("FlatCAMObj.GerberObject.export_gerber() 'clear' --> %s" % str(e))
  1565. if not self.apertures:
  1566. log.debug("FlatCAMObj.GerberObject.export_gerber() --> Gerber Object is empty: no apertures.")
  1567. return 'fail'
  1568. return gerber_code
  1569. def mirror(self, axis, point):
  1570. Gerber.mirror(self, axis=axis, point=point)
  1571. self.replotApertures.emit()
  1572. def offset(self, vect):
  1573. Gerber.offset(self, vect=vect)
  1574. self.replotApertures.emit()
  1575. def rotate(self, angle, point):
  1576. Gerber.rotate(self, angle=angle, point=point)
  1577. self.replotApertures.emit()
  1578. def scale(self, xfactor, yfactor=None, point=None):
  1579. Gerber.scale(self, xfactor=xfactor, yfactor=yfactor, point=point)
  1580. self.replotApertures.emit()
  1581. def skew(self, angle_x, angle_y, point):
  1582. Gerber.skew(self, angle_x=angle_x, angle_y=angle_y, point=point)
  1583. self.replotApertures.emit()
  1584. def buffer(self, distance, join, factor=None):
  1585. Gerber.buffer(self, distance=distance, join=join, factor=factor)
  1586. self.replotApertures.emit()
  1587. def serialize(self):
  1588. return {
  1589. "options": self.options,
  1590. "kind": self.kind
  1591. }