ToolMeasurement.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. ############################################################
  8. from FlatCAMTool import FlatCAMTool
  9. from FlatCAMObj import *
  10. from flatcamGUI.VisPyVisuals import *
  11. from math import sqrt
  12. import gettext
  13. import FlatCAMTranslation as fcTranslate
  14. fcTranslate.apply_language('strings')
  15. import builtins
  16. if '_' not in builtins.__dict__:
  17. _ = gettext.gettext
  18. class Measurement(FlatCAMTool):
  19. toolName = _("Measurement")
  20. def __init__(self, app):
  21. FlatCAMTool.__init__(self, app)
  22. self.app = app
  23. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  24. ## Title
  25. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font><br>" % self.toolName)
  26. self.layout.addWidget(title_label)
  27. ## Form Layout
  28. form_layout = QtWidgets.QFormLayout()
  29. self.layout.addLayout(form_layout)
  30. self.units_label = QtWidgets.QLabel(_("Units:"))
  31. self.units_label.setToolTip(_("Those are the units in which the distance is measured."))
  32. self.units_value = QtWidgets.QLabel("%s" % str({'mm': "METRIC (mm)", 'in': "INCH (in)"}[self.units]))
  33. self.units_value.setDisabled(True)
  34. self.start_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Start'), _('Coords')))
  35. self.start_label.setToolTip(_("This is measuring Start point coordinates."))
  36. self.stop_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Stop'), _('Coords')))
  37. self.stop_label.setToolTip(_("This is the measuring Stop point coordinates."))
  38. self.distance_x_label = QtWidgets.QLabel("Dx:")
  39. self.distance_x_label.setToolTip(_("This is the distance measured over the X axis."))
  40. self.distance_y_label = QtWidgets.QLabel("Dy:")
  41. self.distance_y_label.setToolTip(_("This is the distance measured over the Y axis."))
  42. self.total_distance_label = QtWidgets.QLabel("<b>%s:</b>" % _('DISTANCE'))
  43. self.total_distance_label.setToolTip(_("This is the point to point Euclidian distance."))
  44. self.start_entry = FCEntry()
  45. self.start_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  46. self.start_entry.setToolTip(_("This is measuring Start point coordinates."))
  47. self.stop_entry = FCEntry()
  48. self.stop_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  49. self.stop_entry.setToolTip(_("This is the measuring Stop point coordinates."))
  50. self.distance_x_entry = FCEntry()
  51. self.distance_x_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  52. self.distance_x_entry.setToolTip(_("This is the distance measured over the X axis."))
  53. self.distance_y_entry = FCEntry()
  54. self.distance_y_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  55. self.distance_y_entry.setToolTip(_("This is the distance measured over the Y axis."))
  56. self.total_distance_entry = FCEntry()
  57. self.total_distance_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  58. self.total_distance_entry.setToolTip(_("This is the point to point Euclidian distance."))
  59. self.measure_btn = QtWidgets.QPushButton(_("Measure"))
  60. # self.measure_btn.setFixedWidth(70)
  61. self.layout.addWidget(self.measure_btn)
  62. form_layout.addRow(self.units_label, self.units_value)
  63. form_layout.addRow(self.start_label, self.start_entry)
  64. form_layout.addRow(self.stop_label, self.stop_entry)
  65. form_layout.addRow(self.distance_x_label, self.distance_x_entry)
  66. form_layout.addRow(self.distance_y_label, self.distance_y_entry)
  67. form_layout.addRow(self.total_distance_label, self.total_distance_entry)
  68. # initial view of the layout
  69. self.start_entry.set_value('(0, 0)')
  70. self.stop_entry.set_value('(0, 0)')
  71. self.distance_x_entry.set_value('0')
  72. self.distance_y_entry.set_value('0')
  73. self.total_distance_entry.set_value('0')
  74. self.layout.addStretch()
  75. self.clicked_meas = 0
  76. self.point1 = None
  77. self.point2 = None
  78. # the default state is disabled for the Move command
  79. # self.setVisible(False)
  80. self.active = 0
  81. # VisPy visuals
  82. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
  83. self.measure_btn.clicked.connect(lambda: self.on_measure(activate=True))
  84. def run(self, toggle=False):
  85. self.app.report_usage("ToolMeasurement()")
  86. if self.app.tool_tab_locked is True:
  87. return
  88. self.app.ui.notebook.setTabText(2, _("Meas. Tool"))
  89. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  90. if self.app.ui.splitter.sizes()[0] == 0:
  91. self.app.ui.splitter.setSizes([1, 1])
  92. self.on_measure(activate=True)
  93. def install(self, icon=None, separator=None, **kwargs):
  94. FlatCAMTool.install(self, icon, separator, shortcut='CTRL+M', **kwargs)
  95. def set_tool_ui(self):
  96. # Remove anything else in the GUI
  97. self.app.ui.tool_scroll_area.takeWidget()
  98. # Put ourself in the GUI
  99. self.app.ui.tool_scroll_area.setWidget(self)
  100. # Switch notebook to tool page
  101. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  102. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  103. self.app.command_active = "Measurement"
  104. # initial view of the layout
  105. self.start_entry.set_value('(0, 0)')
  106. self.stop_entry.set_value('(0, 0)')
  107. self.distance_x_entry.set_value('0')
  108. self.distance_y_entry.set_value('0')
  109. self.total_distance_entry.set_value('0')
  110. def activate(self):
  111. # we disconnect the mouse/key handlers from wherever the measurement tool was called
  112. self.app.plotcanvas.vis_disconnect('key_press')
  113. self.app.plotcanvas.vis_disconnect('mouse_double_click')
  114. self.app.plotcanvas.vis_disconnect('mouse_move')
  115. self.app.plotcanvas.vis_disconnect('mouse_press')
  116. self.app.plotcanvas.vis_disconnect('mouse_release')
  117. self.app.plotcanvas.vis_disconnect('key_release')
  118. # we can safely connect the app mouse events to the measurement tool
  119. self.app.plotcanvas.vis_connect('mouse_move', self.on_mouse_move_meas)
  120. self.app.plotcanvas.vis_connect('mouse_release', self.on_mouse_click)
  121. self.app.plotcanvas.vis_connect('key_release', self.on_key_release_meas)
  122. self.set_tool_ui()
  123. def deactivate(self):
  124. # disconnect the mouse/key events from functions of measurement tool
  125. self.app.plotcanvas.vis_disconnect('mouse_move')
  126. self.app.plotcanvas.vis_disconnect('mouse_press')
  127. self.app.plotcanvas.vis_disconnect('key_release')
  128. # reconnect the mouse/key events to the functions from where the tool was called
  129. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  130. self.app.plotcanvas.vis_connect('mouse_double_click', self.app.on_double_click_over_plot)
  131. if self.app.call_source == 'app':
  132. self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  133. self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  134. self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  135. elif self.app.call_source == 'geo_editor':
  136. self.app.geo_editor.canvas.vis_connect('mouse_move', self.app.geo_editor.on_canvas_move)
  137. self.app.geo_editor.canvas.vis_connect('mouse_press', self.app.geo_editor.on_canvas_click)
  138. # self.app.geo_editor.canvas.vis_connect('key_press', self.app.geo_editor.on_canvas_key)
  139. self.app.geo_editor.canvas.vis_connect('mouse_release', self.app.geo_editor.on_canvas_click_release)
  140. elif self.app.call_source == 'exc_editor':
  141. self.app.exc_editor.canvas.vis_connect('mouse_move', self.app.exc_editor.on_canvas_move)
  142. self.app.exc_editor.canvas.vis_connect('mouse_press', self.app.exc_editor.on_canvas_click)
  143. # self.app.exc_editor.canvas.vis_connect('key_press', self.app.exc_editor.on_canvas_key)
  144. self.app.exc_editor.canvas.vis_connect('mouse_release', self.app.exc_editor.on_canvas_click_release)
  145. elif self.app.call_source == 'grb_editor':
  146. self.app.grb_editor.canvas.vis_connect('mouse_move', self.app.grb_editor.on_canvas_move)
  147. self.app.grb_editor.canvas.vis_connect('mouse_press', self.app.grb_editor.on_canvas_click)
  148. # self.app.grb_editor.canvas.vis_connect('key_press', self.app.grb_editor.on_canvas_key)
  149. self.app.grb_editor.canvas.vis_connect('mouse_release', self.app.grb_editor.on_canvas_click_release)
  150. self.app.ui.notebook.setTabText(2, _("Tools"))
  151. def on_measure(self, signal=None, activate=None):
  152. if activate is False or activate is None:
  153. # DISABLE the Measuring TOOL
  154. self.deactivate()
  155. self.app.call_source = 'measurement'
  156. self.app.command_active = None
  157. # delete the measuring line
  158. self.delete_shape()
  159. log.debug("Measurement Tool --> exit tool")
  160. elif activate is True:
  161. # ENABLE the Measuring TOOL
  162. self.clicked_meas = 0
  163. self.app.inform.emit(_("MEASURING: Click on the Start point ..."))
  164. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  165. self.activate()
  166. log.debug("Measurement Tool --> tool initialized")
  167. def on_key_release_meas(self, event):
  168. if event.key == 'escape':
  169. # abort the measurement action
  170. self.on_measure(activate=False)
  171. self.app.inform.emit(_("Measurement Tool exit..."))
  172. return
  173. if event.key == 'G':
  174. # toggle grid status
  175. self.app.ui.grid_snap_btn.trigger()
  176. return
  177. def on_mouse_click(self, event):
  178. # mouse click releases will be accepted only if the left button is clicked
  179. # this is necessary because right mouse click or middle mouse click
  180. # are used for panning on the canvas
  181. if event.button == 1:
  182. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  183. if self.clicked_meas == 0:
  184. self.clicked_meas = 1
  185. # if GRID is active we need to get the snapped positions
  186. if self.app.grid_status() == True:
  187. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  188. else:
  189. pos = pos_canvas[0], pos_canvas[1]
  190. self.point1 = pos
  191. self.start_entry.set_value("(%.4f, %.4f)" % pos)
  192. self.app.inform.emit(_("MEASURING: Click on the Destination point ..."))
  193. else:
  194. try:
  195. # delete the selection bounding box
  196. self.delete_shape()
  197. # if GRID is active we need to get the snapped positions
  198. if self.app.grid_status() == True:
  199. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  200. else:
  201. pos = pos_canvas[0], pos_canvas[1]
  202. dx = pos[0] - self.point1[0]
  203. dy = pos[1] - self.point1[1]
  204. d = sqrt(dx**2 + dy**2)
  205. self.stop_entry.set_value("(%.4f, %.4f)" % pos)
  206. self.app.inform.emit(_("MEASURING: Result D(x) = {d_x} | D(y) = {d_y} | Distance = {d_z}").format(
  207. d_x='%4f' % abs(dx), d_y='%4f' % abs(dy), d_z='%4f' % abs(d)))
  208. self.distance_x_entry.set_value('%.4f' % abs(dx))
  209. self.distance_y_entry.set_value('%.4f' % abs(dy))
  210. self.total_distance_entry.set_value('%.4f' % abs(d))
  211. self.on_measure(activate=False)
  212. # delete the measuring line
  213. self.delete_shape()
  214. except TypeError as e:
  215. log.debug("Measurement.on_click_meas() --> %s" % str(e))
  216. def on_mouse_move_meas(self, event):
  217. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  218. # if GRID is active we need to get the snapped positions
  219. if self.app.grid_status() == True:
  220. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  221. self.app.app_cursor.enabled = True
  222. # Update cursor
  223. self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]), symbol='++', edge_color='black', size=20)
  224. else:
  225. pos = pos_canvas
  226. self.app.app_enabled = False
  227. self.point2 = (pos[0], pos[1])
  228. if self.clicked_meas == 1:
  229. self.update_meas_shape([self.point2, self.point1])
  230. def update_meas_shape(self, pos):
  231. self.delete_shape()
  232. self.draw_shape(pos)
  233. def delete_shape(self):
  234. self.sel_shapes.clear()
  235. self.sel_shapes.redraw()
  236. def draw_shape(self, coords):
  237. self.meas_line = LineString(coords)
  238. self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
  239. def set_meas_units(self, units):
  240. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  241. # end of file