Salome HOME
Compsolid creation in extrusion
[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 #include <GeomAlgoAPI_ShapeTools.h>
26
27 //=================================================================================================
28 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
29 {
30 }
31
32 //=================================================================================================
33 void FeaturesPlugin_Extrusion::initAttributes()
34 {
35   AttributeSelectionListPtr aSelection = 
36     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
37     LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38   // extrusion works with faces always
39   aSelection->setSelectionType("FACE");
40
41   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
42
43   data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
44   data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
45
46   data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
47   data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
48
49   data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
51
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
54 }
55
56 //=================================================================================================
57 void FeaturesPlugin_Extrusion::execute()
58 {
59   // Getting faces.
60   ListOfShape aFacesList;
61   AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
62   for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
63     std::shared_ptr<ModelAPI_AttributeSelection> aFaceSel = aFacesSelectionList->value(anIndex);
64     ResultPtr aContext = aFaceSel->context();
65     std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
66     if(!aContextShape.get()) {
67       static const std::string aContextError = "The selection context is bad";
68       setError(aContextError);
69       break;
70     }
71
72     std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
73     int aFacesNum = -1; // this mean that "aFace" is used
74     ResultConstructionPtr aConstruction = 
75       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
76     if(!aFaceShape.get()) { // this may be the whole sketch result selected, check and get faces
77       if (aConstruction.get()) {
78         aFacesNum = aConstruction->facesNum();
79       } else {
80         static const std::string aFaceError = "Can not find basis for extrusion";
81         setError(aFaceError);
82         break;
83       }
84     }
85     for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
86       std::shared_ptr<GeomAPI_Shape> aBaseShape;
87       if (aFacesNum == -1) {
88         aFacesList.push_back(aFaceShape);
89         break;
90       } else {
91         aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
92         aFacesList.push_back(aFaceShape);
93       }
94     }
95   }
96
97   // Getting sizes.
98   double aToSize = 0.0;
99   double aFromSize = 0.0;
100
101   if(string(CREATION_METHOD())->value() == "BySizes") {
102     aToSize = real(TO_SIZE_ID())->value();
103     aFromSize =  real(FROM_SIZE_ID())->value();
104   } else {
105     aToSize = real(TO_OFFSET_ID())->value();
106     aFromSize =  real(FROM_OFFSET_ID())->value();
107   }
108
109   // Getting bounding planes.
110   std::shared_ptr<GeomAPI_Shape> aToShape;
111   std::shared_ptr<GeomAPI_Shape> aFromShape;
112
113   if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
114     std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
115     if(anObjRef.get() != NULL) {
116       aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
117       if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
118         aToShape =  anObjRef->context()->shape();
119       }
120     }
121     anObjRef = selection(FROM_OBJECT_ID());
122     if(anObjRef.get() != NULL) {
123       aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
124       if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
125         aFromShape = anObjRef->context()->shape();
126       }
127     }
128   }
129
130   // Searching faces with common edges.
131   ListOfShape aShells;
132   ListOfShape aFreeFaces;
133   GeomAlgoAPI_ShapeTools::combineFacesToShells(aFacesList, aShells, aFreeFaces);
134   aShells.merge(aFreeFaces);
135
136   // Generating result for each shell and face.
137   int anIndex = 0, aResultIndex = 0;
138   for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
139     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
140
141     GeomAlgoAPI_Prism aPrismAlgo(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
142     if(!aPrismAlgo.isDone()) {
143       static const std::string aPrismAlgoError = "Extrusion algorithm failed";
144       setError(aPrismAlgoError);
145       aResultIndex = 0;
146       break;
147     }
148
149     // Check if shape is valid
150     if(!aPrismAlgo.shape().get() || aPrismAlgo.shape()->isNull()) {
151       static const std::string aShapeError = "Resulting shape is Null";
152       setError(aShapeError);
153       aResultIndex = 0;
154       break;
155     }
156     if(!aPrismAlgo.isValid()) {
157       std::string aPrismAlgoError = "Warning: resulting shape is not valid";
158       setError(aPrismAlgoError);
159       aResultIndex = 0;
160       break;
161     }
162
163     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
164     loadNamingDS(aPrismAlgo, aResultBody, aBaseShape);
165     setResult(aResultBody, aResultIndex);
166     aResultIndex++;
167   }
168
169   removeResults(aResultIndex);
170 }
171
172 //=================================================================================================
173 void FeaturesPlugin_Extrusion::loadNamingDS(GeomAlgoAPI_Prism& thePrismAlgo,
174                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
175                                             std::shared_ptr<GeomAPI_Shape> theBasis)
176 {
177   //load result
178   ModelAPI_BodyBuilder* aResultBuilder = theResultBody->getBodyBuilder();
179   if(thePrismAlgo.shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
180     int a = 1;
181   }
182   aResultBuilder->storeGenerated(theBasis, thePrismAlgo.shape());
183
184   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = thePrismAlgo.mapOfShapes();
185
186   //Insert lateral face : Face from Edge
187   std::string aLatName = "LateralFace";
188   const int aLatTag = 1;
189   aResultBuilder->loadAndOrientGeneratedShapes(thePrismAlgo.makeShape().get(), theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
190
191   //Insert to faces
192   std::string aToName = "ToFace";
193   const int aToTag = 2;
194   const ListOfShape& aToFaces = thePrismAlgo.toFaces();
195   for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
196     std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
197     if(aSubShapes->isBound(aToFace)) {
198       aToFace = aSubShapes->find(aToFace);
199     }
200     aResultBuilder->generated(aToFace, aToName, aToTag);
201   }
202
203   //Insert from faces
204   std::string aFromName = "FromFace";
205   const int aFromTag = 3;
206   const ListOfShape& aFromFaces = thePrismAlgo.fromFaces();
207   for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
208     std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
209     if(aSubShapes->isBound(aFromFace)) {
210       aFromFace = aSubShapes->find(aFromFace);
211     }
212     aResultBuilder->generated(aFromFace, aFromName, aFromTag);
213   }
214 }