ToolMeasurement.py 17 KB

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