From 5cac8564ae5323c63a7010e9e63527c2ce07e02a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me?= Date: Mon, 16 Nov 2020 19:25:18 +0100 Subject: [PATCH] Modification for "create normal" functionality --- src/FeaturesAPI/FeaturesAPI_NormalToFace.cpp | 24 ++- src/FeaturesAPI/FeaturesAPI_NormalToFace.h | 10 +- src/FeaturesPlugin/CMakeLists.txt | 3 + .../CreateNormalToFace_widget.xml | 22 +++ .../FeaturesPlugin_CreateNormalToFace.cpp | 118 ++++++++++++++ .../FeaturesPlugin_CreateNormalToFace.h | 85 ++++++++++ .../FeaturesPlugin_NormalToFace.cpp | 145 ++++++++++++------ .../FeaturesPlugin_NormalToFace.h | 11 +- src/FeaturesPlugin/FeaturesPlugin_Plugin.cpp | 5 +- src/FeaturesPlugin/plugin-Features.xml | 6 +- 10 files changed, 359 insertions(+), 70 deletions(-) create mode 100644 src/FeaturesPlugin/CreateNormalToFace_widget.xml create mode 100644 src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.cpp create mode 100644 src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.h diff --git a/src/FeaturesAPI/FeaturesAPI_NormalToFace.cpp b/src/FeaturesAPI/FeaturesAPI_NormalToFace.cpp index d6453ad80..2875a9f97 100644 --- a/src/FeaturesAPI/FeaturesAPI_NormalToFace.cpp +++ b/src/FeaturesAPI/FeaturesAPI_NormalToFace.cpp @@ -19,6 +19,7 @@ #include "FeaturesAPI_NormalToFace.h" +#include #include #include @@ -39,9 +40,9 @@ FeaturesAPI_NormalToFace::FeaturesAPI_NormalToFace( :ModelHighAPI_Interface(theFeature) { if (initialize()) { - fillAttribute(theBaseFace, myfaceselected); - fillAttribute(theOptionnelPoint, myvertexselected); - feature()->string(FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID())->setValue("true"); + fillAttribute(theBaseFace, myfaceSelected); + fillAttribute(theOptionnelPoint, myvertexSelected); + feature()->string(FeaturesPlugin_CreateNormalToFace::VERTEX_OPTION_ID())->setValue("true"); execute(); } } @@ -52,8 +53,8 @@ FeaturesAPI_NormalToFace::FeaturesAPI_NormalToFace( :ModelHighAPI_Interface(theFeature) { if (initialize()) { - fillAttribute(theBaseFace, myfaceselected); - feature()->string(FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID())->setValue(""); + fillAttribute(theBaseFace, myfaceSelected); + feature()->string(FeaturesPlugin_CreateNormalToFace::VERTEX_OPTION_ID())->setValue(""); execute(); } } @@ -68,21 +69,18 @@ void FeaturesAPI_NormalToFace::dump(ModelHighAPI_Dumper& theDumper) const const std::string& aDocName = theDumper.name(aBase->document()); AttributeSelectionPtr anAttrObject; - anAttrObject = aBase->selection(FeaturesPlugin_NormalToFace::OBJECTS_LIST_ID()); + anAttrObject = aBase->selection(FeaturesPlugin_CreateNormalToFace::OBJECTS_LIST_ID()); theDumper << aBase << " = model.getNormal(" << aDocName << ", " << anAttrObject; - if ( !aBase->string(FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID())->value().empty()){ + if ( !aBase->string(FeaturesPlugin_CreateNormalToFace::VERTEX_OPTION_ID())->value().empty()){ AttributeSelectionPtr anAttrVertex = - aBase->selection(FeaturesPlugin_NormalToFace::VERTEX_SELECTED_ID()); + aBase->selection(FeaturesPlugin_CreateNormalToFace::VERTEX_SELECTED_ID()); theDumper << ", " << anAttrVertex; } - //if (!aBase->data()->version().empty()) - // theDumper << ", keepSubResults = True"; - theDumper << ")" << std::endl; } @@ -94,7 +92,7 @@ NormalPtr getNormal(const std::shared_ptr& thePart, { FeaturePtr aFeature = - thePart->addFeature(FeaturesAPI_NormalToFace::ID()); + thePart->addFeature(FeaturesPlugin_CreateNormalToFace::ID()); NormalPtr aNormalToface; @@ -108,7 +106,7 @@ NormalPtr getNormal(const std::shared_ptr& thePart, { FeaturePtr aFeature = - thePart->addFeature(FeaturesAPI_NormalToFace::ID()); + thePart->addFeature(FeaturesPlugin_CreateNormalToFace::ID()); NormalPtr aNormalToface; diff --git a/src/FeaturesAPI/FeaturesAPI_NormalToFace.h b/src/FeaturesAPI/FeaturesAPI_NormalToFace.h index 10554383c..a1ad49159 100644 --- a/src/FeaturesAPI/FeaturesAPI_NormalToFace.h +++ b/src/FeaturesAPI/FeaturesAPI_NormalToFace.h @@ -22,7 +22,7 @@ #include "FeaturesAPI.h" -#include +#include #include #include @@ -52,14 +52,14 @@ public: FEATURESAPI_EXPORT virtual ~FeaturesAPI_NormalToFace(); - INTERFACE_3(FeaturesPlugin_NormalToFace::ID(), - faceselected, FeaturesPlugin_NormalToFace::OBJECTS_LIST_ID(), + INTERFACE_3(FeaturesPlugin_CreateNormalToFace::ID(), + faceSelected, FeaturesPlugin_CreateNormalToFace::OBJECTS_LIST_ID(), ModelAPI_AttributeSelection, /** base face */, - vertexselected, FeaturesPlugin_NormalToFace::VERTEX_SELECTED_ID(), + vertexSelected, FeaturesPlugin_CreateNormalToFace::VERTEX_SELECTED_ID(), ModelAPI_AttributeSelection, /** vetex option */, - vertexoption, FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID(), + vertexOption, FeaturesPlugin_CreateNormalToFace::VERTEX_OPTION_ID(), ModelAPI_AttributeString, /** option */) diff --git a/src/FeaturesPlugin/CMakeLists.txt b/src/FeaturesPlugin/CMakeLists.txt index 3e8ae8999..4777f6aea 100644 --- a/src/FeaturesPlugin/CMakeLists.txt +++ b/src/FeaturesPlugin/CMakeLists.txt @@ -71,6 +71,7 @@ SET(PROJECT_HEADERS FeaturesPlugin_Fillet1D.h FeaturesPlugin_Measurement.h FeaturesPlugin_NormalToFace.h + FeaturesPlugin_CreateNormalToFace.h FeaturesPlugin_FusionFaces.h FeaturesPlugin_RemoveResults.h FeaturesPlugin_Chamfer.h @@ -119,6 +120,7 @@ SET(PROJECT_SOURCES FeaturesPlugin_Fillet1D.cpp FeaturesPlugin_Measurement.cpp FeaturesPlugin_NormalToFace.cpp + FeaturesPlugin_CreateNormalToFace.cpp FeaturesPlugin_FusionFaces.cpp FeaturesPlugin_RemoveResults.cpp FeaturesPlugin_Chamfer.cpp @@ -158,6 +160,7 @@ SET(XML_RESOURCES fillet1d_widget.xml measurement_widget.xml NormalToFace_widget.xml + CreateNormalToFace_widget.xml fusion_faces_widget.xml chamfer_widget.xml copy_widget.xml diff --git a/src/FeaturesPlugin/CreateNormalToFace_widget.xml b/src/FeaturesPlugin/CreateNormalToFace_widget.xml new file mode 100644 index 000000000..c2b4bece0 --- /dev/null +++ b/src/FeaturesPlugin/CreateNormalToFace_widget.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + diff --git a/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.cpp b/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.cpp new file mode 100644 index 000000000..4e9f7df11 --- /dev/null +++ b/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.cpp @@ -0,0 +1,118 @@ +// Copyright (C) 2018-2020 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "FeaturesPlugin_CreateNormalToFace.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +FeaturesPlugin_CreateNormalToFace::FeaturesPlugin_CreateNormalToFace() +{ +} + +void FeaturesPlugin_CreateNormalToFace::initAttributes() +{ + // attribute for object selected + data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(VERTEX_SELECTED_ID(), ModelAPI_AttributeSelection::typeId()); + // attributes for result message and values + data()->addAttribute(VERTEX_OPTION_ID(), ModelAPI_AttributeString::typeId()); + + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERTEX_SELECTED_ID()); + +} + +void FeaturesPlugin_CreateNormalToFace::execute() +{ +AttributeSelectionPtr aSelectionFace = selection(OBJECTS_LIST_ID()); +AttributeSelectionPtr aSelectionPoint = selection(VERTEX_SELECTED_ID()); + +GeomShapePtr aShape; +GeomShapePtr aShapePoint; + if(!string(VERTEX_OPTION_ID())->value().empty()) +{ + if (aSelectionPoint && aSelectionPoint->isInitialized()) { + aShapePoint = aSelectionPoint->value(); + if (!aShapePoint && aSelectionPoint->context()) + aShapePoint = aSelectionPoint->context()->shape(); + } +} + +if (aSelectionFace && aSelectionFace->isInitialized()) { + aShape = aSelectionFace->value(); + if (!aShape && aSelectionFace->context()) + aShape = aSelectionFace->context()->shape(); +} + +if (aShape){ + std::string aError; + std::shared_ptr theNormal(new GeomAPI_Edge); + if( !GeomAlgoAPI_NormalToFace::normal(aShape, + aShapePoint, + theNormal, + aError)) + setError("Error in bounding box calculation :" + aError); + + GeomDirPtr theDir; + std::shared_ptr aPnt = theNormal->lastPoint(); + if (theNormal.get()) { + if (theNormal->isLine()) { + theDir = theNormal->line()->direction(); + } + } + aPnt->translate(theDir, 100 ); + + std::shared_ptr anEdge = + GeomAlgoAPI_EdgeBuilder::line(theNormal->firstPoint(), + aPnt); + + ResultConstructionPtr aConstr = document()->createConstruction(data()); + aConstr->setInfinite(true); + aConstr->setShape(anEdge); + setResult(aConstr); + } +} + +void FeaturesPlugin_CreateNormalToFace::attributeChanged(const std::string& theID) +{ +} + + diff --git a/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.h b/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.h new file mode 100644 index 000000000..77ad40c52 --- /dev/null +++ b/src/FeaturesPlugin/FeaturesPlugin_CreateNormalToFace.h @@ -0,0 +1,85 @@ +// Copyright (C) 2018-2020 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef FeaturesPlugin_CreateNormalToFace_H_ +#define FeaturesPlugin_CreateNormalToFace_H_ + +#include "FeaturesPlugin.h" +#include + +#include +#include +#include + +/// \class FeaturesPlugin_CreateNormalToFace +/// \ingroup Plugins +/// \brief Feature for construct normal to face + +class FeaturesPlugin_CreateNormalToFace : public ModelAPI_Feature +{ +public: + inline static const std::string& ID() + { + static const std::string MY_ID("Normal"); + return MY_ID; + } + + /// \return the kind of a feature. + virtual const std::string& getKind() + { + return ID(); + } + + /// Attribute name for face selected. + inline static const std::string& OBJECTS_LIST_ID() + { + static const std::string MY_OBJECTS_LIST_ID("main_objects"); + return MY_OBJECTS_LIST_ID; + } + + /// Attribute name for vertex selected. + inline static const std::string& VERTEX_SELECTED_ID() + { + static const std::string MY_VERTEX_SELECTED_ID("tool_objects"); + return MY_VERTEX_SELECTED_ID; + } + + /// Attribute name of vertex option. + inline static const std::string& VERTEX_OPTION_ID() + { + static const std::string MY_VERTEX_OPTION_ID("vertex_option"); + return MY_VERTEX_OPTION_ID; + } + + /// Performs the algorithm and stores results it in the data structure. + FEATURESPLUGIN_EXPORT virtual void execute(); + + /// Request for initialization of data model of the feature: adding all attributes + FEATURESPLUGIN_EXPORT virtual void initAttributes(); + + /// Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + FEATURESPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + + /// Use plugin manager for features creation + FeaturesPlugin_CreateNormalToFace(); + +}; + +#endif diff --git a/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.cpp b/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.cpp index 87dc63683..ab98fed58 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.cpp @@ -26,6 +26,8 @@ #include #include #include + +#include #include #include #include @@ -34,11 +36,15 @@ #include #include #include +#include #include #include +#include + #include #include +#include FeaturesPlugin_NormalToFace::FeaturesPlugin_NormalToFace() { @@ -59,65 +65,106 @@ void FeaturesPlugin_NormalToFace::initAttributes() void FeaturesPlugin_NormalToFace::execute() { - AttributeSelectionPtr aSelectionFace = selection(OBJECTS_LIST_ID()); - AttributeSelectionPtr aSelectionPoint = selection(VERTEX_SELECTED_ID()); +AttributeSelectionPtr aSelectionFace = selection(OBJECTS_LIST_ID()); +AttributeSelectionPtr aSelectionPoint = selection(VERTEX_SELECTED_ID()); - GeomShapePtr aShape; - GeomShapePtr aShapePoint; - if(!string(VERTEX_OPTION_ID())->value().empty()) - { - if (aSelectionPoint && aSelectionPoint->isInitialized()) { - aShapePoint = aSelectionPoint->value(); - if (!aShapePoint && aSelectionPoint->context()) - aShapePoint = aSelectionPoint->context()->shape(); - } +GeomShapePtr aShape; +GeomShapePtr aShapePoint; + if(!string(VERTEX_OPTION_ID())->value().empty()) +{ + if (aSelectionPoint && aSelectionPoint->isInitialized()) { + aShapePoint = aSelectionPoint->value(); + if (!aShapePoint && aSelectionPoint->context()) + aShapePoint = aSelectionPoint->context()->shape(); } +} - if (aSelectionFace && aSelectionFace->isInitialized()) { - aShape = aSelectionFace->value(); - if (!aShape && aSelectionFace->context()) - aShape = aSelectionFace->context()->shape(); - } +if (aSelectionFace && aSelectionFace->isInitialized()) { + aShape = aSelectionFace->value(); + if (!aShape && aSelectionFace->context()) + aShape = aSelectionFace->context()->shape(); +} - if (aShape){ - std::string aError; - std::shared_ptr theNormal(new GeomAPI_Edge); - if( !GeomAlgoAPI_NormalToFace::normal(aShape, - aShapePoint, - theNormal, - aError)) - setError("Error in bounding box calculation :" + aError); - - ResultBodyPtr aResultBody = document()->createBody(data()); - if( boolean(CREATENORMAL_ID())->value() ) - { - eraseResults(); - std::shared_ptr anEdge = - GeomAlgoAPI_EdgeBuilder::line(theNormal->firstPoint(), - theNormal->lastPoint()); - - ResultConstructionPtr aConstr = document()->createConstruction(data()); - aConstr->setInfinite(true); - aConstr->setShape(anEdge); - setResult(aConstr); - }else - { - eraseResults(); - // Load the result - aResultBody->store(theNormal); - /*int aVertexIndex = 1; - for (GeomAPI_ShapeExplorer anExp(theNormal, GeomAPI_Shape::VERTEX); anExp.more(); anExp.next()) { - std::string aVertexName = "VertexFromNormal_" + std::to_string((long long)aVertexIndex); - aResultBody->generated(anExp.current(), aVertexName); - aVertexIndex++; - }*/ - setResult(aResultBody); +if (aShape){ + std::string aError; + std::shared_ptr theNormal(new GeomAPI_Edge); + if( !GeomAlgoAPI_NormalToFace::normal(aShape, + aShapePoint, + theNormal, + aError)) + setError("Error in bounding box calculation :" + aError); + + GeomDirPtr theDir; + std::shared_ptr aPnt = theNormal->lastPoint(); + if (theNormal.get()) { + if (theNormal->isLine()) { + theDir = theNormal->line()->direction(); } } + aPnt->translate(theDir, 100 ); + + std::shared_ptr anEdge = + GeomAlgoAPI_EdgeBuilder::line(theNormal->firstPoint(), + aPnt); + + ResultConstructionPtr aConstr = document()->createConstruction(data()); + aConstr->setInfinite(true); + aConstr->setShape(anEdge); + setResult(aConstr); + } + + if(boolean(CREATENORMAL_ID())->value()) + { + if( !myCreateFeature.get() ) + createNormal(); + updateNormal(); + }else{ + if( myCreateFeature.get() ) + { + myCreateFeature->eraseResults(); + SessionPtr aSession = ModelAPI_Session::get(); + DocumentPtr aDoc = aSession->activeDocument(); + aDoc->removeFeature(myCreateFeature); + myCreateFeature.reset(); + } + } } void FeaturesPlugin_NormalToFace::attributeChanged(const std::string& theID) { + if (theID == OBJECTS_LIST_ID()) { + if( myCreateFeature.get() ) + updateNormal(); + } } +//================================================================================================= +void FeaturesPlugin_NormalToFace::createNormal() +{ + SessionPtr aSession = ModelAPI_Session::get(); + + DocumentPtr aDoc = aSession->activeDocument(); + + if (aDoc.get()) { + myCreateFeature = aDoc->addFeature(FeaturesPlugin_CreateNormalToFace::ID()); + } +} +void FeaturesPlugin_NormalToFace::updateNormal() +{ + myCreateFeature->selection(FeaturesPlugin_CreateNormalToFace::OBJECTS_LIST_ID()) + ->setValue( selection(OBJECTS_LIST_ID())->context() , + selection(OBJECTS_LIST_ID())->value() ); + + myCreateFeature->string(FeaturesPlugin_CreateNormalToFace::VERTEX_OPTION_ID()) + ->setValue( string(VERTEX_OPTION_ID())->value()); + + if(!string(VERTEX_OPTION_ID())->value().empty()) + { + myCreateFeature->selection(FeaturesPlugin_CreateNormalToFace::VERTEX_SELECTED_ID()) + ->setValue( selection(VERTEX_SELECTED_ID())->context() , + selection(VERTEX_SELECTED_ID())->value() ); + } + + myCreateFeature->execute(); +} \ No newline at end of file diff --git a/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.h b/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.h index fa6e8b3dc..1b171ff9e 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.h +++ b/src/FeaturesPlugin/FeaturesPlugin_NormalToFace.h @@ -36,7 +36,7 @@ class FeaturesPlugin_NormalToFace : public ModelAPI_Feature public: inline static const std::string& ID() { - static const std::string MY_ID("Normal"); + static const std::string MY_ID("NormalMacro"); return MY_ID; } @@ -84,9 +84,18 @@ public: /// \param theID identifier of changed attribute FEATURESPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + /// Reimplemented from ModelAPI_Feature::isMacro(). Returns true. + FEATURESPLUGIN_EXPORT virtual bool isMacro() const { return true; } + /// Use plugin manager for features creation FeaturesPlugin_NormalToFace(); + private: + + void createNormal(); + void updateNormal(); + FeaturePtr myCreateFeature; + }; #endif diff --git a/src/FeaturesPlugin/FeaturesPlugin_Plugin.cpp b/src/FeaturesPlugin/FeaturesPlugin_Plugin.cpp index b4dabda19..8c5e64ad6 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Plugin.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Plugin.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -189,7 +190,9 @@ FeaturePtr FeaturesPlugin_Plugin::createFeature(std::string theFeatureID) } else if (theFeatureID == FeaturesPlugin_Measurement::ID()) { return FeaturePtr(new FeaturesPlugin_Measurement); } else if (theFeatureID == FeaturesPlugin_NormalToFace::ID()) { - return FeaturePtr(new FeaturesPlugin_NormalToFace); + return FeaturePtr(new FeaturesPlugin_NormalToFace); + } else if (theFeatureID == FeaturesPlugin_CreateNormalToFace::ID()) { + return FeaturePtr(new FeaturesPlugin_CreateNormalToFace); } else if (theFeatureID == FeaturesPlugin_RemoveResults::ID()) { return FeaturePtr(new FeaturesPlugin_RemoveResults); } else if (theFeatureID == FeaturesPlugin_Chamfer::ID()) { diff --git a/src/FeaturesPlugin/plugin-Features.xml b/src/FeaturesPlugin/plugin-Features.xml index 50395cf46..3e17f562a 100644 --- a/src/FeaturesPlugin/plugin-Features.xml +++ b/src/FeaturesPlugin/plugin-Features.xml @@ -179,10 +179,14 @@ - + + + -- 2.39.2