ToolCalculators.py 18 KB

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