ToolMeasurement.py 17 KB

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