-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFISTR_solver.py
384 lines (349 loc) · 16.5 KB
/
FISTR_solver.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
# ***************************************************************************
# * Copyright (c) 2020 FrontISTR Commons <https://www.frontistr.com/> *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * 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 FrontISTR Command Class"
__author__ = "FrontISTR Commons"
__url__ = "https://www.frontistr.com/"
import FreeCAD, FreeCADGui, Mesh, Part, MeshPart, Draft, DraftGeomUtils, os
from FreeCAD import Vector
import math
import numpy as np
if FreeCAD.GuiUp:
import FreeCADGui
import Fem
import FemGui
from PySide import QtCore, QtGui
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
from femtools.femutils import is_of_type
else:
# \cond
def translate(ctxt,txt, utf8_decode=False):
return txt
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
class _CommandFISTRsolver:
def GetResources(self):
return {'Pixmap' : FreeCAD.getUserAppDataDir()+ "Mod/FEM_FrontISTR/Resources/FrontISTR_solver.svg" ,
'MenuText': QT_TRANSLATE_NOOP("FISTR_solver","FrontISTR Solver"),
'Accel': "S, X",
'ToolTip': QT_TRANSLATE_NOOP("FISTR_solver","Creates a FrontISTR solver object")}
def IsActive(self):
active = (
FemGui.getActiveAnalysis() is not None
and self.active_analysis_in_active_doc()
)
return active
#return FreeCADGui.ActiveDocument is not None
def active_analysis_in_active_doc(self):
analysis = FemGui.getActiveAnalysis()
if analysis.Document is FreeCAD.ActiveDocument:
self.active_analysis = analysis
return True
else:
return False
def Activated(self):
FreeCAD.ActiveDocument.openTransaction("Create SolverFrontISTR")
FreeCADGui.addModule("ObjectsFISTR")
FreeCADGui.addModule("FemGui")
FreeCADGui.doCommand(
"FemGui.getActiveAnalysis().addObject(ObjectsFISTR."
"makeSolverFrontISTRTools(FreeCAD.ActiveDocument))"
)
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
class _CommandFISTRConstraintTemperature:
def GetResources(self):
return {'Pixmap' : FreeCAD.getUserAppDataDir()+ "Mod/FEM_FrontISTR/Resources/FrontISTR_ConstraintTemperature.svg" ,
'MenuText': QT_TRANSLATE_NOOP("FISTR_ConstraintTemperature","FrontISTR constraint temperature"),
'ToolTip': QT_TRANSLATE_NOOP(
"FISTR_ConstraintTemperature",
"Creates a FrontISTR constraint for temperature acting on a body"
)}
def IsActive(self): # same as above
active = (
FemGui.getActiveAnalysis() is not None
and self.active_analysis_in_active_doc()
)
return active
def active_analysis_in_active_doc(self): # same as above
analysis = FemGui.getActiveAnalysis()
if analysis.Document is FreeCAD.ActiveDocument:
self.active_analysis = analysis
return True
else:
return False
def Activated(self): # do_activated == "add_obj_on_gui_set_edit"
FreeCAD.ActiveDocument.openTransaction("Create FrontISTR constraint temperature")
FreeCADGui.addModule("ObjectsFISTR")
FreeCADGui.addModule("FemGui")
FreeCADGui.doCommand(
"FemGui.getActiveAnalysis().addObject(ObjectsFISTR."
"makeConstraintTemperatureFrontISTR(FreeCAD.ActiveDocument))"
)
FreeCADGui.Selection.clearSelection()
FreeCADGui.doCommand(
"FreeCADGui.ActiveDocument.setEdit(FreeCAD.ActiveDocument.ActiveObject.Name)"
)
FreeCAD.ActiveDocument.recompute()
class _CommandFISTRMaterialHyperelastic:
def GetResources(self):
return {'Pixmap' : FreeCAD.getUserAppDataDir()+ "Mod/FEM_FrontISTR/Resources/FrontISTR_MaterialHyperelastic.svg" ,
'MenuText': QT_TRANSLATE_NOOP("FISTR_MaterialHyperelastic","FrontISTR material hyperelastic"),
'ToolTip': QT_TRANSLATE_NOOP(
"FISTR_MaterialHyperelastic",
"Creates a FrontISTR material hyperelastic"
)}
def IsActive(self): # is_active == "with_material_solid"
active = (
FemGui.getActiveAnalysis() is not None
and self.active_analysis_in_active_doc()
and self.material_solid_selected()
)
return active
def active_analysis_in_active_doc(self): # same as above
analysis = FemGui.getActiveAnalysis()
if analysis.Document is FreeCAD.ActiveDocument:
self.active_analysis = analysis
return True
else:
return False
def material_solid_selected(self):
sel = FreeCADGui.Selection.getSelection()
if (
len(sel) == 1
and sel[0].isDerivedFrom("App::MaterialObjectPython")
and hasattr(sel[0], "Category")
and sel[0].Category == "Solid"
):
self.selobj = sel[0]
return True
else:
return False
def Activated(self):
# see https://github.com/FreeCAD/FreeCAD/blob/040f86c4d55de150d6f56b7c6110f578d4d42a2c/src/Mod/Fem/femcommands/commands.py#L573
# test if there is a hyperelastic material which has the selected material as base material
for o in self.selobj.Document.Objects:
if (
is_of_type(o, "Fem::MaterialHyperelasticFISTR")
and o.LinearBaseMaterial == self.selobj
):
FreeCAD.Console.PrintError(
"Hyperelastic material {} is based on the selected material {}. "
"Only one hyperelastic object allowed for each material.\n"
.format(o.Name, self.selobj.Name)
)
return
# add a hyperelastic material
string_lin_mat_obj = "FreeCAD.ActiveDocument.getObject('" + self.selobj.Name + "')"
command_to_run = (
"FemGui.getActiveAnalysis().addObject(ObjectsFISTR."
"makeMaterialHyperelasticFrontISTR(FreeCAD.ActiveDocument, {}))"
.format(string_lin_mat_obj)
)
FreeCAD.ActiveDocument.openTransaction("Create FrontISTR material hyperelastic")
FreeCADGui.addModule("ObjectsFISTR")
FreeCADGui.addModule("FemGui")
FreeCADGui.doCommand(command_to_run)
# set property of the solver to nonlinear
# (only if one solver is available and if this solver is a FrontISTR solver):
# The original code assumes that SolverCcxTools is at the top. However, SolverFISTRTools is not placed at the top.
solver_object = None
for m in self.active_analysis.Group:
if not solver_object and is_of_type(m, "Fem::SolverFISTRTools"):
solver_object = m
FreeCAD.Console.PrintMessage(
"Set Nonlinearity to yes for {}\n"
.format(solver_object.Label)
)
solver_object.Nonlinearity = "yes"
else:
# we do not change attributes if we have more than one solver
# since we do not know which one to take
solver_object = None
FreeCADGui.Selection.clearSelection()
FreeCAD.ActiveDocument.recompute()
class _CommandFISTRMaterialViscoelastic:
def GetResources(self):
return {'Pixmap' : FreeCAD.getUserAppDataDir()+ "Mod/FEM_FrontISTR/Resources/FrontISTR_MaterialViscoelastic.svg" ,
'MenuText': QT_TRANSLATE_NOOP("FISTR_MaterialViscoelastic","FrontISTR material viscoelastic"),
'ToolTip': QT_TRANSLATE_NOOP(
"FISTR_MaterialViscoelastic",
"Creates a FrontISTR material viscoelastic"
)}
def IsActive(self): # is_active == "with_material_solid"
active = (
FemGui.getActiveAnalysis() is not None
and self.active_analysis_in_active_doc()
and self.material_solid_selected()
)
return active
def active_analysis_in_active_doc(self): # same as above
analysis = FemGui.getActiveAnalysis()
if analysis.Document is FreeCAD.ActiveDocument:
self.active_analysis = analysis
return True
else:
return False
def material_solid_selected(self):
sel = FreeCADGui.Selection.getSelection()
if (
len(sel) == 1
and sel[0].isDerivedFrom("App::MaterialObjectPython")
and hasattr(sel[0], "Category")
and sel[0].Category == "Solid"
):
self.selobj = sel[0]
return True
else:
return False
def Activated(self):
# see https://github.com/FreeCAD/FreeCAD/blob/040f86c4d55de150d6f56b7c6110f578d4d42a2c/src/Mod/Fem/femcommands/commands.py#L573
# test if there is a viscoelastic material which has the selected material as base material
for o in self.selobj.Document.Objects:
if (
is_of_type(o, "Fem::MaterialViscoelasticFISTR")
and o.LinearBaseMaterial == self.selobj
):
FreeCAD.Console.PrintError(
"Viscoelastic material {} is based on the selected material {}. "
"Only one viscoelastic object allowed for each material.\n"
.format(o.Name, self.selobj.Name)
)
return
# add a viscoelastic material
string_lin_mat_obj = "FreeCAD.ActiveDocument.getObject('" + self.selobj.Name + "')"
command_to_run = (
"FemGui.getActiveAnalysis().addObject(ObjectsFISTR."
"makeMaterialViscoelasticFrontISTR(FreeCAD.ActiveDocument, {}))"
.format(string_lin_mat_obj)
)
FreeCAD.ActiveDocument.openTransaction("Create FrontISTR material viscoelastic")
FreeCADGui.addModule("ObjectsFISTR")
FreeCADGui.addModule("FemGui")
FreeCADGui.doCommand(command_to_run)
# set property of the solver to nonlinear
# (only if one solver is available and if this solver is a FrontISTR solver):
# The original code assumes that SolverCcxTools is at the top. However, SolverFISTRTools is not placed at the top.
solver_object = None
for m in self.active_analysis.Group:
if not solver_object and is_of_type(m, "Fem::SolverFISTRTools"):
solver_object = m
FreeCAD.Console.PrintMessage(
"Set Nonlinearity to yes for {}\n"
.format(solver_object.Label)
)
solver_object.Nonlinearity = "yes"
else:
# we do not change attributes if we have more than one solver
# since we do not know which one to take
solver_object = None
FreeCADGui.Selection.clearSelection()
### if use taskpanel for viscoelastic
# FreeCADGui.doCommand(
# "FreeCADGui.ActiveDocument.setEdit(FreeCAD.ActiveDocument.ActiveObject.Name)"
# )
FreeCAD.ActiveDocument.recompute()
class _CommandFISTRMaterialCreep:
def GetResources(self):
return {'Pixmap' : FreeCAD.getUserAppDataDir()+ "Mod/FEM_FrontISTR/Resources/FrontISTR_MaterialCreep.svg" ,
'MenuText': QT_TRANSLATE_NOOP("FISTR_MaterialCreep","FrontISTR material creep"),
'ToolTip': QT_TRANSLATE_NOOP(
"FISTR_MaterialCreep",
"Creates a FrontISTR material creep"
)}
def IsActive(self): # is_active == "with_material_solid"
active = (
FemGui.getActiveAnalysis() is not None
and self.active_analysis_in_active_doc()
and self.material_solid_selected()
)
return active
def active_analysis_in_active_doc(self): # same as above
analysis = FemGui.getActiveAnalysis()
if analysis.Document is FreeCAD.ActiveDocument:
self.active_analysis = analysis
return True
else:
return False
def material_solid_selected(self):
sel = FreeCADGui.Selection.getSelection()
if (
len(sel) == 1
and sel[0].isDerivedFrom("App::MaterialObjectPython")
and hasattr(sel[0], "Category")
and sel[0].Category == "Solid"
):
self.selobj = sel[0]
return True
else:
return False
def Activated(self):
# see https://github.com/FreeCAD/FreeCAD/blob/040f86c4d55de150d6f56b7c6110f578d4d42a2c/src/Mod/Fem/femcommands/commands.py#L573
# test if there is a creep material which has the selected material as base material
for o in self.selobj.Document.Objects:
if (
is_of_type(o, "Fem::MaterialCreepFISTR")
and o.LinearBaseMaterial == self.selobj
):
FreeCAD.Console.PrintError(
"CReep material {} is based on the selected material {}. "
"Only one creep object allowed for each material.\n"
.format(o.Name, self.selobj.Name)
)
return
# add a creep material
string_lin_mat_obj = "FreeCAD.ActiveDocument.getObject('" + self.selobj.Name + "')"
command_to_run = (
"FemGui.getActiveAnalysis().addObject(ObjectsFISTR."
"makeMaterialCreepFrontISTR(FreeCAD.ActiveDocument, {}))"
.format(string_lin_mat_obj)
)
FreeCAD.ActiveDocument.openTransaction("Create FrontISTR material creep")
FreeCADGui.addModule("ObjectsFISTR")
FreeCADGui.addModule("FemGui")
FreeCADGui.doCommand(command_to_run)
# set property of the solver to nonlinear
# (only if one solver is available and if this solver is a FrontISTR solver):
# The original code assumes that SolverCcxTools is at the top. However, SolverFISTRTools is not placed at the top.
solver_object = None
for m in self.active_analysis.Group:
if not solver_object and is_of_type(m, "Fem::SolverFISTRTools"):
solver_object = m
FreeCAD.Console.PrintMessage(
"Set Nonlinearity to yes for {}\n"
.format(solver_object.Label)
)
solver_object.Nonlinearity = "yes"
else:
# we do not change attributes if we have more than one solver
# since we do not know which one to take
solver_object = None
FreeCADGui.Selection.clearSelection()
FreeCAD.ActiveDocument.recompute()
if FreeCAD.GuiUp:
FreeCADGui.addCommand('FISTR_solver',_CommandFISTRsolver())
FreeCADGui.addCommand('FISTR_ConstraintTemperature', _CommandFISTRConstraintTemperature())
FreeCADGui.addCommand('FISTR_MaterialHyperelastic', _CommandFISTRMaterialHyperelastic())
FreeCADGui.addCommand('FISTR_MaterialViscoelastic', _CommandFISTRMaterialViscoelastic())
FreeCADGui.addCommand('FISTR_MaterialCreep', _CommandFISTRMaterialCreep())