Salome HOME
5cff12cc4bf76e600ea651baea1fac88c3d7bdc1
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_NormalToFace.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_NormalToFace.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
27 #include <GeomAPI_Vertex.h>
28 #include <GeomAPI_Edge.h>
29 #include <Config_PropManager.h>
30 #include <GeomAlgoAPI_NormalToFace.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Dir.h>
33 #include <GeomAlgoAPI_EdgeBuilder.h>
34 #include <ModelAPI_ResultConstruction.h>
35
36 #include <iomanip>
37 #include <sstream>
38
39 //=================================================================================================
40 FeaturesPlugin_NormalToFace::FeaturesPlugin_NormalToFace()
41 {
42 }
43
44 //=================================================================================================
45 void FeaturesPlugin_NormalToFace::initAttributes()
46 {
47   // attribute for object selected
48   data()->addAttribute(FACE_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
49   data()->addAttribute(VERTEX_SELECTED_ID(), ModelAPI_AttributeSelection::typeId());
50
51   // attributes for result message and values
52   data()->addAttribute(VERTEX_OPTION_ID(), ModelAPI_AttributeString::typeId());
53
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERTEX_SELECTED_ID());
55 }
56
57 //=================================================================================================
58 void FeaturesPlugin_NormalToFace::attributeChanged(const std::string& /*theID*/)
59 {
60 }
61
62 //=================================================================================================
63 void FeaturesPlugin_NormalToFace::execute()
64 {
65
66   AttributeSelectionPtr aSelectionFace = selection(FACE_SELECTED_ID());
67   AttributeSelectionPtr aSelectionPoint = selection(VERTEX_SELECTED_ID());
68
69   GeomShapePtr aShape;
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();
76       }
77   }
78
79   if (aSelectionFace && aSelectionFace->isInitialized()) {
80     aShape = aSelectionFace->value();
81     if (!aShape && aSelectionFace->context())
82       aShape = aSelectionFace->context()->shape();
83   }
84
85   if (aShape) {
86     std::string aError;
87     std::shared_ptr<GeomAPI_Edge> theNormal(new GeomAPI_Edge);
88     if (!GeomAlgoAPI_NormalToFace::normal(aShape,
89                                           aShapePoint,
90                                           theNormal,
91                                           aError))
92       setError("Error in bounding box calculation :" +  aError);
93
94     GeomDirPtr theDir;
95     std::shared_ptr<GeomAPI_Pnt> aPnt = theNormal->lastPoint();
96     if (theNormal.get()) {
97       if (theNormal->isLine()) {
98         theDir = theNormal->line()->direction();
99       }
100     }
101     aPnt->translate(theDir, 100);
102
103     std::shared_ptr<GeomAPI_Edge> anEdge =
104                         GeomAlgoAPI_EdgeBuilder::line(theNormal->firstPoint(),
105                                                       aPnt);
106
107     ResultConstructionPtr aConstr = document()->createConstruction(data());
108     aConstr->setInfinite(true);
109     aConstr->setShape(anEdge);
110     setResult(aConstr);
111   }
112 }
113
114