ToolMeasurement.py 16 KB

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