Salome HOME
Compsolid result interface.
[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   for(; anIndex < aFaceRefs->size(); anIndex++) {
102     std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
103     ResultPtr aContextRes = aFaceRef->context();
104     std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
105     if (!aContext.get()) {
106       static const std::string aContextError = "The selection context is bad";
107       setError(aContextError);
108       break;
109     }
110
111     std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
112     int aFacesNum = -1; // this mean that "aFace" is used
113     ResultConstructionPtr aConstruction =
114       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
115     if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
116       if (aConstruction.get()) {
117         aFacesNum = aConstruction->facesNum();
118       } else {
119         static const std::string aFaceError = "Can not find basis for extrusion";
120         setError(aFaceError);
121         break;
122       }
123     }
124
125     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
126       //ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
127 #ifdef DEBUG_COMPSOLID
128       ResultCompSolidPtr aResultBody = document()->createCompSolid(data(), aResultIndex);
129 #else
130       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
131 #endif
132       std::shared_ptr<GeomAPI_Shape> aBaseShape;
133       if (aFacesNum == -1) {
134         aBaseShape = aValueFace;
135       } else {
136         aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
137       }
138
139       GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
140       if(!aFeature.isDone()) {
141         static const std::string aFeatureError = "Extrusion algorithm failed";
142         setError(aFeatureError);
143         break;
144       }
145
146       // Check if shape is valid
147       if(!aFeature.shape().get() || aFeature.shape()->isNull()) {
148         static const std::string aShapeError = "Resulting shape is Null";
149         setError(aShapeError);
150         break;
151       }
152       if(!aFeature.isValid()) {
153         std::string aFeatureError = "Warning: resulting shape is not valid";
154         setError(aFeatureError);
155         break;
156       }
157       //LoadNamingDS
158 #ifdef DEBUG_COMPSOLID
159 #else
160       LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
161 #endif
162       setResult(aResultBody, aResultIndex);
163       aResultIndex++;
164
165       if (aFacesNum == -1)
166         break;
167     }
168   }
169   // remove the rest results if there were produced in the previous pass
170   removeResults(aResultIndex);
171 }
172
173 //=================================================================================================
174 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
175                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
176                                             std::shared_ptr<GeomAPI_Shape> theBasis,
177                                             std::shared_ptr<GeomAPI_Shape> theContext)
178 {
179   //load result
180   if(theBasis->isEqual(theContext))
181     theResultBody->store(theFeature.shape());
182   else
183     theResultBody->storeGenerated(theBasis, theFeature.shape());
184
185   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
186
187   //Insert lateral face : Face from Edge
188   std::string aLatName = "LateralFace";
189   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
190
191   //Insert bottom face
192   std::string aBotName = "BottomFace";
193   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
194   if(!aBottomFace->isNull()) {
195     if(aSubShapes->isBound(aBottomFace)) {
196       aBottomFace = aSubShapes->find(aBottomFace);
197     }
198     theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
199   }
200
201   //Insert top face
202   std::string aTopName = "TopFace";
203   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
204   if (!aTopFace->isNull()) {
205     if (aSubShapes->isBound(aTopFace)) {
206       aTopFace = aSubShapes->find(aTopFace);
207     }
208     theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
209   }
210 }