1 // Copyright (C) 2017-20xx 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "BuildPlugin_Filling.h"
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>
30 #include <GeomAlgoAPI_Copy.h>
31 #include <GeomAlgoAPI_Filling.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_Wire.h>
40 struct FillingParameters
52 //=================================================================================================
53 BuildPlugin_Filling::BuildPlugin_Filling()
57 //=================================================================================================
58 void BuildPlugin_Filling::initAttributes()
60 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
61 data()->addAttribute(ADVANCED_OPTIONS_ID(), ModelAPI_AttributeString::typeId());
62 data()->addAttribute(METHOD_ID(), ModelAPI_AttributeString::typeId());
63 data()->addAttribute(MINIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
64 data()->addAttribute(MAXIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
65 data()->addAttribute(NUMBER_OF_ITERATIONS_ID(), ModelAPI_AttributeInteger::typeId());
66 data()->addAttribute(TOLERANCE_2D_ID(), ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(TOLERANCE_3D_ID(), ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(APPROXIMATION_ID(), ModelAPI_AttributeBoolean::typeId());
70 restoreDefaultParameters();
71 string(ADVANCED_OPTIONS_ID())->setValue("");
74 //=================================================================================================
75 void BuildPlugin_Filling::execute()
77 // get parameters of algorithm
78 FillingParameters aParameters;
79 aParameters.method = string(METHOD_ID())->value();
80 aParameters.minDegree = integer(MINIMAL_DEGREE_ID())->value();
81 aParameters.maxDegree = integer(MAXIMAL_DEGREE_ID())->value();
82 aParameters.nbIter = integer(NUMBER_OF_ITERATIONS_ID())->value();
83 aParameters.tol2D = real(TOLERANCE_2D_ID())->value();
84 aParameters.tol3D = real(TOLERANCE_3D_ID())->value();
85 aParameters.isApprox = boolean(APPROXIMATION_ID())->value();
87 if (aParameters.minDegree > aParameters.maxDegree) {
88 setError("Error: " + getKind() + " algorithm failed (max deg < min deg).");
92 std::shared_ptr<GeomAlgoAPI_Filling> aFilling(
93 new GeomAlgoAPI_Filling(aParameters.minDegree, aParameters.maxDegree, aParameters.nbIter,
94 aParameters.tol2D, aParameters.tol3D));
96 // get base objects list
97 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
98 if (aSelectionList->size() <= 1)
101 // collect base shapes
102 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
103 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
104 GeomEdgePtr anEdge = toEdge(aSelection->value(), aParameters.method);
106 myLastEdgeStartPoint = GeomPointPtr();
107 myLastEdgeEndPoint = GeomPointPtr();
110 aFilling->add(anEdge);
112 myLastEdgeStartPoint = GeomPointPtr();
113 myLastEdgeEndPoint = GeomPointPtr();
116 aFilling->build(aParameters.isApprox);
117 if (isAlgorithmFailed(aFilling)) {
123 GeomShapePtr aCreatedFace = aFilling->shape();
124 ResultBodyPtr aResultBody = document()->createBody(data());
125 aResultBody->store(aCreatedFace);
128 for(GeomAPI_ShapeExplorer anExp(aCreatedFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
129 GeomShapePtr anEdge = anExp.current();
130 aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeInd), ++anEdgeInd);
132 setResult(aResultBody, 0);
135 bool BuildPlugin_Filling::isAlgorithmFailed(
136 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
138 if (!theAlgorithm->isDone()) {
139 static const std::string aFeatureError = "Error: filling algorithm failed.";
140 setError(aFeatureError);
143 if (theAlgorithm->shape()->isNull()) {
144 static const std::string aShapeError = "Error: Resulting shape of filling is Null.";
145 setError(aShapeError);
148 if (!theAlgorithm->isValid()) {
149 std::string aFeatureError = "Error: Resulting shape of filling is not valid.";
150 setError(aFeatureError);
156 //=================================================================================================
157 void BuildPlugin_Filling::attributeChanged(const std::string& theID)
159 if (theID == ADVANCED_OPTIONS_ID() && string(ADVANCED_OPTIONS_ID())->value().empty()) {
160 // Advanced options flag just unchecked => restore default state of all parameters
161 restoreDefaultParameters();
165 //=================================================================================================
166 GeomEdgePtr BuildPlugin_Filling::toEdge(const GeomShapePtr& theShape, const std::string& theMethod)
169 switch (theShape->shapeType()) {
170 case GeomAPI_Shape::EDGE:
171 anEdge = GeomEdgePtr(new GeomAPI_Edge(GeomAlgoAPI_Copy(theShape).shape()));
173 case GeomAPI_Shape::WIRE:
174 anEdge = GeomAlgoAPI_ShapeTools::wireToEdge(
175 GeomWirePtr(new GeomAPI_Wire(theShape)));
181 if (!anEdge || anEdge->empty()) {
182 static const std::string aFeatureError =
183 "Error: incorrect type of input feature (edges/wire are supported only).";
184 setError(aFeatureError);
188 // correct edge orientation according to filling method
189 if (theMethod == Method::AUTO_CORRECT_ORIENTATION()) {
190 // check the distance to previous edge boundaries, reverse edge if necessary
191 GeomPointPtr aStartPnt = anEdge->firstPoint();
192 GeomPointPtr aEndPnt = anEdge->lastPoint();
193 if (anEdge->orientation() == GeomAPI_Shape::REVERSED) {
194 aStartPnt = anEdge->lastPoint();
195 aEndPnt = anEdge->firstPoint();
197 bool isReverse = false;
198 if (myLastEdgeStartPoint) {
199 double d1 = myLastEdgeStartPoint->distance(aStartPnt)
200 + myLastEdgeEndPoint->distance(aEndPnt);
201 double d2 = myLastEdgeStartPoint->distance(aEndPnt)
202 + myLastEdgeEndPoint->distance(aStartPnt);
203 if (fabs(d1 - d2) < 1.e-7) {
204 // undefined case => check distance to start point only
205 d1 = myLastEdgeStartPoint->distance(aStartPnt);
206 d2 = myLastEdgeStartPoint->distance(aEndPnt);
213 myLastEdgeStartPoint = aEndPnt;
214 myLastEdgeEndPoint = aStartPnt;
216 myLastEdgeStartPoint = aStartPnt;
217 myLastEdgeEndPoint = aEndPnt;
220 else if (theMethod == Method::USE_CURVE_INFORMATION()) {
221 // make all edges FORWARD to avoid reversing the curves by GeomAlgoAPI_Filling algorithm
222 anEdge->setOrientation(GeomAPI_Shape::FORWARD);
227 //=================================================================================================
228 void BuildPlugin_Filling::restoreDefaultParameters()
230 string(METHOD_ID())->setValue(METHOD_DEFAULT());
231 integer(MINIMAL_DEGREE_ID())->setValue(MINIMAL_DEGREE_DEFAULT());
232 integer(MAXIMAL_DEGREE_ID())->setValue(MAXIMAL_DEGREE_DEFAULT());
233 integer(NUMBER_OF_ITERATIONS_ID())->setValue(NUMBER_OF_ITERATIONS_DEFAULT());
234 real(TOLERANCE_2D_ID())->setValue(TOLERANCE_2D_DEFAULT());
235 real(TOLERANCE_3D_ID())->setValue(TOLERANCE_3D_DEFAULT());
236 boolean(APPROXIMATION_ID())->setValue(APPROXIMATION_DEFAULT());