From 046504234fc4a98bac5af761d2e6ef443b7fbdd5 Mon Sep 17 00:00:00 2001 From: logari81 Date: Thu, 14 Feb 2013 16:06:23 +0100 Subject: [PATCH] PartDesign: Intelligent defaults for the Reversed flag of Revolution/Groove features --- src/Mod/PartDesign/App/FeatureGroove.cpp | 79 +++++++++-------- src/Mod/PartDesign/App/FeatureGroove.h | 6 +- src/Mod/PartDesign/App/FeatureRevolution.cpp | 85 ++++++++++--------- src/Mod/PartDesign/App/FeatureRevolution.h | 6 +- src/Mod/PartDesign/Gui/Command.cpp | 10 ++- .../PartDesign/Gui/TaskGrooveParameters.cpp | 16 ++++ .../Gui/TaskRevolutionParameters.cpp | 16 ++++ 7 files changed, 135 insertions(+), 83 deletions(-) diff --git a/src/Mod/PartDesign/App/FeatureGroove.cpp b/src/Mod/PartDesign/App/FeatureGroove.cpp index 3cf382802..ea2d5391b 100644 --- a/src/Mod/PartDesign/App/FeatureGroove.cpp +++ b/src/Mod/PartDesign/App/FeatureGroove.cpp @@ -85,49 +85,17 @@ App::DocumentObjectExecReturn *Groove::execute(void) if (Reversed.getValue() && !Midplane.getValue()) angle *= (-1.0); - Part::Part2DObject* sketch = 0; std::vector wires; TopoDS_Shape support; try { - sketch = getVerifiedSketch(); wires = getSketchWires(); support = getSupportShape(); } catch (const Base::Exception& e) { return new App::DocumentObjectExecReturn(e.what()); } - // get the Sketch plane - Base::Placement SketchPlm = sketch->Placement.getValue(); - - // get reference axis - App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue(); - const std::vector &subReferenceAxis = ReferenceAxis.getSubValues(); - if (pcReferenceAxis && pcReferenceAxis == sketch) { - bool hasValidAxis=false; - Base::Axis axis; - if (subReferenceAxis[0] == "V_Axis") { - hasValidAxis = true; - axis = sketch->getAxis(Part::Part2DObject::V_Axis); - } - else if (subReferenceAxis[0] == "H_Axis") { - hasValidAxis = true; - axis = sketch->getAxis(Part::Part2DObject::H_Axis); - } - else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") { - int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str()); - if (AxId >= 0 && AxId < sketch->getAxisCount()) { - hasValidAxis = true; - axis = sketch->getAxis(AxId); - } - } - if (hasValidAxis) { - axis *= SketchPlm; - Base::Vector3d base=axis.getBase(); - Base::Vector3d dir=axis.getDirection(); - Base.setValue(base.x,base.y,base.z); - Axis.setValue(dir.x,dir.y,dir.z); - } - } + // update Axis from ReferenceAxis + updateAxis(); // get revolve axis Base::Vector3f b = Base.getValue(); @@ -198,10 +166,11 @@ App::DocumentObjectExecReturn *Groove::execute(void) } } -bool Groove::suggestReversed(void) const +bool Groove::suggestReversed(void) { - // suggest a value for Reversed flag so that material is removed from the support try { + updateAxis(); + Part::Part2DObject* sketch = getVerifiedSketch(); std::vector wires = getSketchWires(); TopoDS_Shape sketchshape = makeFace(wires); @@ -230,7 +199,43 @@ bool Groove::suggestReversed(void) const return norm * cross > 0.f; } catch (...) { - return false; + return Reversed.getValue(); + } +} + +void Groove::updateAxis(void) +{ + Part::Part2DObject* sketch = getVerifiedSketch(); + Base::Placement SketchPlm = sketch->Placement.getValue(); + + // get reference axis + App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue(); + const std::vector &subReferenceAxis = ReferenceAxis.getSubValues(); + if (pcReferenceAxis && pcReferenceAxis == sketch) { + bool hasValidAxis=false; + Base::Axis axis; + if (subReferenceAxis[0] == "V_Axis") { + hasValidAxis = true; + axis = sketch->getAxis(Part::Part2DObject::V_Axis); + } + else if (subReferenceAxis[0] == "H_Axis") { + hasValidAxis = true; + axis = sketch->getAxis(Part::Part2DObject::H_Axis); + } + else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") { + int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str()); + if (AxId >= 0 && AxId < sketch->getAxisCount()) { + hasValidAxis = true; + axis = sketch->getAxis(AxId); + } + } + if (hasValidAxis) { + axis *= SketchPlm; + Base::Vector3d base=axis.getBase(); + Base::Vector3d dir=axis.getDirection(); + Base.setValue(base.x,base.y,base.z); + Axis.setValue(dir.x,dir.y,dir.z); + } } } diff --git a/src/Mod/PartDesign/App/FeatureGroove.h b/src/Mod/PartDesign/App/FeatureGroove.h index 8fb2f2d99..03616a0ff 100644 --- a/src/Mod/PartDesign/App/FeatureGroove.h +++ b/src/Mod/PartDesign/App/FeatureGroove.h @@ -63,7 +63,11 @@ public: } //@} - bool suggestReversed(void) const; + /// suggests a value for Reversed flag so that material is always removed from the support + bool suggestReversed(void); +protected: + /// updates Axis from ReferenceAxis + void updateAxis(void); }; } //namespace PartDesign diff --git a/src/Mod/PartDesign/App/FeatureRevolution.cpp b/src/Mod/PartDesign/App/FeatureRevolution.cpp index 6745da5f4..790f21cf8 100644 --- a/src/Mod/PartDesign/App/FeatureRevolution.cpp +++ b/src/Mod/PartDesign/App/FeatureRevolution.cpp @@ -43,7 +43,6 @@ #include #include "FeatureRevolution.h" -#include using namespace PartDesign; @@ -86,10 +85,8 @@ App::DocumentObjectExecReturn *Revolution::execute(void) if (Reversed.getValue() && !Midplane.getValue()) angle *= (-1.0); - Part::Part2DObject* sketch = 0; std::vector wires; try { - sketch = getVerifiedSketch(); wires = getSketchWires(); } catch (const Base::Exception& e) { return new App::DocumentObjectExecReturn(e.what()); @@ -103,41 +100,8 @@ App::DocumentObjectExecReturn *Revolution::execute(void) support = TopoDS_Shape(); } - // get the Sketch plane - Base::Placement SketchPlm = sketch->Placement.getValue(); - - // get reference axis - App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue(); - const std::vector &subReferenceAxis = ReferenceAxis.getSubValues(); - bool hasValidAxis=false; - if (pcReferenceAxis && pcReferenceAxis == sketch) { - Base::Axis axis; - if (subReferenceAxis[0] == "V_Axis") { - hasValidAxis = true; - axis = sketch->getAxis(Part::Part2DObject::V_Axis); - } - else if (subReferenceAxis[0] == "H_Axis") { - hasValidAxis = true; - axis = sketch->getAxis(Part::Part2DObject::H_Axis); - } - else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") { - int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str()); - if (AxId >= 0 && AxId < sketch->getAxisCount()) { - hasValidAxis = true; - axis = sketch->getAxis(AxId); - } - } - if (hasValidAxis) { - axis *= SketchPlm; - Base::Vector3d base=axis.getBase(); - Base::Vector3d dir=axis.getDirection(); - Base.setValue(base.x,base.y,base.z); - Axis.setValue(dir.x,dir.y,dir.z); - } - } - if (!hasValidAxis) { - return new App::DocumentObjectExecReturn("No valid reference axis defined"); - } + // update Axis from ReferenceAxis + updateAxis(); // get revolve axis Base::Vector3f b = Base.getValue(); @@ -207,10 +171,11 @@ App::DocumentObjectExecReturn *Revolution::execute(void) } } -bool Revolution::suggestReversed(void) const +bool Revolution::suggestReversed(void) { - // suggest a value for Reversed flag so that material is added to the support try { + updateAxis(); + Part::Part2DObject* sketch = getVerifiedSketch(); std::vector wires = getSketchWires(); TopoDS_Shape sketchshape = makeFace(wires); @@ -235,11 +200,47 @@ bool Revolution::suggestReversed(void) const // simply convert double to float Base::Vector3f norm(SketchNormal.x, SketchNormal.y, SketchNormal.z); - // return true if the angle between norm and cross is acute + // return true if the angle between norm and cross is obtuse return norm * cross < 0.f; } catch (...) { - return false; + return Reversed.getValue(); + } +} + +void Revolution::updateAxis(void) +{ + Part::Part2DObject* sketch = getVerifiedSketch(); + Base::Placement SketchPlm = sketch->Placement.getValue(); + + // get reference axis + App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue(); + const std::vector &subReferenceAxis = ReferenceAxis.getSubValues(); + if (pcReferenceAxis && pcReferenceAxis == sketch) { + bool hasValidAxis=false; + Base::Axis axis; + if (subReferenceAxis[0] == "V_Axis") { + hasValidAxis = true; + axis = sketch->getAxis(Part::Part2DObject::V_Axis); + } + else if (subReferenceAxis[0] == "H_Axis") { + hasValidAxis = true; + axis = sketch->getAxis(Part::Part2DObject::H_Axis); + } + else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") { + int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str()); + if (AxId >= 0 && AxId < sketch->getAxisCount()) { + hasValidAxis = true; + axis = sketch->getAxis(AxId); + } + } + if (hasValidAxis) { + axis *= SketchPlm; + Base::Vector3d base=axis.getBase(); + Base::Vector3d dir=axis.getDirection(); + Base.setValue(base.x,base.y,base.z); + Axis.setValue(dir.x,dir.y,dir.z); + } } } diff --git a/src/Mod/PartDesign/App/FeatureRevolution.h b/src/Mod/PartDesign/App/FeatureRevolution.h index 89a79b5a3..14e0c9a0a 100644 --- a/src/Mod/PartDesign/App/FeatureRevolution.h +++ b/src/Mod/PartDesign/App/FeatureRevolution.h @@ -63,7 +63,11 @@ public: } //@} - bool suggestReversed(void) const; + /// suggests a value for Reversed flag so that material is always added to the support + bool suggestReversed(void); +protected: + /// updates Axis from ReferenceAxis + void updateAxis(void); }; } //namespace PartDesign diff --git a/src/Mod/PartDesign/Gui/Command.cpp b/src/Mod/PartDesign/Gui/Command.cpp index 975412ab9..67959a741 100644 --- a/src/Mod/PartDesign/Gui/Command.cpp +++ b/src/Mod/PartDesign/Gui/Command.cpp @@ -48,8 +48,8 @@ #include #include -#include -#include +#include +#include using namespace std; @@ -343,6 +343,9 @@ void CmdPartDesignRevolution::activated(int iMsg) doCommand(Doc,"App.activeDocument().%s.ReferenceAxis = (App.activeDocument().%s,['V_Axis'])", FeatName.c_str(), sketch->getNameInDocument()); doCommand(Doc,"App.activeDocument().%s.Angle = 360.0",FeatName.c_str()); + PartDesign::Revolution* pcRevolution = static_cast(getDocument()->getObject(FeatName.c_str())); + if (pcRevolution && pcRevolution->suggestReversed()) + doCommand(Doc,"App.activeDocument().%s.Reversed = 1",FeatName.c_str()); updateActive(); if (isActiveObjectValid()) { doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument()); @@ -413,6 +416,9 @@ void CmdPartDesignGroove::activated(int iMsg) doCommand(Doc,"App.activeDocument().%s.ReferenceAxis = (App.activeDocument().%s,['V_Axis'])", FeatName.c_str(), sketch->getNameInDocument()); doCommand(Doc,"App.activeDocument().%s.Angle = 360.0",FeatName.c_str()); + PartDesign::Groove* pcGroove = static_cast(getDocument()->getObject(FeatName.c_str())); + if (pcGroove && pcGroove->suggestReversed()) + doCommand(Doc,"App.activeDocument().%s.Reversed = 1",FeatName.c_str()); updateActive(); if (isActiveObjectValid()) { doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument()); diff --git a/src/Mod/PartDesign/Gui/TaskGrooveParameters.cpp b/src/Mod/PartDesign/Gui/TaskGrooveParameters.cpp index 8ec594037..8e3c1add2 100644 --- a/src/Mod/PartDesign/Gui/TaskGrooveParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskGrooveParameters.cpp @@ -134,6 +134,9 @@ void TaskGrooveParameters::onAxisChanged(int num) PartDesign::Groove* pcGroove = static_cast(GrooveView->getObject()); Sketcher::SketchObject *pcSketch = static_cast(pcGroove->Sketch.getValue()); if (pcSketch) { + App::DocumentObject *oldRefAxis = pcGroove->ReferenceAxis.getValue(); + std::vector oldSubRefAxis = pcGroove->ReferenceAxis.getSubValues(); + int maxcount = pcSketch->getAxisCount()+2; if (num == 0) pcGroove->ReferenceAxis.setValue(pcSketch, std::vector(1,"V_Axis")); @@ -146,6 +149,19 @@ void TaskGrooveParameters::onAxisChanged(int num) } if (num < maxcount && ui->axis->count() > maxcount) ui->axis->setMaxCount(maxcount); + + const std::vector &newSubRefAxis = pcGroove->ReferenceAxis.getSubValues(); + if (oldRefAxis != pcSketch || + oldSubRefAxis.size() != newSubRefAxis.size() || + oldSubRefAxis[0] != newSubRefAxis[0]) { + bool reversed = pcGroove->suggestReversed(); + if (reversed != pcGroove->Reversed.getValue()) { + pcGroove->Reversed.setValue(reversed); + ui->checkBoxReversed->blockSignals(true); + ui->checkBoxReversed->setChecked(reversed); + ui->checkBoxReversed->blockSignals(false); + } + } } if (updateView()) pcGroove->getDocument()->recomputeFeature(pcGroove); diff --git a/src/Mod/PartDesign/Gui/TaskRevolutionParameters.cpp b/src/Mod/PartDesign/Gui/TaskRevolutionParameters.cpp index c5e6f75b9..abbf000c4 100644 --- a/src/Mod/PartDesign/Gui/TaskRevolutionParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskRevolutionParameters.cpp @@ -134,6 +134,9 @@ void TaskRevolutionParameters::onAxisChanged(int num) PartDesign::Revolution* pcRevolution = static_cast(RevolutionView->getObject()); Sketcher::SketchObject *pcSketch = static_cast(pcRevolution->Sketch.getValue()); if (pcSketch) { + App::DocumentObject *oldRefAxis = pcRevolution->ReferenceAxis.getValue(); + std::vector oldSubRefAxis = pcRevolution->ReferenceAxis.getSubValues(); + int maxcount = pcSketch->getAxisCount()+2; if (num == 0) pcRevolution->ReferenceAxis.setValue(pcSketch, std::vector(1,"V_Axis")); @@ -146,6 +149,19 @@ void TaskRevolutionParameters::onAxisChanged(int num) } if (num < maxcount && ui->axis->count() > maxcount) ui->axis->setMaxCount(maxcount); + + const std::vector &newSubRefAxis = pcRevolution->ReferenceAxis.getSubValues(); + if (oldRefAxis != pcSketch || + oldSubRefAxis.size() != newSubRefAxis.size() || + oldSubRefAxis[0] != newSubRefAxis[0]) { + bool reversed = pcRevolution->suggestReversed(); + if (reversed != pcRevolution->Reversed.getValue()) { + pcRevolution->Reversed.setValue(reversed); + ui->checkBoxReversed->blockSignals(true); + ui->checkBoxReversed->setChecked(reversed); + ui->checkBoxReversed->blockSignals(false); + } + } } if (updateView()) pcRevolution->getDocument()->recomputeFeature(pcRevolution);