From 439f392e88bd9fdfb5b0a1f1f7aa0bc42bf583dc Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Thu, 14 May 2015 00:18:47 +0300 Subject: [PATCH] Fix crash when adding datum and there's no active body Plus fix double messageboxes about the lack of the body, --- src/Mod/PartDesign/Gui/Command.cpp | 46 +++++++++++++------ src/Mod/PartDesign/Gui/CommandPrimitive.cpp | 4 +- .../Gui/TaskTransformedParameters.cpp | 4 +- src/Mod/PartDesign/Gui/Workbench.cpp | 4 +- src/Mod/PartDesign/Gui/Workbench.h | 2 +- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/Mod/PartDesign/Gui/Command.cpp b/src/Mod/PartDesign/Gui/Command.cpp index 53454b6dc..f3a60a658 100644 --- a/src/Mod/PartDesign/Gui/Command.cpp +++ b/src/Mod/PartDesign/Gui/Command.cpp @@ -153,7 +153,7 @@ CmdPartDesignMoveTip::CmdPartDesignMoveTip() void CmdPartDesignMoveTip::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if(!pcActiveBody) return; std::vector features = getSelection().getObjectsOfType(Part::Feature::getClassTypeId()); @@ -229,7 +229,7 @@ CmdPartDesignDuplicateSelection::CmdPartDesignDuplicateSelection() void CmdPartDesignDuplicateSelection::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if(!pcActiveBody) return; std::vector features = getSelection().getObjectsOfType(Part::Feature::getClassTypeId()); @@ -301,7 +301,7 @@ CmdPartDesignMoveFeature::CmdPartDesignMoveFeature() void CmdPartDesignMoveFeature::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if(!pcActiveBody) return; std::vector features = getSelection().getObjectsOfType(Part::Feature::getClassTypeId()); @@ -381,7 +381,7 @@ CmdPartDesignMoveFeatureInTree::CmdPartDesignMoveFeatureInTree() void CmdPartDesignMoveFeatureInTree::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if(!pcActiveBody) return; std::vector features = getSelection().getObjectsOfType(Part::Feature::getClassTypeId()); @@ -445,11 +445,19 @@ bool CmdPartDesignMoveFeatureInTree::isActive(void) // PartDesign_Datum //=========================================================================== +/** + * @brief getReferenceString Prepares selection to be fed through Python to a datum feature. + * @param cmd + * @return string representing the selection, in format + * "[(App.activeDocument().Pad,'Vertex8'),(App.activeDocument().Pad,'Vertex9')]". + * Zero-length string if there is no selection, or the selection is + * inappropriate. + */ const QString getReferenceString(Gui::Command* cmd) { QString referenceString; - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */false); if(!pcActiveBody) return QString::fromAscii(""); Gui::SelectionFilter GeometryFilter("SELECT Part::Feature SUBELEMENT Face COUNT 1"); @@ -534,7 +542,9 @@ void CmdPartDesignPlane::activated(int iMsg) // create Datum plane std::string FeatName = getUniqueObjectName("DatumPlane"); QString refStr = getReferenceString(this); - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); + if (pcActiveBody == 0) + return; openCommand("Create a datum plane"); doCommand(Doc,"App.activeDocument().addObject('PartDesign::Plane','%s')",FeatName.c_str()); @@ -576,7 +586,9 @@ void CmdPartDesignLine::activated(int iMsg) // create Datum line std::string FeatName = getUniqueObjectName("DatumLine"); QString refStr = getReferenceString(this); - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); + if (pcActiveBody == 0) + return; openCommand("Create a datum line"); doCommand(Doc,"App.activeDocument().addObject('PartDesign::Line','%s')",FeatName.c_str()); @@ -616,7 +628,9 @@ void CmdPartDesignPoint::activated(int iMsg) // create Datum point std::string FeatName = getUniqueObjectName("DatumPoint"); QString refStr = getReferenceString(this); - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); + if (pcActiveBody == 0) + return; openCommand("Create a datum point"); doCommand(Doc,"App.activeDocument().addObject('PartDesign::Point','%s')",FeatName.c_str()); @@ -659,7 +673,7 @@ CmdPartDesignNewSketch::CmdPartDesignNewSketch() void CmdPartDesignNewSketch::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); // No PartDesign feature without Body past FreeCAD 0.13 if(!pcActiveBody) return; @@ -866,7 +880,9 @@ bool CmdPartDesignNewSketch::isActive(void) void finishFeature(const Gui::Command* cmd, const std::string& FeatName, const bool hidePrevSolid = true) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */false); + if (pcActiveBody == 0) + throw Base::Exception("No active body!"); cmd->doCommand(cmd->Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)", pcActiveBody->getNameInDocument(), FeatName.c_str()); @@ -924,7 +940,7 @@ void finishFeature(const Gui::Command* cmd, const std::string& FeatName, const b } // Check whether this sketch belongs to the active body - PartDesign::Body* body = PartDesignGui::getBody(); + PartDesign::Body* body = PartDesignGui::getBody(/*messageIfNot = */false); if (!body->hasFeature(*s)) { status.push_back(PartDesignGui::TaskFeaturePick::otherBody); continue; @@ -962,7 +978,7 @@ void finishFeature(const Gui::Command* cmd, const std::string& FeatName, const b void prepareSketchBased(Gui::Command* cmd, const std::string& which, boost::function func) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if (!pcActiveBody) return; // Get a valid sketch from the user @@ -1223,7 +1239,7 @@ bool CmdPartDesignGroove::isActive(void) void makeChamferOrFillet(Gui::Command* cmd, const std::string& which) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */false); if (!pcActiveBody) return; @@ -1427,7 +1443,7 @@ CmdPartDesignDraft::CmdPartDesignDraft() void CmdPartDesignDraft::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if (!pcActiveBody) return; std::vector selection = getSelection().getSelectionEx(); @@ -1788,7 +1804,7 @@ CmdPartDesignMultiTransform::CmdPartDesignMultiTransform() void CmdPartDesignMultiTransform::activated(int iMsg) { - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if (!pcActiveBody) return; std::vector features; diff --git a/src/Mod/PartDesign/Gui/CommandPrimitive.cpp b/src/Mod/PartDesign/Gui/CommandPrimitive.cpp index 4c37bdcfe..94f9b049c 100644 --- a/src/Mod/PartDesign/Gui/CommandPrimitive.cpp +++ b/src/Mod/PartDesign/Gui/CommandPrimitive.cpp @@ -56,7 +56,7 @@ void CmdPrimtiveCompAdditive::activated(int iMsg) { Base::Console().Message("activated msg %i\n", iMsg); - PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body *pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */true); if (!pcActiveBody) return; if(iMsg == 0) { @@ -206,4 +206,4 @@ void CreatePartDesignPrimitiveCommands(void) rcCmdMgr.addCommand(new CmdPrimtiveCompAdditive); rcCmdMgr.addCommand(new CmdPrimtiveCompSubtractive); - } \ No newline at end of file + } diff --git a/src/Mod/PartDesign/Gui/TaskTransformedParameters.cpp b/src/Mod/PartDesign/Gui/TaskTransformedParameters.cpp index aac548643..b4c0e7a96 100644 --- a/src/Mod/PartDesign/Gui/TaskTransformedParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskTransformedParameters.cpp @@ -243,7 +243,7 @@ void TaskTransformedParameters::showObject() void TaskTransformedParameters::hideBase() { Gui::Document* doc = Gui::Application::Instance->activeDocument(); - PartDesign::Body* pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body* pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */false); if (doc && pcActiveBody) { App::DocumentObject* prevFeature; if (insideMultiTransform) { @@ -259,7 +259,7 @@ void TaskTransformedParameters::hideBase() void TaskTransformedParameters::showBase() { Gui::Document* doc = Gui::Application::Instance->activeDocument(); - PartDesign::Body* pcActiveBody = PartDesignGui::getBody(); + PartDesign::Body* pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */false); if (doc && pcActiveBody) { App::DocumentObject* prevFeature; if (insideMultiTransform) { diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index 0fb9b3750..2e2975aa6 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -73,11 +73,11 @@ namespace PartDesignGui { // Helper for Body //=========================================================================== -PartDesign::Body *getBody(void) +PartDesign::Body *getBody(bool messageIfNot) { PartDesign::Body * activeBody = Gui::Application::Instance->activeView()->getActiveObject(PDBODYKEY); - if (!activeBody){ + if (!activeBody && messageIfNot){ QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No active Body"), QObject::tr("In order to use PartDesign you need an active Body object in the document. " "Please make one active (double click) or create one. If you have a legacy document " diff --git a/src/Mod/PartDesign/Gui/Workbench.h b/src/Mod/PartDesign/Gui/Workbench.h index a33d843e6..3b9e36ac7 100644 --- a/src/Mod/PartDesign/Gui/Workbench.h +++ b/src/Mod/PartDesign/Gui/Workbench.h @@ -51,7 +51,7 @@ namespace PartDesignGui { //extern Gui::ViewProviderDocumentObject *ActiveVp; /// Return active body or show a warning message -PartDesign::Body *getBody(void); +PartDesign::Body *getBody(bool messageIfNot); /** * @author Werner Mayer