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