|
|
@@ -103,25 +103,23 @@ class Measurement(FlatCAMTool):
|
|
|
|
|
|
self.layout.addStretch()
|
|
|
|
|
|
- self.clicked_meas = 0
|
|
|
-
|
|
|
- self.point1 = None
|
|
|
- self.point2 = None
|
|
|
+ # store here the first click and second click of the measurement process
|
|
|
+ self.points = []
|
|
|
|
|
|
- # the default state is disabled for the Move command
|
|
|
- # self.setVisible(False)
|
|
|
- self.active = 0
|
|
|
+ self.active = False
|
|
|
|
|
|
self.original_call_source = 'app'
|
|
|
|
|
|
# VisPy visuals
|
|
|
self.sel_shapes = ShapeCollection(parent=self.app.plotcanvas.vispy_canvas.view.scene, layers=1)
|
|
|
|
|
|
- self.measure_btn.clicked.connect(lambda: self.on_measure(activate=True))
|
|
|
+ self.measure_btn.clicked.connect(self.activate_measure_tool)
|
|
|
|
|
|
def run(self, toggle=False):
|
|
|
self.app.report_usage("ToolMeasurement()")
|
|
|
|
|
|
+ self.points[:] = []
|
|
|
+
|
|
|
if self.app.tool_tab_locked is True:
|
|
|
return
|
|
|
|
|
|
@@ -130,8 +128,13 @@ class Measurement(FlatCAMTool):
|
|
|
# if the splitter is hidden, display it
|
|
|
if self.app.ui.splitter.sizes()[0] == 0:
|
|
|
self.app.ui.splitter.setSizes([1, 1])
|
|
|
+ if toggle:
|
|
|
+ pass
|
|
|
|
|
|
- self.on_measure(activate=True)
|
|
|
+ if self.active is False:
|
|
|
+ self.activate_measure_tool()
|
|
|
+ else:
|
|
|
+ self.deactivate_measure_tool()
|
|
|
|
|
|
def install(self, icon=None, separator=None, **kwargs):
|
|
|
FlatCAMTool.install(self, icon, separator, shortcut='CTRL+M', **kwargs)
|
|
|
@@ -156,101 +159,104 @@ class Measurement(FlatCAMTool):
|
|
|
self.distance_x_entry.set_value('0')
|
|
|
self.distance_y_entry.set_value('0')
|
|
|
self.total_distance_entry.set_value('0')
|
|
|
+ log.debug("Measurement Tool --> tool initialized")
|
|
|
|
|
|
def activate_measure_tool(self):
|
|
|
- # we disconnect the mouse/key handlers from wherever the measurement tool was called
|
|
|
- self.canvas.vis_disconnect('mouse_move')
|
|
|
- self.canvas.vis_disconnect('mouse_press')
|
|
|
- self.canvas.vis_disconnect('mouse_release')
|
|
|
+ # ENABLE the Measuring TOOL
|
|
|
+ self.active = True
|
|
|
+
|
|
|
+ self.clicked_meas = 0
|
|
|
+ self.original_call_source = copy(self.app.call_source)
|
|
|
+
|
|
|
+ self.app.inform.emit(_("MEASURING: Click on the Start point ..."))
|
|
|
+ self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
|
|
|
|
|
|
- # we can safely connect the app mouse events to the measurement tool
|
|
|
+ # we can connect the app mouse events to the measurement tool
|
|
|
+ # NEVER DISCONNECT THOSE before connecting some other handlers; it breaks something in VisPy
|
|
|
self.canvas.vis_connect('mouse_move', self.on_mouse_move_meas)
|
|
|
self.canvas.vis_connect('mouse_release', self.on_mouse_click_release)
|
|
|
|
|
|
+ # we disconnect the mouse/key handlers from wherever the measurement tool was called
|
|
|
+ if self.app.call_source == 'app':
|
|
|
+ self.canvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
|
|
|
+ self.canvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
|
|
|
+ self.canvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
|
|
|
+ elif self.app.call_source == 'geo_editor':
|
|
|
+ self.canvas.vis_disconnect('mouse_move', self.app.geo_editor.on_canvas_move)
|
|
|
+ self.canvas.vis_disconnect('mouse_press', self.app.geo_editor.on_canvas_click)
|
|
|
+ self.canvas.vis_disconnect('mouse_release', self.app.geo_editor.on_geo_click_release)
|
|
|
+ elif self.app.call_source == 'exc_editor':
|
|
|
+ self.canvas.vis_disconnect('mouse_move', self.app.exc_editor.on_canvas_move)
|
|
|
+ self.canvas.vis_disconnect('mouse_press', self.app.exc_editor.on_canvas_click)
|
|
|
+ self.canvas.vis_disconnect('mouse_release', self.app.exc_editor.on_exc_click_release)
|
|
|
+ elif self.app.call_source == 'grb_editor':
|
|
|
+ self.canvas.vis_disconnect('mouse_move', self.app.grb_editor.on_canvas_move)
|
|
|
+ self.canvas.vis_disconnect('mouse_press', self.app.grb_editor.on_canvas_click)
|
|
|
+ self.canvas.vis_disconnect('mouse_release', self.app.grb_editor.on_grb_click_release)
|
|
|
+
|
|
|
+ self.app.call_source = 'measurement'
|
|
|
+
|
|
|
self.set_tool_ui()
|
|
|
|
|
|
def deactivate_measure_tool(self):
|
|
|
- # disconnect the mouse/key events from functions of measurement tool
|
|
|
- self.canvas.vis_disconnect('mouse_move', self.on_mouse_move_meas)
|
|
|
- self.canvas.vis_disconnect('mouse_release', self.on_mouse_click_release)
|
|
|
+ # DISABLE the Measuring TOOL
|
|
|
+ self.active = False
|
|
|
+ self.points = []
|
|
|
|
|
|
- if self.app.call_source == 'app':
|
|
|
+ self.app.call_source = copy(self.original_call_source)
|
|
|
+ if self.original_call_source == 'app':
|
|
|
self.canvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
|
|
|
self.canvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
|
|
|
self.canvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
|
|
|
- elif self.app.call_source == 'geo_editor':
|
|
|
+ elif self.original_call_source == 'geo_editor':
|
|
|
self.canvas.vis_connect('mouse_move', self.app.geo_editor.on_canvas_move)
|
|
|
self.canvas.vis_connect('mouse_press', self.app.geo_editor.on_canvas_click)
|
|
|
self.canvas.vis_connect('mouse_release', self.app.geo_editor.on_geo_click_release)
|
|
|
- elif self.app.call_source == 'exc_editor':
|
|
|
+ elif self.original_call_source == 'exc_editor':
|
|
|
self.canvas.vis_connect('mouse_move', self.app.exc_editor.on_canvas_move)
|
|
|
self.canvas.vis_connect('mouse_press', self.app.exc_editor.on_canvas_click)
|
|
|
self.canvas.vis_connect('mouse_release', self.app.exc_editor.on_exc_click_release)
|
|
|
- elif self.app.call_source == 'grb_editor':
|
|
|
+ elif self.original_call_source == 'grb_editor':
|
|
|
self.canvas.vis_connect('mouse_move', self.app.grb_editor.on_canvas_move)
|
|
|
self.canvas.vis_connect('mouse_press', self.app.grb_editor.on_canvas_click)
|
|
|
self.canvas.vis_connect('mouse_release', self.app.grb_editor.on_grb_click_release)
|
|
|
|
|
|
- self.app.ui.notebook.setTabText(2, _("Tools"))
|
|
|
- self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
|
|
|
-
|
|
|
- def on_measure(self, signal=None, activate=None):
|
|
|
- log.debug("Measurement.on_measure()")
|
|
|
- if activate is True:
|
|
|
- # ENABLE the Measuring TOOL
|
|
|
- self.clicked_meas = 0
|
|
|
- self.original_call_source = copy(self.app.call_source)
|
|
|
- self.app.call_source = 'measurement'
|
|
|
+ # disconnect the mouse/key events from functions of measurement tool
|
|
|
+ self.canvas.vis_disconnect('mouse_move', self.on_mouse_move_meas)
|
|
|
+ self.canvas.vis_disconnect('mouse_release', self.on_mouse_click_release)
|
|
|
|
|
|
- self.app.inform.emit(_("MEASURING: Click on the Start point ..."))
|
|
|
- self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
|
|
|
+ # self.app.ui.notebook.setTabText(2, _("Tools"))
|
|
|
+ # self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
|
|
|
|
|
|
- self.activate_measure_tool()
|
|
|
- log.debug("Measurement Tool --> tool initialized")
|
|
|
- else:
|
|
|
- # DISABLE the Measuring TOOL
|
|
|
- self.deactivate_measure_tool()
|
|
|
- self.app.call_source = copy(self.original_call_source)
|
|
|
- self.app.command_active = None
|
|
|
+ self.app.command_active = None
|
|
|
|
|
|
- # delete the measuring line
|
|
|
- self.delete_shape()
|
|
|
+ # delete the measuring line
|
|
|
+ self.delete_shape()
|
|
|
|
|
|
- log.debug("Measurement Tool --> exit tool")
|
|
|
+ log.debug("Measurement Tool --> exit tool")
|
|
|
|
|
|
def on_mouse_click_release(self, event):
|
|
|
# mouse click releases will be accepted only if the left button is clicked
|
|
|
# this is necessary because right mouse click or middle mouse click
|
|
|
# are used for panning on the canvas
|
|
|
+ log.debug("Measuring Tool --> mouse click release")
|
|
|
|
|
|
if event.button == 1:
|
|
|
pos_canvas = self.canvas.vispy_canvas.translate_coords(event.pos)
|
|
|
+ # if GRID is active we need to get the snapped positions
|
|
|
+ if self.app.grid_status() == True:
|
|
|
+ pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
+ else:
|
|
|
+ pos = pos_canvas[0], pos_canvas[1]
|
|
|
+ self.points.append(pos)
|
|
|
|
|
|
- if self.clicked_meas == 0:
|
|
|
- self.clicked_meas = 1
|
|
|
-
|
|
|
- # if GRID is active we need to get the snapped positions
|
|
|
- if self.app.grid_status() == True:
|
|
|
- pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
- else:
|
|
|
- pos = pos_canvas[0], pos_canvas[1]
|
|
|
-
|
|
|
- self.point1 = pos
|
|
|
+ if len(self.points) == 1:
|
|
|
self.start_entry.set_value("(%.4f, %.4f)" % pos)
|
|
|
self.app.inform.emit(_("MEASURING: Click on the Destination point ..."))
|
|
|
|
|
|
- else:
|
|
|
- # delete the selection bounding box
|
|
|
- self.delete_shape()
|
|
|
-
|
|
|
- # if GRID is active we need to get the snapped positions
|
|
|
- if self.app.grid_status() is True:
|
|
|
- pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
- else:
|
|
|
- pos = pos_canvas[0], pos_canvas[1]
|
|
|
-
|
|
|
- dx = pos[0] - self.point1[0]
|
|
|
- dy = pos[1] - self.point1[1]
|
|
|
+ if len(self.points) == 2:
|
|
|
+ dx = self.points[1][0] - self.points[0][0]
|
|
|
+ dy = self.points[1][1] - self.points[0][1]
|
|
|
d = sqrt(dx ** 2 + dy ** 2)
|
|
|
|
|
|
self.stop_entry.set_value("(%.4f, %.4f)" % pos)
|
|
|
@@ -262,33 +268,25 @@ class Measurement(FlatCAMTool):
|
|
|
self.distance_y_entry.set_value('%.4f' % abs(dy))
|
|
|
self.total_distance_entry.set_value('%.4f' % abs(d))
|
|
|
|
|
|
- # TODO: I don't understand why I have to do it twice ... but without it the mouse handlers are
|
|
|
- # TODO: are left disconnected ...
|
|
|
- self.on_measure(activate=False)
|
|
|
- self.deactivate()
|
|
|
+ self.deactivate_measure_tool()
|
|
|
|
|
|
def on_mouse_move_meas(self, event):
|
|
|
pos_canvas = self.canvas.vispy_canvas.translate_coords(event.pos)
|
|
|
-
|
|
|
- # if GRID is active we need to get the snapped positions
|
|
|
+ # Update cursor
|
|
|
+ pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
if self.app.grid_status() == True:
|
|
|
- pos = self.app.geo_editor.snap(pos_canvas[0], pos_canvas[1])
|
|
|
- self.app.app_cursor.enabled = True
|
|
|
- # Update cursor
|
|
|
self.app.app_cursor.set_data(np.asarray([(pos[0], pos[1])]), symbol='++', edge_color='black', size=20)
|
|
|
- else:
|
|
|
- pos = pos_canvas
|
|
|
- self.app.app_enabled = False
|
|
|
-
|
|
|
- self.point2 = (pos[0], pos[1])
|
|
|
|
|
|
# update utility geometry
|
|
|
- if self.clicked_meas == 1:
|
|
|
- # first delete old shape
|
|
|
- self.delete_shape()
|
|
|
- # second draw the new shape of the utility geometry
|
|
|
- self.meas_line = LineString([self.point2, self.point1])
|
|
|
- self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
|
|
|
+ if len(self.points) == 1:
|
|
|
+ self.utility_geometry(pos=pos)
|
|
|
+
|
|
|
+ def utility_geometry(self, pos):
|
|
|
+ # first delete old shape
|
|
|
+ self.delete_shape()
|
|
|
+ # second draw the new shape of the utility geometry
|
|
|
+ self.meas_line = LineString([pos, self.points[0]])
|
|
|
+ self.sel_shapes.add(self.meas_line, color='black', update=True, layer=0, tolerance=None)
|
|
|
|
|
|
def delete_shape(self):
|
|
|
self.sel_shapes.clear()
|