1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_InspectNormalToFace.h"
22 #include <FeaturesPlugin_NormalToFace.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Validator.h>
32 #include <GeomAPI_Dir.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_Vertex.h>
37 #include <GeomAlgoAPI_EdgeBuilder.h>
38 #include <GeomAlgoAPI_NormalToFace.h>
39 #include <GeomAlgoAPI_PointBuilder.h>
41 #include <Config_PropManager.h>
46 //=================================================================================================
47 FeaturesPlugin_InspectNormalToFace::FeaturesPlugin_InspectNormalToFace() {}
49 //=================================================================================================
50 void FeaturesPlugin_InspectNormalToFace::initAttributes()
52 // attribute for object selected
53 data()->addAttribute(FACE_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(VERTEX_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
55 // attributes for result message and values
56 data()->addAttribute(CREATENORMAL_ID(), ModelAPI_AttributeBoolean::typeId());
57 data()->addAttribute(VERTEX_OPTION_ID(), ModelAPI_AttributeString::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERTEX_SELECTED_ID());
63 //=================================================================================================
64 void FeaturesPlugin_InspectNormalToFace::execute()
66 AttributeSelectionPtr aSelectionFace = selection(FACE_SELECTED_ID());
67 AttributeSelectionPtr aSelectionPoint = selection(VERTEX_SELECTED_ID());
70 GeomShapePtr aShapePoint;
71 if (!string(VERTEX_OPTION_ID())->value().empty()) {
72 if (aSelectionPoint && aSelectionPoint->isInitialized()) {
73 aShapePoint = aSelectionPoint->value();
74 if (!aShapePoint && aSelectionPoint->context())
75 aShapePoint = aSelectionPoint->context()->shape();
79 if (aSelectionFace && aSelectionFace->isInitialized()) {
80 aShape = aSelectionFace->value();
81 if (!aShape && aSelectionFace->context())
82 aShape = aSelectionFace->context()->shape();
87 std::shared_ptr<GeomAPI_Edge> theNormal(new GeomAPI_Edge);
88 if (!GeomAlgoAPI_NormalToFace::normal(aShape,
92 setError("Error in bounding box calculation :" + aError);
95 std::shared_ptr<GeomAPI_Pnt> aPnt = theNormal->lastPoint();
96 if (theNormal.get()) {
97 if (theNormal->isLine()) {
98 theDir = theNormal->line()->direction();
101 aPnt->translate(theDir, 100);
103 std::shared_ptr<GeomAPI_Edge> anEdge =
104 GeomAlgoAPI_EdgeBuilder::line(theNormal->firstPoint(), aPnt);
106 ResultConstructionPtr aConstr = document()->createConstruction(data());
107 aConstr->setInfinite(true);
108 aConstr->setShape(anEdge);
112 if (boolean(CREATENORMAL_ID())->value()) {
113 if (!myCreateFeature.get())
117 if (myCreateFeature.get()) {
118 myCreateFeature->eraseResults();
119 SessionPtr aSession = ModelAPI_Session::get();
120 DocumentPtr aDoc = aSession->activeDocument();
121 aDoc->removeFeature(myCreateFeature);
122 myCreateFeature.reset();
127 //=================================================================================================
128 void FeaturesPlugin_InspectNormalToFace::attributeChanged(const std::string& theID)
130 if (theID == FACE_SELECTED_ID()) {
131 if (myCreateFeature.get())
136 //=================================================================================================
137 void FeaturesPlugin_InspectNormalToFace::createNormal()
139 SessionPtr aSession = ModelAPI_Session::get();
141 DocumentPtr aDoc = aSession->activeDocument();
144 myCreateFeature = aDoc->addFeature(FeaturesPlugin_NormalToFace::ID());
148 //=================================================================================================
149 void FeaturesPlugin_InspectNormalToFace::updateNormal()
151 myCreateFeature->selection(FeaturesPlugin_NormalToFace::FACE_SELECTED_ID())
152 ->setValue(selection(FACE_SELECTED_ID())->context() ,
153 selection(FACE_SELECTED_ID())->value());
155 myCreateFeature->string(FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID())
156 ->setValue(string(VERTEX_OPTION_ID())->value());
158 if (!string(VERTEX_OPTION_ID())->value().empty()) {
159 myCreateFeature->selection(FeaturesPlugin_NormalToFace::VERTEX_SELECTED_ID())
160 ->setValue(selection(VERTEX_SELECTED_ID())->context() ,
161 selection(VERTEX_SELECTED_ID())->value());
164 myCreateFeature->execute();