Salome HOME
Merge commit 'refs/tags/V9_2_0^{}'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-2017  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 "FeaturesPlugin_Extrusion.h"
22
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>
28
29 #include <GeomAlgoAPI_Prism.h>
30 #include <GeomAlgoAPI_Tools.h>
31
32 #include <GeomAPI_Dir.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_ShapeIterator.h>
36
37 //=================================================================================================
38 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
39 {
40 }
41
42 //=================================================================================================
43 void FeaturesPlugin_Extrusion::initAttributes()
44 {
45   initCompositeSketchAttribtues(InitBaseObjectsList);
46
47   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
48
49   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
50   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51
52   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
53   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54
55   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
56   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
57
58   data()->addAttribute(DIRECTION_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
59
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());
63
64   initCompositeSketchAttribtues(InitSketchLauncher);
65 }
66
67 //=================================================================================================
68 void FeaturesPlugin_Extrusion::execute()
69 {
70   ListOfShape aBaseShapesList;
71   ListOfMakeShape aMakeShapesList;
72
73   // Make extrusions.
74   if(!makeExtrusions(aBaseShapesList, aMakeShapesList)) {
75     return;
76   }
77
78   // Store results.
79   int aResultIndex = 0;
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     storeResult(*aBaseIt, *anAlgoIt, aResultIndex++);
85   }
86
87   removeResults(aResultIndex);
88 }
89
90 //=================================================================================================
91 bool FeaturesPlugin_Extrusion::makeExtrusions(ListOfShape& theBaseShapes,
92                                               ListOfMakeShape& theMakeShapes)
93 {
94   theMakeShapes.clear();
95
96   // Getting base shapes.
97   getBaseShapes(theBaseShapes);
98
99   //Getting direction.
100   static const std::string aSelectionError = "Error: The direction shape selection is bad.";
101   AttributeSelectionPtr aSelection = selection(DIRECTION_OBJECT_ID());
102   GeomShapePtr aShape = aSelection->value();
103   if (!aShape.get()) {
104     if (aSelection->context().get()) {
105       aShape = aSelection->context()->shape();
106     }
107   }
108
109   GeomEdgePtr anEdge;
110   if (aShape.get()) {
111     if (aShape->isEdge())
112     {
113       anEdge = aShape->edge();
114     }
115     else if (aShape->isCompound())
116     {
117       GeomAPI_ShapeIterator anIt(aShape);
118       anEdge = anIt.current()->edge();
119     }
120   }
121
122   std::shared_ptr<GeomAPI_Dir> aDir;
123   if(anEdge.get()) {
124     if(anEdge->isLine()) {
125       aDir = anEdge->line()->direction();
126     }
127   }
128
129   // Getting sizes.
130   double aToSize = 0.0;
131   double aFromSize = 0.0;
132
133   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_SIZES()) {
134     aToSize = real(TO_SIZE_ID())->value();
135     aFromSize = real(FROM_SIZE_ID())->value();
136   } else {
137     aToSize = real(TO_OFFSET_ID())->value();
138     aFromSize = real(FROM_OFFSET_ID())->value();
139   }
140
141   // Getting bounding planes.
142   GeomShapePtr aToShape;
143   GeomShapePtr aFromShape;
144
145   if(string(CREATION_METHOD())->value() == CREATION_METHOD_BY_PLANES()) {
146     aSelection = selection(TO_OBJECT_ID());
147     if(aSelection.get()) {
148       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
149       if(!aToShape.get() && aSelection->context().get()) {
150         aToShape = aSelection->context()->shape();
151       }
152       if (aToShape.get() && aToShape->isCompound()) {
153         GeomAPI_ShapeIterator anIt(aToShape);
154         aToShape = anIt.current();
155       }
156     }
157     aSelection = selection(FROM_OBJECT_ID());
158     if(aSelection.get()) {
159       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aSelection->value());
160       if(!aFromShape.get() && aSelection->context().get()) {
161         aFromShape = aSelection->context()->shape();
162       }
163       if (aFromShape.get() && aFromShape->isCompound()) {
164         GeomAPI_ShapeIterator anIt(aFromShape);
165         aFromShape = anIt.current();
166       }
167     }
168   }
169
170   // Generating result for each base shape.
171   std::string anError;
172   for(ListOfShape::const_iterator
173       anIter = theBaseShapes.cbegin(); anIter != theBaseShapes.cend(); anIter++) {
174     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
175
176     std::shared_ptr<GeomAlgoAPI_Prism> aPrismAlgo(new GeomAlgoAPI_Prism(aBaseShape, aDir,
177                                                                         aToShape, aToSize,
178                                                                         aFromShape, aFromSize));
179     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPrismAlgo, getKind(), anError)) {
180       setError(anError);
181       return false;
182     }
183
184     theMakeShapes.push_back(aPrismAlgo);
185   }
186
187   return true;
188 }