ToolMeasurement.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. # elif self.app.call_source == 'grb_editor':
  174. # self.app.grb_editor.canvas.vis_connect('mouse_move', self.app.grb_editor.on_canvas_move)
  175. # self.app.grb_editor.canvas.vis_connect('mouse_press', self.app.grb_editor.on_canvas_click)
  176. # self.app.grb_editor.canvas.vis_connect('key_press', self.app.grb_editor.on_canvas_key)
  177. # self.app.grb_editor.canvas.vis_connect('mouse_release', self.app.grb_editor.on_canvas_click_release)
  178. self.app.call_source = 'measurement'
  179. self.clicked_meas = 0
  180. self.app.command_active = None
  181. # delete the measuring line
  182. self.delete_shape()
  183. return
  184. else:
  185. # ENABLE the Measuring TOOL
  186. self.active = True
  187. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  188. # we disconnect the mouse/key handlers from wherever the measurement tool was called
  189. if self.app.call_source == 'app':
  190. self.app.plotcanvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  191. self.app.plotcanvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  192. self.app.plotcanvas.vis_disconnect('key_press', self.app.ui.keyPressEvent)
  193. self.app.plotcanvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  194. elif self.app.call_source == 'geo_editor':
  195. self.app.geo_editor.canvas.vis_disconnect('mouse_move', self.app.geo_editor.on_canvas_move)
  196. self.app.geo_editor.canvas.vis_disconnect('mouse_press', self.app.geo_editor.on_canvas_click)
  197. self.app.geo_editor.canvas.vis_disconnect('key_press', self.app.geo_editor.on_canvas_key)
  198. self.app.geo_editor.canvas.vis_disconnect('mouse_release', self.app.geo_editor.on_canvas_click_release)
  199. elif self.app.call_source == 'exc_editor':
  200. self.app.exc_editor.canvas.vis_disconnect('mouse_move', self.app.exc_editor.on_canvas_move)
  201. self.app.exc_editor.canvas.vis_disconnect('mouse_press', self.app.exc_editor.on_canvas_click)
  202. self.app.exc_editor.canvas.vis_disconnect('key_press', self.app.exc_editor.on_canvas_key)
  203. self.app.exc_editor.canvas.vis_disconnect('mouse_release', self.app.exc_editor.on_canvas_click_release)
  204. # elif self.app.call_source == 'grb_editor':
  205. # self.app.grb_editor.canvas.vis_disconnect('mouse_move', self.app.grb_editor.on_canvas_move)
  206. # self.app.grb_editor.canvas.vis_disconnect('mouse_press', self.app.grb_editor.on_canvas_click)
  207. # self.app.grb_editor.canvas.vis_disconnect('key_press', self.app.grb_editor.on_canvas_key)
  208. # self.app.grb_editor.canvas.vis_disconnect('mouse_release', self.app.grb_editor.on_canvas_click_release)
  209. # we can safely connect the app mouse events to the measurement tool
  210. self.app.plotcanvas.vis_connect('mouse_move', self.on_mouse_move_meas)
  211. self.app.plotcanvas.vis_connect('mouse_release', self.on_click_meas)
  212. self.app.plotcanvas.vis_connect('key_release', self.on_key_release_meas)
  213. self.app.command_active = "Measurement"
  214. # initial view of the layout
  215. self.start_entry.set_value('(0, 0)')
  216. self.stop_entry.set_value('(0, 0)')
  217. self.distance_x_entry.set_value('0')
  218. self.distance_y_entry.set_value('0')
  219. self.total_distance_entry.set_value('0')
  220. self.units_entry_1.set_value(str(self.units))
  221. self.units_entry_2.set_value(str(self.units))
  222. self.units_entry_3.set_value(str(self.units))
  223. self.units_entry_4.set_value(str(self.units))
  224. self.units_entry_5.set_value(str(self.units))
  225. self.app.inform.emit(_("MEASURING: Click on the Start point ..."))
  226. def on_key_release_meas(self, event):
  227. if event.key == 'escape':
  228. # abort the measurement action
  229. self.toggle_f()
  230. self.app.inform.emit("")
  231. return
  232. if event.key == 'G':
  233. # toggle grid status
  234. self.app.ui.grid_snap_btn.trigger()
  235. return
  236. def on_click_meas(self, event):
  237. # mouse click releases will be accepted only if the left button is clicked
  238. # this is necessary because right mouse click or middle mouse click
  239. # are used for panning on the canvas
  240. if event.button == 1:
  241. if self.clicked_meas == 0:
  242. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  243. # if GRID is active we need to get the snapped positions
  244. if self.app.grid_status() == True:
  245. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  246. else:
  247. pos = pos_canvas[0], pos_canvas[1]
  248. self.point1 = pos
  249. self.start_entry.set_value("(%.4f, %.4f)" % pos)
  250. self.app.inform.emit(_("MEASURING: Click on the Destination point ..."))
  251. if self.clicked_meas == 1:
  252. try:
  253. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  254. # delete the selection bounding box
  255. self.delete_shape()
  256. # if GRID is active we need to get the snapped positions
  257. if self.app.grid_status() == True:
  258. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  259. else:
  260. pos = pos_canvas[0], pos_canvas[1]
  261. dx = pos[0] - self.point1[0]
  262. dy = pos[1] - self.point1[1]
  263. d = sqrt(dx**2 + dy**2)
  264. self.stop_entry.set_value("(%.4f, %.4f)" % pos)
  265. self.app.inform.emit(_("MEASURING: Result D(x) = {d_x} | D(y) = {d_y} | Distance = {d_z}").format(
  266. d_x='%4f' % abs(dx), d_y='%4f' % abs(dy), d_z='%4f' % abs(d)))
  267. self.distance_x_entry.set_value('%.4f' % abs(dx))
  268. self.distance_y_entry.set_value('%.4f' % abs(dy))
  269. self.total_distance_entry.set_value('%.4f' % abs(d))
  270. self.clicked_meas = 0
  271. self.toggle_f()
  272. # delete the measuring line
  273. self.delete_shape()
  274. return
  275. except TypeError:
  276. pass
  277. self.clicked_meas = 1
  278. def on_mouse_move_meas(self, event):
  279. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  280. # if GRID is active we need to get the snapped positions
  281. if self.app.grid_status() == True:
  282. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  283. self.app.app_cursor.enabled = True
  284. # Update cursor
  285. self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]), symbol='++', edge_color='black', size=20)
  286. else:
  287. pos = pos_canvas
  288. self.app.app_enabled = False
  289. self.point2 = (pos[0], pos[1])
  290. if self.clicked_meas == 1:
  291. self.update_meas_shape([self.point2, self.point1])
  292. def update_meas_shape(self, pos):
  293. self.delete_shape()
  294. self.draw_shape(pos)
  295. def delete_shape(self):
  296. self.sel_shapes.clear()
  297. self.sel_shapes.redraw()
  298. def draw_shape(self, coords):
  299. self.meas_line = LineString(coords)
  300. self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
  301. def set_meas_units(self, units):
  302. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  303. # end of file