diff --git a/src/Gui/Placement.cpp b/src/Gui/Placement.cpp index 9cabe08dad..3abf904b5d 100644 --- a/src/Gui/Placement.cpp +++ b/src/Gui/Placement.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -263,6 +264,32 @@ void Placement::onPlacementChanged(int) /*emit*/ placementChanged(data, incr, false); } +void Placement::on_centerOfMass_toggled(bool on) +{ + ui->xCnt->setDisabled(on); + ui->yCnt->setDisabled(on); + ui->zCnt->setDisabled(on); + + if (on) { + cntOfMass.Set(0,0,0); + std::vector sel = Gui::Selection().getObjectsOfType + (App::GeoFeature::getClassTypeId()); + if (!sel.empty()) { + for (auto it : sel) { + const App::PropertyComplexGeoData* propgeo = static_cast(it)->getPropertyOfGeometry(); + const Data::ComplexGeoData* geodata = propgeo ? propgeo->getComplexData() : nullptr; + if (geodata && geodata->getCenterOfGravity(cntOfMass)) { + break; + } + } + } + + ui->xCnt->setValue(cntOfMass.x); + ui->yCnt->setValue(cntOfMass.y); + ui->zCnt->setValue(cntOfMass.z); + } +} + void Placement::on_applyIncrementalPlacement_toggled(bool on) { if (on) { @@ -419,6 +446,15 @@ Base::Placement Placement::getPlacement() const return p; } +Base::Vector3d Placement::getCenterData() const +{ + if (ui->centerOfMass->isChecked()) + return this->cntOfMass; + return Base::Vector3d(ui->xCnt->value().getValue(), + ui->yCnt->value().getValue(), + ui->zCnt->value().getValue()); +} + Base::Placement Placement::getPlacementData() const { int index = ui->rotationInput->currentIndex(); @@ -427,7 +463,7 @@ Base::Placement Placement::getPlacementData() const Base::Vector3d cnt; pos = Base::Vector3d(ui->xPos->value().getValue(),ui->yPos->value().getValue(),ui->zPos->value().getValue()); - cnt = Base::Vector3d(ui->xCnt->value().getValue(),ui->yCnt->value().getValue(),ui->zCnt->value().getValue()); + cnt = getCenterData(); if (index == 0) { Base::Vector3d dir = getDirection(); @@ -448,6 +484,7 @@ QString Placement::getPlacementString() const { QString cmd; int index = ui->rotationInput->currentIndex(); + Base::Vector3d cnt = getCenterData(); if (index == 0) { Base::Vector3d dir = getDirection(); @@ -460,9 +497,9 @@ QString Placement::getPlacementString() const .arg(dir.y) .arg(dir.z) .arg(ui->angle->value().getValue()) - .arg(ui->xCnt->value().getValue()) - .arg(ui->yCnt->value().getValue()) - .arg(ui->zCnt->value().getValue()); + .arg(cnt.x) + .arg(cnt.y) + .arg(cnt.z); } else if (index == 1) { cmd = QString::fromLatin1( @@ -473,9 +510,9 @@ QString Placement::getPlacementString() const .arg(ui->yawAngle->value().getValue()) .arg(ui->pitchAngle->value().getValue()) .arg(ui->rollAngle->value().getValue()) - .arg(ui->xCnt->value().getValue()) - .arg(ui->yCnt->value().getValue()) - .arg(ui->zCnt->value().getValue()); + .arg(cnt.x) + .arg(cnt.y) + .arg(cnt.z); } return cmd; diff --git a/src/Gui/Placement.h b/src/Gui/Placement.h index ec2d19e6c0..f2926fddb9 100644 --- a/src/Gui/Placement.h +++ b/src/Gui/Placement.h @@ -63,11 +63,13 @@ private Q_SLOTS: void on_applyIncrementalPlacement_toggled(bool); void onPlacementChanged(int); void on_resetButton_clicked(); + void on_centerOfMass_toggled(bool); private: bool onApply(); void setPlacementData(const Base::Placement&); Base::Placement getPlacementData() const; + Base::Vector3d getCenterData() const; QString getPlacementString() const; void directionActivated(int); void applyPlacement(const Base::Placement& p, bool incremental); @@ -87,6 +89,7 @@ private: QSignalMapper* signalMapper; Connection connectAct; Base::Placement ref; + Base::Vector3d cntOfMass; std::string propertyName; // the name of the placement property std::set documents; diff --git a/src/Gui/Placement.ui b/src/Gui/Placement.ui index 0ce4827c69..00725d5d8a 100644 --- a/src/Gui/Placement.ui +++ b/src/Gui/Placement.ui @@ -108,7 +108,7 @@ 6 - + Qt::Vertical @@ -169,6 +169,13 @@ + + + + Use center of mass + + +