ToolMeasurement.py 16 KB

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