ToolMeasurement.py 12 KB

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