FlatCAMGerber.py 89 KB

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