]> SALOME platform Git repositories - modules/shaper.git/blob - src/BuildPlugin/BuildPlugin_Solid.cpp
Salome HOME
Issue #2386: New build features: Solid, Compsolid, Compound
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Solid.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_Solid.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25
26 #include <GeomAPI_ShapeIterator.h>
27
28 #include <GeomAlgoAPI_MakeVolume.h>
29
30 //=================================================================================================
31 BuildPlugin_Solid::BuildPlugin_Solid()
32 {
33 }
34
35 //=================================================================================================
36 void BuildPlugin_Solid::initAttributes()
37 {
38   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
39 }
40
41 //=================================================================================================
42 void BuildPlugin_Solid::execute()
43 {
44   ListOfShape anOriginalShapes;
45   std::shared_ptr<GeomAlgoAPI_MakeShape> aVolumeMaker;
46   if (!build(anOriginalShapes, aVolumeMaker))
47     return;
48
49   // check and process result of volume maker
50   GeomShapePtr aResShape = getSingleSubshape(aVolumeMaker->shape(), GeomAPI_Shape::SOLID);
51   int anIndex = 0;
52   if (aResShape) {
53     storeResult(anOriginalShapes, aResShape, aVolumeMaker);
54     ++anIndex;
55   }
56   removeResults(anIndex);
57 }
58
59 //=================================================================================================
60 bool BuildPlugin_Solid::build(ListOfShape& theOriginalShapes,
61                               std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
62 {
63   // Get base objects list.
64   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
65   if (!aSelectionList.get()) {
66     setError("Error: Could not get selection list.");
67     return false;
68   }
69   if (aSelectionList->size() == 0) {
70     setError("Error: Empty selection list.");
71     return false;
72   }
73
74   // Collect base shapes.
75   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
76     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
77     GeomShapePtr aShape = aSelection->value();
78     if (!aShape.get())
79       aShape = aSelection->context()->shape();
80     theOriginalShapes.push_back(aShape);
81   }
82
83   theAlgorithm =
84       std::shared_ptr<GeomAlgoAPI_MakeVolume>(new GeomAlgoAPI_MakeVolume(theOriginalShapes));
85   return !isAlgorithmFailed(theAlgorithm);
86 }
87
88 void BuildPlugin_Solid::storeResult(const ListOfShape& theOriginalShapes,
89                                     const GeomShapePtr& theResultShape,
90                                     const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
91 {
92   ResultBodyPtr aResultBody = document()->createBody(data());
93   aResultBody->store(theResultShape);
94
95   // Store faces
96   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubs = theAlgorithm->mapOfSubShapes();
97   int aModifiedTag = 1;
98   for(ListOfShape::const_iterator anIt = theOriginalShapes.cbegin();
99       anIt != theOriginalShapes.cend(); ++anIt) {
100     GeomShapePtr aShape = *anIt;
101     aResultBody->loadAndOrientModifiedShapes(theAlgorithm.get(), aShape, GeomAPI_Shape::FACE,
102         aModifiedTag, "Modified_Face", *aMapOfSubs.get(), false, true, true);
103   }
104
105   setResult(aResultBody);
106 }
107
108 GeomShapePtr BuildPlugin_Solid::getSingleSubshape(const GeomShapePtr& theCompound,
109                                                   const GeomAPI_Shape::ShapeType theShapeType)
110 {
111   if (theCompound->shapeType() == theShapeType)
112     return theCompound;
113   else if (theCompound->shapeType() == GeomAPI_Shape::COMPOUND) {
114     GeomAPI_ShapeIterator anIt(theCompound);
115     GeomShapePtr aFoundSub;
116     for (; anIt.more() && !aFoundSub; anIt.next()) {
117       aFoundSub = anIt.current();
118       if (aFoundSub->shapeType() != theShapeType)
119         return GeomShapePtr(); // not alone sub-shape
120     }
121     if (anIt.more()) {
122       std::string anError = "Error: unable to build a ";
123       switch (theShapeType) {
124       case GeomAPI_Shape::SOLID: anError += "solid"; break;
125       case GeomAPI_Shape::COMPSOLID: anError += "compsolid"; break;
126       default: anError += "subshape"; break;
127       }
128       setError(anError);
129     } else
130       return aFoundSub;
131   }
132   // not a solid
133   return GeomShapePtr();
134 }
135
136 bool BuildPlugin_Solid::isAlgorithmFailed(
137     const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
138 {
139   if (!theAlgorithm->isDone()) {
140     static const std::string aFeatureError = "Error: MakeVolume algorithm failed.";
141     setError(aFeatureError);
142     return true;
143   }
144   if (theAlgorithm->shape()->isNull()) {
145     static const std::string aShapeError = "Error: Resulting shape of MakeVolume is Null.";
146     setError(aShapeError);
147     return true;
148   }
149   if (!theAlgorithm->isValid()) {
150     std::string aFeatureError = "Error: Resulting shape of MakeVolume is not valid.";
151     setError(aFeatureError);
152     return true;
153   }
154   return false;
155 }