FEM: constraint pressure: some changes in core implementation

This commit is contained in:
vdwalts 2016-08-01 21:58:30 +01:00 committed by wmayer
parent 0e7c97b356
commit 26dfd6ef92
3 changed files with 196 additions and 137 deletions

View File

@ -45,6 +45,12 @@
#include <App/Application.h>
#include <Gui/Command.h>
#include <Gui/Selection.h>
#include <Gui/SelectionFilter.h>
using namespace FemGui;
using namespace Gui;
@ -52,7 +58,7 @@ using namespace Gui;
TaskFemConstraintPressure::TaskFemConstraintPressure(ViewProviderFemConstraintPressure *ConstraintView,QWidget *parent)
: TaskFemConstraint(ConstraintView, parent, "fem-constraint-pressure")
{
{ //Note change "pressure" in line above to new constraint name
proxy = new QWidget(this);
ui = new Ui_TaskFemConstraintPressure();
ui->setupUi(proxy);
@ -62,35 +68,28 @@ TaskFemConstraintPressure::TaskFemConstraintPressure(ViewProviderFemConstraintPr
action->connect(action, SIGNAL(triggered()), this, SLOT(onReferenceDeleted()));
ui->lw_references->addAction(action);
ui->lw_references->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(ui->if_pressure, SIGNAL(valueChanged(Base::Quantity)),
this, SLOT(onPressureChanged(Base::Quantity)));
connect(ui->b_add_reference, SIGNAL(pressed()),
this, SLOT(onButtonReference()));
connect(ui->cb_reverse_direction, SIGNAL(toggled(bool)),
this, SLOT(onCheckReverse(bool)));
connect(ui->lw_references, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(setSelection(QListWidgetItem*)));
this->groupLayout()->addWidget(proxy);
// Temporarily prevent unnecessary feature recomputes
ui->if_pressure->blockSignals(true);
ui->lw_references->blockSignals(true);
ui->b_add_reference->blockSignals(true);
ui->cb_reverse_direction->blockSignals(true);
/* Note: */
// Get the feature data
Fem::ConstraintPressure* pcConstraint = static_cast<Fem::ConstraintPressure*>(ConstraintView->getObject());
double f = pcConstraint->Pressure.getValue();
std::vector<App::DocumentObject*> Objects = pcConstraint->References.getValues();
std::vector<std::string> SubElements = pcConstraint->References.getSubValues();
bool reversed = pcConstraint->Reversed.getValue();
// Fill data into dialog elements
ui->if_pressure->setMinimum(0);
ui->if_pressure->setMaximum(FLOAT_MAX);
//1000 because FreeCAD used kPa internally
Base::Quantity p = Base::Quantity(1000 * f, Base::Unit::Stress);
ui->if_pressure->setValue(p);
Base::Quantity p = Base::Quantity(1000 * (pcConstraint->Pressure.getValue()), Base::Unit::Stress);
ui->if_pressure->setValue(p);
bool reversed = pcConstraint->Reversed.getValue();
ui->checkBoxReverse->setChecked(reversed);
/* */
ui->lw_references->clear();
for (std::size_t i = 0; i < Objects.size(); i++) {
ui->lw_references->addItem(makeRefText(Objects[i], SubElements[i]));
@ -98,12 +97,10 @@ TaskFemConstraintPressure::TaskFemConstraintPressure(ViewProviderFemConstraintPr
if (Objects.size() > 0) {
ui->lw_references->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
}
ui->cb_reverse_direction->setChecked(reversed);
ui->if_pressure->blockSignals(false);
ui->lw_references->blockSignals(false);
ui->b_add_reference->blockSignals(false);
ui->cb_reverse_direction->blockSignals(false);
//Selection buttons
connect(ui->btnAdd, SIGNAL(clicked()), this, SLOT(addToSelection()));
connect(ui->btnRemove, SIGNAL(clicked()), this, SLOT(removeFromSelection()));
updateUI();
}
@ -122,71 +119,132 @@ void TaskFemConstraintPressure::updateUI()
}
}
void TaskFemConstraintPressure::onSelectionChanged(const Gui::SelectionChanges& msg)
void TaskFemConstraintPressure::addToSelection()
{
if ((msg.Type != Gui::SelectionChanges::AddSelection) ||
// Don't allow selection in other document
(strcmp(msg.pDocName, ConstraintView->getObject()->getDocument()->getName()) != 0) ||
// Don't allow selection mode none
(selectionMode != selref) ||
// Don't allow empty smenu/submenu
(!msg.pSubName || msg.pSubName[0] == '\0')) {
std::vector<Gui::SelectionObject> selection = Gui::Selection().getSelectionEx(); //gets vector of selected objects of active document
if (selection.size()==0){
QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!"));
return;
}
std::string subName(msg.pSubName);
Fem::ConstraintPressure* pcConstraint = static_cast<Fem::ConstraintPressure*>(ConstraintView->getObject());
App::DocumentObject* obj = ConstraintView->getObject()->getDocument()->getObject(msg.pObjectName);
std::vector<App::DocumentObject*> Objects = pcConstraint->References.getValues();
std::vector<std::string> SubElements = pcConstraint->References.getSubValues();
if (subName.substr(0,4) != "Face") {
QMessageBox::warning(this, tr("Selection error"), tr("Only faces can be picked"));
return;
}
// Avoid duplicates
std::size_t pos = 0;
for (; pos < Objects.size(); pos++) {
if (obj == Objects[pos])
break;
}
if (pos != Objects.size()) {
if (subName == SubElements[pos])
for (std::vector<Gui::SelectionObject>::iterator it = selection.begin(); it != selection.end(); ++it){//for every selected object
if (static_cast<std::string>(it->getTypeName()).substr(0,4).compare(std::string("Part"))!=0){
QMessageBox::warning(this, tr("Selection error"),tr("Selected object is not a part!"));
return;
}
std::vector<std::string> subNames=it->getSubNames();
App::DocumentObject* obj = ConstraintView->getObject()->getDocument()->getObject(it->getFeatName());
for (unsigned int subIt=0;subIt<(subNames.size());++subIt){// for every selected sub element
bool addMe=true;
if (subNames[subIt].substr(0,4) != "Face") {
QMessageBox::warning(this, tr("Selection error"), tr("Only faces can be picked"));
return;
}
for (std::vector<std::string>::iterator itr=std::find(SubElements.begin(),SubElements.end(),subNames[subIt]);
itr!= SubElements.end();
itr = std::find(++itr,SubElements.end(),subNames[subIt]))
{// for every sub element in selection that matches one in old list
if (obj==Objects[std::distance(SubElements.begin(),itr)]){//if selected sub element's object equals the one in old list then it was added before so don't add
addMe=false;
}
}
if (addMe){
disconnect(ui->lw_references, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(setSelection(QListWidgetItem*)));
Objects.push_back(obj);
SubElements.push_back(subNames[subIt]);
ui->lw_references->addItem(makeRefText(obj, subNames[subIt]));
connect(ui->lw_references, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(setSelection(QListWidgetItem*)));
}
}
}
// add the new reference
Objects.push_back(obj);
SubElements.push_back(subName);
//Update UI
pcConstraint->References.setValues(Objects,SubElements);
ui->lw_references->addItem(makeRefText(obj, subName));
// Turn off reference selection mode
onButtonReference(false);
Gui::Selection().clearSelection();
updateUI();
}
void TaskFemConstraintPressure::onPressureChanged(const Base::Quantity& f)
void TaskFemConstraintPressure::removeFromSelection()
{
std::vector<Gui::SelectionObject> selection = Gui::Selection().getSelectionEx(); //gets vector of selected objects of active document
if (selection.size()==0){
QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!"));
return;
}
Fem::ConstraintPressure* pcConstraint = static_cast<Fem::ConstraintPressure*>(ConstraintView->getObject());
double val = f.getValueAs(Base::Quantity::MegaPascal);
pcConstraint->Pressure.setValue(val);
std::vector<App::DocumentObject*> Objects = pcConstraint->References.getValues();
std::vector<std::string> SubElements = pcConstraint->References.getSubValues();
std::vector<int> itemsToDel;
for (std::vector<Gui::SelectionObject>::iterator it = selection.begin(); it != selection.end(); ++it){//for every selected object
if (static_cast<std::string>(it->getTypeName()).substr(0,4).compare(std::string("Part"))!=0){
QMessageBox::warning(this, tr("Selection error"),tr("Selected object is not a part!"));
return;
}
std::vector<std::string> subNames=it->getSubNames();
App::DocumentObject* obj = ConstraintView->getObject()->getDocument()->getObject(it->getFeatName());
for (unsigned int subIt=0;subIt<(subNames.size());++subIt){// for every selected sub element
for (std::vector<std::string>::iterator itr=std::find(SubElements.begin(),SubElements.end(),subNames[subIt]);
itr!= SubElements.end();
itr = std::find(++itr,SubElements.end(),subNames[subIt]))
{// for every sub element in selection that matches one in old list
if (obj==Objects[std::distance(SubElements.begin(),itr)]){//if selected sub element's object equals the one in old list then it was added before so mark for deletion
itemsToDel.push_back(std::distance(SubElements.begin(),itr));
}
}
}
}
std::sort(itemsToDel.begin(),itemsToDel.end());
while (itemsToDel.size()>0){
Objects.erase(Objects.begin()+itemsToDel.back());
SubElements.erase(SubElements.begin()+itemsToDel.back());
itemsToDel.pop_back();
}
//Update UI
disconnect(ui->lw_references, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(setSelection(QListWidgetItem*)));
ui->lw_references->clear();
for (unsigned int j=0;j<Objects.size();j++){
ui->lw_references->addItem(makeRefText(Objects[j], SubElements[j]));
}
connect(ui->lw_references, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(setSelection(QListWidgetItem*)));
pcConstraint->References.setValues(Objects,SubElements);
updateUI();
}
void TaskFemConstraintPressure::setSelection(QListWidgetItem* item){
std::string docName=ConstraintView->getObject()->getDocument()->getName();
std::string s = item->text().toStdString();
std::string delimiter = ":";
size_t pos = 0;
std::string objName;
std::string subName;
pos = s.find(delimiter);
objName = s.substr(0, pos);
s.erase(0, pos + delimiter.length());
subName=s;
Gui::Selection().clearSelection();
Gui::Selection().addSelection(docName.c_str(),objName.c_str(),subName.c_str(),0,0,0);
}
void TaskFemConstraintPressure::onReferenceDeleted() {
int row = ui->lw_references->currentIndex().row();
TaskFemConstraint::onReferenceDeleted(row);
ui->lw_references->model()->removeRow(row);
ui->lw_references->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
}
void TaskFemConstraintPressure::onCheckReverse(const bool pressed)
{
Fem::ConstraintPressure* pcConstraint = static_cast<Fem::ConstraintPressure*>(ConstraintView->getObject());
pcConstraint->Reversed.setValue(pressed);
TaskFemConstraintPressure::removeFromSelection();
}
const std::string TaskFemConstraintPressure::getReferences() const
@ -199,26 +257,21 @@ const std::string TaskFemConstraintPressure::getReferences() const
return TaskFemConstraint::getReferences(items);
}
double TaskFemConstraintPressure::getPressure(void) const
/* Note: */
double TaskFemConstraintPressure::get_Pressure() const
{
Base::Quantity pressure = ui->if_pressure->getQuantity();
double pressure_in_MPa = pressure.getValueAs(Base::Quantity::MegaPascal);
return pressure_in_MPa;
}
bool TaskFemConstraintPressure::getReverse() const
bool TaskFemConstraintPressure::get_Reverse() const
{
return ui->cb_reverse_direction->isChecked();
return ui->checkBoxReverse->isChecked();
}
/* */
void TaskFemConstraintPressure::changeEvent(QEvent *e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->if_pressure->blockSignals(true);
ui->retranslateUi(proxy);
ui->if_pressure->blockSignals(false);
}
void TaskFemConstraintPressure::changeEvent(QEvent *e){
}
//**************************************************************************
@ -249,33 +302,23 @@ void TaskDlgFemConstraintPressure::open()
bool TaskDlgFemConstraintPressure::accept()
{
/* Note: */
std::string name = ConstraintView->getObject()->getNameInDocument();
const TaskFemConstraintPressure* parameterPressure = static_cast<const TaskFemConstraintPressure*>(parameter);
std::string scale = "1";
try {
if (parameterPressure->getPressure()<=0)
{
QMessageBox::warning(parameter, tr("Input error"), tr("Please specify a pressure greater than 0"));
return false;
}
else
{
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Pressure = %f",
name.c_str(), parameterPressure->getPressure());
}
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Pressure = %f",
name.c_str(), parameterPressure->get_Pressure());
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Reversed = %s",
name.c_str(), parameterPressure->getReverse() ? "True" : "False");
scale = parameterPressure->getScale(); //OvG: determine modified scale
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Scale = %s",
name.c_str(), scale.c_str()); //OvG: implement modified scale
name.c_str(), parameterPressure->get_Reverse() ? "True" : "False");
std::string scale = parameterPressure->getScale(); //OvG: determine modified scale
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Scale = %s", name.c_str(), scale.c_str()); //OvG: implement modified scale
}
catch (const Base::Exception& e) {
QMessageBox::warning(parameter, tr("Input error"), QString::fromLatin1(e.what()));
return false;
}
/* */
return TaskDlgFemConstraint::accept();
}

View File

@ -1,6 +1,9 @@
/***************************************************************************
* Copyright (c) 2015 Przemo Firszt <przemo@firszt.eu> *
* Based on Force constraint *
* Copyright (c) 2015 FreeCAD Developers *
* Authors: Michael Hindley <hindlemp@eskom.co.za> *
* Ruan Olwagen <olwager@eskom.co.za> *
* Oswald van Ginkel <vginkeo@eskom.co.za> *
* Based on Force constraint by Jan Rheinländer *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
@ -32,6 +35,11 @@
#include "TaskFemConstraint.h"
#include "ViewProviderFemConstraintPressure.h"
#include <QObject>
#include <Base/Console.h>
#include <App/DocumentObject.h>
#include <QListWidgetItem>
class Ui_TaskFemConstraintPressure;
namespace FemGui {
@ -41,23 +49,26 @@ class TaskFemConstraintPressure : public TaskFemConstraint
public:
TaskFemConstraintPressure(ViewProviderFemConstraintPressure *ConstraintView,QWidget *parent = 0);
virtual ~TaskFemConstraintPressure();
double getPressure(void) const;
virtual const std::string getReferences() const;
bool getReverse(void) const;
~TaskFemConstraintPressure();
const std::string getReferences() const;
double get_Pressure()const;
bool get_Reverse()const;
private Q_SLOTS:
void onReferenceDeleted(void);
void onPressureChanged(const Base::Quantity & f);
void onCheckReverse(bool);
void addToSelection();
void removeFromSelection();
void setSelection(QListWidgetItem* item);
protected:
virtual void changeEvent(QEvent *e);
void changeEvent(QEvent *e);
private:
virtual void onSelectionChanged(const Gui::SelectionChanges& msg);
//void onSelectionChanged(const Gui::SelectionChanges& msg);
void updateUI();
Ui_TaskFemConstraintPressure* ui;
};
class TaskDlgFemConstraintPressure : public TaskDlgFemConstraint
@ -66,9 +77,9 @@ class TaskDlgFemConstraintPressure : public TaskDlgFemConstraint
public:
TaskDlgFemConstraintPressure(ViewProviderFemConstraintPressure *ConstraintView);
virtual void open();
virtual bool accept();
virtual bool reject();
void open();
bool accept();
bool reject();
};
} //namespace FemGui

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>257</width>
<height>250</height>
<width>226</width>
<height>253</height>
</rect>
</property>
<property name="windowTitle">
@ -15,19 +15,37 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="b_add_reference">
<widget class="QLabel" name="lbl_info">
<property name="text">
<string>Add reference</string>
<string>Select multiple face(s), click Add or Remove</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="hLayout1">
<item>
<widget class="QPushButton" name="btnAdd">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemove">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="lw_references"/>
</item>
<item>
<layout class="QHBoxLayout" name="layoutPressure">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="l_pressure">
<widget class="QLabel" name="labelParameter1">
<property name="text">
<string>Pressure</string>
</property>
@ -36,7 +54,7 @@
<item>
<widget class="Gui::InputField" name="if_pressure">
<property name="text">
<string>1 MPa</string>
<string>0 MPa</string>
</property>
<property name="unit" stdset="0">
<string notr="true"/>
@ -46,25 +64,12 @@
</layout>
</item>
<item>
<widget class="QCheckBox" name="cb_reverse_direction">
<widget class="QCheckBox" name="checkBoxReverse">
<property name="text">
<string>Reverse direction</string>
<string>Reverse Direction</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>17</width>
<height>56</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>