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::typeId()));
34 // extrusion works with faces always
35 aSelection->setSelectionType("FACE");
36 data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::typeId());
37 data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
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 if (aFacesNum == -1) {
79 aFace = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
81 GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
82 if(!aFeature.isDone()) {
83 static const std::string aFeatureError = "Extrusion algorithm failed";
84 setError(aFeatureError);
88 // Check if shape is valid
89 if (aFeature.shape()->isNull()) {
90 static const std::string aShapeError = "Resulting shape is Null";
91 setError(aShapeError);
94 if(!aFeature.isValid()) {
95 std::string aFeatureError = "Warning: resulting shape is not valid";
96 setError(aFeatureError);
100 LoadNamingDS(aFeature, aResultBody, aFace, aContext);
102 setResult(aResultBody, aResultIndex);
109 // remove the rest results if there were produced in the previous pass
110 removeResults(aResultIndex);
113 //============================================================================
114 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature,
115 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
116 std::shared_ptr<GeomAPI_Shape> theBasis,
117 std::shared_ptr<GeomAPI_Shape> theContext)
122 if(theBasis->isEqual(theContext))
123 theResultBody->store(theFeature.shape());
125 theResultBody->storeGenerated(theContext, theFeature.shape());
127 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
128 theFeature.mapOfShapes(*aSubShapes);
130 //Insert lateral face : Face from Edge
131 std::string aLatName = "LateralFace";
132 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
135 std::string aBotName = "BottomFace";
136 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
137 if (!aBottomFace->isNull()) {
138 if (aSubShapes->isBound(aBottomFace)) {
139 aBottomFace = aSubShapes->find(aBottomFace);
141 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
147 std::string aTopName = "TopFace";
148 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
149 if (!aTopFace->isNull()) {
150 if (aSubShapes->isBound(aTopFace)) {
151 aTopFace = aSubShapes->find(aTopFace);
153 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
158 //============================================================================
159 void FeaturesPlugin_Extrusion::clearResult()
161 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
162 std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
163 aResultBody->store(anEmptyShape);
164 setResult(aResultBody);