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"
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
28 #include <GeomAlgoAPI_Prism.h>
29 #include <GeomAlgoAPI_Tools.h>
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Lin.h>
34 #include <GeomAPI_ShapeIterator.h>
36 //=================================================================================================
37 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
41 //=================================================================================================
42 void FeaturesPlugin_Extrusion::initAttributes()
44 initCompositeSketchAttribtues(InitBaseObjectsList);
46 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
48 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
55 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_OBJECT_ID());
63 initCompositeSketchAttribtues(InitSketchLauncher);
66 //=================================================================================================
67 void FeaturesPlugin_Extrusion::execute()
69 ListOfShape aBaseShapesList;
70 ListOfMakeShape aMakeShapesList;
73 if(!makeExtrusions(aBaseShapesList, aMakeShapesList)) {
79 ListOfShape::const_iterator aBaseIt = aBaseShapesList.cbegin();
80 ListOfMakeShape::const_iterator anAlgoIt = aMakeShapesList.cbegin();
81 for(; aBaseIt != aBaseShapesList.cend() && anAlgoIt != aMakeShapesList.cend();
82 ++aBaseIt, ++anAlgoIt) {
83 storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
86 removeResults(aResultIndex);
89 //=================================================================================================
90 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
91 ListOfMakeShape& theMakeShapes)
93 theMakeShapes.clear();
95 // Getting base shapes.
96 getBaseShapes(theBaseShapes);
99 static const std::string aSelectionError = "Error: The direction shape selection is bad.";
100 AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
101 GeomShapePtr aShape = aSelection->value();
103 if (aSelection->context().get()) {
104 aShape = aSelection->context()->shape();
110 if (aShape->isEdge())
112 anEdge = aShape->edge();
114 else if (aShape->isCompound())
116 GeomAPI_ShapeIterator anIt(aShape);
117 anEdge = anIt.current()->edge();
121 std::shared_ptr<GeomAPI_Dir> aDir;
123 if(anEdge->isLine()) {
124 aDir = anEdge->line()->direction();
129 double aToSize = 0.0;
130 double aFromSize = 0.0;
132 if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
133 aToSize = real(TO_SIZE_ID())->value();
134 aFromSize = real(FROM_SIZE_ID())->value();
136 aToSize = real(TO_OFFSET_ID())->value();
137 aFromSize = real(FROM_OFFSET_ID())->value();
140 // Getting bounding planes.
141 GeomShapePtr aToShape;
142 GeomShapePtr aFromShape;
144 if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
145 aSelection = selection(TO_OBJECT_ID());
146 if(aSelection.get()) {
147 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
148 if(!aToShape.get() && aSelection->context().get()) {
149 aToShape = aSelection->context()->shape();
151 if (aToShape.get() && aToShape->isCompound()) {
152 GeomAPI_ShapeIterator anIt(aToShape);
153 aToShape = anIt.current();
156 aSelection = selection(FROM_OBJECT_ID());
157 if(aSelection.get()) {
158 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
159 if(!aFromShape.get() && aSelection->context().get()) {
160 aFromShape = aSelection->context()->shape();
162 if (aFromShape.get() && aFromShape->isCompound()) {
163 GeomAPI_ShapeIterator anIt(aFromShape);
164 aFromShape = anIt.current();
169 // Generating result for each base shape.
171 for(ListOfShape::const_iterator
172 anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
173 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
175 std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
177 aFromShape, aFromSize));
178 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
183 theMakeShapes.push_back(aPrismAlgo);