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