Salome HOME
added new Sewing feature
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Sewing.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_Sewing.h>
21
22 //#include <GeomAlgoAPI_MakeShape.h>
23 #include <GeomAlgoAPI_Sewing.h>
24 #include <GeomAlgoAPI_Tools.h>
25
26 //#include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Shape.h>
28 //#include <GeomAPI_ShapeExplorer.h>
29 //#include <GeomAPI_ShapeIterator.h>
30
31 #include <ModelAPI_AttributeBoolean.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_ResultBody.h>
35 //#include <ModelAPI_ResultConstruction.h>
36
37 //---------------------------------------------------------
38 // #define USE_DEBUG
39 // #define DEBUG
40 // static const char *dbg_class = "FeaturesPlugin_Sewing";
41 // #include "MBDebug.h"
42 // #include "MBModel.h"
43 // #include "MBGeom.h"
44 //---------------------------------------------------------
45
46
47
48 //=================================================================================================
49 FeaturesPlugin_Sewing::FeaturesPlugin_Sewing()
50 {
51 //  DBG_FUN();
52 }
53
54 //=================================================================================================
55 void FeaturesPlugin_Sewing::initAttributes()
56 {
57 //  DBG_FUN();
58
59   data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60   data()->addAttribute(ALLOW_NON_MANIFOLD_ID(), ModelAPI_AttributeBoolean::typeId());
61   data()->addAttribute(TOLERANCE_ID(), ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(ALWAYS_CREATE_RESULT_ID(), ModelAPI_AttributeBoolean::typeId());
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_Sewing::execute()
67 {
68 //  DBG_FUN();
69
70   // Collect all base shapes
71   ListOfShape aShapes;
72   getOriginalShapes(OBJECTS_LIST_ID(), aShapes);
73 //  SHOW(aShapes);
74
75   // Get all other feature arguments
76   bool isAllowNonManifold = boolean(FeaturesPlugin_Sewing::ALLOW_NON_MANIFOLD_ID())->value();
77   bool isAlwaysCreateResult = boolean(FeaturesPlugin_Sewing::ALWAYS_CREATE_RESULT_ID())->value();
78   double aTolerance = real(FeaturesPlugin_Sewing::TOLERANCE_ID())->value();
79   // SHOW(isAllowNonManifold);
80   // SHOW(isAlwaysCreateResult);
81   // SHOW(aTolerance);
82
83   std::shared_ptr<GeomAlgoAPI_Sewing> aSewingAlgo(new GeomAlgoAPI_Sewing(aShapes, isAllowNonManifold, aTolerance));
84
85   std::string anError;
86   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSewingAlgo, getKind(), anError))
87   {
88     // SHOW(anError);
89     setError(anError);
90     return;
91   }
92     
93   // Store result.
94   GeomShapePtr aResult = aSewingAlgo->shape();
95   // SHOW(aResult);
96
97   int anIndex = 0;
98   ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
99   // SHOW(aResultBody);
100   aResultBody->storeModified(aShapes, aResult, aSewingAlgo);
101   // SHOW(aResultBody);
102
103   if (!isSewn(aShapes, aResult) && !isAlwaysCreateResult)
104   {
105     static const std::string anError = "Error: No faces were sewn.";
106     setError(anError);
107     return;
108   }
109
110   setResult(aResultBody, anIndex);
111 }
112
113 //=================================================================================================
114 void FeaturesPlugin_Sewing::getOriginalShapes(const std::string& theAttributeName,
115                                               ListOfShape&       theShapes)
116 {
117   // DBG_FUN();
118   // ARG(theAttributeName);
119
120   // Collect all selections into a single list of shapes
121   AttributeSelectionListPtr aSelectionList = selectionList(theAttributeName);
122   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex)
123   {
124     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
125     GeomShapePtr aShape = aSelection->value();
126     GeomShapePtr aContext = aSelection->context()->shape();
127     if (!aShape.get())
128     {
129       aShape = aContext;
130     }
131     theShapes.push_back(aShape);
132   }
133 }
134
135 //=================================================================================================
136 bool FeaturesPlugin_Sewing::isSewn(const ListOfShape& theInputs,
137                                    const GeomShapePtr theResult)
138 {
139   // DBG_FUN();
140   // ARG(theInputs);
141   // ARG(theResult);
142
143   // Consider the list of input shapes the same as the result, if
144   //  * both arguments have the same number of shells
145   //  * the total number of faces in these shells did NOT change.
146   int nbInputShells = 0, nbInputFaces = 0;
147   for (ListOfShape::const_iterator anIt = theInputs.cbegin();
148        anIt != theInputs.cend();
149        ++anIt)
150   {
151     GeomShapePtr aShape = *anIt;
152     if (aShape.get() && aShape->isShell())
153     {
154       nbInputShells++;
155       nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
156     }
157   }
158   // SHOW(nbInputShells);
159   // SHOW(nbInputFaces);
160
161   int nbResultShells = 0, nbResultFaces = 0;
162   if (theResult->isCompound())
163   {
164     // MSGEL("...result is COMPOUND");
165     ListOfShape shells = theResult->subShapes(GeomAPI_Shape::SHELL, true);
166     nbResultShells = shells.size();
167     for (ListOfShape::const_iterator anIt = shells.cbegin();
168         anIt != shells.cend();
169         ++anIt)
170     {
171       GeomShapePtr aShape = *anIt;
172       if (aShape.get() && aShape->isShell())
173       {
174         nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
175       }
176     }
177   }
178   else if (theResult->isShell())
179   {
180     // MSGEL("...result is single SHELL");
181     nbResultShells = 1;
182     nbResultFaces = theResult->subShapes(GeomAPI_Shape::FACE, true).size();
183   }
184   // SHOW(nbResultShells);
185   // SHOW(nbResultFaces);
186
187   return (nbResultShells >= nbInputShells && nbResultFaces > nbInputFaces);
188 }