From 83770c7e51b6fb551ef233ef19f6099a68ca55f6 Mon Sep 17 00:00:00 2001 From: mpv Date: Thu, 4 Sep 2014 14:29:25 +0400 Subject: [PATCH] Make circle with radius 0 not crashed: check validity before execution --- .../ExchangePlugin_ImportFeature.h | 18 +++++++++--------- src/Model/Model_Data.cpp | 6 ++++++ src/Model/Model_Data.h | 2 ++ src/Model/Model_Update.cpp | 11 +++++++++-- src/ModelAPI/ModelAPI_Data.h | 3 +++ src/ModelAPI/ModelAPI_Feature.cpp | 17 +++++++++++++++++ src/ModelAPI/ModelAPI_Feature.h | 2 ++ src/SketchPlugin/SketchPlugin_Plugin.cpp | 2 ++ src/SketchPlugin/SketchPlugin_Validators.cpp | 9 +++++++++ src/SketchPlugin/SketchPlugin_Validators.h | 16 +++++++++++++++- src/SketchPlugin/plugin-Sketch.xml | 4 +++- 11 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h index 4b3b7a803..3745d1660 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.h +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.h @@ -27,7 +27,7 @@ #define UnLoadLib( handle ) dlclose( handle ); #endif -class EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature : public ModelAPI_Feature +class ExchangePlugin_ImportFeature : public ModelAPI_Feature { public: /// Extrusion kind @@ -43,26 +43,26 @@ class EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature : public ModelAPI_Featu return MY_FILE_PATH_ID; } - ExchangePlugin_ImportFeature(); - virtual ~ExchangePlugin_ImportFeature(); + EXCHANGEPLUGIN_EXPORT ExchangePlugin_ImportFeature(); + EXCHANGEPLUGIN_EXPORT virtual ~ExchangePlugin_ImportFeature(); /// Returns the unique kind of a feature - virtual const std::string& getKind(); + EXCHANGEPLUGIN_EXPORT virtual const std::string& getKind(); /// Request for initialization of data model of the feature: adding all attributes - virtual void initAttributes(); + EXCHANGEPLUGIN_EXPORT virtual void initAttributes(); /// Computes or recomputes the results - virtual void execute(); + EXCHANGEPLUGIN_EXPORT virtual void execute(); - virtual bool isInHistory() + EXCHANGEPLUGIN_EXPORT virtual bool isInHistory() { return false; } protected: - bool importFile(const std::string& theFileName); - LibHandle loadImportPlugin(const std::string& theFormatName); + EXCHANGEPLUGIN_EXPORT bool importFile(const std::string& theFileName); + EXCHANGEPLUGIN_EXPORT LibHandle loadImportPlugin(const std::string& theFormatName); }; diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 7813c461f..5dba72cbd 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -282,3 +282,9 @@ void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr) ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent); } } + +void Model_Data::erase() +{ + if (!myLab.IsNull()) + myLab.ForgetAllAttributes(); +} diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index f8ad2f0a7..f6a82a099 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -127,6 +127,8 @@ class Model_Data : public ModelAPI_Data { myObject = theObject; } + + MODEL_EXPORT virtual void erase(); }; #endif diff --git a/src/Model/Model_Update.cpp b/src/Model/Model_Update.cpp index a57471405..9b76e5f5a 100644 --- a/src/Model/Model_Update.cpp +++ b/src/Model/Model_Update.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -94,8 +95,14 @@ bool Model_Update::updateFeature(FeaturePtr theFeature) if (aMustbeUpdated) { if (boost::dynamic_pointer_cast(theFeature->document())->executeFeatures() || - !theFeature->isPersistentResult()) - theFeature->execute(); + !theFeature->isPersistentResult()) { + ModelAPI_ValidatorsFactory* aFactory = ModelAPI_PluginManager::get()->validators(); + if (aFactory->validate(theFeature)) { + theFeature->execute(); + } else { + theFeature->eraseResults(); + } + } // redisplay all results static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY); const std::list >& aResults = theFeature->results(); diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index c4fe4712f..2daf2302b 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -83,6 +83,9 @@ class MODELAPI_EXPORT ModelAPI_Data /// makes attribute initialized virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr) = 0; + /// Erases all the data from the data model + virtual void erase() = 0; + /// To virtually destroy the fields of successors virtual ~ModelAPI_Data() { diff --git a/src/ModelAPI/ModelAPI_Feature.cpp b/src/ModelAPI/ModelAPI_Feature.cpp index 158675e9f..796bf5b0b 100644 --- a/src/ModelAPI/ModelAPI_Feature.cpp +++ b/src/ModelAPI/ModelAPI_Feature.cpp @@ -5,6 +5,7 @@ #include "ModelAPI_Feature.h" #include #include +#include #include #include @@ -63,11 +64,27 @@ void ModelAPI_Feature::removeResult(const boost::shared_ptr& th std::list >::iterator aResIter = myResults.begin(); for(; aResIter != myResults.end(); aResIter++) { if (*aResIter == theResult) { + std::string aGroup = (*aResIter)->groupName(); + (*aResIter)->data()->erase(); myResults.erase(aResIter); + static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED); + ModelAPI_EventCreator::get()->sendDeleted(document(), aGroup); + break; } } } +void ModelAPI_Feature::eraseResults() +{ + std::list >::iterator aResIter = myResults.begin(); + for(; aResIter != myResults.end(); aResIter++) { + (*aResIter)->data()->erase(); + static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED); + ModelAPI_EventCreator::get()->sendDeleted(document(), (*aResIter)->groupName()); + } + myResults.clear(); +} + boost::shared_ptr ModelAPI_Feature::documentToAdd() { return ModelAPI_PluginManager::get()->currentDocument(); diff --git a/src/ModelAPI/ModelAPI_Feature.h b/src/ModelAPI/ModelAPI_Feature.h index fed0ea648..ef1212afc 100644 --- a/src/ModelAPI/ModelAPI_Feature.h +++ b/src/ModelAPI/ModelAPI_Feature.h @@ -59,6 +59,8 @@ class ModelAPI_Feature : public ModelAPI_Object const int theIndex); /// removes the result from the feature MODELAPI_EXPORT void removeResult(const boost::shared_ptr& theResult); + /// removes all results from the feature + MODELAPI_EXPORT void eraseResults(); /// Returns true if result is persistent (stored in document) and on undo-redo, save-open /// it is not needed to recompute it. diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index 930253dad..6fcb0c400 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -29,6 +29,8 @@ SketchPlugin_Plugin::SketchPlugin_Plugin() ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); aFactory->registerValidator("SketchPlugin_DistanceAttrValidator", new SketchPlugin_DistanceAttrValidator); + aFactory->registerValidator("SketchPlugin_RadiusValidator", + new SketchPlugin_RadiusValidator); aFactory->registerValidator("SketchPlugin_ResultPointValidator", new SketchPlugin_ResultPointValidator); aFactory->registerValidator("SketchPlugin_ResultLineValidator", new SketchPlugin_ResultLineValidator); aFactory->registerValidator("SketchPlugin_ResultArcValidator", new SketchPlugin_ResultArcValidator); diff --git a/src/SketchPlugin/SketchPlugin_Validators.cpp b/src/SketchPlugin/SketchPlugin_Validators.cpp index 4115d11a4..7880ff033 100644 --- a/src/SketchPlugin/SketchPlugin_Validators.cpp +++ b/src/SketchPlugin/SketchPlugin_Validators.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include bool SketchPlugin_DistanceAttrValidator::isValid(const FeaturePtr& theFeature, @@ -42,3 +43,11 @@ bool SketchPlugin_DistanceAttrValidator::isValid( } return true; // it may be not reference attribute, in this case, it is OK } + +bool SketchPlugin_RadiusValidator::isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const +{ + boost::shared_ptr aDouble = + boost::dynamic_pointer_cast(theAttribute); + return aDouble->isInitialized() && aDouble->value() > 1.e-5; +} diff --git a/src/SketchPlugin/SketchPlugin_Validators.h b/src/SketchPlugin/SketchPlugin_Validators.h index 4da0f73e9..8a5a82bb0 100644 --- a/src/SketchPlugin/SketchPlugin_Validators.h +++ b/src/SketchPlugin/SketchPlugin_Validators.h @@ -8,7 +8,7 @@ #include "SketchPlugin.h" #include -class SketchPlugin_DistanceAttrValidator : public ModelAPI_AttributeValidator +class SketchPlugin_DistanceAttrValidator : public ModelAPI_RefAttrValidator { public: //! returns true if attribute is valid @@ -22,4 +22,18 @@ class SketchPlugin_DistanceAttrValidator : public ModelAPI_AttributeValidator }; +/** + * It must be bigger than 1.e-5 + */ +class SketchPlugin_RadiusValidator : public ModelAPI_AttributeValidator +{ + public: + //! returns true if attribute is valid + //! \param theAttribute the checked attribute + //! \param theArguments arguments of the attribute + virtual bool isValid( + const AttributePtr& theAttribute, const std::list& theArguments) const; + +}; + #endif diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 7c26e28e2..4c1d80a06 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -14,7 +14,9 @@ - + + + -- 2.39.2