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"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <GeomAlgoAPI_Extrusion.h>
21 #include <GeomAlgoAPI_FaceBuilder.h>
22 #include <GeomAlgoAPI_Prism.h>
25 #define _LATERAL_TAG 1
30 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
34 void FeaturesPlugin_Extrusion::initAttributes()
36 AttributeSelectionListPtr aSelection =
37 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38 FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39 // extrusion works with faces always
40 aSelection->setSelectionType("FACE");
42 data()->addAttribute(FeaturesPlugin_Extrusion::TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
43 data()->addAttribute(FeaturesPlugin_Extrusion::FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(FeaturesPlugin_Extrusion::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(FeaturesPlugin_Extrusion::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::TO_OBJECT_ID());
52 void FeaturesPlugin_Extrusion::execute()
54 AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
56 std::shared_ptr<GeomAPI_Shape> aFromShape;
57 std::shared_ptr<GeomAPI_Shape> aToShape;
59 // Getting bounding planes.
60 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
62 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
64 anObjRef = selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
66 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
70 double aFromSize = real(FeaturesPlugin_Extrusion::FROM_SIZE_ID())->value();
71 double aToSize = real(FeaturesPlugin_Extrusion::TO_SIZE_ID())->value();
73 // for each selected face generate a result
74 int anIndex = 0, aResultIndex = 0;
75 for(; anIndex < aFaceRefs->size(); anIndex++) {
76 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
77 ResultPtr aContextRes = aFaceRef->context();
78 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
79 if (!aContext.get()) {
80 static const std::string aContextError = "The selection context is bad";
81 setError(aContextError);
85 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
86 int aFacesNum = -1; // this mean that "aFace" is used
87 ResultConstructionPtr aConstruction =
88 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
89 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
90 if (aConstruction.get()) {
91 aFacesNum = aConstruction->facesNum();
93 static const std::string aFaceError = "Can not find basis for extrusion";
99 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
100 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
101 std::shared_ptr<GeomAPI_Shape> aBaseShape;
102 if (aFacesNum == -1) {
103 aBaseShape = aValueFace;
105 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
108 GeomAlgoAPI_Prism aFeature(aBaseShape, aFromShape, aFromSize, aToShape, aToSize);
109 if(!aFeature.isDone()) {
110 static const std::string aFeatureError = "Extrusion algorithm failed";
111 setError(aFeatureError);
115 // Check if shape is valid
116 if(aFeature.shape()->isNull()) {
117 static const std::string aShapeError = "Resulting shape is Null";
118 setError(aShapeError);
121 if(!aFeature.isValid()) {
122 std::string aFeatureError = "Warning: resulting shape is not valid";
123 setError(aFeatureError);
127 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
129 setResult(aResultBody, aResultIndex);
136 // remove the rest results if there were produced in the previous pass
137 removeResults(aResultIndex);
140 //============================================================================
141 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
142 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
143 std::shared_ptr<GeomAPI_Shape> theBasis,
144 std::shared_ptr<GeomAPI_Shape> theContext)
147 if(theBasis->isEqual(theContext))
148 theResultBody->store(theFeature.shape());
150 theResultBody->storeGenerated(theBasis, theFeature.shape());
152 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
154 //Insert lateral face : Face from Edge
155 std::string aLatName = "LateralFace";
156 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes.get());
159 std::string aBotName = "BottomFace";
160 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
161 if(!aBottomFace->isNull()) {
162 if(aSubShapes->isBound(aBottomFace)) {
163 aBottomFace = aSubShapes->find(aBottomFace);
165 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
169 std::string aTopName = "TopFace";
170 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
171 if (!aTopFace->isNull()) {
172 if (aSubShapes->isBound(aTopFace)) {
173 aTopFace = aSubShapes->find(aTopFace);
175 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);