FEM: Python geometry selection class, adapt it to Solid selection and use it for gmsh mesh region

This commit is contained in:
Bernd Hahnebach
2018-05-21 23:05:40 +02:00
committed by wmayer
parent 366af6d778
commit 5f66ca215a
3 changed files with 116 additions and 256 deletions

View File

@@ -80,53 +80,6 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_1">
<property name="title">
<string>References</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="pushButton_Reference">
<property name="text">
<string>Add reference</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="list_References"/>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QRadioButton" name="rb_solid">
<property name="text">
<string>Solid</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="rb_standard">
<property name="text">
<string>Face, Edge, Vertex</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="l_label_text_5">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Selection&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">

View File

@@ -29,6 +29,7 @@ __url__ = "http://www.freecadweb.org"
import FreeCAD
import FreeCADGui
import femmesh.meshtools as FemMeshTools
from PySide import QtGui
from PySide import QtCore
@@ -214,6 +215,7 @@ class GeometryElementsSelection(QtGui.QWidget):
super(GeometryElementsSelection, self).__init__()
# init ui stuff
FreeCADGui.Selection.clearSelection()
self.selection_mode_solid = False
self.sel_server = None
self.obj_notvisible = []
self.initElemTypes(eltypes)
@@ -233,6 +235,8 @@ class GeometryElementsSelection(QtGui.QWidget):
self.sel_elem_text += (e + ', ')
self.sel_elem_text = self.sel_elem_text.rstrip(', ')
# FreeCAD.Console.PrintMessage('Selection of: ' + self.sel_elem_text + ' is allowed.\n')
self.selection_mode_std_print_message = "Single click on a " + self.sel_elem_text + " will add it to the list"
self.selection_mode_solid_print_message = "Single click on a Face or Edge which belongs to one Solid will add the Solid to the list"
def initUI(self):
# auch ArchPanel ist coded ohne ui-file
@@ -249,17 +253,33 @@ class GeometryElementsSelection(QtGui.QWidget):
" The following geometry elemets are allowed to select: ") + self.sel_elem_text)
# list
self.list_References = QtGui.QListWidget()
# layout
# radiobutton down the list
self.lb_selmod = QtGui.QLabel()
self.lb_selmod.setText(self.tr("Selection mode"))
self.rb_standard = QtGui.QRadioButton(self.tr(self.sel_elem_text.lstrip('Solid, ')))
self.rb_solid = QtGui.QRadioButton(self.tr("Solid"))
self.rb_standard.setChecked(True)
self.rb_solid.setChecked(False)
# radio butoon layout
rbtnLayout = QtGui.QHBoxLayout()
rbtnLayout.addWidget(self.lb_selmod)
rbtnLayout.addWidget(self.rb_standard)
rbtnLayout.addWidget(self.rb_solid)
# main layout
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self._helpTextLbl)
mainLayout.addWidget(self.pushButton_Add)
mainLayout.addWidget(self.list_References)
if 'Solid' in self.sel_elem_types:
mainLayout.addLayout(rbtnLayout)
self.setLayout(mainLayout)
# signals and slots
self.list_References.itemSelectionChanged.connect(self.select_clicked_reference_shape)
self.list_References.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.list_References.connect(self.list_References, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), self.references_list_right_clicked)
QtCore.QObject.connect(self.pushButton_Add, QtCore.SIGNAL("clicked()"), self.add_references)
QtCore.QObject.connect(self.rb_standard, QtCore.SIGNAL("toggled(bool)"), self.choose_selection_mode_standard)
QtCore.QObject.connect(self.rb_solid, QtCore.SIGNAL("toggled(bool)"), self.choose_selection_mode_solid)
def get_references(self):
for ref in self.tuplereferences:
@@ -300,7 +320,25 @@ class GeometryElementsSelection(QtGui.QWidget):
self.obj_notvisible.append(ref[0])
ref[0].ViewObject.Visibility = True
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(ref[0], ref[1])
if ref[1].startswith('Solid') and (ref[0].Shape.ShapeType == 'Compound' or ref[0].Shape.ShapeType == 'CompSolid'):
# selection of Solids of Compounds or CompSolids is not possible, because a Solid is no Subelement
# since only Subelements can be selected, we gone select all Faces of such an Solids
solid = FemMeshTools.get_element(ref[0], ref[1]) # the method getElement(element) does not return Solid elements
if not solid:
return
faces = []
for fs in solid.Faces:
# find these faces in ref[0]
for i, fref in enumerate(ref[0].Shape.Faces):
if fs.isSame(fref):
fref_elstring = 'Face' + str(i + 1)
if fref_elstring not in faces:
faces.append(fref_elstring)
for f in faces:
FreeCADGui.Selection.addSelection(ref[0], f)
else:
# Selection of all other element types is supported
FreeCADGui.Selection.addSelection(ref[0], ref[1])
def setback_listobj_visibility(self):
'''set back Visibility of the list objects
@@ -337,6 +375,16 @@ class GeometryElementsSelection(QtGui.QWidget):
self.references = []
self.rebuild_list_References()
def choose_selection_mode_standard(self, state):
self.selection_mode_solid = not state
if self.sel_server and not self.selection_mode_solid:
FreeCAD.Console.PrintMessage(self.selection_mode_std_print_message + '\n')
def choose_selection_mode_solid(self, state):
self.selection_mode_solid = state
if self.sel_server and self.selection_mode_solid:
FreeCAD.Console.PrintMessage(self.selection_mode_solid_print_message + '\n')
def add_references(self):
'''Called if Button add_reference is triggered'''
# in constraints EditTaskPanel the selection is active as soon as the taskpanel is open
@@ -344,7 +392,10 @@ class GeometryElementsSelection(QtGui.QWidget):
self.setback_listobj_visibility()
FreeCADGui.Selection.clearSelection()
# start SelectionObserver and parse the function to add the References to the widget
print_message = "Single click on a " + self.sel_elem_text + " will add it to the list"
if self.selection_mode_solid: # print message on button click
print_message = self.selection_mode_solid_print_message
else:
print_message = self.selection_mode_std_print_message
if not self.sel_server:
# if we do not check, we would start a new SelectionObserver on every click on addReference button
# but close only one SelectionObserver on leaving the task panel
@@ -352,15 +403,48 @@ class GeometryElementsSelection(QtGui.QWidget):
self.sel_server = FemSelectionObserver.FemSelectionObserver(self.selectionParser, print_message)
def selectionParser(self, selection):
# print('selection: ', selection[0].Shape.ShapeType, ' ', selection[0].Name, ' ', selection[1])
if hasattr(selection[0], "Shape"):
if selection[1]:
elt = selection[0].Shape.getElement(selection[1])
if elt.ShapeType in self.sel_elem_types:
print('selection: ', selection[0].Shape.ShapeType, ' ', selection[0].Name, ' ', selection[1])
if hasattr(selection[0], "Shape") and selection[1]:
elt = selection[0].Shape.getElement(selection[1])
ele_ShapeType = elt.ShapeType
if self.selection_mode_solid and 'Solid' in self.sel_elem_types:
# in solid selection mode use edges and faces for selection of a solid
# adapt selection variable to hold the Solid
solid_to_add = None
if ele_ShapeType == 'Edge':
found_edge = False
for i, s in enumerate(selection[0].Shape.Solids):
for e in s.Edges:
if elt.isSame(e):
if not found_edge:
solid_to_add = str(i + 1)
else:
FreeCAD.Console.PrintMessage('Edge belongs to more than one solid\n')
solid_to_add = None
found_edge = True
elif ele_ShapeType == 'Face':
found_face = False
for i, s in enumerate(selection[0].Shape.Solids):
for e in s.Faces:
if elt.isSame(e):
if not found_face:
solid_to_add = str(i + 1)
else:
FreeCAD.Console.PrintMessage('Face belongs to more than one solid\n')
solid_to_add = None
found_edge = True
if solid_to_add:
selection = (selection[0], 'Solid' + solid_to_add)
ele_ShapeType = 'Solid'
print('selection variable adaped to hold the Solid: ', selection[0].Shape.ShapeType, ' ', selection[0].Name, ' ', selection[1])
else:
return
if ele_ShapeType in self.sel_elem_types:
if (self.selection_mode_solid and ele_ShapeType == 'Solid') or self.selection_mode_solid is False:
if selection not in self.references:
self.references.append(selection)
self.rebuild_list_References(self.get_allitems_text().index(self.get_item_text(selection)))
else:
FreeCAD.Console.PrintMessage(selection[0].Name + ' --> ' + selection[1] + ' is in reference list already!\n')
else:
FreeCAD.Console.PrintMessage(elt.ShapeType + ' not allowed to add to the list!\n')
else:
FreeCAD.Console.PrintMessage(ele_ShapeType + ' not allowed to add to the list!\n')

View File

@@ -33,9 +33,8 @@ import FemGui # needed to display the icons in TreeView
False if False else FemGui.__name__ # dummy usage of FemGui for flake8, just returns 'FemGui'
# for the panel
import femmesh.meshtools as FemMeshTools
from PySide import QtCore
from PySide import QtGui
from . import FemSelectionWidgets
class _ViewProviderFemMeshRegion:
@@ -104,215 +103,39 @@ class _TaskPanelFemMeshRegion:
'''The TaskPanel for editing References property of FemMeshRegion objects'''
def __init__(self, obj):
FreeCADGui.Selection.clearSelection()
self.sel_server = None
# parameter widget
self.obj = obj
self.selection_mode_solid = False
self.selection_mode_std_print_message = "Select Faces, Edges and Vertices by single click on them to add them to the list."
self.selection_mode_solid_print_message = "Select Solids by single click on a Face or Edge which belongs to the Solid, to add the Solid to the list."
self.obj_notvisible = []
self.parameterWidget = FreeCADGui.PySideUic.loadUi(FreeCAD.getHomePath() + "Mod/Fem/Resources/ui/MeshRegion.ui")
QtCore.QObject.connect(self.parameterWidget.if_elelen, QtCore.SIGNAL("valueChanged(Base::Quantity)"), self.elelen_changed)
self.init_parameter_widget()
self.form = FreeCADGui.PySideUic.loadUi(FreeCAD.getHomePath() + "Mod/Fem/Resources/ui/MeshRegion.ui")
QtCore.QObject.connect(self.form.if_elelen, QtCore.SIGNAL("valueChanged(Base::Quantity)"), self.elelen_changed)
QtCore.QObject.connect(self.form.rb_standard, QtCore.SIGNAL("toggled(bool)"), self.choose_selection_mode_standard)
QtCore.QObject.connect(self.form.rb_solid, QtCore.SIGNAL("toggled(bool)"), self.choose_selection_mode_solid)
QtCore.QObject.connect(self.form.pushButton_Reference, QtCore.SIGNAL("clicked()"), self.add_references)
self.form.list_References.itemSelectionChanged.connect(self.select_clicked_reference_shape)
self.form.list_References.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.form.list_References.connect(self.form.list_References, QtCore.SIGNAL("customContextMenuRequested(QPoint)"), self.references_list_right_clicked)
# geometry selection widget
self.selectionWidget = FemSelectionWidgets.GeometryElementsSelection(obj.References, ['Solid', 'Face', 'Edge', 'Vertex']) # start with Solid in list!
self.get_meshregion_props()
self.update()
# form made from param and selection widget
self.form = [self.parameterWidget, self.selectionWidget]
def accept(self):
self.setback_listobj_visibility()
self.set_meshregion_props()
if self.sel_server:
FreeCADGui.Selection.removeObserver(self.sel_server)
FreeCADGui.ActiveDocument.resetEdit()
self.obj.CharacteristicLength = self.elelen
self.obj.References = self.selectionWidget.references
FreeCAD.ActiveDocument.recompute()
self.set_back_all()
return True
def reject(self):
self.setback_listobj_visibility()
if self.sel_server:
FreeCADGui.Selection.removeObserver(self.sel_server)
FreeCADGui.ActiveDocument.resetEdit()
self.set_back_all()
return True
def get_meshregion_props(self):
def set_back_all(self):
self.selectionWidget.setback_listobj_visibility()
if self.selectionWidget.sel_server:
FreeCADGui.Selection.removeObserver(self.selectionWidget.sel_server)
FreeCADGui.ActiveDocument.resetEdit()
def init_parameter_widget(self):
self.elelen = self.obj.CharacteristicLength
self.references = []
if self.obj.References:
self.tuplereferences = self.obj.References
self.get_references()
def set_meshregion_props(self):
self.obj.References = self.references
self.obj.CharacteristicLength = self.elelen
def update(self):
'fills the widgets'
self.form.if_elelen.setText(self.elelen.UserString)
self.rebuild_list_References()
self.parameterWidget.if_elelen.setText(self.elelen.UserString)
def elelen_changed(self, base_quantity_value):
self.elelen = base_quantity_value
def choose_selection_mode_standard(self, state):
self.selection_mode_solid = not state
if self.sel_server and not self.selection_mode_solid:
print(self.selection_mode_std_print_message)
def choose_selection_mode_solid(self, state):
self.selection_mode_solid = state
if self.sel_server and self.selection_mode_solid:
print(self.selection_mode_solid_print_message)
def get_references(self):
for ref in self.tuplereferences:
for elem in ref[1]:
self.references.append((ref[0], elem))
def references_list_right_clicked(self, QPos):
self.form.contextMenu = QtGui.QMenu()
menu_item_remove_selected = self.form.contextMenu.addAction("Remove selected reference")
menu_item_remove_all = self.form.contextMenu.addAction("Remove all references")
if not self.references:
menu_item_remove_selected.setDisabled(True)
menu_item_remove_all.setDisabled(True)
self.form.connect(menu_item_remove_selected, QtCore.SIGNAL("triggered()"), self.remove_selected_reference)
self.form.connect(menu_item_remove_all, QtCore.SIGNAL("triggered()"), self.remove_all_references)
parentPosition = self.form.list_References.mapToGlobal(QtCore.QPoint(0, 0))
self.form.contextMenu.move(parentPosition + QPos)
self.form.contextMenu.show()
def remove_selected_reference(self):
if not self.references:
return
currentItemName = str(self.form.list_References.currentItem().text())
currentRow = self.form.list_References.currentRow()
for ref in self.references:
refname_to_compare_listentry = ref[0].Name + ':' + ref[1]
if refname_to_compare_listentry == currentItemName:
self.references.remove(ref)
self.rebuild_list_References(currentRow)
def remove_all_references(self):
self.references = []
self.rebuild_list_References()
def add_references(self):
'''Called if Button add_reference is triggered'''
# in constraints EditTaskPanel the selection is active as soon as the taskpanel is open
# here the addReference button EditTaskPanel has to be triggered to start selection mode
self.setback_listobj_visibility()
FreeCADGui.Selection.clearSelection()
# start SelectionObserver and parse the function to add the References to the widget
if self.selection_mode_solid: # print message on button click
print_message = self.selection_mode_solid_print_message
else:
print_message = self.selection_mode_std_print_message
if not self.sel_server:
# if we do not check, we would start a new SelectionObserver on every click on addReference button
# but close only one SelectionObserver on leaving the task panel
from . import FemSelectionObserver
self.sel_server = FemSelectionObserver.FemSelectionObserver(self.selectionParser, print_message)
def selectionParser(self, selection):
print('selection: ', selection[0].Shape.ShapeType, ' ', selection[0].Name, ' ', selection[1])
if hasattr(selection[0], "Shape") and selection[1]:
elt = selection[0].Shape.getElement(selection[1])
if self.selection_mode_solid:
# in solid selection mode use edges and faces for selection of a solid
solid_to_add = None
if elt.ShapeType == 'Edge':
found_edge = False
for i, s in enumerate(selection[0].Shape.Solids):
for e in s.Edges:
if elt.isSame(e):
if not found_edge:
solid_to_add = str(i + 1)
else:
FreeCAD.Console.PrintMessage('Edge belongs to more than one solid\n')
solid_to_add = None
found_edge = True
elif elt.ShapeType == 'Face':
found_face = False
for i, s in enumerate(selection[0].Shape.Solids):
for e in s.Faces:
if elt.isSame(e):
if not found_face:
solid_to_add = str(i + 1)
else:
FreeCAD.Console.PrintMessage('Face belongs to more than one solid\n')
solid_to_add = None
found_edge = True
if solid_to_add:
selection = (selection[0], 'Solid' + solid_to_add)
print('selection element changed to Solid: ', selection[0].Shape.ShapeType, ' ', selection[0].Name, ' ', selection[1])
else:
return
if selection not in self.references:
self.references.append(selection)
self.rebuild_list_References()
else:
FreeCAD.Console.PrintMessage(selection[0].Name + ' --> ' + selection[1] + ' is in reference list already!\n')
def rebuild_list_References(self, current_row=0):
self.form.list_References.clear()
items = []
for ref in self.references:
item_name = ref[0].Name + ':' + ref[1]
items.append(item_name)
for listItemName in sorted(items):
self.form.list_References.addItem(listItemName)
if current_row > self.form.list_References.count() - 1: # first row is 0
current_row = self.form.list_References.count() - 1
if self.form.list_References.count() > 0:
self.form.list_References.setCurrentItem(self.form.list_References.item(current_row))
def select_clicked_reference_shape(self):
self.setback_listobj_visibility()
if self.sel_server:
FreeCADGui.Selection.removeObserver(self.sel_server)
self.sel_server = None
if not self.sel_server:
if not self.references:
return
currentItemName = str(self.form.list_References.currentItem().text())
for ref in self.references:
refname_to_compare_listentry = ref[0].Name + ':' + ref[1]
if refname_to_compare_listentry == currentItemName:
# print('found: shape: ' + ref[0].Name + ' element: ' + ref[1])
if not ref[0].ViewObject.Visibility:
self.obj_notvisible.append(ref[0])
ref[0].ViewObject.Visibility = True
FreeCADGui.Selection.clearSelection()
if ref[1].startswith('Solid') and (ref[0].Shape.ShapeType == 'Compound' or ref[0].Shape.ShapeType == 'CompSolid'):
# selection of Solids of Compounds or CompSolids is not possible, because a Solid is no Subelement
# since only Subelements can be selected, we gone select all Faces of such an Solids
solid = FemMeshTools.get_element(ref[0], ref[1]) # the method getElement(element) does not return Solid elements
if not solid:
return
faces = []
for fs in solid.Faces:
# find these faces in ref[0]
for i, fref in enumerate(ref[0].Shape.Faces):
if fs.isSame(fref):
fref_elstring = 'Face' + str(i + 1)
if fref_elstring not in faces:
faces.append(fref_elstring)
for f in faces:
FreeCADGui.Selection.addSelection(ref[0], f)
else:
# Selection of all other element types is supported
FreeCADGui.Selection.addSelection(ref[0], ref[1])
def setback_listobj_visibility(self):
'''set back Visibility of the list objects
'''
FreeCADGui.Selection.clearSelection()
for obj in self.obj_notvisible:
obj.ViewObject.Visibility = False
self.obj_notvisible = []