ToolCalculators.py 18 KB

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