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_Document.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_AttributeBoolean.h>
17 #include <GeomAlgoAPI_Extrusion.h>
20 #define _LATERAL_TAG 1
25 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
29 void FeaturesPlugin_Extrusion::initAttributes()
31 AttributeSelectionListPtr aSelection =
32 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
33 FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::type()));
34 // extrusion works with faces always
35 aSelection->setSelectionType("FACE");
36 data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
37 data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
40 void FeaturesPlugin_Extrusion::execute()
42 AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
44 // for each selected face generate a result
45 int anIndex = 0, aResultIndex = 0;
46 for(; anIndex < aFaceRefs->size(); anIndex++) {
47 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
48 ResultPtr aContextRes = aFaceRef->context();
49 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
50 if (!aContext.get()) {
51 static const std::string aContextError = "The selection context is bad";
52 setError(aContextError);
55 double aSize = real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
56 if (boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
59 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
60 int aFacesNum = -1; // this mean that "aFace" is used
61 ResultConstructionPtr aConstruction =
62 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
63 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
64 if (aConstruction.get()) {
65 aFacesNum = aConstruction->facesNum();
67 static const std::string aFaceError = "Can not find basis for extrusion";
73 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
74 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
75 std::shared_ptr<GeomAPI_Shape> aFace =
76 aFacesNum == -1 ? aValueFace : aConstruction->face(aFaceIndex);
77 GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
78 if(!aFeature.isDone()) {
79 static const std::string aFeatureError = "Extrusion algorithm failed";
80 setError(aFeatureError);
84 // Check if shape is valid
85 if (aFeature.shape()->isNull()) {
86 static const std::string aShapeError = "Resulting shape is Null";
87 setError(aShapeError);
90 if(!aFeature.isValid()) {
91 std::string aFeatureError = "Warning: resulting shape is not valid";
92 setError(aFeatureError);
96 LoadNamingDS(aFeature, aResultBody, aFace, aContext);
98 setResult(aResultBody, aResultIndex);
105 // remove the rest results if there were produced in the previous pass
106 removeResults(aResultIndex);
109 //============================================================================
110 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature,
111 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
112 std::shared_ptr<GeomAPI_Shape> theBasis,
113 std::shared_ptr<GeomAPI_Shape> theContext)
118 if(theBasis->isEqual(theContext))
119 theResultBody->store(theFeature.shape());
121 theResultBody->storeGenerated(theContext, theFeature.shape());
123 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
124 theFeature.mapOfShapes(*aSubShapes);
126 //Insert lateral face : Face from Edge
127 std::string aLatName = "LateralFace";
128 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
131 std::string aBotName = "BottomFace";
132 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
133 if (!aBottomFace->isNull()) {
134 if (aSubShapes->isBound(aBottomFace)) {
135 aBottomFace = aSubShapes->find(aBottomFace);
137 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
143 std::string aTopName = "TopFace";
144 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
145 if (!aTopFace->isNull()) {
146 if (aSubShapes->isBound(aTopFace)) {
147 aTopFace = aSubShapes->find(aTopFace);
149 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
154 //============================================================================
155 void FeaturesPlugin_Extrusion::clearResult()
157 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
158 std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
159 aResultBody->store(anEmptyShape);
160 setResult(aResultBody);