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_Session.h>
27 #include <ModelAPI_Validator.h>
29 #include <GeomAlgoAPI_Prism.h>
30 #include <GeomAlgoAPI_Tools.h>
32 #include <GeomAPI_Dir.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_ShapeIterator.h>
37 //=================================================================================================
38 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
42 //=================================================================================================
43 void FeaturesPlugin_Extrusion::initAttributes()
45 initCompositeSketchAttribtues(InitBaseObjectsList);
47 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
52 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
53 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
56 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
64 initCompositeSketchAttribtues(InitSketchLauncher);
67 //=================================================================================================
68 void FeaturesPlugin_Extrusion::execute()
70 ListOfShape aBaseShapesList, aBoundaryShapes;
71 ListOfMakeShape aMakeShapesList;
74 if(!makeExtrusions(aBaseShapesList, aBoundaryShapes, aMakeShapesList)) {
80 ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
81 ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
82 for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
83 ++aBaseIt, ++anAlgoIt) {
84 if (aBoundaryShapes.empty())
85 storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
87 storeResultWithBoundaries(*aBaseIt, aBoundaryShapes, *anAlgoIt, aResultIndex++);
90 removeResults(aResultIndex);
93 //=================================================================================================
94 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
95 ListOfShape& theBoundaryShapes,
96 ListOfMakeShape& theMakeShapes)
98 theMakeShapes.clear();
100 // Getting base shapes.
101 getBaseShapes(theBaseShapes);
104 static const std::string aSelectionError = "Error: The direction shape selection is bad.";
105 AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
106 GeomShapePtr aShape = aSelection->value();
108 if (aSelection->context().get()) {
109 aShape = aSelection->context()->shape();
115 if (aShape->isEdge())
117 anEdge = aShape->edge();
119 else if (aShape->isCompound())
121 GeomAPI_ShapeIterator anIt(aShape);
122 anEdge = anIt.current()->edge();
126 std::shared_ptr<GeomAPI_Dir> aDir;
128 if(anEdge->isLine()) {
129 aDir = anEdge->line()->direction();
134 double aToSize = 0.0;
135 double aFromSize = 0.0;
137 if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
138 aToSize = real(TO_SIZE_ID())->value();
139 aFromSize = real(FROM_SIZE_ID())->value();
141 aToSize = real(TO_OFFSET_ID())->value();
142 aFromSize = real(FROM_OFFSET_ID())->value();
145 // Getting bounding planes.
146 GeomShapePtr aToShape;
147 GeomShapePtr aFromShape;
149 if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
150 aSelection = selection(TO_OBJECT_ID());
151 if(aSelection.get()) {
152 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
153 if(!aToShape.get() && aSelection->context().get()) {
154 aToShape = aSelection->context()->shape();
156 if (aToShape.get() && aToShape->isCompound()) {
157 GeomAPI_ShapeIterator anIt(aToShape);
158 aToShape = anIt.current();
161 aSelection = selection(FROM_OBJECT_ID());
162 if(aSelection.get()) {
163 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
164 if(!aFromShape.get() && aSelection->context().get()) {
165 aFromShape = aSelection->context()->shape();
167 if (aFromShape.get() && aFromShape->isCompound()) {
168 GeomAPI_ShapeIterator anIt(aFromShape);
169 aFromShape = anIt.current();
173 if (aToShape && !aToShape->isPlanar())
174 theBoundaryShapes.push_back(aToShape);
175 if (aFromShape && !aFromShape->isPlanar())
176 theBoundaryShapes.push_back(aFromShape);
178 // Generating result for each base shape.
180 for(ListOfShape::const_iterator
181 anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
182 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
184 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
186 aFromShape, aFromSize));
187 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
192 theMakeShapes.push_back(aPrismAlgo);
198 //=================================================================================================
199 void FeaturesPlugin_Extrusion::storeResultWithBoundaries(
200 const GeomShapePtr theBaseShape,
201 const ListOfShape& theBoundaryShapes,
202 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
205 // Create result body.
206 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
208 // Store modified shapes.
209 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBoundaryShapes, ListOfShape(),
210 theMakeShape, theMakeShape->shape());
212 // Store generated edges/faces.
213 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
215 setResult(aResultBody, theIndex);