ToolCalculators.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. from PyQt5 import QtGui
  2. from GUIElements import FCEntry
  3. from FlatCAMTool import FlatCAMTool
  4. from FlatCAMObj import *
  5. import math
  6. import gettext
  7. import FlatCAMTranslation as fcTranslate
  8. fcTranslate.apply_language('ToolCalculators')
  9. class ToolCalculator(FlatCAMTool):
  10. toolName = _("Calculators")
  11. v_shapeName = _("V-Shape Tool Calculator")
  12. unitsName = _("Units Calculator")
  13. eplateName = _("ElectroPlating Calculator")
  14. def __init__(self, app):
  15. FlatCAMTool.__init__(self, app)
  16. self.app = app
  17. ## Title
  18. title_label = QtWidgets.QLabel("%s" % self.toolName)
  19. title_label.setStyleSheet("""
  20. QLabel
  21. {
  22. font-size: 16px;
  23. font-weight: bold;
  24. }
  25. """)
  26. self.layout.addWidget(title_label)
  27. ######################
  28. ## Units Calculator ##
  29. ######################
  30. self.unists_spacer_label = QtWidgets.QLabel(" ")
  31. self.layout.addWidget(self.unists_spacer_label)
  32. ## Title of the Units Calculator
  33. units_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.unitsName)
  34. self.layout.addWidget(units_label)
  35. #Grid Layout
  36. grid_units_layout = QtWidgets.QGridLayout()
  37. self.layout.addLayout(grid_units_layout)
  38. inch_label = QtWidgets.QLabel("INCH")
  39. mm_label = QtWidgets.QLabel("MM")
  40. grid_units_layout.addWidget(mm_label, 0, 0)
  41. grid_units_layout.addWidget( inch_label, 0, 1)
  42. self.inch_entry = FCEntry()
  43. # self.inch_entry.setFixedWidth(70)
  44. self.inch_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  45. self.inch_entry.setToolTip(_("Here you enter the value to be converted from INCH to MM"))
  46. self.mm_entry = FCEntry()
  47. # self.mm_entry.setFixedWidth(130)
  48. self.mm_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  49. self.mm_entry.setToolTip(_("Here you enter the value to be converted from MM to INCH"))
  50. grid_units_layout.addWidget(self.mm_entry, 1, 0)
  51. grid_units_layout.addWidget(self.inch_entry, 1, 1)
  52. ############################
  53. ## V-shape Tool Calculator ##
  54. ############################
  55. self.v_shape_spacer_label = QtWidgets.QLabel(" ")
  56. self.layout.addWidget(self.v_shape_spacer_label)
  57. ## Title of the V-shape Tools Calculator
  58. v_shape_title_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.v_shapeName)
  59. self.layout.addWidget(v_shape_title_label)
  60. ## Form Layout
  61. form_layout = QtWidgets.QFormLayout()
  62. self.layout.addLayout(form_layout)
  63. self.tipDia_label = QtWidgets.QLabel(_("Tip Diameter:"))
  64. self.tipDia_entry = FCEntry()
  65. # self.tipDia_entry.setFixedWidth(70)
  66. self.tipDia_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  67. self.tipDia_label.setToolTip(_('This is the diameter of the tool tip.\n'
  68. 'The manufacturer specifies it.'))
  69. self.tipAngle_label = QtWidgets.QLabel("Tip Angle:")
  70. self.tipAngle_entry = FCEntry()
  71. # self.tipAngle_entry.setFixedWidth(70)
  72. self.tipAngle_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  73. self.tipAngle_label.setToolTip(_("This is the angle of the tip of the tool.\n"
  74. "It is specified by manufacturer."))
  75. self.cutDepth_label = QtWidgets.QLabel("Cut Z:")
  76. self.cutDepth_entry = FCEntry()
  77. # self.cutDepth_entry.setFixedWidth(70)
  78. self.cutDepth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  79. self.cutDepth_label.setToolTip(_("This is the depth to cut into the material.\n"
  80. "In the CNCJob is the CutZ parameter."))
  81. self.effectiveToolDia_label = QtWidgets.QLabel("Tool Diameter:")
  82. self.effectiveToolDia_entry = FCEntry()
  83. # self.effectiveToolDia_entry.setFixedWidth(70)
  84. self.effectiveToolDia_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  85. self.effectiveToolDia_label.setToolTip(_("This is the tool diameter to be entered into\n"
  86. "FlatCAM Gerber section.\n"
  87. "In the CNCJob section it is called >Tool dia<."))
  88. # self.effectiveToolDia_entry.setEnabled(False)
  89. form_layout.addRow(self.tipDia_label, self.tipDia_entry)
  90. form_layout.addRow(self.tipAngle_label, self.tipAngle_entry)
  91. form_layout.addRow(self.cutDepth_label, self.cutDepth_entry)
  92. form_layout.addRow(self.effectiveToolDia_label, self.effectiveToolDia_entry)
  93. ## Buttons
  94. self.calculate_vshape_button = QtWidgets.QPushButton(_("Calculate"))
  95. # self.calculate_button.setFixedWidth(70)
  96. self.calculate_vshape_button.setToolTip(
  97. _("Calculate either the Cut Z or the effective tool diameter,\n "
  98. "depending on which is desired and which is known. ")
  99. )
  100. self.empty_label = QtWidgets.QLabel(" ")
  101. form_layout.addRow(self.empty_label, self.calculate_vshape_button)
  102. ####################################
  103. ## ElectroPlating Tool Calculator ##
  104. ####################################
  105. self.plate_spacer_label = QtWidgets.QLabel(" ")
  106. self.layout.addWidget(self.plate_spacer_label)
  107. ## Title of the ElectroPlating Tools Calculator
  108. plate_title_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.eplateName)
  109. plate_title_label.setToolTip(
  110. _("This calculator is useful for those who plate the via/pad/drill holes,\n"
  111. "using a method like grahite ink or calcium hypophosphite ink or palladium chloride.")
  112. )
  113. self.layout.addWidget(plate_title_label)
  114. ## Plate Form Layout
  115. plate_form_layout = QtWidgets.QFormLayout()
  116. self.layout.addLayout(plate_form_layout)
  117. self.pcblengthlabel = QtWidgets.QLabel(_("Board Length:"))
  118. self.pcblength_entry = FCEntry()
  119. # self.pcblengthlabel.setFixedWidth(70)
  120. self.pcblength_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  121. self.pcblengthlabel.setToolTip(_('This is the board length. In centimeters.'))
  122. self.pcbwidthlabel = QtWidgets.QLabel(_("Board Width:"))
  123. self.pcbwidth_entry = FCEntry()
  124. # self.pcbwidthlabel.setFixedWidth(70)
  125. self.pcbwidth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  126. self.pcbwidthlabel.setToolTip(_('This is the board width.In centimeters.'))
  127. self.cdensity_label = QtWidgets.QLabel(_("Current Density:"))
  128. self.cdensity_entry = FCEntry()
  129. # self.cdensity_entry.setFixedWidth(70)
  130. self.cdensity_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  131. self.cdensity_label.setToolTip(_("Current density to pass through the board. \n"
  132. "In Amps per Square Feet ASF."))
  133. self.growth_label = QtWidgets.QLabel(_("Copper Growth:"))
  134. self.growth_entry = FCEntry()
  135. # self.growth_entry.setFixedWidth(70)
  136. self.growth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  137. self.growth_label.setToolTip(_("How thick the copper growth is intended to be.\n"
  138. "In microns."))
  139. # self.growth_entry.setEnabled(False)
  140. self.cvaluelabel = QtWidgets.QLabel(_("Current Value:"))
  141. self.cvalue_entry = FCEntry()
  142. # self.cvaluelabel.setFixedWidth(70)
  143. self.cvalue_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  144. self.cvaluelabel.setToolTip(_('This is the current intensity value\n'
  145. 'to be set on the Power Supply. In Amps.'))
  146. self.cvalue_entry.setDisabled(True)
  147. self.timelabel = QtWidgets.QLabel(_("Time:"))
  148. self.time_entry = FCEntry()
  149. # self.timelabel.setFixedWidth(70)
  150. self.time_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  151. self.timelabel.setToolTip(_('This is the calculated time required for the procedure.\n'
  152. 'In minutes.'))
  153. self.time_entry.setDisabled(True)
  154. plate_form_layout.addRow(self.pcblengthlabel, self.pcblength_entry)
  155. plate_form_layout.addRow(self.pcbwidthlabel, self.pcbwidth_entry)
  156. plate_form_layout.addRow(self.cdensity_label, self.cdensity_entry)
  157. plate_form_layout.addRow(self.growth_label, self.growth_entry)
  158. plate_form_layout.addRow(self.cvaluelabel, self.cvalue_entry)
  159. plate_form_layout.addRow(self.timelabel, self.time_entry)
  160. ## Buttons
  161. self.calculate_plate_button = QtWidgets.QPushButton(_("Calculate"))
  162. # self.calculate_button.setFixedWidth(70)
  163. self.calculate_plate_button.setToolTip(
  164. _("Calculate the current intensity value and the procedure time,\n "
  165. "depending on the parameters above")
  166. )
  167. self.empty_label_2 = QtWidgets.QLabel(" ")
  168. plate_form_layout.addRow(self.empty_label_2, self.calculate_plate_button)
  169. self.layout.addStretch()
  170. ## Signals
  171. self.cutDepth_entry.textChanged.connect(self.on_calculate_tool_dia)
  172. self.cutDepth_entry.editingFinished.connect(self.on_calculate_tool_dia)
  173. self.tipDia_entry.editingFinished.connect(self.on_calculate_tool_dia)
  174. self.tipAngle_entry.editingFinished.connect(self.on_calculate_tool_dia)
  175. self.calculate_vshape_button.clicked.connect(self.on_calculate_tool_dia)
  176. self.mm_entry.editingFinished.connect(self.on_calculate_inch_units)
  177. self.inch_entry.editingFinished.connect(self.on_calculate_mm_units)
  178. self.calculate_plate_button.clicked.connect(self.on_calculate_eplate)
  179. def run(self, toggle=False):
  180. self.app.report_usage("ToolCalculators()")
  181. if toggle:
  182. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  183. if self.app.ui.splitter.sizes()[0] == 0:
  184. self.app.ui.splitter.setSizes([1, 1])
  185. else:
  186. try:
  187. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  188. self.app.ui.splitter.setSizes([0, 1])
  189. except AttributeError:
  190. pass
  191. FlatCAMTool.run(self)
  192. self.set_tool_ui()
  193. self.app.ui.notebook.setTabText(2, "Calc. Tool")
  194. def install(self, icon=None, separator=None, **kwargs):
  195. FlatCAMTool.install(self, icon, separator, shortcut='ALT+C', **kwargs)
  196. def set_tool_ui(self):
  197. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  198. ## Initialize form
  199. self.mm_entry.set_value('0')
  200. self.inch_entry.set_value('0')
  201. length = self.app.defaults["tools_calc_electro_length"]
  202. width = self.app.defaults["tools_calc_electro_width"]
  203. density = self.app.defaults["tools_calc_electro_cdensity"]
  204. growth = self.app.defaults["tools_calc_electro_growth"]
  205. self.pcblength_entry.set_value(length)
  206. self.pcbwidth_entry.set_value(width)
  207. self.cdensity_entry.set_value(density)
  208. self.growth_entry.set_value(growth)
  209. self.cvalue_entry.set_value(0.00)
  210. self.time_entry.set_value(0.0)
  211. tip_dia = self.app.defaults["tools_calc_vshape_tip_dia"]
  212. tip_angle = self.app.defaults["tools_calc_vshape_tip_angle"]
  213. cut_z = self.app.defaults["tools_calc_vshape_cut_z"]
  214. self.tipDia_entry.set_value(tip_dia)
  215. self.tipAngle_entry.set_value(tip_angle)
  216. self.cutDepth_entry.set_value(cut_z)
  217. self.effectiveToolDia_entry.set_value('0.0000')
  218. def on_calculate_tool_dia(self):
  219. # Calculation:
  220. # Manufacturer gives total angle of the the tip but we need only half of it
  221. # tangent(half_tip_angle) = opposite side / adjacent = part_of _real_dia / depth_of_cut
  222. # effective_diameter = tip_diameter + part_of_real_dia_left_side + part_of_real_dia_right_side
  223. # tool is symmetrical therefore: part_of_real_dia_left_side = part_of_real_dia_right_side
  224. # effective_diameter = tip_diameter + (2 * part_of_real_dia_left_side)
  225. # effective diameter = tip_diameter + (2 * depth_of_cut * tangent(half_tip_angle))
  226. try:
  227. tip_diameter = float(self.tipDia_entry.get_value())
  228. except ValueError:
  229. # try to convert comma to decimal point. if it's still not working error message and return
  230. try:
  231. tip_diameter = float(self.tipDia_entry.get_value().replace(',', '.'))
  232. except ValueError:
  233. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  234. "use a number."))
  235. return
  236. try:
  237. half_tip_angle = float(self.tipAngle_entry.get_value())
  238. except ValueError:
  239. # try to convert comma to decimal point. if it's still not working error message and return
  240. try:
  241. half_tip_angle = float(self.tipAngle_entry.get_value().replace(',', '.'))
  242. except ValueError:
  243. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  244. "use a number."))
  245. return
  246. half_tip_angle /= 2
  247. try:
  248. cut_depth = float(self.cutDepth_entry.get_value())
  249. except ValueError:
  250. # try to convert comma to decimal point. if it's still not working error message and return
  251. try:
  252. cut_depth = float(self.cutDepth_entry.get_value().replace(',', '.'))
  253. except ValueError:
  254. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  255. "use a number."))
  256. return
  257. tool_diameter = tip_diameter + (2 * cut_depth * math.tan(math.radians(half_tip_angle)))
  258. self.effectiveToolDia_entry.set_value("%.4f" % tool_diameter)
  259. def on_calculate_inch_units(self):
  260. try:
  261. mm_val = float(self.mm_entry.get_value())
  262. except ValueError:
  263. # try to convert comma to decimal point. if it's still not working error message and return
  264. try:
  265. mm_val = float(self.mm_entry.get_value().replace(',', '.'))
  266. except ValueError:
  267. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  268. "use a number."))
  269. return
  270. self.inch_entry.set_value('%.6f' % (mm_val / 25.4))
  271. def on_calculate_mm_units(self):
  272. try:
  273. inch_val = float(self.inch_entry.get_value())
  274. except ValueError:
  275. # try to convert comma to decimal point. if it's still not working error message and return
  276. try:
  277. inch_val = float(self.inch_entry.get_value().replace(',', '.'))
  278. except ValueError:
  279. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  280. "use a number."))
  281. return
  282. self.mm_entry.set_value('%.6f' % (inch_val * 25.4))
  283. def on_calculate_eplate(self):
  284. try:
  285. length = float(self.pcblength_entry.get_value())
  286. except ValueError:
  287. # try to convert comma to decimal point. if it's still not working error message and return
  288. try:
  289. length = float(self.pcblength_entry.get_value().replace(',', '.'))
  290. except ValueError:
  291. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  292. "use a number."))
  293. return
  294. try:
  295. width = float(self.pcbwidth_entry.get_value())
  296. except ValueError:
  297. # try to convert comma to decimal point. if it's still not working error message and return
  298. try:
  299. width = float(self.pcbwidth_entry.get_value().replace(',', '.'))
  300. except ValueError:
  301. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  302. "use a number."))
  303. return
  304. try:
  305. density = float(self.cdensity_entry.get_value())
  306. except ValueError:
  307. # try to convert comma to decimal point. if it's still not working error message and return
  308. try:
  309. density = float(self.cdensity_entry.get_value().replace(',', '.'))
  310. except ValueError:
  311. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  312. "use a number."))
  313. return
  314. try:
  315. copper = float(self.growth_entry.get_value())
  316. except ValueError:
  317. # try to convert comma to decimal point. if it's still not working error message and return
  318. try:
  319. copper = float(self.growth_entry.get_value().replace(',', '.'))
  320. except ValueError:
  321. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  322. "use a number."))
  323. return
  324. calculated_current = (length * width * density) * 0.0021527820833419
  325. calculated_time = copper * 2.142857142857143 * float(20 / density)
  326. self.cvalue_entry.set_value('%.2f' % calculated_current)
  327. self.time_entry.set_value('%.1f' % calculated_time)
  328. # end of file