1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Extrusion.h"
21 #include "FeaturesPlugin_Tools.h"
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_Prism.h>
31 #include <GeomAlgoAPI_Tools.h>
33 #include <GeomAPI_Dir.h>
34 #include <GeomAPI_Edge.h>
35 #include <GeomAPI_Lin.h>
36 #include <GeomAPI_ShapeIterator.h>
38 //=================================================================================================
39 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
43 //=================================================================================================
44 void FeaturesPlugin_Extrusion::initAttributes()
46 initCompositeSketchAttribtues(InitBaseObjectsList);
48 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
50 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
59 data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
61 data()->addAttribute(USE_SHAPES_ID(), ModelAPI_AttributeInteger::typeId());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
65 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
67 initCompositeSketchAttribtues(InitSketchLauncher);
70 //=================================================================================================
71 void FeaturesPlugin_Extrusion::execute()
73 ListOfShape aBaseShapesList, aBoundaryShapes;
74 ListOfMakeShape aMakeShapesList;
77 if(!makeExtrusions(aBaseShapesList, aBoundaryShapes, aMakeShapesList)) {
83 ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
84 ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
85 for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
86 ++aBaseIt, ++anAlgoIt) {
87 if (aBoundaryShapes.empty())
88 storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
90 storeResultWithBoundaries(*aBaseIt, aBoundaryShapes, *anAlgoIt, aResultIndex++);
93 removeResults(aResultIndex);
96 //=================================================================================================
97 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
98 ListOfShape& theBoundaryShapes,
99 ListOfMakeShape& theMakeShapes)
101 theMakeShapes.clear();
103 // Getting base shapes.
104 getBaseShapes(theBaseShapes);
107 std::shared_ptr<GeomAPI_Dir> aDir;
111 double aToSize = 0.0;
112 double aFromSize = 0.0;
113 getSizes(aToSize, aFromSize);
115 // Getting bounding planes.
116 GeomShapePtr aToShape;
117 GeomShapePtr aFromShape;
119 if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
120 AttributeSelectionPtr aSelection = selection(TO_OBJECT_ID());
121 if(aSelection.get()) {
122 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
123 if(!aToShape.get() && aSelection->context().get()) {
124 aToShape = aSelection->context()->shape();
126 if (aToShape.get() && aToShape->isCompound()) {
127 GeomAPI_ShapeIterator anIt(aToShape);
128 aToShape = anIt.current();
131 aSelection = selection(FROM_OBJECT_ID());
132 if(aSelection.get()) {
133 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
134 if(!aFromShape.get() && aSelection->context().get()) {
135 aFromShape = aSelection->context()->shape();
137 if (aFromShape.get() && aFromShape->isCompound()) {
138 GeomAPI_ShapeIterator anIt(aFromShape);
139 aFromShape = anIt.current();
143 if (aToShape && !aToShape->isPlanar())
144 theBoundaryShapes.push_back(aToShape);
145 if (aFromShape && !aFromShape->isPlanar())
146 theBoundaryShapes.push_back(aFromShape);
148 // Generating result for each base shape.
150 for(ListOfShape::const_iterator
151 anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
152 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
154 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
156 aFromShape, aFromSize));
157 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
162 theMakeShapes.push_back(aPrismAlgo);
168 //=================================================================================================
169 void FeaturesPlugin_Extrusion::storeResultWithBoundaries(
170 const GeomShapePtr theBaseShape,
171 const ListOfShape& theBoundaryShapes,
172 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
175 // Create result body.
176 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
178 // Store modified shapes.
179 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBoundaryShapes, ListOfShape(),
180 theMakeShape, theMakeShape->shape());
182 // Store generated edges/faces.
183 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
185 setResult(aResultBody, theIndex);
188 //=================================================================================================
189 void FeaturesPlugin_Extrusion::getDirection(std::shared_ptr<GeomAPI_Dir>& theDir)
191 static const std::string aSelectionError = "Error: The direction shape selection is bad.";
192 AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
193 GeomShapePtr aShape = aSelection->value();
195 if (aSelection->context().get()) {
196 aShape = aSelection->context()->shape();
202 if (aShape->isEdge())
204 anEdge = aShape->edge();
206 else if (aShape->isCompound())
208 GeomAPI_ShapeIterator anIt(aShape);
209 anEdge = anIt.current()->edge();
214 if (anEdge->isLine()) {
215 theDir = anEdge->line()->direction();
220 //=================================================================================================
221 void FeaturesPlugin_Extrusion::getSizes(double& theToSize, double& theFromSize)
223 if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
224 theToSize = real(TO_SIZE_ID())->value();
225 theFromSize = real(FROM_SIZE_ID())->value();
226 } if (string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
227 theToSize = real(TO_OFFSET_ID())->value();
228 theFromSize = real(FROM_OFFSET_ID())->value();