/
_ViewProviderFemMaterialReinforced.py
502 lines (451 loc) · 22.2 KB
/
_ViewProviderFemMaterialReinforced.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# ***************************************************************************
# * Copyright (c) 2019 Bernd Hahnebach <bernd@bimstatik.org> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
__title__ = "FreeCAD FEM material reinforced ViewProvider for the document object"
__author__ = "Bernd Hahnebach"
__url__ = "http://www.freecadweb.org"
## @package ViewProviderFemMaterialReinforced
# \ingroup FEM
# \brief FreeCAD FEM _ViewProviderFemMaterialReinforced
import FreeCAD
import FreeCADGui
import FemGui # needed to display the icons in TreeView
# task panel
# from . import FemSelectionWidgets
from PySide import QtCore
from PySide import QtGui
import sys
False if False else FemGui.__name__ # flake8, dummy FemGui usage, returns "FemGui"
if sys.version_info.major >= 3:
unicode = str
class _ViewProviderFemMaterialReinforced:
"A View Provider for the FemMaterialReinfocement object"
def __init__(self, vobj):
vobj.Proxy = self
def getIcon(self):
return ":/icons/fem-material-reinforced.svg"
def attach(self, vobj):
from pivy import coin
self.ViewObject = vobj
self.Object = vobj.Object
self.standard = coin.SoGroup()
vobj.addDisplayMode(self.standard, "Default")
def getDisplayModes(self, obj):
return ["Default"]
def updateData(self, obj, prop):
return
def onChanged(self, vobj, prop):
return
def setEdit(self, vobj, mode=0):
# hide all meshes
for o in FreeCAD.ActiveDocument.Objects:
if o.isDerivedFrom("Fem::FemMeshObject"):
o.ViewObject.hide()
# hide all meshes
for o in FreeCAD.ActiveDocument.Objects:
if o.isDerivedFrom("Fem::FemMeshObject"):
o.ViewObject.hide()
# show task panel
taskd = _TaskPanelFemMaterialReinforced(self.Object)
taskd.obj = vobj.Object
FreeCADGui.Control.showDialog(taskd)
return True
def unsetEdit(self, vobj, mode=0):
FreeCADGui.Control.closeDialog()
return True
def doubleClicked(self, vobj):
guidoc = FreeCADGui.getDocument(vobj.Object.Document)
# check if another VP is in edit mode
# https://forum.freecadweb.org/viewtopic.php?t=13077#p104702
if not guidoc.getInEdit():
guidoc.setEdit(vobj.Object.Name)
else:
from PySide.QtGui import QMessageBox
message = (
"Active Task Dialog found! "
"Please close this one before opening a new one!"
)
QMessageBox.critical(None, "Error in tree view", message)
FreeCAD.Console.PrintError(message + "\n")
return True
def __getstate__(self):
return None
def __setstate__(self, state):
return None
class _TaskPanelFemMaterialReinforced:
"""The editmode TaskPanel for FemMaterialReinforced objects"""
if sys.version_info.major >= 3:
unicode = str
def __init__(self, obj):
FreeCAD.Console.PrintMessage("\n") # empty line on start task panel
self.obj = obj
# init matrix and reinforcement material
self.material_m = self.obj.Material
self.card_path_m = ""
self.has_transient_mat_m = False
self.material_r = self.obj.Reinforcement
self.card_path_r = ""
self.has_transient_mat_r = False
# mat_card is the FCMat file
# card_name is the file name of the mat_card
# card_path is the whole file path of the mat_card
# material_name is the value of the key name in FreeCAD material dictionary
# they might not match because of special letters in the material_name which are
# changed in the card_name to english standard characters
# init for collecting all mat data and icons
self.materials = {} # { card_path : FreeCAD material dict }
self.cards = {} # { card_path : card_names, ... }
self.icons = {} # { card_path : icon_path }
# parameter widget
self.parameterWidget = FreeCADGui.PySideUic.loadUi(
FreeCAD.getHomePath() + "Mod/Fem/Resources/ui/MaterialReinforcement.ui"
)
# globals
QtCore.QObject.connect(
self.parameterWidget.cb_materials_m,
QtCore.SIGNAL("activated(int)"),
self.choose_material_m
)
QtCore.QObject.connect(
self.parameterWidget.pb_edit_m,
QtCore.SIGNAL("clicked()"),
self.edit_material_m
)
QtCore.QObject.connect(
self.parameterWidget.cb_materials_r,
QtCore.SIGNAL("activated(int)"),
self.choose_material_r
)
QtCore.QObject.connect(
self.parameterWidget.pb_edit_r,
QtCore.SIGNAL("clicked()"),
self.edit_material_r
)
# get all available materials (fill self.materials, self.cards and self.icons)
from materialtools.cardutils import import_materials as getmats
self.materials, self.cards, self.icons = getmats()
# fill the material comboboxes with material cards
self.add_cards_to_combo_boxes()
# search for exact the mat_card_m and mat_card_r in all known cards
# choose the current matrix material
self.card_path_m = self.get_material_card(self.material_m)
FreeCAD.Console.PrintLog("card_path: {}\n".format(self.card_path_m))
if not self.card_path_m:
# we have not found our material in self.materials dict :-(
# we're going to add a user-defined temporary material: a document material
FreeCAD.Console.PrintMessage(
"Previously used material card cannot be found in material directories. "
"Add document material.\n"
)
self.card_path_m = "_Document_Matrix_Material"
self.materials[self.card_path_m] = self.material_m
self.parameterWidget.cb_materials_m.addItem(
QtGui.QIcon(":/icons/help-browser.svg"),
self.card_path_m,
self.card_path_m
)
index = self.parameterWidget.cb_materials_m.findData(self.card_path_m)
# print(index)
# fill input fields and set the current material in the cb widget
self.choose_material_m(index)
else:
# we found our exact material in self.materials dict :-)
FreeCAD.Console.PrintLog(
"Previously used material card was found in material directories. "
"We will use this material.\n"
)
index = self.parameterWidget.cb_materials_m.findData(self.card_path_m)
# set the current material in the cb widget
self.choose_material_m(index)
# choose the current reinforcement material
self.card_path_r = self.get_material_card(self.material_r)
FreeCAD.Console.PrintLog("card_path: {}\n".format(self.card_path_r))
if not self.card_path_r:
# we have not found our material in self.materials dict :-(
# we're going to add a user-defined temporary material: a document material
FreeCAD.Console.PrintMessage(
"Previously used material card cannot be found in material directories. "
"Add document material.\n"
)
self.card_path_r = "_Document_Reinforcement_Material"
self.materials[self.card_path_r] = self.material_r
self.parameterWidget.cb_materials_r.addItem(
QtGui.QIcon(":/icons/help-browser.svg"),
self.card_path_r,
self.card_path_r
)
index = self.parameterWidget.cb_materials_r.findData(self.card_path_r)
# set the current material in the cb widget
self.choose_material_r(index)
else:
# we found our exact material in self.materials dict :-)
FreeCAD.Console.PrintLog(
"Previously used material card was found in material directories. "
"We will use this material.\n"
)
index = self.parameterWidget.cb_materials_r.findData(self.card_path_r)
# print(index)
# fill input fields and set the current material in the cb widget
self.choose_material_r(index)
# set up the form
self.form = self.parameterWidget
# leave task panel ***************************************************************************
def accept(self):
from materialtools.cardutils import check_mat_units as checkunits
if checkunits(self.material_m) is True and checkunits(self.material_r) is True:
self.obj.Material = self.material_m
self.obj.Reinforcement = self.material_r
else:
error_message = (
"Due to some wrong material quantity units in the changed "
"material data, the task panel changes where not accepted.\n"
)
FreeCAD.Console.PrintError(error_message)
QtGui.QMessageBox.critical(None, "Material data not changed", error_message)
self.recompute_and_set_back_all()
return True
def reject(self):
self.recompute_and_set_back_all()
return True
def recompute_and_set_back_all(self):
guidoc = FreeCADGui.getDocument(self.obj.Document)
guidoc.Document.recompute()
guidoc.resetEdit()
self.output_obj_mat_param()
def output_obj_mat_param(self):
self.print_mat_dict(self.obj.Material)
self.print_mat_dict(self.obj.Reinforcement)
print("\n")
def print_mat_dict(self, mat_dict):
if "Name" in mat_dict:
print("Material: {}".format(mat_dict["Name"]))
else:
print("Matrix material: no Name")
for key in mat_dict:
print(" {}: {}".format(key, mat_dict[key]))
# choose material card ***********************************************************************
def get_material_card(self, material):
for a_mat in self.materials:
unmatched_items = set(self.materials[a_mat].items()) ^ set(material.items())
# print(a_mat + " --> unmatched_items = " + str(len(unmatched_items)))
if len(unmatched_items) < 4:
FreeCAD.Console.PrintLog("{}\n".format(unmatched_items))
if len(unmatched_items) == 0:
return a_mat
return ""
def choose_material_m(self, index):
if index < 0:
return
# get the whole card path
self.card_path_m = self.parameterWidget.cb_materials_m.itemData(index)
FreeCAD.Console.PrintMessage(
"choose_material in FEM material task panel:\n"
" {}\n".format(self.card_path_m)
)
self.material_m = self.materials[self.card_path_m]
self.parameterWidget.cb_materials_m.setCurrentIndex(index)
gen_mat_desc = ""
gen_mat_name = ""
if "Description" in self.material_m:
gen_mat_desc = self.material_m["Description"]
if "Name" in self.material_m:
gen_mat_name = self.material_m["Name"]
self.parameterWidget.l_description_m.setText(gen_mat_desc)
self.parameterWidget.l_name_m.setText(gen_mat_name)
def choose_material_r(self, index):
if index < 0:
return
# get the whole card path
self.card_path_r = self.parameterWidget.cb_materials_r.itemData(index)
FreeCAD.Console.PrintMessage(
"choose_material in FEM material task panel:\n"
" {}\n".format(self.card_path_r)
)
self.material_r = self.materials[self.card_path_r]
self.parameterWidget.cb_materials_r.setCurrentIndex(index)
gen_mat_desc = ""
gen_mat_name = ""
if "Description" in self.material_r:
gen_mat_desc = self.material_r["Description"]
if "Name" in self.material_r:
gen_mat_name = self.material_r["Name"]
self.parameterWidget.l_description_r.setText(gen_mat_desc)
self.parameterWidget.l_name_r.setText(gen_mat_name)
# transient material is needed if the user changed mat parameter by the mat editor
def set_transient_material_m(self):
self.card_path_m = "_Transient_Matrix_Material"
self.materials[self.card_path_m] = self.material_m # = the current matrix mat dict
index = self.parameterWidget.cb_materials_m.findData(self.card_path_m)
self.choose_material_m(index)
def add_transient_material_m(self):
self.has_transient_mat_m = True
self.card_path_m = "_Transient_Matrix_Material"
self.parameterWidget.cb_materials_m.addItem(
QtGui.QIcon(":/icons/help-browser.svg"),
self.card_path_m,
self.card_path_m
)
self.set_transient_material_m()
def set_transient_material_r(self):
self.card_path_r = "_Transient_Reinforcement_Material"
self.materials[self.card_path_r] = self.material_r # = the current reinforced mat dict
index = self.parameterWidget.cb_materials_r.findData(self.card_path_r)
self.choose_material_r(index)
def add_transient_material_r(self):
self.has_transient_mat_r = True
self.card_path_r = "_Transient_Reinforcement_Material"
self.parameterWidget.cb_materials_r.addItem(
QtGui.QIcon(":/icons/help-browser.svg"),
self.card_path_r,
self.card_path_r
)
self.set_transient_material_r()
# edit material parameter ********************************************************************
# TODO, also all mat parameter checks should be moved to material editor
# and mat parameter checks should be done on analysis precheck in according to the analysis
# should be checked if all needed parameter are defined and have all right values and units
def edit_material_m(self):
# opens the material editor to choose a material or edit material params
import MaterialEditor
if self.card_path_m not in self.cards:
FreeCAD.Console.PrintLog(
"Card path not in cards, material dict will be used to open Material Editor.\n"
)
new_material_params = MaterialEditor.editMaterial(material=self.material_m)
else:
new_material_params = MaterialEditor.editMaterial(card_path=self.card_path_m)
# material editor returns the mat_dict only, not a card_path
# if the material editor was canceled a empty dict will be returned
# do not change the self.material
# check if dict is not empty (do not use "is True")
if new_material_params:
# check material quantity units
from materialtools.cardutils import check_mat_units as checkunits
if checkunits(new_material_params) is True:
self.material_m = new_material_params
self.card_path_m = self.get_material_card(self.material_m)
FreeCAD.Console.PrintMessage("card_path: {}\n".format(self.card_path_m))
if not self.card_path_m:
FreeCAD.Console.PrintMessage(
"Material card chosen by the material editor "
"was not found in material directories.\n"
"Either the card does not exist or some material "
"parameter where changed in material editor.\n"
)
if self.has_transient_mat_m is False:
self.add_transient_material_m()
else:
self.set_transient_material_m()
else:
# we found our exact material in self.materials dict :-)
FreeCAD.Console.PrintLog(
"Material card chosen by the material editor "
"was found in material directories. "
"The found material card will be used.\n"
)
index = self.parameterWidget.cb_materials_m.findData(self.card_path_m)
# print(index)
# set the current material in the cb widget
self.choose_material_m(index)
else:
error_message = (
"Due to some wrong material quantity units in data passed "
"by the material editor, the material data was not changed.\n"
)
FreeCAD.Console.PrintError(error_message)
QtGui.QMessageBox.critical(None, "Material data not changed", error_message)
else:
FreeCAD.Console.PrintMessage("No changes where made by the material editor.\n")
def edit_material_r(self):
# opens the material editor to choose a material or edit material params
import MaterialEditor
if self.card_path_r not in self.cards:
FreeCAD.Console.PrintLog(
"Card path not in cards, material dict will be used to open Material Editor.\n"
)
new_material_params = MaterialEditor.editMaterial(material=self.material_r)
else:
new_material_params = MaterialEditor.editMaterial(card_path=self.card_path_r)
# material editor returns the mat_dict only, not a card_path
# if the material editor was canceled a empty dict will be returned
# do not change the self.material
# check if dict is not empty (do not use "is True")
if new_material_params:
# check material quantity units
from materialtools.cardutils import check_mat_units as checkunits
if checkunits(new_material_params) is True:
self.material_r = new_material_params
self.card_path_r = self.get_material_card(self.material_r)
FreeCAD.Console.PrintMessage("card_path: {}\n".format(self.card_path_r))
if not self.card_path_r:
FreeCAD.Console.PrintMessage(
"Material card chosen by the material editor "
"was not found in material directories.\n"
"Either the card does not exist or some material "
"parameter where changed in material editor.\n"
)
if self.has_transient_mat_r is False:
self.add_transient_material_r()
else:
self.set_transient_material_r()
else:
# we found our exact material in self.materials dict :-)
FreeCAD.Console.PrintLog(
"Material card chosen by the material editor "
"was found in material directories. "
"The found material card will be used.\n"
)
index = self.parameterWidget.cb_materials_r.findData(self.card_path_r)
# print(index)
# set the current material in the cb widget
self.choose_material_r(index)
else:
error_message = (
"Due to some wrong material quantity units in data passed "
"by the material editor, the material data was not changed.\n"
)
FreeCAD.Console.PrintError(error_message)
QtGui.QMessageBox.critical(None, "Material data not changed", error_message)
else:
FreeCAD.Console.PrintMessage("No changes where made by the material editor.\n")
# fill the combo box with cards **************************************************************
def add_cards_to_combo_boxes(self):
# fill comboboxes, in combo box the card name is used not the material name
self.parameterWidget.cb_materials_m.clear()
self.parameterWidget.cb_materials_r.clear()
mat_prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material/Cards")
sort_by_resources = mat_prefs.GetBool("SortByResources", False)
card_name_list = [] # [ [card_name, card_path, icon_path], ... ]
if sort_by_resources is True:
for a_path in sorted(self.materials.keys()):
card_name_list.append([self.cards[a_path], a_path, self.icons[a_path]])
else:
card_names_tmp = {}
for path, name in self.cards.items():
card_names_tmp[name] = path
for a_name in sorted(card_names_tmp.keys()):
a_path = card_names_tmp[a_name]
card_name_list.append([a_name, a_path, self.icons[a_path]])
for mat in card_name_list:
self.parameterWidget.cb_materials_m.addItem(QtGui.QIcon(mat[2]), mat[0], mat[1])
self.parameterWidget.cb_materials_r.addItem(QtGui.QIcon(mat[2]), mat[0], mat[1])
# the whole card path is added to the combo box to make it unique
# see def choose_material:
# for assignment of self.card_path the path form the parameterWidget ist used