FlatCAMGerber.py 76 KB

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