]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_GlueFaces.cpp
Salome HOME
added new GlueFaces feature
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_GlueFaces.cpp
1 // Copyright (C) 2014-2022  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 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_Tools.h>
37
38
39 //==================================================================================================
40 FeaturesPlugin_GlueFaces::FeaturesPlugin_GlueFaces()
41 {
42 }
43
44 //==================================================================================================
45 void FeaturesPlugin_GlueFaces::initAttributes()
46 {
47   data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
48   data()->addAttribute(TOLERANCE_ID(), ModelAPI_AttributeDouble::typeId());
49   data()->addAttribute(KEEP_NON_SOLIDS_ID(), ModelAPI_AttributeBoolean::typeId());
50 }
51
52 //==================================================================================================
53 void FeaturesPlugin_GlueFaces::execute()
54 {
55   // Collect all selected shapes into a single list of shapes
56   ListOfShape aShapes;
57   getOriginalShapes(OBJECTS_LIST_ID(), aShapes);
58
59   // Get all other feature arguments
60   double aTolerance = real(FeaturesPlugin_GlueFaces::TOLERANCE_ID())->value();
61   bool isKeepNonSolids = boolean(FeaturesPlugin_GlueFaces::KEEP_NON_SOLIDS_ID())->value();
62
63   std::shared_ptr<GeomAlgoAPI_GlueFaces> aGluingAlgo(new GeomAlgoAPI_GlueFaces(aShapes, aTolerance, isKeepNonSolids));
64
65   std::string anError;
66   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aGluingAlgo, getKind(), anError))
67   {
68     setError(anError);
69     eraseResults();
70     return;
71   }
72
73   // Store result.
74   GeomShapePtr aResult = aGluingAlgo->shape();
75
76   if (!isGlued(aShapes, aResult))
77   {
78     static const std::string anError2 = "Error: No faces were glued.";
79     setError(anError2);
80     data()->execState(ModelAPI_ExecState::ModelAPI_StateDone);
81     eraseResults();
82     setResultFromInput(aShapes);
83     return;
84   }
85
86   int anIndex = 0;
87   ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
88   aResultBody->storeModified(aShapes, aResult, aGluingAlgo);
89
90   setResult(aResultBody, anIndex);
91 }
92
93 //=================================================================================================
94 void FeaturesPlugin_GlueFaces::getOriginalShapes(const std::string& theAttributeName,
95                                                  ListOfShape&       theShapes)
96 {
97   // Collect all selections into a single list of shapes
98   AttributeSelectionListPtr aSelectionList = selectionList(theAttributeName);
99   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex)
100   {
101     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
102     GeomShapePtr aShape = aSelection->value();
103     GeomShapePtr aContext = aSelection->context()->shape();
104     if (!aShape.get())
105     {
106       aShape = aContext;
107     }
108     theShapes.push_back(aShape);
109   }
110 }
111
112 //=================================================================================================
113 bool FeaturesPlugin_GlueFaces::isGlued(const ListOfShape& theInputs,
114                                        const GeomShapePtr theResult)
115 {
116   if (!theResult.get())
117     return false;
118
119   // Consider the list of input shapes the same as the result, if
120   //  * the total number of faces did NOT change.
121   int nbInputFaces = 0;
122   for (ListOfShape::const_iterator anIt = theInputs.cbegin();
123        anIt != theInputs.cend();
124        ++anIt)
125   {
126     GeomShapePtr aShape = *anIt;
127     if (aShape.get())
128     {
129       nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
130     }
131   }
132
133   int nbResultFaces = 0;
134   nbResultFaces = theResult->subShapes(GeomAPI_Shape::FACE, true).size();
135   return(0 < nbResultFaces && nbResultFaces < nbInputFaces);
136 }
137
138 //=================================================================================================
139 void FeaturesPlugin_GlueFaces::setResultFromInput(const ListOfShape& theInputs)
140 {
141   GeomShapePtr aResult;
142
143   // Make sure the result will be a compound of the input shapes, if not already
144   if (theInputs.size() == 1 && theInputs.front().get() && theInputs.front()->isCompound())
145   {
146     aResult = theInputs.front();
147   }
148   else
149   {
150     aResult = GeomAlgoAPI_CompoundBuilder::compound(theInputs);
151   }
152
153   int anIndex = 0;
154   ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
155   aResultBody->store(aResult);
156
157   setResult(aResultBody, anIndex);
158 }