Salome HOME
Partition feature definition.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Extrusion.cpp
4 // Created:     30 May 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include <FeaturesPlugin_Extrusion.h>
8
9 #include <ModelAPI_BodyBuilder.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12 #include <ModelAPI_Document.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_ResultCompSolid.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20 #include <ModelAPI_AttributeBoolean.h>
21 #include <ModelAPI_AttributeString.h>
22 #include <ModelAPI_AttributeReference.h>
23
24 #include <GeomAlgoAPI_Prism.h>
25
26 #define _LATERAL_TAG 1
27 #define _FIRST_TAG 2
28 #define _LAST_TAG 3
29 #define EDGE 6
30
31 //#define DEBUG_COMPSOLID
32 //#define DEBUG_COMPSOLID_SHAPE
33
34 //=================================================================================================
35 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
36 {
37 }
38
39 //=================================================================================================
40 void FeaturesPlugin_Extrusion::initAttributes()
41 {
42   AttributeSelectionListPtr aSelection = 
43     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
44     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
45   // extrusion works with faces always
46   aSelection->setSelectionType("FACE");
47
48   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49
50   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
52
53   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
61 }
62
63 //=================================================================================================
64 void FeaturesPlugin_Extrusion::execute()
65 {
66   AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
67
68   // Getting sizes.
69   double aToSize = 0.0;
70   double aFromSize = 0.0;
71
72   if(string(CREATION_METHOD())->value() == "BySizes") {
73     aToSize = real(TO_SIZE_ID())->value();
74     aFromSize =  real(FROM_SIZE_ID())->value();
75   } else {
76     aToSize = real(TO_OFFSET_ID())->value();
77     aFromSize =  real(FROM_OFFSET_ID())->value();
78   }
79
80   // Getting bounding planes.
81   std::shared_ptr<GeomAPI_Shape> aToShape;
82   std::shared_ptr<GeomAPI_Shape> aFromShape;
83
84   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
85     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
86     if(anObjRef.get() != NULL) {
87       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
88       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
89         aToShape =  anObjRef->context()->shape();
90       }
91     }
92     anObjRef = selection(FROM_OBJECT_ID());
93     if(anObjRef.get() != NULL) {
94       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
95       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
96         aFromShape = anObjRef->context()->shape();
97       }
98     }
99   }
100
101   // for each selected face generate a result
102   int anIndex = 0, aResultIndex = 0;
103 #ifdef DEBUG_COMPSOLID
104   ResultCompSolidPtr aCompSolidResult = document()->createCompSolid(data(), aResultIndex);
105   setResult(aCompSolidResult, aResultIndex);
106   aResultIndex++;
107 #endif
108 #ifdef DEBUG_COMPSOLID_SHAPE
109   bool aFirstShapeInCompsolid = aFaceRefs->size() > 0;
110   if (aFirstShapeInCompsolid)
111     aResultIndex--;
112 #endif
113   for(; anIndex < aFaceRefs->size(); anIndex++) {
114     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
115     ResultPtr aContextRes = aFaceRef->context();
116     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
117     if (!aContext.get()) {
118       static const std::string aContextError = "The selection context is bad";
119       setError(aContextError);
120       break;
121     }
122
123     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
124     int aFacesNum = -1; // this mean that "aFace" is used
125     ResultConstructionPtr aConstruction =
126       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
127     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
128       if (aConstruction.get()) {
129         aFacesNum = aConstruction->facesNum();
130       } else {
131         static const std::string aFaceError = "Can not find basis for extrusion";
132         setError(aFaceError);
133         break;
134       }
135     }
136     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
137       ResultBodyPtr aResultBody;
138
139 #ifdef DEBUG_COMPSOLID_SHAPE
140       if (aFirstShapeInCompsolid && anIndex == 0)
141         aResultBody = aCompSolidResult;
142       else {
143 #endif
144
145 #ifdef DEBUG_COMPSOLID
146       aResultBody = aCompSolidResult->addResult(aResultIndex);
147 #else
148       aResultBody = document()->createBody(data(), aResultIndex);
149 #endif
150
151 #ifdef DEBUG_COMPSOLID_SHAPE
152       }
153 #endif
154       std::shared_ptr<GeomAPI_Shape> aBaseShape;
155       if (aFacesNum == -1) {
156         aBaseShape = aValueFace;
157       } else {
158         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
159       }
160
161       GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
162       if(!aFeature.isDone()) {
163         static const std::string aFeatureError = "Extrusion algorithm failed";
164         setError(aFeatureError);
165         break;
166       }
167
168       // Check if shape is valid
169       if(!aFeature.shape().get() || aFeature.shape()->isNull()) {
170         static const std::string aShapeError = "Resulting shape is Null";
171         setError(aShapeError);
172         break;
173       }
174       if(!aFeature.isValid()) {
175         std::string aFeatureError = "Warning: resulting shape is not valid";
176         setError(aFeatureError);
177         break;
178       }
179       //LoadNamingDS
180       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
181       setResult(aResultBody, aResultIndex);
182       aResultIndex++;
183
184       if (aFacesNum == -1)
185         break;
186     }
187   }
188   // remove the rest results if there were produced in the previous pass
189   removeResults(aResultIndex);
190 }
191
192 //=================================================================================================
193 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
194                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
195                                             std::shared_ptr<GeomAPI_Shape> theBasis,
196                                             std::shared_ptr<GeomAPI_Shape> theContext)
197 {
198   //load result
199   ModelAPI_BodyBuilder* aResultBuilder = theResultBody->getBodyBuilder();
200   if(theBasis->isEqual(theContext))
201     aResultBuilder->store(theFeature.shape());
202   else
203     aResultBuilder->storeGenerated(theBasis, theFeature.shape());
204
205   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
206
207   //Insert lateral face : Face from Edge
208   std::string aLatName = "LateralFace";
209   aResultBuilder->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
210
211   //Insert bottom face
212   std::string aBotName = "BottomFace";
213   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
214   if(!aBottomFace->isNull()) {
215     if(aSubShapes->isBound(aBottomFace)) {
216       aBottomFace = aSubShapes->find(aBottomFace);
217     }
218     aResultBuilder->generated(aBottomFace, aBotName, _FIRST_TAG);
219   }
220
221   //Insert top face
222   std::string aTopName = "TopFace";
223   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
224   if (!aTopFace->isNull()) {
225     if (aSubShapes->isBound(aTopFace)) {
226       aTopFace = aSubShapes->find(aTopFace);
227     }
228     aResultBuilder->generated(aTopFace, aTopName, _LAST_TAG);
229   }
230 }