1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Extrusion.cpp
4 // Created: 30 May 2014
5 // Author: Vitaly SMETANNIKOV
7 #include <FeaturesPlugin_Extrusion.h>
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>
24 #include <GeomAlgoAPI_Prism.h>
26 #define _LATERAL_TAG 1
31 #define DEBUG_COMPSOLID
32 //#define DEBUG_COMPSOLID_SHAPE
34 //=================================================================================================
35 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
39 //=================================================================================================
40 void FeaturesPlugin_Extrusion::initAttributes()
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");
48 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
50 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
63 //=================================================================================================
64 void FeaturesPlugin_Extrusion::execute()
66 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
70 double aFromSize = 0.0;
72 if(string(CREATION_METHOD())->value() == "BySizes") {
73 aToSize = real(TO_SIZE_ID())->value();
74 aFromSize = real(FROM_SIZE_ID())->value();
76 aToSize = real(TO_OFFSET_ID())->value();
77 aFromSize = real(FROM_OFFSET_ID())->value();
80 // Getting bounding planes.
81 std::shared_ptr<GeomAPI_Shape> aToShape;
82 std::shared_ptr<GeomAPI_Shape> aFromShape;
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();
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();
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);
108 #ifdef DEBUG_COMPSOLID_SHAPE
109 bool aFirstShapeInCompsolid = aFaceRefs->size() > 0;
110 if (aFirstShapeInCompsolid)
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);
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();
131 static const std::string aFaceError = "Can not find basis for extrusion";
132 setError(aFaceError);
136 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
137 ResultBodyPtr aResultBody;
139 #ifdef DEBUG_COMPSOLID_SHAPE
140 if (aFirstShapeInCompsolid && anIndex == 0)
141 aResultBody = aCompSolidResult;
145 #ifdef DEBUG_COMPSOLID
146 aResultBody = aCompSolidResult->addResult(aResultIndex);
148 aResultBody = document()->createBody(data(), aResultIndex);
151 #ifdef DEBUG_COMPSOLID_SHAPE
154 std::shared_ptr<GeomAPI_Shape> aBaseShape;
155 if (aFacesNum == -1) {
156 aBaseShape = aValueFace;
158 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
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);
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);
174 if(!aFeature.isValid()) {
175 std::string aFeatureError = "Warning: resulting shape is not valid";
176 setError(aFeatureError);
180 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
181 setResult(aResultBody, aResultIndex);
188 // remove the rest results if there were produced in the previous pass
189 removeResults(aResultIndex);
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)
199 ModelAPI_BodyBuilder* aResultBuilder = theResultBody->getBodyBuilder();
200 if(theBasis->isEqual(theContext))
201 aResultBuilder->store(theFeature.shape());
203 aResultBuilder->storeGenerated(theBasis, theFeature.shape());
205 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
207 //Insert lateral face : Face from Edge
208 std::string aLatName = "LateralFace";
209 aResultBuilder->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
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);
218 aResultBuilder->generated(aBottomFace, aBotName, _FIRST_TAG);
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);
228 aResultBuilder->generated(aTopFace, aTopName, _LAST_TAG);