2019-04-24 22:13:37 +00:00
|
|
|
############################################################
|
|
|
|
# FlatCAM: 2D Post-processing for Manufacturing #
|
|
|
|
# http://flatcam.org #
|
|
|
|
# File Author: Marius Adrian Stanciu (c) #
|
|
|
|
# Date: 4/24/2019 #
|
|
|
|
# MIT Licence #
|
|
|
|
############################################################
|
|
|
|
|
|
|
|
|
|
|
|
from FlatCAMTool import FlatCAMTool
|
|
|
|
# from copy import copy, deepcopy
|
|
|
|
from ObjectCollection import *
|
2019-04-24 22:23:36 +00:00
|
|
|
import time
|
2019-04-24 22:13:37 +00:00
|
|
|
|
|
|
|
import gettext
|
|
|
|
import FlatCAMTranslation as fcTranslate
|
|
|
|
from shapely.geometry import base
|
|
|
|
import builtins
|
|
|
|
|
|
|
|
fcTranslate.apply_language('strings')
|
|
|
|
if '_' not in builtins.__dict__:
|
|
|
|
_ = gettext.gettext
|
|
|
|
|
|
|
|
|
|
|
|
class ToolSilk(FlatCAMTool):
|
|
|
|
|
|
|
|
toolName = _("Silkscreen Tool")
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
FlatCAMTool.__init__(self, app)
|
|
|
|
|
|
|
|
self.tools_frame = QtWidgets.QFrame()
|
|
|
|
self.tools_frame.setContentsMargins(0, 0, 0, 0)
|
|
|
|
self.layout.addWidget(self.tools_frame)
|
|
|
|
self.tools_box = QtWidgets.QVBoxLayout()
|
|
|
|
self.tools_box.setContentsMargins(0, 0, 0, 0)
|
|
|
|
self.tools_frame.setLayout(self.tools_box)
|
|
|
|
|
|
|
|
# Title
|
|
|
|
title_label = QtWidgets.QLabel("%s" % self.toolName)
|
|
|
|
title_label.setStyleSheet("""
|
|
|
|
QLabel
|
|
|
|
{
|
|
|
|
font-size: 16px;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
self.tools_box.addWidget(title_label)
|
|
|
|
|
|
|
|
# Form Layout
|
|
|
|
form_layout = QtWidgets.QFormLayout()
|
|
|
|
self.tools_box.addLayout(form_layout)
|
|
|
|
|
|
|
|
# Object Silkscreen
|
|
|
|
self.silk_object_combo = QtWidgets.QComboBox()
|
|
|
|
self.silk_object_combo.setModel(self.app.collection)
|
|
|
|
self.silk_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
|
|
|
|
self.silk_object_combo.setCurrentIndex(1)
|
|
|
|
|
|
|
|
self.silk_object_label = QtWidgets.QLabel("Silk Gerber:")
|
|
|
|
self.silk_object_label.setToolTip(
|
|
|
|
_("Silkscreen Gerber object to be adjusted\n"
|
|
|
|
"so it does not intersects the soldermask.")
|
|
|
|
)
|
|
|
|
e_lab_0 = QtWidgets.QLabel('')
|
|
|
|
|
|
|
|
form_layout.addRow(self.silk_object_label, self.silk_object_combo)
|
|
|
|
|
|
|
|
# Object Soldermask
|
|
|
|
self.sm_object_combo = QtWidgets.QComboBox()
|
|
|
|
self.sm_object_combo.setModel(self.app.collection)
|
|
|
|
self.sm_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
|
|
|
|
self.sm_object_combo.setCurrentIndex(1)
|
|
|
|
|
|
|
|
self.sm_object_label = QtWidgets.QLabel("SM Gerber:")
|
|
|
|
self.sm_object_label.setToolTip(
|
|
|
|
_("Soldermask Gerber object that will adjust\n"
|
|
|
|
"the silkscreen so it does not overlap.")
|
|
|
|
)
|
|
|
|
e_lab_1 = QtWidgets.QLabel('')
|
|
|
|
|
|
|
|
form_layout.addRow(self.sm_object_label, self.sm_object_combo)
|
|
|
|
form_layout.addRow(e_lab_1)
|
|
|
|
|
|
|
|
self.intersect_btn = FCButton(_('Remove overlap'))
|
|
|
|
self.intersect_btn.setToolTip(
|
|
|
|
_("Remove the silkscreen geometry\n"
|
|
|
|
"that overlaps over the soldermask.")
|
|
|
|
)
|
|
|
|
self.tools_box.addWidget(self.intersect_btn)
|
|
|
|
|
|
|
|
self.tools_box.addStretch()
|
|
|
|
|
|
|
|
# QTimer for periodic check
|
|
|
|
self.check_thread = QtCore.QTimer()
|
|
|
|
# Every time an intersection job is started we add a promise; every time an intersection job is finished
|
|
|
|
# we remove a promise.
|
|
|
|
# When empty we start the layer rendering
|
|
|
|
self.promises = []
|
|
|
|
|
|
|
|
self.new_apertures = {}
|
|
|
|
self.new_solid_geometry = []
|
|
|
|
|
|
|
|
self.solder_union = None
|
|
|
|
|
|
|
|
self.intersect_btn.clicked.connect(self.on_intersection_click)
|
|
|
|
|
|
|
|
def install(self, icon=None, separator=None, **kwargs):
|
|
|
|
FlatCAMTool.install(self, icon, separator, shortcut='ALT+N', **kwargs)
|
|
|
|
|
|
|
|
def run(self, toggle=True):
|
|
|
|
self.app.report_usage("ToolNonCopperClear()")
|
|
|
|
|
|
|
|
if toggle:
|
|
|
|
# if the splitter is hidden, display it, else hide it but only if the current widget is the same
|
|
|
|
if self.app.ui.splitter.sizes()[0] == 0:
|
|
|
|
self.app.ui.splitter.setSizes([1, 1])
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
|
|
|
|
self.app.ui.splitter.setSizes([0, 1])
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if self.app.ui.splitter.sizes()[0] == 0:
|
|
|
|
self.app.ui.splitter.setSizes([1, 1])
|
|
|
|
|
|
|
|
FlatCAMTool.run(self)
|
|
|
|
self.set_tool_ui()
|
|
|
|
|
|
|
|
self.new_apertures.clear()
|
|
|
|
self.new_solid_geometry = []
|
|
|
|
|
|
|
|
self.app.ui.notebook.setTabText(2, _("Silk Tool"))
|
|
|
|
|
|
|
|
def set_tool_ui(self):
|
|
|
|
self.tools_frame.show()
|
|
|
|
|
|
|
|
def on_intersection_click(self):
|
|
|
|
self.silk_obj_name = self.silk_object_combo.currentText()
|
|
|
|
# Get source object.
|
|
|
|
try:
|
|
|
|
self.silk_obj = self.app.collection.get_by_name(self.silk_obj_name)
|
|
|
|
except:
|
|
|
|
self.app.inform.emit(_("[ERROR_NOTCL] Could not retrieve object: %s") % self.obj_name)
|
|
|
|
return "Could not retrieve object: %s" % self.silk_obj_name
|
|
|
|
|
|
|
|
self.sm_obj_name = self.silk_object_combo.currentText()
|
|
|
|
# Get source object.
|
|
|
|
try:
|
|
|
|
self.sm_obj = self.app.collection.get_by_name(self.sm_obj_name)
|
|
|
|
except:
|
|
|
|
self.app.inform.emit(_("[ERROR_NOTCL] Could not retrieve object: %s") % self.obj_name)
|
|
|
|
return "Could not retrieve object: %s" % self.sm_obj_name
|
|
|
|
|
|
|
|
# crate the new_apertures dict structure
|
|
|
|
for apid in self.silk_obj.apertures:
|
|
|
|
self.new_apertures[apid] = {}
|
|
|
|
self.new_apertures[apid]['type'] = 'C'
|
|
|
|
self.new_apertures[apid]['size'] = self.silk_obj.apertures[apid]['size']
|
|
|
|
self.new_apertures[apid]['solid_geometry'] = []
|
|
|
|
|
|
|
|
geo_union_list = []
|
|
|
|
for apid1 in self.sm_obj.apertures:
|
|
|
|
geo_union_list += self.sm_obj.apertures[apid1]['solid_geometry']
|
|
|
|
self.solder_union = cascaded_union(geo_union_list)
|
|
|
|
|
|
|
|
# start the QTimer with 1 second period check
|
|
|
|
self.periodic_check(1000)
|
|
|
|
for apid in self.silk_obj.apertures:
|
|
|
|
ap_size = self.silk_obj.apertures[apid]['size']
|
|
|
|
geo_list = self.silk_obj.apertures[apid]['solid_geometry']
|
|
|
|
self.app.worker_task.emit({'fcn': self.aperture_intersection,
|
2019-04-25 13:26:31 +00:00
|
|
|
'params': [apid, geo_list]})
|
2019-04-24 22:13:37 +00:00
|
|
|
|
2019-04-25 13:26:31 +00:00
|
|
|
def aperture_intersection(self, apid, geo_list):
|
|
|
|
self.promises.append(apid)
|
2019-04-24 22:13:37 +00:00
|
|
|
new_solid_geometry = []
|
2019-04-25 13:26:31 +00:00
|
|
|
for g in geo_list:
|
|
|
|
print(g.exterior.wkt)
|
2019-04-24 22:13:37 +00:00
|
|
|
|
2019-04-25 13:26:31 +00:00
|
|
|
with self.app.proc_container.new(_("Parsing aperture %s geometry ..." % str(apid))):
|
2019-04-24 22:13:37 +00:00
|
|
|
for geo_silk in geo_list:
|
2019-04-25 13:26:31 +00:00
|
|
|
if geo_silk.exterior.intersects(self.solder_union.exterior):
|
|
|
|
print("yes")
|
|
|
|
new_geo = geo_silk.exterior.difference(self.solder_union.exterior)
|
|
|
|
# if the resulting geometry is not empty add it to the new_apertures solid_geometry
|
|
|
|
try:
|
|
|
|
for g in new_geo:
|
|
|
|
new_solid_geometry.append(g)
|
|
|
|
except TypeError:
|
|
|
|
new_solid_geometry.append(new_geo)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("no")
|
|
|
|
new_solid_geometry.append(geo_silk)
|
|
|
|
|
|
|
|
if new_solid_geometry:
|
|
|
|
try:
|
|
|
|
while not self.new_apertures[apid]['solid_geometry']:
|
|
|
|
self.new_apertures[apid]['solid_geometry'] = new_solid_geometry
|
|
|
|
time.sleep(0.1)
|
|
|
|
except:
|
|
|
|
pass
|
2019-04-24 22:13:37 +00:00
|
|
|
|
|
|
|
try:
|
2019-04-24 22:23:36 +00:00
|
|
|
while True:
|
|
|
|
# removal from list is done in a multithreaded way therefore not always the removal can be done
|
|
|
|
# so we keep trying until it's done
|
2019-04-25 13:47:17 +00:00
|
|
|
if apid in self.promises:
|
|
|
|
self.promises.remove(apid)
|
|
|
|
else:
|
|
|
|
break
|
2019-04-24 22:23:36 +00:00
|
|
|
time.sleep(0.1)
|
2019-04-24 22:13:37 +00:00
|
|
|
except:
|
2019-04-25 13:47:17 +00:00
|
|
|
log.debug("Promise fulfilled: %s" % str(apid))
|
2019-04-24 22:13:37 +00:00
|
|
|
pass
|
|
|
|
|
2019-04-25 13:26:31 +00:00
|
|
|
def periodic_check(self, check_period, reset=False):
|
2019-04-24 22:13:37 +00:00
|
|
|
"""
|
|
|
|
This function starts an QTimer and it will periodically check if intersections are done
|
|
|
|
|
|
|
|
:param check_period: time at which to check periodically
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
log.debug("ToolSilk --> Periodic Check started.")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.check_thread.stop()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-04-25 13:26:31 +00:00
|
|
|
if reset:
|
|
|
|
self.check_thread.setInterval(check_period)
|
|
|
|
try:
|
|
|
|
self.check_thread.timeout.disconnect(self.periodic_check_handler)
|
|
|
|
except:
|
|
|
|
pass
|
2019-04-24 22:13:37 +00:00
|
|
|
|
|
|
|
self.check_thread.timeout.connect(self.periodic_check_handler)
|
|
|
|
self.check_thread.start(QtCore.QThread.HighPriority)
|
|
|
|
|
|
|
|
def periodic_check_handler(self):
|
|
|
|
"""
|
|
|
|
If the intersections workers finished then start creating the solid_geometry
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
# log.debug("checking parsing --> %s" % str(self.parsing_promises))
|
|
|
|
|
|
|
|
outname = self.silk_object_combo.currentText() + '_cleaned'
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not self.promises:
|
|
|
|
self.check_thread.stop()
|
|
|
|
# intersection jobs finished, start the creation of solid_geometry
|
|
|
|
self.app.worker_task.emit({'fcn': self.new_silkscreen_object,
|
|
|
|
'params': [outname]})
|
|
|
|
|
|
|
|
log.debug("ToolPDF --> Periodic check finished.")
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
def new_silkscreen_object(self, outname):
|
|
|
|
|
|
|
|
def obj_init(grb_obj, app_obj):
|
|
|
|
|
|
|
|
grb_obj.apertures = deepcopy(self.new_apertures)
|
|
|
|
|
|
|
|
poly_buff = []
|
|
|
|
for ap in self.new_apertures:
|
|
|
|
for k in self.new_apertures[ap]:
|
|
|
|
if k == 'solid_geometry':
|
|
|
|
poly_buff += self.new_apertures[ap][k]
|
|
|
|
|
|
|
|
poly_buff = unary_union(poly_buff)
|
|
|
|
try:
|
|
|
|
poly_buff = poly_buff.buffer(0.0000001)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
poly_buff = poly_buff.buffer(-0.0000001)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
grb_obj.solid_geometry = deepcopy(poly_buff)
|
|
|
|
# self.new_apertures.clear()
|
|
|
|
|
|
|
|
with self.app.proc_container.new(_("Generating cleaned SS object ...")):
|
|
|
|
ret = self.app.new_object('gerber', outname, obj_init, autoselected=False)
|
|
|
|
if ret == 'fail':
|
|
|
|
self.app.inform.emit(_('[ERROR_NOTCL] Generating SilkScreen file failed.'))
|
|
|
|
return
|
|
|
|
# Register recent file
|
|
|
|
self.app.file_opened.emit('gerber', outname)
|
|
|
|
# GUI feedback
|
|
|
|
self.app.inform.emit(_("[success] Created: %s") % outname)
|
|
|
|
|
|
|
|
def reset_fields(self):
|
|
|
|
self.silk_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
|
|
|
|
self.sm_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
|
|
|
|
|
|
|
|
|