ToolMeasurement.py 15 KB

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