ToolCalculators.py 18 KB

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