Salome HOME
Filling operation: create a face from a set of edges/wires
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Filling.cpp
1 // Copyright (C) 2017-20xx  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "BuildPlugin_Filling.h"
22
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_ResultBody.h>
29
30 #include <GeomAlgoAPI_Filling.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAPI_Wire.h>
36
37 #include <cmath>
38
39 struct FillingParameters
40 {
41   std::string method;
42   int minDegree;
43   int maxDegree;
44   int nbIter;
45   double tol2D;
46   double tol3D;
47   bool isApprox;
48 };
49
50
51 //=================================================================================================
52 BuildPlugin_Filling::BuildPlugin_Filling()
53 {
54 }
55
56 //=================================================================================================
57 void BuildPlugin_Filling::initAttributes()
58 {
59   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
60   data()->addAttribute(ADVANCED_OPTIONS_ID(), ModelAPI_AttributeString::typeId());
61   data()->addAttribute(METHOD_ID(), ModelAPI_AttributeString::typeId());
62   data()->addAttribute(MINIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
63   data()->addAttribute(MAXIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
64   data()->addAttribute(NUMBER_OF_ITERATIONS_ID(), ModelAPI_AttributeInteger::typeId());
65   data()->addAttribute(TOLERANCE_2D_ID(), ModelAPI_AttributeDouble::typeId());
66   data()->addAttribute(TOLERANCE_3D_ID(), ModelAPI_AttributeDouble::typeId());
67   data()->addAttribute(APPROXIMATION_ID(), ModelAPI_AttributeBoolean::typeId());
68
69   restoreDefaultParameters();
70   string(ADVANCED_OPTIONS_ID())->setValue("");
71 }
72
73 //=================================================================================================
74 void BuildPlugin_Filling::execute()
75 {
76   // get parameters of algorithm
77   FillingParameters aParameters;
78   aParameters.method = string(METHOD_ID())->value();
79   aParameters.minDegree = integer(MINIMAL_DEGREE_ID())->value();
80   aParameters.maxDegree = integer(MAXIMAL_DEGREE_ID())->value();
81   aParameters.nbIter = integer(NUMBER_OF_ITERATIONS_ID())->value();
82   aParameters.tol2D = real(TOLERANCE_2D_ID())->value();
83   aParameters.tol3D = real(TOLERANCE_3D_ID())->value();
84   aParameters.isApprox = boolean(APPROXIMATION_ID())->value();
85
86   if (aParameters.minDegree > aParameters.maxDegree) {
87     setError("Error: " + getKind() + " algorithm failed (max deg < min deg).");
88     return;
89   }
90
91   std::shared_ptr<GeomAlgoAPI_Filling> aFilling(
92       new GeomAlgoAPI_Filling(aParameters.minDegree, aParameters.maxDegree, aParameters.nbIter,
93                               aParameters.tol2D, aParameters.tol3D));
94
95   // get base objects list
96   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
97   if (aSelectionList->size() <= 1)
98     return;
99
100   // collect base shapes
101   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
102     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
103     GeomEdgePtr anEdge = toEdge(aSelection->value(), aParameters.method);
104     if (!anEdge) {
105       myLastEdgeStartPoint = GeomPointPtr();
106       myLastEdgeEndPoint = GeomPointPtr();
107       return;
108     }
109     aFilling->add(anEdge);
110   }
111   myLastEdgeStartPoint = GeomPointPtr();
112   myLastEdgeEndPoint = GeomPointPtr();
113
114   // build result
115   aFilling->build(aParameters.isApprox);
116   if (isFailed(aFilling)) {
117     removeResults(0);
118     return;
119   }
120
121   /// store result
122   GeomShapePtr aCreatedFace = aFilling->shape();
123   ResultBodyPtr aResultBody = document()->createBody(data());
124   aResultBody->store(aCreatedFace);
125   // store edges
126   int anEdgeInd = 0;
127   for(GeomAPI_ShapeExplorer anExp(aCreatedFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
128     GeomShapePtr anEdge = anExp.current();
129     aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeInd), ++anEdgeInd);
130   }
131   setResult(aResultBody, 0);
132 }
133
134 bool BuildPlugin_Filling::isFailed(
135     const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
136 {
137   if (!theAlgorithm->isDone()) {
138     static const std::string aFeatureError = "Error: filling algorithm failed.";
139     setError(aFeatureError);
140     return true;
141   }
142   if (theAlgorithm->shape()->isNull()) {
143     static const std::string aShapeError = "Error: Resulting shape of filling is Null.";
144     setError(aShapeError);
145     return true;
146   }
147   if (!theAlgorithm->isValid()) {
148     std::string aFeatureError = "Error: Resulting shape of filling is not valid.";
149     setError(aFeatureError);
150     return true;
151   }
152   return false;
153 }
154
155 //=================================================================================================
156 void BuildPlugin_Filling::attributeChanged(const std::string& theID)
157 {
158   if (theID == ADVANCED_OPTIONS_ID() && string(ADVANCED_OPTIONS_ID())->value().empty()) {
159     // Advanced options flag just unchecked => restore default state of all parameters
160     restoreDefaultParameters();
161   }
162 }
163
164 //=================================================================================================
165 GeomEdgePtr BuildPlugin_Filling::toEdge(const GeomShapePtr& theShape, const std::string& theMethod)
166 {
167   GeomEdgePtr anEdge;
168   switch (theShape->shapeType()) {
169   case GeomAPI_Shape::EDGE:
170     anEdge = GeomEdgePtr(new GeomAPI_Edge(theShape));
171     break;
172   case GeomAPI_Shape::WIRE:
173     anEdge = GeomAlgoAPI_ShapeTools::wireToEdge(
174         GeomWirePtr(new GeomAPI_Wire(theShape)));
175     break;
176   default:
177     break;
178   }
179
180   if (!anEdge || anEdge->empty()) {
181     static const std::string aFeatureError =
182         "Error: incorrect type of input feature (edges/wire are supported only).";
183     setError(aFeatureError);
184     return anEdge;
185   }
186
187   // correct edge orientation according to filling method
188   if (theMethod == Method::AUTO_CORRECT_ORIENTATION()) {
189     // check the distance to previous edge boundaries, reverse edge if necessary
190     GeomPointPtr aStartPnt = anEdge->firstPoint();
191     GeomPointPtr aEndPnt = anEdge->lastPoint();
192     bool isReverse = false;
193     if (myLastEdgeStartPoint) {
194       double d1 = myLastEdgeStartPoint->distance(aStartPnt)
195                 + myLastEdgeEndPoint->distance(aEndPnt);
196       double d2 = myLastEdgeStartPoint->distance(aEndPnt)
197                 + myLastEdgeEndPoint->distance(aStartPnt);
198       if (fabs(d1 - d2) < 1.e-7) {
199         // undefined case => check distance to start point only
200         d1 = myLastEdgeStartPoint->distance(aStartPnt);
201         d2 = myLastEdgeStartPoint->distance(aEndPnt);
202       }
203       isReverse = d2 < d1;
204     }
205
206     if (isReverse) {
207       anEdge->reverse();
208       myLastEdgeStartPoint = aEndPnt;
209       myLastEdgeEndPoint = aStartPnt;
210     } else {
211       myLastEdgeStartPoint = aStartPnt;
212       myLastEdgeEndPoint = aEndPnt;
213     }
214   }
215   else if (theMethod == Method::USE_CURVE_INFORMATION()) {
216     // make all edges FORWARD to avoid reversing the curves by GeomAlgoAPI_Filling algorithm
217     anEdge->setOrientation(GeomAPI_Shape::FORWARD);
218   }
219   return anEdge;
220 }
221
222 //=================================================================================================
223 void BuildPlugin_Filling::restoreDefaultParameters()
224 {
225   string(METHOD_ID())->setValue(METHOD_DEFAULT());
226   integer(MINIMAL_DEGREE_ID())->setValue(MINIMAL_DEGREE_DEFAULT());
227   integer(MAXIMAL_DEGREE_ID())->setValue(MAXIMAL_DEGREE_DEFAULT());
228   integer(NUMBER_OF_ITERATIONS_ID())->setValue(NUMBER_OF_ITERATIONS_DEFAULT());
229   real(TOLERANCE_2D_ID())->setValue(TOLERANCE_2D_DEFAULT());
230   real(TOLERANCE_3D_ID())->setValue(TOLERANCE_3D_DEFAULT());
231   boolean(APPROXIMATION_ID())->setValue(APPROXIMATION_DEFAULT());
232 }