-
Notifications
You must be signed in to change notification settings - Fork 58
/
UnfoldGUI.py
330 lines (278 loc) · 12.2 KB
/
UnfoldGUI.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
##############################################################################
#
# UnfoldGUI.py
#
# Copyright 2014, 2018 Ulrich Brammer <ulrich@Pauline>
# Copyright 2023 Ondsel Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
##############################################################################
from PySide import QtCore, QtGui
from SheetMetalLogger import SMLogger, UnfoldException
from engineering_mode import engineering_mode_enabled
import FreeCAD
import FreeCADGui
import SheetMetalKfactor
import importDXF
import importSVG
import os
import SheetMetalUnfolder as smu
modPath = os.path.dirname(__file__).replace("\\", "/")
GENSKETCHCOLOR = "#000080"
BENDSKETCHCOLOR = "#c00000"
INTSKETCHCOLOR = "#ff5733"
KFACTOR = 0.40
last_selected_mds = "none"
mds_help_url = "https://github.com/shaise/FreeCAD_SheetMetal#material-definition-sheet"
mw = FreeCADGui.getMainWindow()
class SMUnfoldTaskPanel:
def __init__(self):
path = f"{modPath}/Resources/panels/UnfoldOptions.ui"
self.form = FreeCADGui.PySideUic.loadUi(path)
self.pg = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/SheetMetal")
# Technical Debt.
# The command that gets us here is defined in SheetMetalUnfoldCmd.py.
# It limits the selection to planar faces.
# However, once the dialog is open, the user can change the selection
# and select any kind of geometry. This is wrong.
# if it is desirable to allow user to change the selection with the
# dialog open, then a selectiongate should be written to limit
# what the user can select.
# If we want to prevent changing selection, then something else has to
# happen.
# For now, we are setting the reference plane when the user activates
# the command. Any change by the user is ignored.
self.referenceFace = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0]
self.facename = FreeCADGui.Selection.getSelectionEx()[0].SubElementNames[0]
self.object = FreeCADGui.Selection.getSelectionEx()[0].Object
# End Technical debt
self.setupUi()
def _boolToState(self, bool):
return QtCore.Qt.Checked if bool else QtCore.Qt.Unchecked
def _getExportType(self, typeonly=False):
if not typeonly and not self.form.chkExport.isChecked():
return None
if self.form.svgExport.isChecked():
return "svg"
else:
return "dxf"
def _isManualKSelected(self):
return self.form.availableMds.currentIndex() == (
self.form.availableMds.count() - 1
)
def _isNoMdsSelected(self):
return self.form.availableMds.currentIndex() == 0
def _updateSelectedMds(self):
global last_selected_mds
last_selected_mds = self.form.availableMds.currentText()
def _getLastSelectedMdsIndex(self):
global last_selected_mds
for i in range(self.form.availableMds.count()):
if self.form.availableMds.itemText(i) == last_selected_mds:
return i
return -1
def _getData(self):
kFactorStandard = "din" if self.form.kfactorDin.isChecked() else "ansi"
results = {
"exportType": self._getExportType(),
"genObjTransparency": self.form.transSpin.value(),
"genSketchColor": self.form.genColor.property("color").name(),
"bendSketchColor": self.form.bendColor.property("color").name(),
"intSketchColor": self.form.internalColor.property("color").name(),
"separateSketches": self.form.chkSeparate.isChecked(),
"genSketch": self.form.chkSketch.isChecked(),
"kFactorStandard": kFactorStandard,
}
if self._isManualKSelected():
results["lookupTable"] = {1: self.form.kFactSpin.value()}
elif self._isNoMdsSelected():
msg = FreeCAD.Qt.translate(
"Logger", "Unfold operation needs to know K-factor value(s) to be used."
)
SMLogger.warning(msg)
msg += FreeCAD.Qt.translate(
"QMessageBox",
"<ol>\n"
"<li>Either select 'Manual K-factor'</li>\n"
"<li>Or use a <a href='{}'>Material Definition Sheet</a></li>\n"
"</ol>",
).format(mds_help_url)
QtGui.QMessageBox.warning(
None, FreeCAD.Qt.translate("QMessageBox", "Warning"), msg
)
return None
else:
lookupTable = SheetMetalKfactor.KFactorLookupTable(
self.form.availableMds.currentText()
)
results["lookupTable"] = lookupTable.k_factor_lookup
self.pg.SetString("kFactorStandard", str(results["kFactorStandard"]))
self.pg.SetFloat("manualKFactor", float(self.form.kFactSpin.value()))
self.pg.SetBool("genSketch", results["genSketch"])
self.pg.SetString("genColor", results["genSketchColor"])
self.pg.SetString("bendColor", results["bendSketchColor"])
self.pg.SetString("internalColor", results["intSketchColor"])
self.pg.SetBool("separateSketches", results["separateSketches"])
self.pg.SetBool("exportEn", self.form.chkExport.isChecked())
self.pg.SetString("exportType", self._getExportType(True))
return results
def setupUi(self):
kFactorStandard = self.pg.GetString("kFactorStandard", "ansi")
if kFactorStandard == "ansi":
self.form.kfactorAnsi.setChecked(True)
else:
self.form.kfactorDin.setChecked(True)
self.form.chkSketch.stateChanged.connect(self.chkSketchChange)
self.form.chkSeparate.stateChanged.connect(self.chkSketchChange)
self.form.availableMds.currentIndexChanged.connect(self.availableMdsChacnge)
self.form.chkSeparate.setCheckState(
self._boolToState(self.pg.GetBool("separateSketches"))
)
self.form.chkSketch.setCheckState(
self._boolToState(self.pg.GetBool("genSketch"))
)
self.form.genColor.setProperty(
"color", self.pg.GetString("genColor", GENSKETCHCOLOR)
)
self.form.bendColor.setProperty(
"color", self.pg.GetString("bendColor", BENDSKETCHCOLOR)
)
self.form.internalColor.setProperty(
"color", self.pg.GetString("internalColor", INTSKETCHCOLOR)
)
self.form.transSpin.setValue(self.pg.GetInt("genObjTransparency", 50))
self.form.kFactSpin.setValue(self.pg.GetFloat("manualKFactor", KFACTOR))
self.form.chkSeparate.setEnabled(self.pg.GetBool("separateSketches", False))
self.form.chkExport.setCheckState(
self._boolToState(self.pg.GetBool("exportEn", False))
)
if self.pg.GetString("exportType", "dxf") == "dxf":
self.form.dxfExport.setChecked(True)
else:
self.form.svgExport.setChecked(True)
self.chkSketchChange()
self.populateMdsList()
self.availableMdsChacnge()
self.form.update()
FreeCAD.ActiveDocument.openTransaction("Unfold")
def accept(self):
self._updateSelectedMds()
params = self._getData()
if params is None:
return
try:
result = smu.processUnfold(
params["lookupTable"],
self.object,
self.referenceFace,
self.facename,
genSketch=self.form.chkSketch.isChecked(),
splitSketches=self.form.chkSeparate.isChecked(),
sketchColor=params["genSketchColor"],
bendSketchColor=params["bendSketchColor"],
internalSketchColor=params["intSketchColor"],
transparency=params["genObjTransparency"],
kFactorStandard=params["kFactorStandard"],
)
if result:
self.doExport(result[1])
FreeCAD.ActiveDocument.commitTransaction()
FreeCADGui.ActiveDocument.resetEdit()
FreeCADGui.Control.closeDialog()
FreeCAD.ActiveDocument.recompute()
else:
FreeCAD.ActiveDocument.abortTransaction()
FreeCADGui.Control.closeDialog()
FreeCAD.ActiveDocument.recompute()
except UnfoldException:
msg = (
FreeCAD.Qt.translate(
"QMessageBox",
"Unfold is failing.\n"
"Please try to select a different face to unfold your object\n\n"
"If the opposite face also fails then switch Refine to false on feature ",
)
+ FreeCADGui.Selection.getSelection()[0].Name
)
QtGui.QMessageBox.question(
None,
FreeCAD.Qt.translate("QMessageBox", "Warning"),
msg,
QtGui.QMessageBox.Ok,
)
except Exception as e:
raise e
def reject(self):
FreeCAD.ActiveDocument.abortTransaction()
FreeCADGui.Control.closeDialog()
FreeCAD.ActiveDocument.recompute()
def doExport(self, obj):
# Not sure we should be doing export in this dialog but if we want to,
# it should be handled here and not in the unfold function.
# This implementation of export is limited because it doesn't export
# split sketches. More reason to potentially remove it entirely and
# let the user use the standard export functions
if obj is None:
return
if self._getExportType() is None:
return
__objs__ = []
__objs__.append(obj)
filename = f"{FreeCAD.ActiveDocument.FileName[0:-6]}-{obj.Name}.{self._getExportType()}"
print("Exporting to " + filename)
if self._getExportType() == "dxf":
importDXF.export(__objs__, filename)
else:
importSVG.export(__objs__, filename)
del __objs__
def populateMdsList(self):
sheetnames = SheetMetalKfactor.getSpreadSheetNames()
self.form.availableMds.clear()
self.form.availableMds.addItem("Please select")
for mds in sheetnames:
if mds.Label.startswith("material_"):
self.form.availableMds.addItem(mds.Label)
self.form.availableMds.addItem("Manual K-Factor")
selMdsIndex = self._getLastSelectedMdsIndex()
if selMdsIndex >= 0:
self.form.availableMds.setCurrentIndex(selMdsIndex)
elif len(sheetnames) == 1:
self.form.availableMds.setCurrentIndex(1)
elif engineering_mode_enabled():
self.form.availableMds.setCurrentIndex(0)
elif len(sheetnames) == 0:
self.form.availableMds.setCurrentIndex(1)
def chkSketchChange(self):
self.form.chkSeparate.setEnabled(self.form.chkSketch.isChecked())
if self.form.chkSketch.isChecked():
self.form.dxfExport.show()
self.form.svgExport.show()
self.form.genColor.setEnabled(True)
else:
self.form.dxfExport.hide()
self.form.svgExport.hide()
self.form.genColor.setEnabled(False)
enabled = self.form.chkSketch.isChecked() and self.form.chkSeparate.isChecked()
self.form.bendColor.setEnabled(enabled)
self.form.internalColor.setEnabled(enabled)
def availableMdsChacnge(self):
isManualK = self._isManualKSelected()
self.form.kfactorAnsi.setEnabled(isManualK)
self.form.kfactorDin.setEnabled(isManualK)
self.form.kFactSpin.setEnabled(isManualK)