2019-10-22 14:32:33 +00:00
|
|
|
# ##########################################################
|
|
|
|
# FlatCAM: 2D Post-processing for Manufacturing #
|
|
|
|
# File Author: Marius Adrian Stanciu (c) #
|
2019-10-24 14:42:21 +00:00
|
|
|
# Date: 10/24/2019 #
|
2019-10-22 14:32:33 +00:00
|
|
|
# MIT Licence #
|
|
|
|
# ##########################################################
|
|
|
|
|
|
|
|
from PyQt5 import QtWidgets, QtCore
|
|
|
|
|
|
|
|
from FlatCAMTool import FlatCAMTool
|
2019-10-24 14:42:21 +00:00
|
|
|
from flatcamGUI.GUIElements import RadioSet, FCTextArea, FCSpinner, FCDoubleSpinner
|
|
|
|
from flatcamParsers.ParseSVG import *
|
2019-10-22 14:32:33 +00:00
|
|
|
|
|
|
|
from shapely.geometry import Point
|
|
|
|
from shapely.geometry.base import *
|
2019-10-24 14:42:21 +00:00
|
|
|
from shapely.ops import unary_union
|
|
|
|
from shapely.affinity import translate
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
from io import StringIO, BytesIO
|
|
|
|
from collections import Iterable
|
2019-10-22 14:32:33 +00:00
|
|
|
import logging
|
2019-10-23 14:58:06 +00:00
|
|
|
import qrcode
|
|
|
|
import qrcode.image.svg
|
2019-10-22 14:32:33 +00:00
|
|
|
from lxml import etree as ET
|
2019-10-24 14:42:21 +00:00
|
|
|
from copy import copy, deepcopy
|
2019-10-24 15:52:53 +00:00
|
|
|
from numpy import Inf
|
2019-10-22 14:32:33 +00:00
|
|
|
|
|
|
|
import gettext
|
|
|
|
import FlatCAMTranslation as fcTranslate
|
|
|
|
import builtins
|
|
|
|
|
|
|
|
fcTranslate.apply_language('strings')
|
|
|
|
if '_' not in builtins.__dict__:
|
|
|
|
_ = gettext.gettext
|
|
|
|
|
|
|
|
log = logging.getLogger('base')
|
|
|
|
|
|
|
|
|
|
|
|
class QRCode(FlatCAMTool):
|
|
|
|
|
|
|
|
toolName = _("QRCode Tool")
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
FlatCAMTool.__init__(self, app)
|
|
|
|
|
|
|
|
self.app = app
|
|
|
|
self.canvas = self.app.plotcanvas
|
|
|
|
|
|
|
|
self.decimals = 4
|
|
|
|
self.units = ''
|
|
|
|
|
|
|
|
# ## Title
|
|
|
|
title_label = QtWidgets.QLabel("%s" % self.toolName)
|
|
|
|
title_label.setStyleSheet("""
|
|
|
|
QLabel
|
|
|
|
{
|
|
|
|
font-size: 16px;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
self.layout.addWidget(title_label)
|
2019-10-23 19:53:38 +00:00
|
|
|
self.layout.addWidget(QtWidgets.QLabel(''))
|
|
|
|
|
|
|
|
# ## Grid Layout
|
|
|
|
i_grid_lay = QtWidgets.QGridLayout()
|
|
|
|
self.layout.addLayout(i_grid_lay)
|
|
|
|
i_grid_lay.setColumnStretch(0, 0)
|
|
|
|
i_grid_lay.setColumnStretch(1, 1)
|
|
|
|
|
|
|
|
self.grb_object_combo = QtWidgets.QComboBox()
|
|
|
|
self.grb_object_combo.setModel(self.app.collection)
|
|
|
|
self.grb_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
|
|
|
|
self.grb_object_combo.setCurrentIndex(1)
|
|
|
|
|
|
|
|
self.grbobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
|
|
|
|
self.grbobj_label.setToolTip(
|
|
|
|
_("Gerber Object to which the QRCode will be added.")
|
|
|
|
)
|
|
|
|
|
|
|
|
i_grid_lay.addWidget(self.grbobj_label, 0, 0)
|
|
|
|
i_grid_lay.addWidget(self.grb_object_combo, 0, 1, 1, 2)
|
|
|
|
i_grid_lay.addWidget(QtWidgets.QLabel(''), 1, 0)
|
|
|
|
|
|
|
|
# ## Grid Layout
|
|
|
|
grid_lay = QtWidgets.QGridLayout()
|
|
|
|
self.layout.addLayout(grid_lay)
|
|
|
|
grid_lay.setColumnStretch(0, 0)
|
|
|
|
grid_lay.setColumnStretch(1, 1)
|
|
|
|
|
|
|
|
self.qrcode_label = QtWidgets.QLabel('<b>%s</b>' % _('QRCode Parameters'))
|
|
|
|
self.qrcode_label.setToolTip(
|
|
|
|
_("Contain the expected calibration points and the\n"
|
|
|
|
"ones measured.")
|
|
|
|
)
|
|
|
|
grid_lay.addWidget(self.qrcode_label, 0, 0, 1, 2)
|
|
|
|
|
|
|
|
# VERSION #
|
|
|
|
self.version_label = QtWidgets.QLabel('%s:' % _("Version"))
|
|
|
|
self.version_label.setToolTip(
|
|
|
|
_("QRCode version can have values from 1 (21x21 boxes)\n"
|
|
|
|
"to 40 (177x177 boxes).")
|
|
|
|
)
|
|
|
|
self.version_entry = FCSpinner()
|
|
|
|
self.version_entry.set_range(1, 40)
|
|
|
|
self.version_entry.setWrapping(True)
|
|
|
|
|
|
|
|
grid_lay.addWidget(self.version_label, 1, 0)
|
|
|
|
grid_lay.addWidget(self.version_entry, 1, 1)
|
|
|
|
|
|
|
|
# ERROR CORRECTION #
|
|
|
|
self.error_label = QtWidgets.QLabel('%s:' % _("Error correction"))
|
|
|
|
self.error_label.setToolTip(
|
|
|
|
_("Parameter that controls the error correction used for the QR Code.\n"
|
|
|
|
"L = maximum 7% errors can be corrected\n"
|
|
|
|
"M = maximum 15% errors can be corrected\n"
|
|
|
|
"Q = maximum 25% errors can be corrected\n"
|
|
|
|
"H = maximum 30% errors can be corrected.")
|
|
|
|
)
|
|
|
|
self.error_radio = RadioSet([{'label': 'L', 'value': 'L'},
|
|
|
|
{'label': 'M', 'value': 'M'},
|
|
|
|
{'label': 'Q', 'value': 'Q'},
|
|
|
|
{'label': 'H', 'value': 'H'}])
|
|
|
|
self.error_radio.setToolTip(
|
|
|
|
_("Parameter that controls the error correction used for the QR Code.\n"
|
|
|
|
"L = maximum 7% errors can be corrected\n"
|
|
|
|
"M = maximum 15% errors can be corrected\n"
|
|
|
|
"Q = maximum 25% errors can be corrected\n"
|
|
|
|
"H = maximum 30% errors can be corrected.")
|
|
|
|
)
|
|
|
|
grid_lay.addWidget(self.error_label, 2, 0)
|
|
|
|
grid_lay.addWidget(self.error_radio, 2, 1)
|
|
|
|
|
|
|
|
# BOX SIZE #
|
|
|
|
self.bsize_label = QtWidgets.QLabel('%s:' % _("Box Size"))
|
|
|
|
self.bsize_label.setToolTip(
|
|
|
|
_("Box size control the overall size of the QRcode\n"
|
|
|
|
"by adjusting the size of each box in the code.")
|
|
|
|
)
|
|
|
|
self.bsize_entry = FCSpinner()
|
|
|
|
self.bsize_entry.set_range(1, 9999)
|
|
|
|
self.bsize_entry.setWrapping(True)
|
|
|
|
|
|
|
|
grid_lay.addWidget(self.bsize_label, 3, 0)
|
|
|
|
grid_lay.addWidget(self.bsize_entry, 3, 1)
|
|
|
|
|
|
|
|
# BORDER SIZE #
|
|
|
|
self.border_size_label = QtWidgets.QLabel('%s:' % _("Border Size"))
|
|
|
|
self.border_size_label.setToolTip(
|
|
|
|
_("Size of the QRCode border. How many boxes thick is the border.\n"
|
|
|
|
"Default value is 4.")
|
|
|
|
)
|
|
|
|
self.border_size_entry = FCSpinner()
|
|
|
|
self.border_size_entry.set_range(1, 9999)
|
|
|
|
self.border_size_entry.setWrapping(True)
|
|
|
|
self.border_size_entry.set_value(4)
|
|
|
|
|
|
|
|
grid_lay.addWidget(self.border_size_label, 4, 0)
|
|
|
|
grid_lay.addWidget(self.border_size_entry, 4, 1)
|
|
|
|
|
|
|
|
# Text box
|
|
|
|
self.text_label = QtWidgets.QLabel('%s:' % _("QRCode Data"))
|
|
|
|
self.text_label.setToolTip(
|
|
|
|
_("QRCode Data. Alphanumeric text to be encoded in the QRCode.")
|
|
|
|
)
|
|
|
|
self.text_data = FCTextArea()
|
2019-10-24 14:42:21 +00:00
|
|
|
self.text_data.setPlaceholderText(
|
|
|
|
_("Add here the text to be included in the QRData...")
|
|
|
|
)
|
2019-10-23 19:53:38 +00:00
|
|
|
grid_lay.addWidget(self.text_label, 5, 0)
|
|
|
|
grid_lay.addWidget(self.text_data, 6, 0, 1, 2)
|
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
# POLARITY CHOICE #
|
|
|
|
self.pol_label = QtWidgets.QLabel('%s:' % _("Polarity"))
|
|
|
|
self.pol_label.setToolTip(
|
|
|
|
_("Parameter that controls the error correction used for the QR Code.\n"
|
|
|
|
"L = maximum 7% errors can be corrected\n"
|
|
|
|
"M = maximum 15% errors can be corrected\n"
|
|
|
|
"Q = maximum 25% errors can be corrected\n"
|
|
|
|
"H = maximum 30% errors can be corrected.")
|
|
|
|
)
|
|
|
|
self.pol_radio = RadioSet([{'label': _('Negative'), 'value': 'neg'},
|
|
|
|
{'label': _('Positive'), 'value': 'pos'}])
|
|
|
|
self.error_radio.setToolTip(
|
|
|
|
_("Choose the type of QRCode to be created.\n"
|
|
|
|
"If added on a Silkscreen Gerber you may add\n"
|
|
|
|
"it as positive. If you add it to a Copper\n"
|
|
|
|
"Gerber then perhaps you can add it as positive.")
|
|
|
|
)
|
|
|
|
grid_lay.addWidget(self.pol_label, 7, 0)
|
|
|
|
grid_lay.addWidget(self.pol_radio, 7, 1)
|
|
|
|
|
|
|
|
# BOUNDARY THICKNESS #
|
|
|
|
self.boundary_label = QtWidgets.QLabel('%s:' % _("Boundary Thickness"))
|
|
|
|
self.boundary_label.setToolTip(
|
|
|
|
_("The width of the clearance around the QRCode.")
|
|
|
|
)
|
|
|
|
self.boundary_entry = FCDoubleSpinner()
|
|
|
|
self.boundary_entry.set_range(0.0, 9999.9999)
|
|
|
|
self.boundary_entry.set_precision(self.decimals)
|
|
|
|
self.boundary_entry.setWrapping(True)
|
|
|
|
|
|
|
|
grid_lay.addWidget(self.boundary_label, 8, 0)
|
|
|
|
grid_lay.addWidget(self.boundary_entry, 8, 1)
|
|
|
|
|
2019-10-23 19:53:38 +00:00
|
|
|
# ## Create QRCode
|
|
|
|
self.qrcode_button = QtWidgets.QPushButton(_("Create QRCode"))
|
|
|
|
self.qrcode_button.setToolTip(
|
|
|
|
_("Create the QRCode object.")
|
|
|
|
)
|
2019-10-24 14:42:21 +00:00
|
|
|
grid_lay.addWidget(self.qrcode_button, 9, 0, 1, 2)
|
|
|
|
grid_lay.addWidget(QtWidgets.QLabel(''), 10, 0)
|
2019-10-23 19:53:38 +00:00
|
|
|
|
|
|
|
self.layout.addStretch()
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
self.grb_object = None
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
self.origin = (0, 0)
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
self.mm = None
|
|
|
|
self.mr = None
|
|
|
|
self.kr = None
|
|
|
|
|
|
|
|
self.shapes = self.app.move_tool.sel_shapes
|
|
|
|
self.qrcode_geometry = list()
|
2019-10-24 15:52:53 +00:00
|
|
|
self.qrcode_utility_geometry = list()
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-22 14:32:33 +00:00
|
|
|
def run(self, toggle=True):
|
|
|
|
self.app.report_usage("QRCode()")
|
|
|
|
|
|
|
|
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:
|
|
|
|
# if tab is populated with the tool but it does not have the focus, focus on it
|
|
|
|
if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
|
|
|
|
# focus on Tool Tab
|
|
|
|
self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
|
|
|
|
else:
|
|
|
|
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.app.ui.notebook.setTabText(2, _("QRCode Tool"))
|
|
|
|
|
|
|
|
def install(self, icon=None, separator=None, **kwargs):
|
|
|
|
FlatCAMTool.install(self, icon, separator, shortcut='ALT+Q', **kwargs)
|
|
|
|
|
|
|
|
def set_tool_ui(self):
|
2019-10-23 19:53:38 +00:00
|
|
|
self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value()
|
|
|
|
self.version_entry.set_value(1)
|
|
|
|
self.error_radio.set_value('M')
|
|
|
|
self.bsize_entry.set_value(3)
|
|
|
|
self.border_size_entry.set_value(4)
|
2019-10-24 14:42:21 +00:00
|
|
|
self.pol_radio.set_value('pos')
|
2019-10-23 19:53:38 +00:00
|
|
|
|
|
|
|
# Signals #
|
|
|
|
self.qrcode_button.clicked.connect(self.execute)
|
2019-10-22 14:32:33 +00:00
|
|
|
|
|
|
|
def execute(self):
|
2019-10-23 19:53:38 +00:00
|
|
|
|
|
|
|
text_data = self.text_data.get_value()
|
|
|
|
if text_data == '':
|
|
|
|
self.app.inform.emit('[ERROR_NOTCL] %s' % _("Cancelled. There is no QRCode Data in the text box."))
|
|
|
|
return 'fail'
|
|
|
|
|
|
|
|
error_code = {
|
|
|
|
'L': qrcode.constants.ERROR_CORRECT_L,
|
|
|
|
'M': qrcode.constants.ERROR_CORRECT_M,
|
|
|
|
'Q': qrcode.constants.ERROR_CORRECT_Q,
|
|
|
|
'H': qrcode.constants.ERROR_CORRECT_H
|
|
|
|
}[self.error_radio.get_value()]
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-23 19:53:38 +00:00
|
|
|
qr = qrcode.QRCode(
|
|
|
|
version=self.version_entry.get_value(),
|
|
|
|
error_correction=error_code,
|
|
|
|
box_size=self.bsize_entry.get_value(),
|
|
|
|
border=self.border_size_entry.get_value(),
|
|
|
|
image_factory=qrcode.image.svg.SvgFragmentImage
|
|
|
|
)
|
|
|
|
qr.add_data(text_data)
|
|
|
|
qr.make()
|
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
svg_file = BytesIO()
|
2019-10-23 19:53:38 +00:00
|
|
|
img = qr.make_image()
|
|
|
|
img.save(svg_file)
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-23 19:53:38 +00:00
|
|
|
svg_text = StringIO(svg_file.getvalue().decode('UTF-8'))
|
2019-10-24 14:42:21 +00:00
|
|
|
svg_geometry = self.convert_svg_to_geo(svg_text, units=self.units)
|
2019-10-24 15:52:53 +00:00
|
|
|
self.qrcode_geometry = deepcopy(svg_geometry)
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
svg_geometry = unary_union(svg_geometry).buffer(0.0000001).buffer(-0.0000001)
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
self.qrcode_utility_geometry = svg_geometry
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
# if we have an object selected then we can safely activate the mouse events
|
|
|
|
self.mm = self.app.plotcanvas.graph_event_connect('mouse_move', self.on_mouse_move)
|
|
|
|
self.mr = self.app.plotcanvas.graph_event_connect('mouse_release', self.on_mouse_release)
|
|
|
|
self.kr = self.app.plotcanvas.graph_event_connect('key_release', self.on_key_release)
|
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
selection_index = self.grb_object_combo.currentIndex()
|
|
|
|
model_index = self.app.collection.index(selection_index, 0, self.grb_object_combo.rootModelIndex())
|
|
|
|
try:
|
|
|
|
self.grb_object = model_index.internalPointer().obj
|
|
|
|
except Exception as e:
|
|
|
|
self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
|
|
|
|
return 'fail'
|
|
|
|
|
|
|
|
self.app.inform.emit(_("Click on the Destination point ..."))
|
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
def make(self, pos):
|
|
|
|
if self.app.is_legacy is False:
|
|
|
|
self.app.plotcanvas.graph_event_disconnect('mouse_move', self.on_mouse_move)
|
|
|
|
self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_release)
|
|
|
|
self.app.plotcanvas.graph_event_disconnect('key_release', self.on_key_release)
|
|
|
|
else:
|
|
|
|
self.app.plotcanvas.graph_event_disconnect(self.mm)
|
|
|
|
self.app.plotcanvas.graph_event_disconnect(self.mr)
|
|
|
|
self.app.plotcanvas.graph_event_disconnect(self.kr)
|
2019-10-23 19:53:38 +00:00
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
self.clicked_move = 0
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
# delete the utility geometry
|
|
|
|
self.delete_utility_geo()
|
2019-10-23 19:53:38 +00:00
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
# add the svg geometry to the selected Gerber object solid_geometry and in obj.apertures, apid = 0
|
2019-10-24 15:52:53 +00:00
|
|
|
if not isinstance(self.grb_object.solid_geometry, Iterable):
|
|
|
|
self.grb_object.solid_geometry = list(self.grb_object.solid_geometry)
|
|
|
|
|
|
|
|
# I use the utility geometry (self.qrcode_utility_geometry) because it is already buffered
|
|
|
|
self.grb_object.solid_geometry += self.qrcode_utility_geometry
|
|
|
|
|
|
|
|
box_size = float(self.bsize_entry.get_value()) / 10.0
|
|
|
|
|
|
|
|
sort_apid = list()
|
|
|
|
new_apid = '10'
|
|
|
|
if self.grb_object.apertures:
|
|
|
|
for k, v in list(self.grb_object.apertures.items()):
|
|
|
|
sort_apid.append(int(k))
|
|
|
|
sorted_apertures = sorted(sort_apid)
|
|
|
|
new_apid = str(10 + len(sorted_apertures))
|
|
|
|
|
|
|
|
if new_apid not in self.grb_object.apertures:
|
|
|
|
self.grb_object.apertures[new_apid] = dict()
|
|
|
|
self.grb_object.apertures[new_apid]['geometry'] = list()
|
|
|
|
self.grb_object.apertures[new_apid]['type'] = 'R'
|
|
|
|
self.grb_object.apertures[new_apid]['height'] = box_size
|
|
|
|
self.grb_object.apertures[new_apid]['width'] = box_size
|
|
|
|
self.grb_object.apertures[new_apid]['size'] = math.sqrt(box_size ** 2 + box_size ** 2)
|
|
|
|
|
|
|
|
if self.grb_object.options['xmin'] == Inf or self.grb_object.options['xmin'] == -Inf:
|
|
|
|
try:
|
|
|
|
a, b, c, d = self.grb_object.bounds()
|
|
|
|
self.grb_object.options['xmin'] = a
|
|
|
|
self.grb_object.options['ymin'] = b
|
|
|
|
self.grb_object.options['xmax'] = c
|
|
|
|
self.grb_object.options['ymax'] = d
|
|
|
|
except Exception as e:
|
|
|
|
log.debug("QRCode.make() bounds error --> %s" % str(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
for geo in self.qrcode_geometry:
|
|
|
|
geo_elem = dict()
|
|
|
|
geo_elem['solid'] = geo
|
|
|
|
geo_elem['follow'] = geo.centroid
|
|
|
|
self.grb_object.apertures[new_apid]['geometry'].append(deepcopy(geo_elem))
|
|
|
|
except TypeError:
|
|
|
|
geo_elem = dict()
|
|
|
|
geo_elem['solid'] = self.qrcode_geometry
|
|
|
|
self.grb_object.apertures['0']['geometry'].append(deepcopy(geo_elem))
|
|
|
|
print(self.grb_object.apertures)
|
|
|
|
self.replot()
|
2019-10-23 19:53:38 +00:00
|
|
|
|
2019-10-24 14:42:21 +00:00
|
|
|
def draw_utility_geo(self, pos):
|
|
|
|
|
|
|
|
face = '#0000FF' + str(hex(int(0.2 * 255)))[2:]
|
|
|
|
outline = '#0000FFAF'
|
|
|
|
|
|
|
|
offset_geo = list()
|
|
|
|
|
|
|
|
try:
|
2019-10-24 15:52:53 +00:00
|
|
|
for poly in self.qrcode_utility_geometry:
|
2019-10-24 14:42:21 +00:00
|
|
|
offset_geo.append(translate(poly.exterior, xoff=pos[0], yoff=pos[1]))
|
|
|
|
for geo_int in poly.interiors:
|
|
|
|
offset_geo.append(translate(geo_int, xoff=pos[0], yoff=pos[1]))
|
|
|
|
except TypeError:
|
2019-10-24 15:52:53 +00:00
|
|
|
offset_geo.append(translate(self.qrcode_utility_geometry.exterior, xoff=pos[0], yoff=pos[1]))
|
|
|
|
for geo_int in self.qrcode_utility_geometry.interiors:
|
2019-10-24 14:42:21 +00:00
|
|
|
offset_geo.append(translate(geo_int, xoff=pos[0], yoff=pos[1]))
|
|
|
|
|
|
|
|
for shape in offset_geo:
|
|
|
|
self.shapes.add(shape, color=outline, face_color=face, update=True, layer=0, tolerance=None)
|
|
|
|
|
|
|
|
if self.app.is_legacy is True:
|
|
|
|
self.shapes.redraw()
|
|
|
|
|
|
|
|
def delete_utility_geo(self):
|
|
|
|
self.shapes.clear()
|
|
|
|
self.shapes.redraw()
|
2019-10-23 19:53:38 +00:00
|
|
|
|
|
|
|
def on_mouse_move(self, event):
|
2019-10-24 14:42:21 +00:00
|
|
|
if self.app.is_legacy is False:
|
|
|
|
event_pos = event.pos
|
|
|
|
else:
|
|
|
|
event_pos = (event.xdata, event.ydata)
|
|
|
|
|
|
|
|
try:
|
|
|
|
x = float(event_pos[0])
|
|
|
|
y = float(event_pos[1])
|
|
|
|
except TypeError:
|
|
|
|
return
|
|
|
|
|
|
|
|
pos_canvas = self.app.plotcanvas.translate_coords((x, y))
|
|
|
|
|
|
|
|
# if GRID is active we need to get the snapped positions
|
|
|
|
if self.app.grid_status() == True:
|
|
|
|
pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
|
else:
|
|
|
|
pos = pos_canvas
|
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
dx = pos[0] - self.origin[0]
|
|
|
|
dy = pos[1] - self.origin[1]
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
# delete the utility geometry
|
|
|
|
self.delete_utility_geo()
|
|
|
|
self.draw_utility_geo((dx, dy))
|
2019-10-22 14:32:33 +00:00
|
|
|
|
2019-10-23 19:53:38 +00:00
|
|
|
def on_mouse_release(self, event):
|
2019-10-24 14:42:21 +00:00
|
|
|
# mouse click will be accepted only if the left button is clicked
|
|
|
|
# this is necessary because right mouse click and middle mouse click
|
|
|
|
# are used for panning on the canvas
|
|
|
|
|
|
|
|
if self.app.is_legacy is False:
|
|
|
|
event_pos = event.pos
|
|
|
|
else:
|
|
|
|
event_pos = (event.xdata, event.ydata)
|
|
|
|
|
|
|
|
if event.button == 1:
|
|
|
|
pos_canvas = self.app.plotcanvas.translate_coords(event_pos)
|
2019-10-24 15:52:53 +00:00
|
|
|
self.delete_utility_geo()
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
# if GRID is active we need to get the snapped positions
|
|
|
|
if self.app.grid_status() == True:
|
|
|
|
pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
|
else:
|
|
|
|
pos = pos_canvas
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
dx = pos[0] - self.origin[0]
|
|
|
|
dy = pos[1] - self.origin[1]
|
2019-10-24 14:42:21 +00:00
|
|
|
|
2019-10-24 15:52:53 +00:00
|
|
|
self.make(pos=(dx, dy))
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
def on_key_release(self, event):
|
2019-10-23 19:53:38 +00:00
|
|
|
pass
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
def convert_svg_to_geo(self, filename, object_type=None, flip=True, units='MM'):
|
|
|
|
"""
|
|
|
|
Convert shapes from an SVG file into a geometry list.
|
|
|
|
|
|
|
|
:param filename: A String Stream file.
|
|
|
|
:param object_type: parameter passed further along. What kind the object will receive the SVG geometry
|
|
|
|
:param flip: Flip the vertically.
|
|
|
|
:type flip: bool
|
|
|
|
:param units: FlatCAM units
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Parse into list of shapely objects
|
|
|
|
svg_tree = ET.parse(filename)
|
|
|
|
svg_root = svg_tree.getroot()
|
|
|
|
|
|
|
|
# Change origin to bottom left
|
|
|
|
# h = float(svg_root.get('height'))
|
|
|
|
# w = float(svg_root.get('width'))
|
|
|
|
h = svgparselength(svg_root.get('height'))[0] # TODO: No units support yet
|
|
|
|
geos = getsvggeo(svg_root, object_type)
|
|
|
|
|
|
|
|
if flip:
|
|
|
|
geos = [translate(scale(g, 1.0, -1.0, origin=(0, 0)), yoff=h) for g in geos]
|
|
|
|
|
|
|
|
# flatten the svg geometry for the case when the QRCode SVG is added into a Gerber object
|
|
|
|
solid_geometry = list(self.flatten_list(geos))
|
|
|
|
|
|
|
|
geos_text = getsvgtext(svg_root, object_type, units=units)
|
|
|
|
if geos_text is not None:
|
|
|
|
geos_text_f = []
|
|
|
|
if flip:
|
|
|
|
# Change origin to bottom left
|
|
|
|
for i in geos_text:
|
|
|
|
_, minimy, _, maximy = i.bounds
|
|
|
|
h2 = (maximy - minimy) * 0.5
|
|
|
|
geos_text_f.append(translate(scale(i, 1.0, -1.0, origin=(0, 0)), yoff=(h + h2)))
|
|
|
|
if geos_text_f:
|
|
|
|
solid_geometry += geos_text_f
|
|
|
|
return solid_geometry
|
|
|
|
|
|
|
|
def flatten_list(self, list):
|
|
|
|
for item in list:
|
|
|
|
if isinstance(item, Iterable) and not isinstance(item, (str, bytes)):
|
|
|
|
yield from self.flatten_list(item)
|
|
|
|
else:
|
|
|
|
yield item
|
|
|
|
|
|
|
|
def replot(self):
|
2019-10-24 15:52:53 +00:00
|
|
|
obj = self.grb_object
|
2019-10-24 14:42:21 +00:00
|
|
|
|
|
|
|
def worker_task():
|
|
|
|
with self.app.proc_container.new('%s...' % _("Plotting")):
|
|
|
|
obj.plot()
|
|
|
|
|
|
|
|
self.app.worker_task.emit({'fcn': worker_task, 'params': []})
|