Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_GlueFaces.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_GlueFaces.h"
21
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <GeomAlgoAPI_CompoundBuilder.h>
35 #include <GeomAlgoAPI_GlueFaces.h>
36 #include <GeomAlgoAPI_ShapeTools.h>
37 #include <GeomAlgoAPI_Tools.h>
38
39 #include <sstream>
40
41
42 //==================================================================================================
43 FeaturesPlugin_GlueFaces::FeaturesPlugin_GlueFaces()
44 {
45 }
46
47 //==================================================================================================
48 void FeaturesPlugin_GlueFaces::initAttributes()
49 {
50   data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
51   data()->addAttribute(TOLERANCE_ID(), ModelAPI_AttributeDouble::typeId());
52   data()->addAttribute(KEEP_NON_SOLIDS_ID(), ModelAPI_AttributeBoolean::typeId());
53 }
54
55 //==================================================================================================
56 void FeaturesPlugin_GlueFaces::execute()
57 {
58   // Collect all selected shapes into a single list of shapes
59   ListOfShape aShapes;
60   getOriginalShapes(OBJECTS_LIST_ID(), aShapes);
61
62   // Get all other feature arguments
63   double aTolerance = real(FeaturesPlugin_GlueFaces::TOLERANCE_ID())->value();
64   bool isKeepNonSolids = boolean(FeaturesPlugin_GlueFaces::KEEP_NON_SOLIDS_ID())->value();
65
66   std::shared_ptr<GeomAlgoAPI_GlueFaces> aGluingAlgo(new GeomAlgoAPI_GlueFaces(aShapes, aTolerance, isKeepNonSolids));
67
68   std::string anError;
69   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aGluingAlgo, getKind(), anError))
70   {
71     setError(anError);
72     eraseResults();
73     return;
74   }
75
76   // Store result.
77   GeomShapePtr aResult = aGluingAlgo->shape();
78
79   if (!isGlued(aShapes, aResult))
80   {
81     static const std::string anError2 = "Error: No faces were glued.";
82     setError(anError2);
83     data()->execState(ModelAPI_ExecState::ModelAPI_StateDone);
84     eraseResults();
85     setResultFromInput(aShapes);
86     return;
87   }
88
89   if (aResult->isCompound()) {
90     aResult = GeomAlgoAPI_ShapeTools::groupSharedTopology(aResult);
91
92     // If the subshape of the compound is already a Compound or CompSolid,
93     // use that compound subshape as the result.
94     if (aResult->typeOfCompoundShapes() == GeomAPI_Shape::COMPSOLID) {
95       ListOfShape aResults;
96       aResult = GeomAlgoAPI_ShapeTools::combineShapes(aResult, GeomAPI_Shape::COMPSOLID, aResults);
97     }
98     else if (aResult->typeOfCompoundShapes() == GeomAPI_Shape::COMPOUND) {
99       aResult = aResult->subShapes(GeomAPI_Shape::COMPOUND).front();
100     }
101   }
102
103   int anIndex = 0;
104   ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
105   aResultBody->storeModified(aShapes, aResult, aGluingAlgo);
106
107   // Ensure the result body is named after this feature
108   std::wstring aBaseName = feature(aResultBody)->data()->name();
109   std::wostringstream aNameStr;
110   aNameStr << aBaseName << "_" << (anIndex+1);
111   std::wstring aName = aNameStr.str();
112   aResultBody->data()->setName(aName);
113
114   setResult(aResultBody, anIndex);
115 }
116
117 //=================================================================================================
118 void FeaturesPlugin_GlueFaces::getOriginalShapes(const std::string& theAttributeName,
119                                                  ListOfShape&       theShapes)
120 {
121   // Collect all selections into a single list of shapes
122   AttributeSelectionListPtr aSelectionList = selectionList(theAttributeName);
123   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex)
124   {
125     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
126     GeomShapePtr aShape = aSelection->value();
127     GeomShapePtr aContext = aSelection->context()->shape();
128     if (!aShape.get())
129     {
130       aShape = aContext;
131     }
132     theShapes.push_back(aShape);
133   }
134 }
135
136 //=================================================================================================
137 bool FeaturesPlugin_GlueFaces::isGlued(const ListOfShape& theInputs,
138                                        const GeomShapePtr theResult)
139 {
140   if (!theResult.get())
141     return false;
142
143   // Consider the list of input shapes the same as the result, if
144   //  * the total number of faces did NOT change.
145   int nbInputFaces = 0, nbInputEdges = 0;
146   for (ListOfShape::const_iterator anIt = theInputs.cbegin();
147        anIt != theInputs.cend();
148        ++anIt)
149   {
150     GeomShapePtr aShape = *anIt;
151     if (aShape.get())
152     {
153       nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
154       nbInputEdges += aShape->subShapes(GeomAPI_Shape::EDGE, true).size();
155     }
156   }
157
158   int nbResultFaces = 0, nbResultEdges = 0;
159   nbResultFaces = theResult->subShapes(GeomAPI_Shape::FACE, true).size();
160   nbResultEdges = theResult->subShapes(GeomAPI_Shape::EDGE, true).size();
161   return(0 < nbResultFaces && ((nbResultFaces < nbInputFaces) || (nbResultFaces == nbInputFaces && nbResultEdges < nbInputEdges)));
162 }
163
164 //=================================================================================================
165 void FeaturesPlugin_GlueFaces::setResultFromInput(const ListOfShape& theInputs)
166 {
167   GeomShapePtr aResult;
168
169   // Make sure the result will be a compound of the input shapes, if not already
170   if (theInputs.size() == 1 && theInputs.front().get() && theInputs.front()->isCompound())
171   {
172     aResult = theInputs.front();
173   }
174   else
175   {
176     aResult = GeomAlgoAPI_CompoundBuilder::compound(theInputs);
177   }
178
179   int anIndex = 0;
180   ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
181   aResultBody->store(aResult);
182
183   setResult(aResultBody, anIndex);
184 }