PartDesign/Feature: add an optional parameter to getBaseObject() to prevent it from throwing exceptions

In most cases getBaseObject() is used with an exception handler which
only detects if it failed but not the reason. This modification allows
to use it without excess exception handlers.
Also add the same parameter to SketchBased::getVerifiedSketch().
This commit is contained in:
Alexander Golubev 2015-07-26 09:21:00 +03:00 committed by Stefan Tröger
parent 013381ccb6
commit 55f3f52f54
10 changed files with 121 additions and 69 deletions

View File

@ -91,15 +91,26 @@ const gp_Pnt Feature::getPointFromFace(const TopoDS_Face& f)
throw Base::Exception("getPointFromFace(): Not implemented yet for this case");
}
Part::Feature* Feature::getBaseObject() const {
Part::Feature* Feature::getBaseObject(bool silent) const {
App::DocumentObject* BaseLink = BaseFeature.getValue();
if (BaseLink == NULL) throw Base::Exception("Base property not set");
Part::Feature* BaseObject = NULL;
if (BaseLink->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
BaseObject = static_cast<Part::Feature*>(BaseLink);
Part::Feature* BaseObject = nullptr;
const char *err = nullptr;
if (BaseObject == NULL)
throw Base::Exception("No base feature linked");
if (BaseLink) {
if (BaseLink->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) {
BaseObject = static_cast<Part::Feature*>(BaseLink);
}
if (!BaseObject) {
err = "No base feature linked";
}
} else {
err = "Base property not set";
}
// If the funtion not in silent mode throw the exception discribing the error
if (!silent && err) {
throw Base::Exception(err);
}
return BaseObject;
}

View File

@ -56,8 +56,13 @@ public:
/// Check whether the given feature is a datum feature
static bool isDatum(const App::DocumentObject* feature);
/// Returns the BaseFeature property's object (if any)
virtual Part::Feature* getBaseObject() const;
/**
* Returns the BaseFeature property's object (if any)
* @param silent if couldn't determine the base feature and silent == true,
* silently return a nullptr, otherwice throw Base::Exception.
* Default is false.
*/
virtual Part::Feature* getBaseObject(bool silent=false) const;
/// Returns the BaseFeature property's shape (if any)
virtual const TopoDS_Shape& getBaseShape() const;
/// Returns the BaseFeature property's TopoShape (if any)

View File

@ -69,10 +69,9 @@ short Boolean::mustExecute() const
App::DocumentObjectExecReturn *Boolean::execute(void)
{
// Check the parameters
const Part::Feature* baseFeature;
try {
baseFeature = getBaseObject();
} catch (const Base::Exception&) {
const Part::Feature* baseFeature = this->getBaseObject(/* silent = */ true);
if (!baseFeature) {
return new App::DocumentObjectExecReturn("Cannot do boolean operation with invalid BaseFeature");
}

View File

@ -64,18 +64,30 @@ void DressUp::positionByBaseFeature(void)
this->Placement.setValue(base->Placement.getValue());
}
Part::Feature *DressUp::getBaseObject() const
Part::Feature *DressUp::getBaseObject(bool silent) const
{
try {
return Feature::getBaseObject();
} catch (const Base::Exception &) {
App::DocumentObject* base = Base.getValue();
if(!base)
throw Base::Exception("No Base object linked");
if(!base->isDerivedFrom(Part::Feature::getClassTypeId()))
throw Base::Exception("Linked object is not a Part object");
return static_cast<Part::Feature*>(base);
Part::Feature *rv = Feature::getBaseObject(/* silent = */ true);
if (rv) {
return rv;
}
const char* err = nullptr;
App::DocumentObject* base = Base.getValue();
if (base) {
if(base->isDerivedFrom(Part::Feature::getClassTypeId())) {
rv = static_cast<Part::Feature*>(base);
} else {
err = "Linked object is not a Part object";
}
} else {
err = "No Base object linked";
}
if (!silent && err) {
throw Base::Exception(err);
}
return rv;
}
void DressUp::getContiniusEdges(Part::TopoShape TopShape, std::vector< std::string >& SubNames) {

View File

@ -50,8 +50,11 @@ public:
/**
* Returns the BaseFeature property's object if it's set othervice returns Base's
* feature property object otherviceeature property's object (if any)
* @param silent if couldn't determine the base feature and silent == true,
* silently return a nullptr, otherwice throw Base::Exception.
* Default is false.
*/
virtual Part::Feature* getBaseObject() const;
virtual Part::Feature* getBaseObject(bool silent=false) const;
/// extracts all edges from the subshapes (inkluding face edges) and furthermore adds
/// all C0 continius edges to the vector
void getContiniusEdges(Part::TopoShape, std::vector< std::string >&);

View File

@ -123,11 +123,10 @@ short SketchBased::mustExecute() const
void SketchBased::positionByPrevious(void)
{
try{
//use placement of base
Part::Feature* feat = getBaseObject();
Part::Feature* feat = getBaseObject(/* silent = */ true);
if (feat) {
this->Placement.setValue(feat->Placement.getValue());
} catch (Base::Exception) {
} else {
//no base. Use either Sketch support's placement, or sketch's placement itself.
Part::Part2DObject *sketch = getVerifiedSketch();
App::DocumentObject* support = sketch->Support.getValue();
@ -141,22 +140,31 @@ void SketchBased::positionByPrevious(void)
void SketchBased::transformPlacement(const Base::Placement &transform)
{
try{
Part::Feature* feat = getBaseObject();
Part::Feature* feat = getBaseObject(/* silent = */ true);
if (feat) {
feat->transformPlacement(transform);
} catch (Base::Exception) {
} else {
Part::Part2DObject *sketch = getVerifiedSketch();
sketch->transformPlacement(transform);
}
positionByPrevious();
}
Part::Part2DObject* SketchBased::getVerifiedSketch() const {
Part::Part2DObject* SketchBased::getVerifiedSketch(bool silent) const {
App::DocumentObject* result = Sketch.getValue();
if (!result)
throw Base::Exception("No sketch linked");
if (!result->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
throw Base::Exception("Linked object is not a Sketch or Part2DObject");
const char* err = nullptr;
if (!result) {
err = "No sketch linked";
} else {
if (!result->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
err = "Linked object is not a Sketch or Part2DObject";
}
if (!silent && err) {
throw Base::Exception (err);
}
return static_cast<Part::Part2DObject*>(result);
}
@ -231,19 +239,39 @@ int SketchBased::getSketchAxisCount(void) const
return sketch->getAxisCount();
}
Part::Feature *SketchBased::getBaseObject() const
Part::Feature *SketchBased::getBaseObject(bool silent) const
{
try{
return Feature::getBaseObject();
} catch (Base::Exception) {
Part::Part2DObject* sketch = getVerifiedSketch();
App::DocumentObject* spt = sketch->Support.getValue();
if(!spt)
throw Base::Exception ("No base set, no sketch support either");
if(!spt->isDerivedFrom(Part::Feature::getClassTypeId()))
throw Base::Exception ("No base set, sketch support is not Part::Feature");
return static_cast<Part::Feature*>(spt);
// Test the base's class feature.
Part::Feature *rv = Feature::getBaseObject(/* silent = */ true);
if (rv) {
return rv;
}
// getVerifiedSketch() may throw it's own exception if fail
Part::Part2DObject* sketch = getVerifiedSketch(silent);
if (!sketch) {
return nullptr;
}
const char* err = nullptr;
App::DocumentObject* spt = sketch->Support.getValue();
if (spt) {
if (spt->isDerivedFrom(Part::Feature::getClassTypeId())) {
rv = static_cast<Part::Feature*>(spt);
} else {
err = "No base set, sketch support is not Part::Feature";
}
} else {
err = "No base set, no sketch support either";
}
if (!silent && err) {
throw Base::Exception (err);
}
return rv;
}
void SketchBased::onChanged(const App::Property* prop)

View File

@ -67,8 +67,13 @@ public:
*/
virtual void transformPlacement(const Base::Placement &transform);
/// Verifies the linked Sketch object
Part::Part2DObject* getVerifiedSketch() const;
/**
* Verifies the linked Sketch object
* @param silent if sketch property is malformed and the parameter is true
* silently returns nullptr, otherwice throw a Base::Exception.
* Default is false.
*/
Part::Part2DObject* getVerifiedSketch(bool silent=false) const;
/// Returns the wires the sketch is composed of
std::vector<TopoDS_Wire> getSketchWires() const;
/// Returns the face of the sketch support (if any)
@ -77,7 +82,7 @@ public:
/// retrieves the number of axes in the linked sketch (defined as construction lines)
int getSketchAxisCount(void) const;
virtual Part::Feature* getBaseObject() const;
virtual Part::Feature* getBaseObject(bool silent=false) const;
protected:
void onChanged(const App::Property* prop);

View File

@ -1827,7 +1827,7 @@ void CmdPartDesignMirrored::activated(int iMsg)
return;
if(features.front()->isDerivedFrom(PartDesign::SketchBased::getClassTypeId())) {
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch();
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch(/* silent =*/ true);
if (sketch)
Gui::Command::doCommand(Doc,"App.activeDocument().%s.MirrorPlane = (App.activeDocument().%s, [\"V_Axis\"])",
FeatName.c_str(), sketch->getNameInDocument());
@ -1874,7 +1874,7 @@ void CmdPartDesignLinearPattern::activated(int iMsg)
return;
if(features.front()->isDerivedFrom(PartDesign::SketchBased::getClassTypeId())) {
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch();
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch(/* silent =*/ true);
if (sketch)
doCommand(Doc,"App.activeDocument().%s.Direction = (App.activeDocument().%s, [\"H_Axis\"])",
FeatName.c_str(), sketch->getNameInDocument());
@ -1923,7 +1923,7 @@ void CmdPartDesignPolarPattern::activated(int iMsg)
return;
if(features.front()->isDerivedFrom(PartDesign::SketchBased::getClassTypeId())) {
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch();
Part::Part2DObject *sketch = (static_cast<PartDesign::SketchBased*>(features.front()))->getVerifiedSketch(/* silent =*/ true);
if (sketch)
doCommand(Doc,"App.activeDocument().%s.Axis = (App.activeDocument().%s, [\"N_Axis\"])",
FeatName.c_str(), sketch->getNameInDocument());

View File

@ -59,11 +59,11 @@ bool TaskDlgFeatureParameters::accept() {
throw Base::Exception("Bad object processed in the feature dialog.");
}
App::DocumentObject* support = static_cast<PartDesign::Feature*>(feature)->BaseFeature.getValue();
App::DocumentObject* previous = static_cast<PartDesign::Feature*>(feature)->getBaseObject(/* silent = */ true );
if (support) {
if (previous) {
Gui::Command::doCommand(Gui::Command::Gui,"Gui.activeDocument().hide(\"%s\")",
support->getNameInDocument());
previous->getNameInDocument());
}
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.recompute()");
@ -91,12 +91,7 @@ bool TaskDlgFeatureParameters::reject()
// Find out previous feature we won't be able to do it after abort
// (at least in the body case)
App::DocumentObject* previous;
try {
previous = feature->getBaseObject(); // throws on errors
} catch (const Base::Exception &ex) {
previous = 0;
}
App::DocumentObject* previous = feature->getBaseObject(/* silent = */ true );
// roll back the done things
Gui::Command::abortCommand();

View File

@ -167,14 +167,8 @@ void ViewProvider::updateData(const App::Property* prop)
bool ViewProvider::onDelete(const std::vector<std::string> &)
{
App::DocumentObject* previous;
PartDesign::Feature* feature = static_cast<PartDesign::Feature*>(getObject());
try {
previous = feature->getBaseObject();
} catch (const Base::Exception &ex) {
previous = 0;
}
App::DocumentObject* previous = feature->getBaseObject(/* silent = */ true );
// Make the tip or the previous feature visiable again with preference to the previous one
// if the feature was visiable itself