ToolDistanceMin.py 12 KB

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