Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_InspectNormalToFace.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_InspectNormalToFace.h"
21
22 #include <FeaturesPlugin_NormalToFace.h>
23
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>
31
32 #include <GeomAPI_Dir.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_Vertex.h>
36
37 #include <GeomAlgoAPI_EdgeBuilder.h>
38 #include <GeomAlgoAPI_NormalToFace.h>
39 #include <GeomAlgoAPI_PointBuilder.h>
40
41 #include <Config_PropManager.h>
42
43 #include <iomanip>
44 #include <sstream>
45
46 //=================================================================================================
47 FeaturesPlugin_InspectNormalToFace::FeaturesPlugin_InspectNormalToFace() {}
48
49 //=================================================================================================
50 void FeaturesPlugin_InspectNormalToFace::initAttributes()
51 {
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());
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERTEX_SELECTED_ID());
60
61 }
62
63 //=================================================================================================
64 void FeaturesPlugin_InspectNormalToFace::execute()
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(), aPnt);
105
106     ResultConstructionPtr aConstr = document()->createConstruction(data());
107     aConstr->setInfinite(true);
108     aConstr->setShape(anEdge);
109     setResult(aConstr);
110   }
111
112   if (boolean(CREATENORMAL_ID())->value()) {
113     if (!myCreateFeature.get())
114       createNormal();
115     updateNormal();
116   } else {
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();
123     }
124   }
125 }
126
127 //=================================================================================================
128 void FeaturesPlugin_InspectNormalToFace::attributeChanged(const std::string& theID)
129 {
130    if (theID == FACE_SELECTED_ID()) {
131     if (myCreateFeature.get())
132       updateNormal();
133   }
134 }
135
136 //=================================================================================================
137 void FeaturesPlugin_InspectNormalToFace::createNormal()
138 {
139   SessionPtr aSession = ModelAPI_Session::get();
140
141   DocumentPtr aDoc =  aSession->activeDocument();
142
143   if (aDoc.get()) {
144     myCreateFeature = aDoc->addFeature(FeaturesPlugin_NormalToFace::ID());
145   }
146 }
147
148 //=================================================================================================
149 void FeaturesPlugin_InspectNormalToFace::updateNormal()
150 {
151   myCreateFeature->selection(FeaturesPlugin_NormalToFace::FACE_SELECTED_ID())
152                         ->setValue(selection(FACE_SELECTED_ID())->context() ,
153                                     selection(FACE_SELECTED_ID())->value());
154
155   myCreateFeature->string(FeaturesPlugin_NormalToFace::VERTEX_OPTION_ID())
156                         ->setValue(string(VERTEX_OPTION_ID())->value());
157
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());
162   }
163
164   myCreateFeature->execute();
165 }