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_Solid.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
26 #include <GeomAPI_ShapeIterator.h>
28 #include <GeomAlgoAPI_MakeVolume.h>
30 //=================================================================================================
31 BuildPlugin_Solid::BuildPlugin_Solid()
35 //=================================================================================================
36 void BuildPlugin_Solid::initAttributes()
38 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
41 //=================================================================================================
42 void BuildPlugin_Solid::execute()
44 ListOfShape anOriginalShapes;
45 std::shared_ptr<GeomAlgoAPI_MakeShape> aVolumeMaker;
46 if (!build(anOriginalShapes, aVolumeMaker))
49 // check and process result of volume maker
50 GeomShapePtr aResShape = getSingleSubshape(aVolumeMaker->shape(), GeomAPI_Shape::SOLID);
53 storeResult(anOriginalShapes, aResShape, aVolumeMaker);
56 removeResults(anIndex);
59 //=================================================================================================
60 bool BuildPlugin_Solid::build(ListOfShape& theOriginalShapes,
61 std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
63 // Get base objects list.
64 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
65 if (!aSelectionList.get()) {
66 setError("Error: Could not get selection list.");
69 if (aSelectionList->size() == 0) {
70 setError("Error: Empty selection list.");
74 // Collect base shapes.
75 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
76 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
77 GeomShapePtr aShape = aSelection->value();
79 aShape = aSelection->context()->shape();
80 theOriginalShapes.push_back(aShape);
84 std::shared_ptr<GeomAlgoAPI_MakeVolume>(new GeomAlgoAPI_MakeVolume(theOriginalShapes));
85 return !isAlgorithmFailed(theAlgorithm);
88 void BuildPlugin_Solid::storeResult(const ListOfShape& theOriginalShapes,
89 const GeomShapePtr& theResultShape,
90 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
92 ResultBodyPtr aResultBody = document()->createBody(data());
93 aResultBody->store(theResultShape);
96 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubs = theAlgorithm->mapOfSubShapes();
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);
105 setResult(aResultBody);
108 GeomShapePtr BuildPlugin_Solid::getSingleSubshape(const GeomShapePtr& theCompound,
109 const GeomAPI_Shape::ShapeType theShapeType)
111 if (theCompound->shapeType() == theShapeType)
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
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;
133 return GeomShapePtr();
136 bool BuildPlugin_Solid::isAlgorithmFailed(
137 const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgorithm)
139 if (!theAlgorithm->isDone()) {
140 static const std::string aFeatureError = "Error: MakeVolume algorithm failed.";
141 setError(aFeatureError);
144 if (theAlgorithm->shape()->isNull()) {
145 static const std::string aShapeError = "Error: Resulting shape of MakeVolume is Null.";
146 setError(aShapeError);
149 if (!theAlgorithm->isValid()) {
150 std::string aFeatureError = "Error: Resulting shape of MakeVolume is not valid.";
151 setError(aFeatureError);