-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCAMFeedsAndSpeedsGui.py
292 lines (237 loc) · 10.5 KB
/
CAMFeedsAndSpeedsGui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Feed and Speed Calculator
# Provides a basic feeds and speeds calculator for use with FreeCAD Path
import os
import re
from PySide import QtGui
import FreeCAD
import FreeCADGui
import Path
from importFCMat import read
import MaterialEditor
import CAMFeedsAndSpeeds
dir = os.path.dirname(__file__)
ui_name = "CAMFeedsAndSpeedsGui.ui"
path_to_ui = os.path.join(dir, ui_name)
material_dir = os.path.join(dir, 'Materials')
iconPath = os.path.join(dir, 'Icons')
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Resources")
class FeedSpeedPanel():
def __init__(self):
# Build GUI
self.form = FreeCADGui.PySideUic.loadUi(path_to_ui)
self.customMaterialDir = "" # store the original custom materials directory
self.materials = [] # materials loaded from FreeCAD Materials
self.material = {} # selected material
# Init
self.calculation = CAMFeedsAndSpeeds.FSCalculation()
self.load_materials()
self.setup_ui()
self.calculate()
# connect
self.form.material_CB.currentIndexChanged.connect(self.set_material)
self.form.hss_RB.toggled.connect(self.set_tool_material)
self.form.cbd_RB.toggled.connect(self.set_tool_material)
self.form.flutes_SB.valueChanged.connect(self.calculate)
self.form.FPT_SB.valueChanged.connect(self.calculate)
self.form.WOC_SP.textChanged.connect(self.calculate)
self.form.DOC_SP.textChanged.connect(self.calculate)
self.form.ss_LE.textChanged.connect(self.calculate)
self.form.rpm_LE.textChanged.connect(self.calculate)
self.form.toolController_CB.currentIndexChanged.connect(self.load_tool_properties)
self.form.update_PB.clicked.connect(self.update_tool_controller)
self.form.close_PB.clicked.connect(self.quit)
self.form.material_editor_PB.clicked.connect(self.show_material_editor)
def setup_ui(self):
"""setup the user interface"""
# load widget data
self.load_material_combobox()
self.set_tool_properties()
self.form.material_editor_PB.setIcon(QtGui.QIcon(os.path.join(iconPath, "Material.svg")))
# set input validation
self.onlyInt = QtGui.QIntValidator()
self.onlyInt.setBottom(1)
self.form.ss_LE.setValidator(self.onlyInt)
self.form.rpm_LE.setValidator(self.onlyInt)
self.form.WOC_SP.setProperty("minimum", 0.0)
self.form.DOC_SP.setProperty("minimum", 0.0)
self.load_tools()
self.load_tool_properties()
self.set_tool_material()
self.set_material()
def load_materials(self):
"""load all materials that contain the required properties"""
for file in os.listdir(material_dir):
if file.endswith(".FCMat"):
material_card = read(os.path.join(material_dir, file))
if self.is_path_material(material_card):
self.materials.append(material_card)
# sort the material list alphabetically
self.materials = sorted(self.materials, key=lambda d: d['Name'])
def load_material_combobox(self):
"""display materials on the form"""
self.form.material_CB.clear()
for material in (d['Name'] for d in self.materials):
self.form.material_CB.addItem(material)
def show_material_editor(self):
"""load the FreeCAD material editor"""
new_material = MaterialEditor.editMaterial(material=self.material)
if new_material:
if self.is_path_material(new_material):
# clear all materials and load just the new material
self.materials = []
self.materials.append(new_material)
self.load_material_combobox()
else:
QtGui.QMessageBox.warning(FreeCADGui.getMainWindow(), "Warning", "Material is missing path parameters")
def set_tool_properties(self, dia=6, flutes=2, chipload=None, material="HSS"):
"""set the tool properties for the selected tool"""
self.form.toolDia_LE.setText(str(dia))
self.form.flutes_SB.setValue(flutes)
if chipload is None or chipload == 0:
chipload = dia * 0.005
self.form.FPT_SB.setValue(chipload)
self.form.WOC_SP.setText(str(round(dia * 0.2, 2)))
self.form.WOC_SP.setProperty("maximum", float(dia))
self.form.DOC_SP.setText(str(dia))
if material == "HSS":
self.form.hss_RB.setChecked(True)
elif material == "Carbide":
self.form.cbd_RB.setChecked(True)
def is_path_material(self, material_card):
"""check the material contains the path properties"""
#TODO: Add additional props
if 'SurfaceSpeed_HSS' in material_card:
return True
return False
def set_material(self):
"""set the material properties"""
if len(self.materials) and self.form.material_CB.count():
self.material = self.materials[self.form.material_CB.currentIndex()]
self.calculation.set_material(self.material)
self.set_surface_speed()
self.calculate()
def set_surface_speed(self):
"""set the surface speed for the selected material and tool"""
ss = self.material.get("SurfaceSpeed_" + self.tool_material)
self.form.ss_LE.setText(str(ss))
def set_tool_material(self):
"""set the tool material"""
self.tool_material = "HSS" if self.form.hss_RB.isChecked() else "Carbide"
self.set_surface_speed()
self.calculate()
def load_tools(self):
"""load the tools for all jobs in the current document"""
jobs = FreeCAD.ActiveDocument.findObjects("Path::FeaturePython", "Job.*")
for job in jobs:
for idx, tc in enumerate(job.Tools.Group):
self.form.toolController_CB.addItem(tc.Label)
def load_tool_properties(self):
"""load the properties from the selected tool"""
tc = self.get_tool_controller()
if tc:
tool = tc.Tool
dia = tool.Diameter
# Hacky way to check for legacy tools, remove this after the release of 0.21
if not hasattr(tool, "BitShape"):
FreeCAD.Console.PrintError("Legacy Tools Not Supported: " + tool.Name + "\n")
self.set_tool_properties(dia)
return
flutes = tool.Flutes
material = tool.Material
chipload = tool.Chipload
self.set_tool_properties(dia, flutes, chipload, material)
def get_tool_controller(self):
"""get the tool controller"""
jobs = FreeCAD.ActiveDocument.findObjects("Path::FeaturePython", "Job.*")
tcStr = self.form.toolController_CB.currentText()
for job in jobs:
for tc in job.Tools.Group:
if tc.Label == tcStr:
return tc
return None
def update_tool_controller(self):
"""write the calculated parameters to the selected tool controller"""
tc = self.get_tool_controller()
if tc:
rpm = re.sub("[^0-9.]", "", self.form.rpm_result.text())
hfeed = self.form.hfeed_result.text()
vfeed = self.form.vfeed_result.text()
# TODO: Add a confirmation dialog
tc.HorizFeed = hfeed
tc.VertFeed = vfeed
tc.SpindleSpeed = float(rpm)
def validate_input(self):
""" validate the user input"""
if self.form.WOC_SP.text() == "":
return False
if self.form.DOC_SP.text() == "":
return False
if self.form.ss_LE.text() == "":
return False
return True
def calculate(self):
"""perform the feeds and speeds calculation"""
if self.calculation.material is None:
return
if not self.validate_input():
return
tool = CAMFeedsAndSpeeds.Tool()
self.calculation.rpm_overide = self.form.rpm_LE.text()
surfaceSpeed = float(self.form.ss_LE.text())
if not self.form.rpm_LE.text() == "":
self.form.rpm_result.setEnabled(False)
else:
self.form.rpm_result.setEnabled(True)
tool.toolDia = FreeCAD.Units.Quantity(self.form.toolDia_LE.text())
tool.flutes = self.form.flutes_SB.value()
self.calculation.feedPerTooth = float(self.form.FPT_SB.value())
self.calculation.WOC = FreeCAD.Units.Quantity(self.form.WOC_SP.text())
self.calculation.DOC = FreeCAD.Units.Quantity(self.form.DOC_SP.text())
self.calculation.toolWear = 1.1 # Tool Wear pg: 1048
if not self.calculation.feedPerTooth:
return
rpm, hfeed, vfeed, Hp = self.calculation.calculate(tool, surfaceSpeed)
self.form.rpm_result.setText(str(rpm))
self.form.rpm_result.setText(str(rpm) + " rpm")
self.form.hfeed_result.setText(str(hfeed) + " mm/min")
self.form.vfeed_result.setText(str(vfeed) + " mm/min")
self.form.hp_result.setText("-")
if Hp is not None:
watts = Hp * 745.69
self.form.hp_result.setText(str(round(Hp, 2)) + " hp / " + str(round(watts, 2)) + " watts")
def show(self):
"""show the form"""
# get the current custom materials directory
self.customMaterialDir = prefs.GetString("CustomMaterialsDir", "")
# set the custom materials directory to the supplied materials directory
prefs.SetString("CustomMaterialsDir", material_dir)
# show the form using the builtin exec_() function
self.form.exec_()
def reject(self):
"""handle reject calls"""
FreeCAD.Console.PrintMessage("Reject Signal")
self.quit()
def accept(self):
"""handle accept calls"""
self.quit()
def quit(self):
"""handle quit calls, close the form"""
# restore the custom materials directory
prefs.SetString("CustomMaterialsDir", self.customMaterialDir)
self.form.close()
def reset(self):
"""handle reset calls"""
pass
def Show():
""" show the speeds and feeds dialog"""
if not FreeCAD.ActiveDocument:
QtGui.QMessageBox.warning(FreeCADGui.getMainWindow(), "Warning", "No Active Document")
return
jobs = FreeCAD.ActiveDocument.findObjects("Path::FeaturePython", "Job.*")
if len(jobs) == 0:
QtGui.QMessageBox.warning(FreeCADGui.getMainWindow(), "Warning", "No Path Job in Current Document")
return
# create a FeedSpeedPanel form object
panel = FeedSpeedPanel()
# Show the form
panel.show()