ToolCalculators.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 FlatCAMTool import FlatCAMTool
  8. from FlatCAMObj import *
  9. import math
  10. import gettext
  11. import FlatCAMTranslation as fcTranslate
  12. import builtins
  13. fcTranslate.apply_language('strings')
  14. if '_' not in builtins.__dict__:
  15. _ = gettext.gettext
  16. class ToolCalculator(FlatCAMTool):
  17. toolName = _("Calculators")
  18. v_shapeName = _("V-Shape Tool Calculator")
  19. unitsName = _("Units Calculator")
  20. eplateName = _("ElectroPlating Calculator")
  21. def __init__(self, app):
  22. FlatCAMTool.__init__(self, app)
  23. self.app = app
  24. self.decimals = 6
  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 = FCDoubleSpinner()
  73. self.tipDia_entry.set_precision(self.decimals)
  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 = FCSpinner()
  81. # self.tipAngle_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  82. self.tipAngle_label.setToolTip(_("This is the angle of the tip of the tool.\n"
  83. "It is specified by manufacturer."))
  84. self.cutDepth_label = QtWidgets.QLabel('%s:' % _("Cut Z"))
  85. self.cutDepth_entry = FCDoubleSpinner()
  86. self.cutDepth_entry.setMinimum(-1e10) # to allow negative numbers without actually adding a real limit
  87. self.cutDepth_entry.set_precision(self.decimals)
  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 = FCDoubleSpinner()
  93. self.effectiveToolDia_entry.set_precision(self.decimals)
  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.layout.addWidget(self.calculate_vshape_button)
  111. # ####################################
  112. # ## ElectroPlating Tool Calculator ##
  113. # ####################################
  114. self.plate_spacer_label = QtWidgets.QLabel(" ")
  115. self.layout.addWidget(self.plate_spacer_label)
  116. # ## Title of the ElectroPlating Tools Calculator
  117. plate_title_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.eplateName)
  118. plate_title_label.setToolTip(
  119. _("This calculator is useful for those who plate the via/pad/drill holes,\n"
  120. "using a method like grahite ink or calcium hypophosphite ink or palladium chloride.")
  121. )
  122. self.layout.addWidget(plate_title_label)
  123. # ## Plate Form Layout
  124. plate_form_layout = QtWidgets.QFormLayout()
  125. self.layout.addLayout(plate_form_layout)
  126. self.pcblengthlabel = QtWidgets.QLabel('%s:' % _("Board Length"))
  127. self.pcblength_entry = FCDoubleSpinner()
  128. self.pcblength_entry.set_precision(self.decimals)
  129. # self.pcblength_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  130. self.pcblengthlabel.setToolTip(_('This is the board length. In centimeters.'))
  131. self.pcbwidthlabel = QtWidgets.QLabel('%s:' % _("Board Width"))
  132. self.pcbwidth_entry = FCDoubleSpinner()
  133. self.pcbwidth_entry.set_precision(self.decimals)
  134. # self.pcbwidth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  135. self.pcbwidthlabel.setToolTip(_('This is the board width.In centimeters.'))
  136. self.cdensity_label = QtWidgets.QLabel('%s:' % _("Current Density"))
  137. self.cdensity_entry = FCDoubleSpinner()
  138. self.cdensity_entry.set_precision(self.decimals)
  139. # self.cdensity_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  140. self.cdensity_label.setToolTip(_("Current density to pass through the board. \n"
  141. "In Amps per Square Feet ASF."))
  142. self.growth_label = QtWidgets.QLabel('%s:' % _("Copper Growth"))
  143. self.growth_entry = FCDoubleSpinner()
  144. self.growth_entry.set_precision(self.decimals)
  145. # self.growth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  146. self.growth_label.setToolTip(_("How thick the copper growth is intended to be.\n"
  147. "In microns."))
  148. # self.growth_entry.setEnabled(False)
  149. self.cvaluelabel = QtWidgets.QLabel('%s:' % _("Current Value"))
  150. self.cvalue_entry = FCDoubleSpinner()
  151. self.cvalue_entry.set_precision(self.decimals)
  152. # self.cvalue_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  153. self.cvaluelabel.setToolTip(_('This is the current intensity value\n'
  154. 'to be set on the Power Supply. In Amps.'))
  155. self.cvalue_entry.setReadOnly(True)
  156. self.timelabel = QtWidgets.QLabel('%s:' % _("Time"))
  157. self.time_entry = FCDoubleSpinner()
  158. self.time_entry.set_precision(self.decimals)
  159. # self.time_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  160. self.timelabel.setToolTip(_('This is the calculated time required for the procedure.\n'
  161. 'In minutes.'))
  162. self.time_entry.setReadOnly(True)
  163. plate_form_layout.addRow(self.pcblengthlabel, self.pcblength_entry)
  164. plate_form_layout.addRow(self.pcbwidthlabel, self.pcbwidth_entry)
  165. plate_form_layout.addRow(self.cdensity_label, self.cdensity_entry)
  166. plate_form_layout.addRow(self.growth_label, self.growth_entry)
  167. plate_form_layout.addRow(self.cvaluelabel, self.cvalue_entry)
  168. plate_form_layout.addRow(self.timelabel, self.time_entry)
  169. # ## Buttons
  170. self.calculate_plate_button = QtWidgets.QPushButton(_("Calculate"))
  171. # self.calculate_button.setFixedWidth(70)
  172. self.calculate_plate_button.setToolTip(
  173. _("Calculate the current intensity value and the procedure time,\n"
  174. "depending on the parameters above")
  175. )
  176. self.layout.addWidget(self.calculate_plate_button)
  177. self.layout.addStretch()
  178. self.units = ''
  179. # ## Signals
  180. self.cutDepth_entry.valueChanged.connect(self.on_calculate_tool_dia)
  181. self.cutDepth_entry.returnPressed.connect(self.on_calculate_tool_dia)
  182. self.tipDia_entry.returnPressed.connect(self.on_calculate_tool_dia)
  183. self.tipAngle_entry.returnPressed.connect(self.on_calculate_tool_dia)
  184. self.calculate_vshape_button.clicked.connect(self.on_calculate_tool_dia)
  185. self.mm_entry.editingFinished.connect(self.on_calculate_inch_units)
  186. self.inch_entry.editingFinished.connect(self.on_calculate_mm_units)
  187. self.calculate_plate_button.clicked.connect(self.on_calculate_eplate)
  188. def run(self, toggle=True):
  189. self.app.report_usage("ToolCalculators()")
  190. if toggle:
  191. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  192. if self.app.ui.splitter.sizes()[0] == 0:
  193. self.app.ui.splitter.setSizes([1, 1])
  194. else:
  195. try:
  196. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  197. # if tab is populated with the tool but it does not have the focus, focus on it
  198. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  199. # focus on Tool Tab
  200. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  201. else:
  202. self.app.ui.splitter.setSizes([0, 1])
  203. except AttributeError:
  204. pass
  205. else:
  206. if self.app.ui.splitter.sizes()[0] == 0:
  207. self.app.ui.splitter.setSizes([1, 1])
  208. FlatCAMTool.run(self)
  209. self.set_tool_ui()
  210. self.app.ui.notebook.setTabText(2, _("Calc. Tool"))
  211. def install(self, icon=None, separator=None, **kwargs):
  212. FlatCAMTool.install(self, icon, separator, shortcut='ALT+C', **kwargs)
  213. def set_tool_ui(self):
  214. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  215. # ## Initialize form
  216. self.mm_entry.set_value('%.*f' % (self.decimals, 0))
  217. self.inch_entry.set_value('%.*f' % (self.decimals, 0))
  218. length = self.app.defaults["tools_calc_electro_length"]
  219. width = self.app.defaults["tools_calc_electro_width"]
  220. density = self.app.defaults["tools_calc_electro_cdensity"]
  221. growth = self.app.defaults["tools_calc_electro_growth"]
  222. self.pcblength_entry.set_value(length)
  223. self.pcbwidth_entry.set_value(width)
  224. self.cdensity_entry.set_value(density)
  225. self.growth_entry.set_value(growth)
  226. self.cvalue_entry.set_value(0.00)
  227. self.time_entry.set_value(0.0)
  228. tip_dia = self.app.defaults["tools_calc_vshape_tip_dia"]
  229. tip_angle = self.app.defaults["tools_calc_vshape_tip_angle"]
  230. cut_z = self.app.defaults["tools_calc_vshape_cut_z"]
  231. self.tipDia_entry.set_value(tip_dia)
  232. self.tipAngle_entry.set_value(tip_angle)
  233. self.cutDepth_entry.set_value(cut_z)
  234. self.effectiveToolDia_entry.set_value('0.0000')
  235. def on_calculate_tool_dia(self):
  236. # Calculation:
  237. # Manufacturer gives total angle of the the tip but we need only half of it
  238. # tangent(half_tip_angle) = opposite side / adjacent = part_of _real_dia / depth_of_cut
  239. # effective_diameter = tip_diameter + part_of_real_dia_left_side + part_of_real_dia_right_side
  240. # tool is symmetrical therefore: part_of_real_dia_left_side = part_of_real_dia_right_side
  241. # effective_diameter = tip_diameter + (2 * part_of_real_dia_left_side)
  242. # effective diameter = tip_diameter + (2 * depth_of_cut * tangent(half_tip_angle))
  243. tip_diameter = float(self.tipDia_entry.get_value())
  244. half_tip_angle = float(self.tipAngle_entry.get_value())
  245. half_tip_angle /= 2
  246. cut_depth = float(self.cutDepth_entry.get_value())
  247. cut_depth = -cut_depth if cut_depth < 0 else cut_depth
  248. tool_diameter = tip_diameter + (2 * cut_depth * math.tan(math.radians(half_tip_angle)))
  249. self.effectiveToolDia_entry.set_value("%.*f" % (self.decimals, tool_diameter))
  250. def on_calculate_inch_units(self):
  251. mm_val = float(self.mm_entry.get_value())
  252. self.inch_entry.set_value('%.*f' % (self.decimals,(mm_val / 25.4)))
  253. def on_calculate_mm_units(self):
  254. inch_val = float(self.inch_entry.get_value())
  255. self.mm_entry.set_value('%.*f' % (self.decimals,(inch_val * 25.4)))
  256. def on_calculate_eplate(self):
  257. length = float(self.pcblength_entry.get_value())
  258. width = float(self.pcbwidth_entry.get_value())
  259. density = float(self.cdensity_entry.get_value())
  260. copper = float(self.growth_entry.get_value())
  261. calculated_current = (length * width * density) * 0.0021527820833419
  262. calculated_time = copper * 2.142857142857143 * float(20 / density)
  263. self.cvalue_entry.set_value('%.2f' % calculated_current)
  264. self.time_entry.set_value('%.1f' % calculated_time)
  265. # end of file