ToolMeasurement.py 13 KB

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