ToolCalculators.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. self.decimals = 6
  26. # ## Title
  27. title_label = QtWidgets.QLabel("%s" % self.toolName)
  28. title_label.setStyleSheet("""
  29. QLabel
  30. {
  31. font-size: 16px;
  32. font-weight: bold;
  33. }
  34. """)
  35. self.layout.addWidget(title_label)
  36. # #####################
  37. # ## Units Calculator #
  38. # #####################
  39. self.unists_spacer_label = QtWidgets.QLabel(" ")
  40. self.layout.addWidget(self.unists_spacer_label)
  41. # ## Title of the Units Calculator
  42. units_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.unitsName)
  43. self.layout.addWidget(units_label)
  44. # Grid Layout
  45. grid_units_layout = QtWidgets.QGridLayout()
  46. self.layout.addLayout(grid_units_layout)
  47. inch_label = QtWidgets.QLabel(_("INCH"))
  48. mm_label = QtWidgets.QLabel(_("MM"))
  49. grid_units_layout.addWidget(mm_label, 0, 0)
  50. grid_units_layout.addWidget(inch_label, 0, 1)
  51. self.inch_entry = FCEntry()
  52. # self.inch_entry.setFixedWidth(70)
  53. # self.inch_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  54. self.inch_entry.setToolTip(_("Here you enter the value to be converted from INCH to MM"))
  55. self.mm_entry = FCEntry()
  56. # self.mm_entry.setFixedWidth(130)
  57. # self.mm_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  58. self.mm_entry.setToolTip(_("Here you enter the value to be converted from MM to INCH"))
  59. grid_units_layout.addWidget(self.mm_entry, 1, 0)
  60. grid_units_layout.addWidget(self.inch_entry, 1, 1)
  61. # ##############################
  62. # ## V-shape Tool Calculator ###
  63. # ##############################
  64. self.v_shape_spacer_label = QtWidgets.QLabel(" ")
  65. self.layout.addWidget(self.v_shape_spacer_label)
  66. # ## Title of the V-shape Tools Calculator
  67. v_shape_title_label = QtWidgets.QLabel("<font size=3><b>%s</b></font>" % self.v_shapeName)
  68. self.layout.addWidget(v_shape_title_label)
  69. # ## Form Layout
  70. form_layout = QtWidgets.QFormLayout()
  71. self.layout.addLayout(form_layout)
  72. self.tipDia_label = QtWidgets.QLabel('%s:' % _("Tip Diameter"))
  73. self.tipDia_entry = FCDoubleSpinner()
  74. self.tipDia_entry.set_precision(self.decimals)
  75. # self.tipDia_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  76. self.tipDia_label.setToolTip(
  77. _("This is the tool tip diameter.\n"
  78. "It is specified by manufacturer.")
  79. )
  80. self.tipAngle_label = QtWidgets.QLabel('%s:' % _("Tip Angle"))
  81. self.tipAngle_entry = FCSpinner()
  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 = FCDoubleSpinner()
  87. self.cutDepth_entry.setMinimum(-1e10) # to allow negative numbers without actually adding a real limit
  88. self.cutDepth_entry.set_precision(self.decimals)
  89. # self.cutDepth_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  90. self.cutDepth_label.setToolTip(_("This is the depth to cut into the material.\n"
  91. "In the CNCJob is the CutZ parameter."))
  92. self.effectiveToolDia_label = QtWidgets.QLabel('%s:' % _("Tool Diameter"))
  93. self.effectiveToolDia_entry = FCDoubleSpinner()
  94. self.effectiveToolDia_entry.set_precision(self.decimals)
  95. # self.effectiveToolDia_entry.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  96. self.effectiveToolDia_label.setToolTip(_("This is the tool diameter to be entered into\n"
  97. "FlatCAM Gerber section.\n"
  98. "In the CNCJob section it is called >Tool dia<."))
  99. # self.effectiveToolDia_entry.setEnabled(False)
  100. form_layout.addRow(self.tipDia_label, self.tipDia_entry)
  101. form_layout.addRow(self.tipAngle_label, self.tipAngle_entry)
  102. form_layout.addRow(self.cutDepth_label, self.cutDepth_entry)
  103. form_layout.addRow(self.effectiveToolDia_label, self.effectiveToolDia_entry)
  104. # ## Buttons
  105. self.calculate_vshape_button = QtWidgets.QPushButton(_("Calculate"))
  106. # self.calculate_button.setFixedWidth(70)
  107. self.calculate_vshape_button.setToolTip(
  108. _("Calculate either the Cut Z or the effective tool diameter,\n "
  109. "depending on which is desired and which is known. ")
  110. )
  111. self.layout.addWidget(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 = FCDoubleSpinner()
  129. self.pcblength_entry.set_precision(self.decimals)
  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 = FCDoubleSpinner()
  134. self.pcbwidth_entry.set_precision(self.decimals)
  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 = FCDoubleSpinner()
  139. self.cdensity_entry.set_precision(self.decimals)
  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 = FCDoubleSpinner()
  145. self.growth_entry.set_precision(self.decimals)
  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 = FCDoubleSpinner()
  152. self.cvalue_entry.set_precision(self.decimals)
  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.setReadOnly(True)
  157. self.timelabel = QtWidgets.QLabel('%s:' % _("Time"))
  158. self.time_entry = FCDoubleSpinner()
  159. self.time_entry.set_precision(self.decimals)
  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.setReadOnly(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.layout.addWidget(self.calculate_plate_button)
  178. self.layout.addStretch()
  179. self.units = ''
  180. # ## Signals
  181. self.cutDepth_entry.valueChanged.connect(self.on_calculate_tool_dia)
  182. self.cutDepth_entry.editingFinished.connect(self.on_calculate_tool_dia)
  183. self.tipDia_entry.editingFinished.connect(self.on_calculate_tool_dia)
  184. self.tipAngle_entry.editingFinished.connect(self.on_calculate_tool_dia)
  185. self.calculate_vshape_button.clicked.connect(self.on_calculate_tool_dia)
  186. self.mm_entry.editingFinished.connect(self.on_calculate_inch_units)
  187. self.inch_entry.editingFinished.connect(self.on_calculate_mm_units)
  188. self.calculate_plate_button.clicked.connect(self.on_calculate_eplate)
  189. def run(self, toggle=True):
  190. self.app.report_usage("ToolCalculators()")
  191. if toggle:
  192. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  193. if self.app.ui.splitter.sizes()[0] == 0:
  194. self.app.ui.splitter.setSizes([1, 1])
  195. else:
  196. try:
  197. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  198. # if tab is populated with the tool but it does not have the focus, focus on it
  199. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  200. # focus on Tool Tab
  201. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  202. else:
  203. self.app.ui.splitter.setSizes([0, 1])
  204. except AttributeError:
  205. pass
  206. else:
  207. if self.app.ui.splitter.sizes()[0] == 0:
  208. self.app.ui.splitter.setSizes([1, 1])
  209. FlatCAMTool.run(self)
  210. self.set_tool_ui()
  211. self.app.ui.notebook.setTabText(2, _("Calc. Tool"))
  212. def install(self, icon=None, separator=None, **kwargs):
  213. FlatCAMTool.install(self, icon, separator, shortcut='ALT+C', **kwargs)
  214. def set_tool_ui(self):
  215. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  216. # ## Initialize form
  217. self.mm_entry.set_value('%.*f' % (self.decimals, 0))
  218. self.inch_entry.set_value('%.*f' % (self.decimals, 0))
  219. length = self.app.defaults["tools_calc_electro_length"]
  220. width = self.app.defaults["tools_calc_electro_width"]
  221. density = self.app.defaults["tools_calc_electro_cdensity"]
  222. growth = self.app.defaults["tools_calc_electro_growth"]
  223. self.pcblength_entry.set_value(length)
  224. self.pcbwidth_entry.set_value(width)
  225. self.cdensity_entry.set_value(density)
  226. self.growth_entry.set_value(growth)
  227. self.cvalue_entry.set_value(0.00)
  228. self.time_entry.set_value(0.0)
  229. tip_dia = self.app.defaults["tools_calc_vshape_tip_dia"]
  230. tip_angle = self.app.defaults["tools_calc_vshape_tip_angle"]
  231. cut_z = self.app.defaults["tools_calc_vshape_cut_z"]
  232. self.tipDia_entry.set_value(tip_dia)
  233. self.tipAngle_entry.set_value(tip_angle)
  234. self.cutDepth_entry.set_value(cut_z)
  235. self.effectiveToolDia_entry.set_value('0.0000')
  236. def on_calculate_tool_dia(self):
  237. # Calculation:
  238. # Manufacturer gives total angle of the the tip but we need only half of it
  239. # tangent(half_tip_angle) = opposite side / adjacent = part_of _real_dia / depth_of_cut
  240. # effective_diameter = tip_diameter + part_of_real_dia_left_side + part_of_real_dia_right_side
  241. # tool is symmetrical therefore: part_of_real_dia_left_side = part_of_real_dia_right_side
  242. # effective_diameter = tip_diameter + (2 * part_of_real_dia_left_side)
  243. # effective diameter = tip_diameter + (2 * depth_of_cut * tangent(half_tip_angle))
  244. tip_diameter = float(self.tipDia_entry.get_value())
  245. half_tip_angle = float(self.tipAngle_entry.get_value())
  246. half_tip_angle /= 2
  247. cut_depth = float(self.cutDepth_entry.get_value())
  248. cut_depth = -cut_depth if cut_depth < 0 else cut_depth
  249. tool_diameter = tip_diameter + (2 * cut_depth * math.tan(math.radians(half_tip_angle)))
  250. self.effectiveToolDia_entry.set_value("%.*f" % (self.decimals, tool_diameter))
  251. def on_calculate_inch_units(self):
  252. mm_val = float(self.mm_entry.get_value())
  253. self.inch_entry.set_value('%.*f' % (self.decimals,(mm_val / 25.4)))
  254. def on_calculate_mm_units(self):
  255. inch_val = float(self.inch_entry.get_value())
  256. self.mm_entry.set_value('%.*f' % (self.decimals,(inch_val * 25.4)))
  257. def on_calculate_eplate(self):
  258. length = float(self.pcblength_entry.get_value())
  259. width = float(self.pcbwidth_entry.get_value())
  260. density = float(self.cdensity_entry.get_value())
  261. copper = float(self.growth_entry.get_value())
  262. calculated_current = (length * width * density) * 0.0021527820833419
  263. calculated_time = copper * 2.142857142857143 * float(20 / density)
  264. self.cvalue_entry.set_value('%.2f' % calculated_current)
  265. self.time_entry.set_value('%.1f' % calculated_time)
  266. # end of file