ToolDistanceMin.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 09/29/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.VisPyVisuals import *
  10. from flatcamGUI.GUIElements import FCEntry
  11. from shapely.ops import nearest_points
  12. from shapely.geometry import Point
  13. import math
  14. import logging
  15. import gettext
  16. import FlatCAMTranslation as fcTranslate
  17. import builtins
  18. fcTranslate.apply_language('strings')
  19. if '_' not in builtins.__dict__:
  20. _ = gettext.gettext
  21. log = logging.getLogger('base')
  22. class DistanceMin(FlatCAMTool):
  23. toolName = _("Minimum Distance Tool")
  24. def __init__(self, app):
  25. FlatCAMTool.__init__(self, app)
  26. self.app = app
  27. self.canvas = self.app.plotcanvas
  28. self.units = self.app.defaults['units'].lower()
  29. # ## Title
  30. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font><br>" % self.toolName)
  31. self.layout.addWidget(title_label)
  32. # ## Form Layout
  33. form_layout = QtWidgets.QFormLayout()
  34. self.layout.addLayout(form_layout)
  35. self.units_label = QtWidgets.QLabel('%s:' % _("Units"))
  36. self.units_label.setToolTip(_("Those are the units in which the distance is measured."))
  37. self.units_value = QtWidgets.QLabel("%s" % str({'mm': _("METRIC (mm)"), 'in': _("INCH (in)")}[self.units]))
  38. self.units_value.setDisabled(True)
  39. self.start_label = QtWidgets.QLabel("%s:" % _('First object point'))
  40. self.start_label.setToolTip(_("This is first object point coordinates.\n"
  41. "This is the start point for measuring distance."))
  42. self.stop_label = QtWidgets.QLabel("%s:" % _('Second object point'))
  43. self.stop_label.setToolTip(_("This is second object point coordinates.\n"
  44. "This is the end point for measuring distance."))
  45. self.distance_x_label = QtWidgets.QLabel('%s:' % _("Dx"))
  46. self.distance_x_label.setToolTip(_("This is the distance measured over the X axis."))
  47. self.distance_y_label = QtWidgets.QLabel('%s:' % _("Dy"))
  48. self.distance_y_label.setToolTip(_("This is the distance measured over the Y axis."))
  49. self.angle_label = QtWidgets.QLabel('%s:' % _("Angle"))
  50. self.angle_label.setToolTip(_("This is orientation angle of the measuring line."))
  51. self.total_distance_label = QtWidgets.QLabel("<b>%s:</b>" % _('DISTANCE'))
  52. self.total_distance_label.setToolTip(_("This is the point to point Euclidean distance."))
  53. self.half_point_label = QtWidgets.QLabel("<b>%s:</b>" % _('Half Point'))
  54. self.half_point_label.setToolTip(_("This is the middle point of the point to point Euclidean distance."))
  55. self.start_entry = FCEntry()
  56. self.start_entry.setReadOnly(True)
  57. self.start_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  58. self.start_entry.setToolTip(_("This is first object point coordinates.\n"
  59. "This is the start point for measuring distance."))
  60. self.stop_entry = FCEntry()
  61. self.stop_entry.setReadOnly(True)
  62. self.stop_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  63. self.stop_entry.setToolTip(_("This is second object point coordinates.\n"
  64. "This is the end point for measuring distance."))
  65. self.distance_x_entry = FCEntry()
  66. self.distance_x_entry.setReadOnly(True)
  67. self.distance_x_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  68. self.distance_x_entry.setToolTip(_("This is the distance measured over the X axis."))
  69. self.distance_y_entry = FCEntry()
  70. self.distance_y_entry.setReadOnly(True)
  71. self.distance_y_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  72. self.distance_y_entry.setToolTip(_("This is the distance measured over the Y axis."))
  73. self.angle_entry = FCEntry()
  74. self.angle_entry.setReadOnly(True)
  75. self.angle_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  76. self.angle_entry.setToolTip(_("This is orientation angle of the measuring line."))
  77. self.total_distance_entry = FCEntry()
  78. self.total_distance_entry.setReadOnly(True)
  79. self.total_distance_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  80. self.total_distance_entry.setToolTip(_("This is the point to point Euclidean distance."))
  81. self.half_point_entry = FCEntry()
  82. self.half_point_entry.setReadOnly(True)
  83. self.half_point_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  84. self.half_point_entry.setToolTip(_("This is the middle point of the point to point Euclidean distance."))
  85. self.measure_btn = QtWidgets.QPushButton(_("Measure"))
  86. self.layout.addWidget(self.measure_btn)
  87. self.jump_hp_btn = QtWidgets.QPushButton(_("Jump to Half Point"))
  88. self.layout.addWidget(self.jump_hp_btn)
  89. self.jump_hp_btn.setDisabled(True)
  90. form_layout.addRow(self.units_label, self.units_value)
  91. form_layout.addRow(self.start_label, self.start_entry)
  92. form_layout.addRow(self.stop_label, self.stop_entry)
  93. form_layout.addRow(self.distance_x_label, self.distance_x_entry)
  94. form_layout.addRow(self.distance_y_label, self.distance_y_entry)
  95. form_layout.addRow(self.angle_label, self.angle_entry)
  96. form_layout.addRow(self.total_distance_label, self.total_distance_entry)
  97. form_layout.addRow(self.half_point_label, self.half_point_entry)
  98. # initial view of the layout
  99. self.start_entry.set_value('(0, 0)')
  100. self.stop_entry.set_value('(0, 0)')
  101. self.distance_x_entry.set_value('0.0')
  102. self.distance_y_entry.set_value('0.0')
  103. self.angle_entry.set_value('0.0')
  104. self.total_distance_entry.set_value('0.0')
  105. self.half_point_entry.set_value('(0, 0)')
  106. self.layout.addStretch()
  107. self.decimals = 4
  108. self.h_point = (0, 0)
  109. self.measure_btn.clicked.connect(self.activate_measure_tool)
  110. self.jump_hp_btn.clicked.connect(self.on_jump_to_half_point)
  111. def run(self, toggle=False):
  112. self.app.report_usage("ToolDistanceMin()")
  113. if self.app.tool_tab_locked is True:
  114. return
  115. self.app.ui.notebook.setTabText(2, _("Minimum Distance Tool"))
  116. # if the splitter is hidden, display it
  117. if self.app.ui.splitter.sizes()[0] == 0:
  118. self.app.ui.splitter.setSizes([1, 1])
  119. if toggle:
  120. pass
  121. self.set_tool_ui()
  122. self.app.inform.emit('MEASURING: %s' %
  123. _("Select two objects and no more, to measure the distance between them ..."))
  124. def install(self, icon=None, separator=None, **kwargs):
  125. FlatCAMTool.install(self, icon, separator, shortcut='SHIFT+M', **kwargs)
  126. def set_tool_ui(self):
  127. # Remove anything else in the GUI
  128. self.app.ui.tool_scroll_area.takeWidget()
  129. # Put oneself in the GUI
  130. self.app.ui.tool_scroll_area.setWidget(self)
  131. # Switch notebook to tool page
  132. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  133. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  134. # initial view of the layout
  135. self.start_entry.set_value('(0, 0)')
  136. self.stop_entry.set_value('(0, 0)')
  137. self.distance_x_entry.set_value('0.0')
  138. self.distance_y_entry.set_value('0.0')
  139. self.angle_entry.set_value('0.0')
  140. self.total_distance_entry.set_value('0.0')
  141. self.half_point_entry.set_value('(0, 0)')
  142. self.jump_hp_btn.setDisabled(True)
  143. log.debug("Minimum Distance Tool --> tool initialized")
  144. def activate_measure_tool(self):
  145. # ENABLE the Measuring TOOL
  146. self.jump_hp_btn.setDisabled(False)
  147. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower()
  148. if self.app.call_source == 'app':
  149. selected_objs = self.app.collection.get_selected()
  150. if len(selected_objs) != 2:
  151. self.app.inform.emit('[WARNING_NOTCL] %s %s' %
  152. (_("Select two objects and no more. Currently the selection has objects: "),
  153. str(len(selected_objs))))
  154. return
  155. else:
  156. first_pos, last_pos = nearest_points(selected_objs[0].solid_geometry, selected_objs[1].solid_geometry)
  157. elif self.app.call_source == 'geo_editor':
  158. selected_objs = self.app.geo_editor.selected
  159. if len(selected_objs) != 2:
  160. self.app.inform.emit('[WARNING_NOTCL] %s %s' %
  161. (_("Select two objects and no more. Currently the selection has objects: "),
  162. str(len(selected_objs))))
  163. return
  164. else:
  165. first_pos, last_pos = nearest_points(selected_objs[0].geo, selected_objs[1].geo)
  166. elif self.app.call_source == 'exc_editor':
  167. selected_objs = self.app.exc_editor.selected
  168. if len(selected_objs) != 2:
  169. self.app.inform.emit('[WARNING_NOTCL] %s %s' %
  170. (_("Select two objects and no more. Currently the selection has objects: "),
  171. str(len(selected_objs))))
  172. return
  173. else:
  174. # the objects are really MultiLinesStrings made out of 2 lines in cross shape
  175. xmin, ymin, xmax, ymax = selected_objs[0].geo.bounds
  176. first_geo_radius = (xmax - xmin) / 2
  177. first_geo_center = Point(xmin + first_geo_radius, ymin + first_geo_radius)
  178. first_geo = first_geo_center.buffer(first_geo_radius)
  179. # the objects are really MultiLinesStrings made out of 2 lines in cross shape
  180. xmin, ymin, xmax, ymax = selected_objs[1].geo.bounds
  181. last_geo_radius = (xmax - xmin) / 2
  182. last_geo_center = Point(xmin + last_geo_radius, ymin + last_geo_radius)
  183. last_geo = last_geo_center.buffer(last_geo_radius)
  184. first_pos, last_pos = nearest_points(first_geo, last_geo)
  185. elif self.app.call_source == 'grb_editor':
  186. selected_objs = self.app.grb_editor.selected
  187. if len(selected_objs) != 2:
  188. self.app.inform.emit('[WARNING_NOTCL] %s %s' %
  189. (_("Select two objects and no more. Currently the selection has objects: "),
  190. str(len(selected_objs))))
  191. return
  192. else:
  193. first_pos, last_pos = nearest_points(selected_objs[0].geo['solid'], selected_objs[1].geo['solid'])
  194. else:
  195. first_pos, last_pos = 0, 0
  196. self.start_entry.set_value("(%.*f, %.*f)" % (self.decimals, first_pos.x, self.decimals, first_pos.y))
  197. self.stop_entry.set_value("(%.*f, %.*f)" % (self.decimals, last_pos.x, self.decimals, last_pos.y))
  198. dx = first_pos.x - last_pos.x
  199. dy = first_pos.y - last_pos.y
  200. self.distance_x_entry.set_value('%.*f' % (self.decimals, abs(dx)))
  201. self.distance_y_entry.set_value('%.*f' % (self.decimals, abs(dy)))
  202. try:
  203. angle = math.degrees(math.atan(dy / dx))
  204. self.angle_entry.set_value('%.*f' % (self.decimals, angle))
  205. except Exception as e:
  206. pass
  207. d = math.sqrt(dx ** 2 + dy ** 2)
  208. self.total_distance_entry.set_value('%.*f' % (self.decimals, abs(d)))
  209. self.h_point = (min(first_pos.x, last_pos.x) + (abs(dx) / 2), min(first_pos.y, last_pos.y) + (abs(dy) / 2))
  210. if d != 0:
  211. self.half_point_entry.set_value(
  212. "(%.*f, %.*f)" % (self.decimals, self.h_point[0], self.decimals, self.h_point[1])
  213. )
  214. else:
  215. self.half_point_entry.set_value(
  216. "(%.*f, %.*f)" % (self.decimals, 0.0, self.decimals, 0.0)
  217. )
  218. if d != 0:
  219. self.app.inform.emit(_("MEASURING: Result D(x) = {d_x} | D(y) = {d_y} | Distance = {d_z}").format(
  220. d_x='%*f' % (self.decimals, abs(dx)),
  221. d_y='%*f' % (self.decimals, abs(dy)),
  222. d_z='%*f' % (self.decimals, abs(d)))
  223. )
  224. else:
  225. self.app.inform.emit('[WARNING_NOTCL] %s: %s' %
  226. (_("Objects intersects or touch at"),
  227. "(%.*f, %.*f)" % (self.decimals, self.h_point[0], self.decimals, self.h_point[1])))
  228. def on_jump_to_half_point(self):
  229. self.app.on_jump_to(custom_location=self.h_point)
  230. self.app.inform.emit('[success] %s: %s' %
  231. (_("Jumped to the half point between the two selected objects"),
  232. "(%.*f, %.*f)" % (self.decimals, self.h_point[0], self.decimals, self.h_point[1])))
  233. def set_meas_units(self, units):
  234. self.meas.units_label.setText("[" + self.app.options["units"].lower() + "]")
  235. # end of file