ToolMeasurement.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. if self.app.tool_tab_locked is True:
  118. return
  119. self.toggle()
  120. # Remove anything else in the GUI
  121. self.app.ui.tool_scroll_area.takeWidget()
  122. # Put ourself in the GUI
  123. self.app.ui.tool_scroll_area.setWidget(self)
  124. # Switch notebook to tool page
  125. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  126. self.units = self.app.general_options_form.general_app_group.units_radio.get_value().lower()
  127. self.show()
  128. self.app.ui.notebook.setTabText(2, "Meas. Tool")
  129. def install(self, icon=None, separator=None, **kwargs):
  130. FlatCAMTool.install(self, icon, separator, shortcut='CTRL+M', **kwargs)
  131. def on_key_release_meas(self, event):
  132. if event.key == 'escape':
  133. # abort the measurement action
  134. self.toggle()
  135. return
  136. if event.key == 'G':
  137. # toggle grid status
  138. self.app.ui.grid_snap_btn.trigger()
  139. return
  140. def toggle(self):
  141. # the self.active var is doing the 'toggle'
  142. if self.active is True:
  143. # DISABLE the Measuring TOOL
  144. self.active = False
  145. # disconnect the mouse/key events from functions of measurement tool
  146. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_mouse_move_meas)
  147. self.app.plotcanvas.vis_disconnect('mouse_press', self.on_click_meas)
  148. self.app.plotcanvas.vis_disconnect('key_release', self.on_key_release_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.on_key_over_plot)
  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('key_release', self.app.geo_editor.on_canvas_key_release)
  160. self.app.geo_editor.canvas.vis_connect('mouse_release', self.app.geo_editor.on_canvas_click_release)
  161. elif self.app.call_source == 'exc_editor':
  162. self.app.exc_editor.canvas.vis_connect('mouse_move', self.app.exc_editor.on_canvas_move)
  163. self.app.exc_editor.canvas.vis_connect('mouse_press', self.app.exc_editor.on_canvas_click)
  164. self.app.exc_editor.canvas.vis_connect('key_press', self.app.exc_editor.on_canvas_key)
  165. self.app.exc_editor.canvas.vis_connect('key_release', self.app.exc_editor.on_canvas_key_release)
  166. self.app.exc_editor.canvas.vis_connect('mouse_release', self.app.exc_editor.on_canvas_click_release)
  167. self.clicked_meas = 0
  168. self.app.command_active = None
  169. # delete the measuring line
  170. self.delete_shape()
  171. return
  172. else:
  173. # ENABLE the Measuring TOOL
  174. self.active = True
  175. self.units = self.app.general_options_form.general_app_group.units_radio.get_value().lower()
  176. # we disconnect the mouse/key handlers from wherever the measurement tool was called
  177. if self.app.call_source == 'app':
  178. self.app.plotcanvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  179. self.app.plotcanvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  180. self.app.plotcanvas.vis_disconnect('key_press', self.app.on_key_over_plot)
  181. self.app.plotcanvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  182. elif self.app.call_source == 'geo_editor':
  183. self.app.geo_editor.canvas.vis_disconnect('mouse_move', self.app.geo_editor.on_canvas_move)
  184. self.app.geo_editor.canvas.vis_disconnect('mouse_press', self.app.geo_editor.on_canvas_click)
  185. self.app.geo_editor.canvas.vis_disconnect('key_press', self.app.geo_editor.on_canvas_key)
  186. self.app.geo_editor.canvas.vis_disconnect('key_release', self.app.geo_editor.on_canvas_key_release)
  187. self.app.geo_editor.canvas.vis_disconnect('mouse_release', self.app.geo_editor.on_canvas_click_release)
  188. elif self.app.call_source == 'exc_editor':
  189. self.app.exc_editor.canvas.vis_disconnect('mouse_move', self.app.exc_editor.on_canvas_move)
  190. self.app.exc_editor.canvas.vis_disconnect('mouse_press', self.app.exc_editor.on_canvas_click)
  191. self.app.exc_editor.canvas.vis_disconnect('key_press', self.app.exc_editor.on_canvas_key)
  192. self.app.exc_editor.canvas.vis_disconnect('key_release', self.app.exc_editor.on_canvas_key_release)
  193. self.app.exc_editor.canvas.vis_disconnect('mouse_release', self.app.exc_editor.on_canvas_click_release)
  194. # we can safely connect the app mouse events to the measurement tool
  195. self.app.plotcanvas.vis_connect('mouse_move', self.on_mouse_move_meas)
  196. self.app.plotcanvas.vis_connect('mouse_press', self.on_click_meas)
  197. self.app.plotcanvas.vis_connect('key_release', self.on_key_release_meas)
  198. self.app.command_active = "Measurement"
  199. # initial view of the layout
  200. self.start_entry.set_value('(0, 0)')
  201. self.stop_entry.set_value('(0, 0)')
  202. self.distance_x_entry.set_value('0')
  203. self.distance_y_entry.set_value('0')
  204. self.total_distance_entry.set_value('0')
  205. self.units_entry_1.set_value(str(self.units))
  206. self.units_entry_2.set_value(str(self.units))
  207. self.units_entry_3.set_value(str(self.units))
  208. self.units_entry_4.set_value(str(self.units))
  209. self.units_entry_5.set_value(str(self.units))
  210. self.app.inform.emit("MEASURING: Click on the Start point ...")
  211. def on_click_meas(self, event):
  212. # mouse click will be accepted only if the left button is clicked
  213. # this is necessary because right mouse click and middle mouse click
  214. # are used for panning on the canvas
  215. if event.button == 1:
  216. if self.clicked_meas == 0:
  217. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  218. # if GRID is active we need to get the snapped positions
  219. if self.app.grid_status() == True:
  220. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  221. else:
  222. pos = pos_canvas[0], pos_canvas[1]
  223. self.point1 = pos
  224. self.start_entry.set_value("(%.4f, %.4f)" % pos)
  225. self.app.inform.emit("MEASURING: Click on the Destination point ...")
  226. if self.clicked_meas == 1:
  227. try:
  228. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  229. # delete the selection bounding box
  230. self.delete_shape()
  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. dx = pos[0] - self.point1[0]
  237. dy = pos[1] - self.point1[1]
  238. d = sqrt(dx**2 + dy**2)
  239. self.stop_entry.set_value("(%.4f, %.4f)" % pos)
  240. self.app.inform.emit("MEASURING: Result D(x) = %.4f | D(y) = %.4f | Distance = %.4f" %
  241. (abs(dx), abs(dy), abs(d)))
  242. self.distance_x_entry.set_value('%.4f' % abs(dx))
  243. self.distance_y_entry.set_value('%.4f' % abs(dy))
  244. self.total_distance_entry.set_value('%.4f' % abs(d))
  245. self.clicked_meas = 0
  246. self.toggle()
  247. # delete the measuring line
  248. self.delete_shape()
  249. return
  250. except TypeError:
  251. pass
  252. self.clicked_meas = 1
  253. def on_mouse_move_meas(self, event):
  254. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  255. # if GRID is active we need to get the snapped positions
  256. if self.app.grid_status() == True:
  257. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  258. self.app.app_cursor.enabled = True
  259. # Update cursor
  260. self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]), symbol='++', edge_color='black', size=20)
  261. else:
  262. pos = pos_canvas
  263. self.app.app_enabled = False
  264. self.point2 = (pos[0], pos[1])
  265. if self.clicked_meas == 1:
  266. self.update_meas_shape([self.point2, self.point1])
  267. def update_meas_shape(self, pos):
  268. self.delete_shape()
  269. self.draw_shape(pos)
  270. def delete_shape(self):
  271. self.sel_shapes.clear()
  272. self.sel_shapes.redraw()
  273. def draw_shape(self, coords):
  274. self.meas_line = LineString(coords)
  275. self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
  276. def set_meas_units(self, units):
  277. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  278. # end of file