ToolDistanceMin.py 13 KB

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