ToolMeasurement.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.app = app
  23. self.canvas = self.app.plotcanvas
  24. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  25. # ## Title
  26. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font><br>" % self.toolName)
  27. self.layout.addWidget(title_label)
  28. # ## Form Layout
  29. form_layout = QtWidgets.QFormLayout()
  30. self.layout.addLayout(form_layout)
  31. self.units_label = QtWidgets.QLabel(_("Units:"))
  32. self.units_label.setToolTip(_("Those are the units in which the distance is measured."))
  33. self.units_value = QtWidgets.QLabel("%s" % str({'mm': "METRIC (mm)", 'in': "INCH (in)"}[self.units]))
  34. self.units_value.setDisabled(True)
  35. self.start_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Start'), _('Coords')))
  36. self.start_label.setToolTip(_("This is measuring Start point coordinates."))
  37. self.stop_label = QtWidgets.QLabel("<b>%s</b> %s:" % (_('Stop'), _('Coords')))
  38. self.stop_label.setToolTip(_("This is the measuring Stop point coordinates."))
  39. self.distance_x_label = QtWidgets.QLabel(_("Dx:"))
  40. self.distance_x_label.setToolTip(_("This is the distance measured over the X axis."))
  41. self.distance_y_label = QtWidgets.QLabel(_("Dy:"))
  42. self.distance_y_label.setToolTip(_("This is the distance measured over the Y axis."))
  43. self.total_distance_label = QtWidgets.QLabel("<b>%s:</b>" % _('DISTANCE'))
  44. self.total_distance_label.setToolTip(_("This is the point to point Euclidian distance."))
  45. self.start_entry = FCEntry()
  46. self.start_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  47. self.start_entry.setToolTip(_("This is measuring Start point coordinates."))
  48. self.stop_entry = FCEntry()
  49. self.stop_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  50. self.stop_entry.setToolTip(_("This is the measuring Stop point coordinates."))
  51. self.distance_x_entry = FCEntry()
  52. self.distance_x_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  53. self.distance_x_entry.setToolTip(_("This is the distance measured over the X axis."))
  54. self.distance_y_entry = FCEntry()
  55. self.distance_y_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  56. self.distance_y_entry.setToolTip(_("This is the distance measured over the Y axis."))
  57. self.total_distance_entry = FCEntry()
  58. self.total_distance_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  59. self.total_distance_entry.setToolTip(_("This is the point to point Euclidian distance."))
  60. self.measure_btn = QtWidgets.QPushButton(_("Measure"))
  61. # self.measure_btn.setFixedWidth(70)
  62. self.layout.addWidget(self.measure_btn)
  63. form_layout.addRow(self.units_label, self.units_value)
  64. form_layout.addRow(self.start_label, self.start_entry)
  65. form_layout.addRow(self.stop_label, self.stop_entry)
  66. form_layout.addRow(self.distance_x_label, self.distance_x_entry)
  67. form_layout.addRow(self.distance_y_label, self.distance_y_entry)
  68. form_layout.addRow(self.total_distance_label, self.total_distance_entry)
  69. # initial view of the layout
  70. self.start_entry.set_value('(0, 0)')
  71. self.stop_entry.set_value('(0, 0)')
  72. self.distance_x_entry.set_value('0')
  73. self.distance_y_entry.set_value('0')
  74. self.total_distance_entry.set_value('0')
  75. self.layout.addStretch()
  76. # store here the first click and second click of the measurement process
  77. self.points = []
  78. self.rel_point1 = None
  79. self.rel_point2 = None
  80. self.active = False
  81. self.original_call_source = 'app'
  82. # VisPy visuals
  83. self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
  84. self.measure_btn.clicked.connect(self.activate_measure_tool)
  85. def run(self, toggle=False):
  86. self.app.report_usage("ToolMeasurement()")
  87. self.points[:] = []
  88. self.rel_point1 = None
  89. self.rel_point2 = None
  90. if self.app.tool_tab_locked is True:
  91. return
  92. self.app.ui.notebook.setTabText(2, _("Meas. Tool"))
  93. # if the splitter is hidden, display it
  94. if self.app.ui.splitter.sizes()[0] == 0:
  95. self.app.ui.splitter.setSizes([1, 1])
  96. if toggle:
  97. pass
  98. if self.active is False:
  99. self.activate_measure_tool()
  100. else:
  101. self.deactivate_measure_tool()
  102. def install(self, icon=None, separator=None, **kwargs):
  103. FlatCAMTool.install(self, icon, separator, shortcut='CTRL+M', **kwargs)
  104. def set_tool_ui(self):
  105. # Remove anything else in the GUI
  106. self.app.ui.tool_scroll_area.takeWidget()
  107. # Put ourself in the GUI
  108. self.app.ui.tool_scroll_area.setWidget(self)
  109. # Switch notebook to tool page
  110. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  111. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  112. self.app.command_active = "Measurement"
  113. # initial view of the layout
  114. self.start_entry.set_value('(0, 0)')
  115. self.stop_entry.set_value('(0, 0)')
  116. self.distance_x_entry.set_value('0')
  117. self.distance_y_entry.set_value('0')
  118. self.total_distance_entry.set_value('0')
  119. log.debug("Measurement Tool --> tool initialized")
  120. def activate_measure_tool(self):
  121. # ENABLE the Measuring TOOL
  122. self.active = True
  123. self.clicked_meas = 0
  124. self.original_call_source = copy(self.app.call_source)
  125. self.app.inform.emit(_("MEASURING: Click on the Start point ..."))
  126. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  127. # we can connect the app mouse events to the measurement tool
  128. # NEVER DISCONNECT THOSE before connecting some other handlers; it breaks something in VisPy
  129. self.canvas.vis_connect('mouse_move', self.on_mouse_move_meas)
  130. self.canvas.vis_connect('mouse_release', self.on_mouse_click_release)
  131. # we disconnect the mouse/key handlers from wherever the measurement tool was called
  132. if self.app.call_source == 'app':
  133. self.canvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  134. self.canvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  135. self.canvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  136. elif self.app.call_source == 'geo_editor':
  137. self.canvas.vis_disconnect('mouse_move', self.app.geo_editor.on_canvas_move)
  138. self.canvas.vis_disconnect('mouse_press', self.app.geo_editor.on_canvas_click)
  139. self.canvas.vis_disconnect('mouse_release', self.app.geo_editor.on_geo_click_release)
  140. elif self.app.call_source == 'exc_editor':
  141. self.canvas.vis_disconnect('mouse_move', self.app.exc_editor.on_canvas_move)
  142. self.canvas.vis_disconnect('mouse_press', self.app.exc_editor.on_canvas_click)
  143. self.canvas.vis_disconnect('mouse_release', self.app.exc_editor.on_exc_click_release)
  144. elif self.app.call_source == 'grb_editor':
  145. self.canvas.vis_disconnect('mouse_move', self.app.grb_editor.on_canvas_move)
  146. self.canvas.vis_disconnect('mouse_press', self.app.grb_editor.on_canvas_click)
  147. self.canvas.vis_disconnect('mouse_release', self.app.grb_editor.on_grb_click_release)
  148. self.app.call_source = 'measurement'
  149. self.set_tool_ui()
  150. def deactivate_measure_tool(self):
  151. # DISABLE the Measuring TOOL
  152. self.active = False
  153. self.points = []
  154. self.app.call_source = copy(self.original_call_source)
  155. if self.original_call_source == 'app':
  156. self.canvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  157. self.canvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  158. self.canvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  159. elif self.original_call_source == 'geo_editor':
  160. self.canvas.vis_connect('mouse_move', self.app.geo_editor.on_canvas_move)
  161. self.canvas.vis_connect('mouse_press', self.app.geo_editor.on_canvas_click)
  162. self.canvas.vis_connect('mouse_release', self.app.geo_editor.on_geo_click_release)
  163. elif self.original_call_source == 'exc_editor':
  164. self.canvas.vis_connect('mouse_move', self.app.exc_editor.on_canvas_move)
  165. self.canvas.vis_connect('mouse_press', self.app.exc_editor.on_canvas_click)
  166. self.canvas.vis_connect('mouse_release', self.app.exc_editor.on_exc_click_release)
  167. elif self.original_call_source == 'grb_editor':
  168. self.canvas.vis_connect('mouse_move', self.app.grb_editor.on_canvas_move)
  169. self.canvas.vis_connect('mouse_press', self.app.grb_editor.on_canvas_click)
  170. self.canvas.vis_connect('mouse_release', self.app.grb_editor.on_grb_click_release)
  171. # disconnect the mouse/key events from functions of measurement tool
  172. self.canvas.vis_disconnect('mouse_move', self.on_mouse_move_meas)
  173. self.canvas.vis_disconnect('mouse_release', self.on_mouse_click_release)
  174. # self.app.ui.notebook.setTabText(2, _("Tools"))
  175. # self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  176. self.app.command_active = None
  177. # delete the measuring line
  178. self.delete_shape()
  179. log.debug("Measurement Tool --> exit tool")
  180. def on_mouse_click_release(self, event):
  181. # mouse click releases will be accepted only if the left button is clicked
  182. # this is necessary because right mouse click or middle mouse click
  183. # are used for panning on the canvas
  184. log.debug("Measuring Tool --> mouse click release")
  185. if event.button == 1:
  186. pos_canvas = self.canvas.vispy_canvas.translate_coords(event.pos)
  187. # if GRID is active we need to get the snapped positions
  188. if self.app.grid_status():
  189. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  190. else:
  191. pos = pos_canvas[0], pos_canvas[1]
  192. self.points.append(pos)
  193. # Reset here the relative coordinates so there is a new reference on the click position
  194. if self.rel_point1 is None:
  195. self.app.ui.rel_position_label.setText("<b>Dx</b>: %.4f&nbsp;&nbsp; <b>Dy</b>: "
  196. "%.4f&nbsp;&nbsp;&nbsp;&nbsp;" % (0.0, 0.0))
  197. self.rel_point1 = pos
  198. else:
  199. self.rel_point2 = copy(self.rel_point1)
  200. self.rel_point1 = pos
  201. if len(self.points) == 1:
  202. self.start_entry.set_value("(%.4f, %.4f)" % pos)
  203. self.app.inform.emit(_("MEASURING: Click on the Destination point ..."))
  204. if len(self.points) == 2:
  205. dx = self.points[1][0] - self.points[0][0]
  206. dy = self.points[1][1] - self.points[0][1]
  207. d = sqrt(dx ** 2 + dy ** 2)
  208. self.stop_entry.set_value("(%.4f, %.4f)" % pos)
  209. self.app.inform.emit(_("MEASURING: Result D(x) = {d_x} | D(y) = {d_y} | Distance = {d_z}").format(
  210. d_x='%4f' % abs(dx), d_y='%4f' % abs(dy), d_z='%4f' % abs(d)))
  211. self.distance_x_entry.set_value('%.4f' % abs(dx))
  212. self.distance_y_entry.set_value('%.4f' % abs(dy))
  213. self.total_distance_entry.set_value('%.4f' % abs(d))
  214. self.app.ui.rel_position_label.setText("<b>Dx</b>: %.4f&nbsp;&nbsp; <b>Dy</b>: "
  215. "%.4f&nbsp;&nbsp;&nbsp;&nbsp;" % (pos[0], pos[1]))
  216. self.deactivate_measure_tool()
  217. def on_mouse_move_meas(self, event):
  218. try: # May fail in case mouse not within axes
  219. pos_canvas = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  220. if self.app.grid_status():
  221. pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
  222. self.app.app_cursor.enabled = True
  223. # Update cursor
  224. self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]),
  225. symbol='++', edge_color='black', size=20)
  226. else:
  227. pos = (pos_canvas[0], pos_canvas[1])
  228. self.app.app_cursor.enabled = False
  229. if self.rel_point1 is not None:
  230. dx = pos[0] - self.rel_point1[0]
  231. dy = pos[1] - self.rel_point1[1]
  232. else:
  233. dx = pos[0]
  234. dy = pos[1]
  235. self.app.ui.position_label.setText("&nbsp;&nbsp;&nbsp;&nbsp;<b>X</b>: %.4f&nbsp;&nbsp; "
  236. "<b>Y</b>: %.4f" % (pos[0], pos[1]))
  237. self.app.ui.rel_position_label.setText("<b>Dx</b>: %.4f&nbsp;&nbsp; <b>Dy</b>: "
  238. "%.4f&nbsp;&nbsp;&nbsp;&nbsp;" % (dx, dy))
  239. # update utility geometry
  240. if len(self.points) == 1:
  241. self.utility_geometry(pos=pos)
  242. except:
  243. self.app.ui.position_label.setText("")
  244. self.app.ui.rel_position_label.setText("")
  245. def utility_geometry(self, pos):
  246. # first delete old shape
  247. self.delete_shape()
  248. # second draw the new shape of the utility geometry
  249. self.meas_line = LineString([pos, self.points[0]])
  250. self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
  251. def delete_shape(self):
  252. self.sel_shapes.clear()
  253. self.sel_shapes.redraw()
  254. def set_meas_units(self, units):
  255. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  256. # end of file