ToolMeasurement.py 16 KB

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