FlatCAMGerber.py 76 KB

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