ToolMeasurement.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.canvas = self.app.plotcanvas
  24. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  25. ## Title
  26. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font><br>" % self.toolName)
  27. self.layout.addWidget(title_label)
  28. ## Form Layout
  29. form_layout = QtWidgets.QFormLayout()
  30. self.layout.addLayout(form_layout)
  31. self.units_label = QtWidgets.QLabel(_("Units:"))
  32. self.units_label.setToolTip(_("Those are the units in which the distance is measured."))
  33. self.units_value = QtWidgets.QLabel("%s" % str({'mm': "METRIC (mm)", 'in': "INCH (in)"}[self.units]))
  34. self.units_value.setDisabled(True)
  35. self.start_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Start'), _('Coords')))
  36. self.start_label.setToolTip(_("This is measuring Start point coordinates."))
  37. self.stop_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Stop'), _('Coords')))
  38. self.stop_label.setToolTip(_("This is the measuring Stop point coordinates."))
  39. self.distance_x_label = QtWidgets.QLabel("Dx:")
  40. self.distance_x_label.setToolTip(_("This is the distance measured over the X axis."))
  41. self.distance_y_label = QtWidgets.QLabel("Dy:")
  42. self.distance_y_label.setToolTip(_("This is the distance measured over the Y axis."))
  43. self.total_distance_label = QtWidgets.QLabel("<b>%s:</b>" % _('DISTANCE'))
  44. self.total_distance_label.setToolTip(_("This is the point to point Euclidian distance."))
  45. self.start_entry = FCEntry()
  46. self.start_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  47. self.start_entry.setToolTip(_("This is measuring Start point coordinates."))
  48. self.stop_entry = FCEntry()
  49. self.stop_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  50. self.stop_entry.setToolTip(_("This is the measuring Stop point coordinates."))
  51. self.distance_x_entry = FCEntry()
  52. self.distance_x_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  53. self.distance_x_entry.setToolTip(_("This is the distance measured over the X axis."))
  54. self.distance_y_entry = FCEntry()
  55. self.distance_y_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  56. self.distance_y_entry.setToolTip(_("This is the distance measured over the Y axis."))
  57. self.total_distance_entry = FCEntry()
  58. self.total_distance_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  59. self.total_distance_entry.setToolTip(_("This is the point to point Euclidian distance."))
  60. self.measure_btn = QtWidgets.QPushButton(_("Measure"))
  61. # self.measure_btn.setFixedWidth(70)
  62. self.layout.addWidget(self.measure_btn)
  63. form_layout.addRow(self.units_label, self.units_value)
  64. form_layout.addRow(self.start_label, self.start_entry)
  65. form_layout.addRow(self.stop_label, self.stop_entry)
  66. form_layout.addRow(self.distance_x_label, self.distance_x_entry)
  67. form_layout.addRow(self.distance_y_label, self.distance_y_entry)
  68. form_layout.addRow(self.total_distance_label, self.total_distance_entry)
  69. # initial view of the layout
  70. self.start_entry.set_value('(0, 0)')
  71. self.stop_entry.set_value('(0, 0)')
  72. self.distance_x_entry.set_value('0')
  73. self.distance_y_entry.set_value('0')
  74. self.total_distance_entry.set_value('0')
  75. self.layout.addStretch()
  76. self.clicked_meas = 0
  77. self.point1 = None
  78. self.point2 = None
  79. # the default state is disabled for the Move command
  80. # self.setVisible(False)
  81. self.active = 0
  82. # VisPy visuals
  83. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
  84. self.measure_btn.clicked.connect(lambda: self.on_measure(activate=True))
  85. def run(self, toggle=False):
  86. self.app.report_usage("ToolMeasurement()")
  87. if self.app.tool_tab_locked is True:
  88. return
  89. self.app.ui.notebook.setTabText(2, _("Meas. Tool"))
  90. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  91. if self.app.ui.splitter.sizes()[0] == 0:
  92. self.app.ui.splitter.setSizes([1, 1])
  93. self.on_measure(activate=True)
  94. def install(self, icon=None, separator=None, **kwargs):
  95. FlatCAMTool.install(self, icon, separator, shortcut='CTRL+M', **kwargs)
  96. def set_tool_ui(self):
  97. # Remove anything else in the GUI
  98. self.app.ui.tool_scroll_area.takeWidget()
  99. # Put ourself in the GUI
  100. self.app.ui.tool_scroll_area.setWidget(self)
  101. # Switch notebook to tool page
  102. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  103. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  104. self.app.command_active = "Measurement"
  105. # initial view of the layout
  106. self.start_entry.set_value('(0, 0)')
  107. self.stop_entry.set_value('(0, 0)')
  108. self.distance_x_entry.set_value('0')
  109. self.distance_y_entry.set_value('0')
  110. self.total_distance_entry.set_value('0')
  111. def activate(self):
  112. # we disconnect the mouse/key handlers from wherever the measurement tool was called
  113. self.canvas.vis_disconnect('key_press')
  114. self.canvas.vis_disconnect('mouse_move')
  115. self.canvas.vis_disconnect('mouse_press')
  116. self.canvas.vis_disconnect('mouse_release')
  117. self.canvas.vis_disconnect('key_release')
  118. # we can safely connect the app mouse events to the measurement tool
  119. self.canvas.vis_connect('mouse_move', self.on_mouse_move_meas)
  120. self.canvas.vis_connect('mouse_release', self.on_mouse_click)
  121. self.canvas.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.canvas.vis_disconnect('mouse_move', self.on_mouse_move_meas)
  126. self.canvas.vis_disconnect('mouse_release', self.on_mouse_click)
  127. self.canvas.vis_disconnect('key_release', self.on_key_release_meas)
  128. # reconnect the mouse/key events to the functions from where the tool was called
  129. self.canvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  130. if self.app.call_source == 'app':
  131. self.canvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  132. self.canvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  133. self.canvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  134. elif self.app.call_source == 'geo_editor':
  135. self.canvas.vis_connect('mouse_move', self.app.geo_editor.on_canvas_move)
  136. self.canvas.vis_connect('mouse_press', self.app.geo_editor.on_canvas_click)
  137. # self.canvas.vis_connect('key_press', self.app.geo_editor.on_canvas_key)
  138. self.canvas.vis_connect('mouse_release', self.app.geo_editor.on_canvas_click_release)
  139. elif self.app.call_source == 'exc_editor':
  140. self.canvas.vis_connect('mouse_move', self.app.exc_editor.on_canvas_move)
  141. self.canvas.vis_connect('mouse_press', self.app.exc_editor.on_canvas_click)
  142. # self.canvas.vis_connect('key_press', self.app.exc_editor.on_canvas_key)
  143. self.canvas.vis_connect('mouse_release', self.app.exc_editor.on_canvas_click_release)
  144. elif self.app.call_source == 'grb_editor':
  145. self.canvas.vis_connect('mouse_move', self.app.grb_editor.on_canvas_move)
  146. self.canvas.vis_connect('mouse_press', self.app.grb_editor.on_canvas_click)
  147. # self.canvas.vis_connect('key_press', self.app.grb_editor.on_canvas_key)
  148. self.canvas.vis_connect('mouse_release', self.app.grb_editor.on_canvas_click_release)
  149. self.app.ui.notebook.setTabText(2, _("Tools"))
  150. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  151. def on_measure(self, signal=None, activate=None):
  152. log.debug("Measurement.on_measure()")
  153. if activate is False or activate is None:
  154. # DISABLE the Measuring TOOL
  155. self.deactivate()
  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.canvas.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. # delete the selection bounding box
  195. self.delete_shape()
  196. # if GRID is active we need to get the snapped positions
  197. if self.app.grid_status() is True:
  198. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  199. else:
  200. pos = pos_canvas[0], pos_canvas[1]
  201. dx = pos[0] - self.point1[0]
  202. dy = pos[1] - self.point1[1]
  203. d = sqrt(dx ** 2 + dy ** 2)
  204. self.stop_entry.set_value("(%.4f, %.4f)" % pos)
  205. self.app.inform.emit(_("MEASURING: Result D(x) = {d_x} | D(y) = {d_y} | Distance = {d_z}").format(
  206. d_x='%4f' % abs(dx), d_y='%4f' % abs(dy), d_z='%4f' % abs(d)))
  207. self.distance_x_entry.set_value('%.4f' % abs(dx))
  208. self.distance_y_entry.set_value('%.4f' % abs(dy))
  209. self.total_distance_entry.set_value('%.4f' % abs(d))
  210. # TODO: I don't understand why I have to do it twice ... but without it the mouse handlers are
  211. # TODO: are left disconnected ...
  212. self.on_measure(activate=False)
  213. self.deactivate()
  214. def on_mouse_move_meas(self, event):
  215. pos_canvas = self.canvas.vispy_canvas.translate_coords(event.pos)
  216. # if GRID is active we need to get the snapped positions
  217. if self.app.grid_status() == True:
  218. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  219. self.app.app_cursor.enabled = True
  220. # Update cursor
  221. self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]), symbol='++', edge_color='black', size=20)
  222. else:
  223. pos = pos_canvas
  224. self.app.app_enabled = False
  225. self.point2 = (pos[0], pos[1])
  226. # update utility geometry
  227. if self.clicked_meas == 1:
  228. # first delete old shape
  229. self.delete_shape()
  230. # second draw the new shape of the utility geometry
  231. self.meas_line = LineString([self.point2, self.point1])
  232. self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
  233. def delete_shape(self):
  234. self.sel_shapes.clear()
  235. self.sel_shapes.redraw()
  236. def set_meas_units(self, units):
  237. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  238. # end of file