From 314b1e87c30b9c4e2f8dfc7fc33ee2ca5f094b0d Mon Sep 17 00:00:00 2001 From: dbv Date: Tue, 30 Jun 2015 13:47:50 +0300 Subject: [PATCH] Improvement #610: Fuse: tools or objects are not needed. --- src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp | 31 ++++++++-- src/FeaturesPlugin/boolean_widget.xml | 5 +- src/GeomValidators/CMakeLists.txt | 2 + .../GeomValidators_BooleanArguments.cpp | 59 +++++++++++++++++++ .../GeomValidators_BooleanArguments.h | 33 +++++++++++ .../GeomValidators_ShapeType.cpp | 25 ++++---- src/GeomValidators/GeomValidators_ShapeType.h | 7 ++- src/PartSet/PartSet_Module.cpp | 4 ++ 8 files changed, 145 insertions(+), 21 deletions(-) create mode 100644 src/GeomValidators/GeomValidators_BooleanArguments.cpp create mode 100644 src/GeomValidators/GeomValidators_BooleanArguments.h diff --git a/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp b/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp index bfb8655f4..7fb46f2c9 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -42,6 +44,9 @@ void FeaturesPlugin_Boolean::initAttributes() FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); // extrusion works with faces always aSelection->setSelectionType("SOLID"); + + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID()); } //================================================================================================= @@ -72,9 +77,6 @@ void FeaturesPlugin_Boolean::execute() // Getting objects. AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID()); - if (anObjectsSelList->size() == 0) { - return; - } for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { std::shared_ptr anObjectAttr = anObjectsSelList->value(anObjectsIndex); std::shared_ptr anObject = anObjectAttr->value(); @@ -86,9 +88,6 @@ void FeaturesPlugin_Boolean::execute() // Getting tools. AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID()); - if (aToolsSelList->size() == 0) { - return; - } for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) { std::shared_ptr aToolAttr = aToolsSelList->value(aToolsIndex); std::shared_ptr aTool = aToolAttr->value(); @@ -103,6 +102,12 @@ void FeaturesPlugin_Boolean::execute() switch(aType) { case GeomAlgoAPI_Boolean::BOOL_CUT: case GeomAlgoAPI_Boolean::BOOL_COMMON:{ + if(anObjects.empty() || aTools.empty()) { + std::string aFeatureError = "Not enough objects for boolean operation"; + setError(aFeatureError); + return; + } + // Cut each object with all tools for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) { std::shared_ptr anObject = *anObjectsIt; @@ -137,6 +142,20 @@ void FeaturesPlugin_Boolean::execute() break; } case GeomAlgoAPI_Boolean::BOOL_FUSE: { + if(anObjects.empty() && aTools.size() > 1) { + anObjects.push_back(aTools.back()); + aTools.pop_back(); + }else if(aTools.empty() && anObjects.size() > 1) { + aTools.push_back(anObjects.back()); + anObjects.pop_back(); + } + + if(anObjects.empty() || aTools.empty()) { + std::string aFeatureError = "Not enough objects for boolean operation"; + setError(aFeatureError); + return; + } + // Fuse all objects and all tools. GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType); diff --git a/src/FeaturesPlugin/boolean_widget.xml b/src/FeaturesPlugin/boolean_widget.xml index 22d958cca..8fc0925ca 100644 --- a/src/FeaturesPlugin/boolean_widget.xml +++ b/src/FeaturesPlugin/boolean_widget.xml @@ -8,7 +8,7 @@ type_choice="Solids" concealment="true"> - + - + + diff --git a/src/GeomValidators/CMakeLists.txt b/src/GeomValidators/CMakeLists.txt index 2c40e8cb8..07004c22a 100644 --- a/src/GeomValidators/CMakeLists.txt +++ b/src/GeomValidators/CMakeLists.txt @@ -4,6 +4,7 @@ INCLUDE(Common) SET(PROJECT_HEADERS GeomValidators.h + GeomValidators_BooleanArguments.h GeomValidators_ConstructionComposite.h GeomValidators_Face.h GeomValidators_Positive.h @@ -13,6 +14,7 @@ SET(PROJECT_HEADERS ) SET(PROJECT_SOURCES + GeomValidators_BooleanArguments.cpp GeomValidators_ConstructionComposite.cpp GeomValidators_Face.cpp GeomValidators_Positive.cpp diff --git a/src/GeomValidators/GeomValidators_BooleanArguments.cpp b/src/GeomValidators/GeomValidators_BooleanArguments.cpp new file mode 100644 index 000000000..738b1ecc0 --- /dev/null +++ b/src/GeomValidators/GeomValidators_BooleanArguments.cpp @@ -0,0 +1,59 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: GeomValidators_BooleanArguments.h +// Created: 30 June 2015 +// Author: Dmitry Bobylev + +#include + +#include +#include + +//================================================================================================= +bool GeomValidators_BooleanArguments::isValid(const std::shared_ptr& theFeature, + const std::list& theArguments) const +{ + if(theArguments.size() != 3) { + return false; + } + + int anObjectsNb = 0, aToolsNb = 0; + int anOperationType = 0; + + std::list::const_iterator anIt = theArguments.begin(), aLast = theArguments.end(); + + std::shared_ptr anAttrSelList = theFeature->selectionList(*anIt); + if(anAttrSelList) { + anObjectsNb = anAttrSelList->size(); + } + anIt++; + + anAttrSelList = theFeature->selectionList(*anIt); + if(anAttrSelList) { + aToolsNb = anAttrSelList->size(); + } + anIt++; + + std::shared_ptr anAttrInt = theFeature->integer(*anIt); + if(anAttrInt) { + anOperationType = anAttrInt->value(); + } + + if(anOperationType == 1 && (anObjectsNb + aToolsNb > 1)) { + return true; + } else if (anOperationType != 1 && anObjectsNb > 0 && aToolsNb > 0) { + return true; + } + + return false; +} + +//================================================================================================= +bool GeomValidators_BooleanArguments::isNotObligatory(std::string theFeature, std::string theAttribute) +{ + if(theAttribute == "main_objects" || theAttribute == "tool_objects") { + return true; + } + + return false; +} diff --git a/src/GeomValidators/GeomValidators_BooleanArguments.h b/src/GeomValidators/GeomValidators_BooleanArguments.h new file mode 100644 index 000000000..c7134745f --- /dev/null +++ b/src/GeomValidators/GeomValidators_BooleanArguments.h @@ -0,0 +1,33 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: GeomValidators_BooleanArguments.h +// Created: 30 June 2015 +// Author: Dmitry Bobylev + +#ifndef GeomValidators_BooleanArguments_H +#define GeomValidators_BooleanArguments_H + +#include +#include +#include + +/** \class GeomValidators_BooleanArguments + * \ingroup Validators + * \brief Validates that boolean operation have enough arguments. + */ +class GeomValidators_BooleanArguments : public ModelAPI_FeatureValidator +{ +public: + /** \brief Returns true if feature and/or attributes are valid. + * \param[in] theFeature the validated feature. + * \param[in] theArguments the arguments in the configuration file for this validator. + * \returns true if feature is valid. + */ + GEOMVALIDATORS_EXPORT virtual bool isValid(const std::shared_ptr& theFeature, + const std::list& theArguments) const; + + /// \return true if the attribute in feature is not obligatory for the feature execution. + GEOMVALIDATORS_EXPORT virtual bool isNotObligatory(std::string theFeature, std::string theAttribute); +}; + +#endif diff --git a/src/GeomValidators/GeomValidators_ShapeType.cpp b/src/GeomValidators/GeomValidators_ShapeType.cpp index 50c4bcdb2..3d68f6a8f 100644 --- a/src/GeomValidators/GeomValidators_ShapeType.cpp +++ b/src/GeomValidators/GeomValidators_ShapeType.cpp @@ -19,21 +19,23 @@ typedef std::map EdgeTypes; -static EdgeTypes MyEdgeTypes; GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType) { - if (MyEdgeTypes.size() == 0) { - MyEdgeTypes["vertex"] = Vertex; - MyEdgeTypes["edge"] = Edge; - MyEdgeTypes["line"] = Line; - MyEdgeTypes["circle"] = Circle; - MyEdgeTypes["solid"] = Solid; - MyEdgeTypes["face"] = Face; + static EdgeTypes MyShapeTypes; + + if (MyShapeTypes.size() == 0) { + MyShapeTypes["empty"] = Empty; + MyShapeTypes["vertex"] = Vertex; + MyShapeTypes["edge"] = Edge; + MyShapeTypes["line"] = Line; + MyShapeTypes["circle"] = Circle; + MyShapeTypes["face"] = Face; + MyShapeTypes["solid"] = Solid; } std::string aType = std::string(theType.c_str()); - if (MyEdgeTypes.find(aType) != MyEdgeTypes.end()) - return MyEdgeTypes[aType]; + if (MyShapeTypes.find(aType) != MyShapeTypes.end()) + return MyShapeTypes[aType]; Events_Error::send("Shape type defined in XML is not implemented!"); return AnyShape; @@ -63,6 +65,9 @@ bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute, AttributeSelectionListPtr aListAttr = std::dynamic_pointer_cast(theAttribute); if (aListAttr.get()) { + if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) { + return true; + } for (int i = 0; i < aListAttr->size(); i++) { aValid = isValidAttribute(aListAttr->value(i), aShapeType); if (!aValid) // if at least one attribute is invalid, the result is false diff --git a/src/GeomValidators/GeomValidators_ShapeType.h b/src/GeomValidators/GeomValidators_ShapeType.h index de7fae9ba..782d9eaeb 100644 --- a/src/GeomValidators/GeomValidators_ShapeType.h +++ b/src/GeomValidators/GeomValidators_ShapeType.h @@ -26,14 +26,15 @@ class GeomValidators_ShapeType : public ModelAPI_AttributeValidator // the edge type enum TypeOfShape { - AnyShape, + Empty, Vertex, Edge, Line, Circle, - Solid, Face, - Compound + Solid, + Compound, + AnyShape }; public: diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 2e87de581..cd842eedc 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -196,6 +197,9 @@ void PartSet_Module::registerValidators() aFactory->registerValidator("GeomValidators_ZeroOffset", new GeomValidators_ZeroOffset); + aFactory->registerValidator("GeomValidators_BooleanArguments", + new GeomValidators_BooleanArguments); + aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator); -- 2.39.2