ToolDistanceMin.py 13 KB

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