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_Session.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeSelection.h>
17 #include <ModelAPI_AttributeSelectionList.h>
18 #include <ModelAPI_AttributeBoolean.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_AttributeReference.h>
22 #include <GeomAlgoAPI_Prism.h>
24 #define _LATERAL_TAG 1
29 //=================================================================================================
30 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
34 //=================================================================================================
35 void FeaturesPlugin_Extrusion::initAttributes()
37 AttributeSelectionListPtr aSelection =
38 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40 // extrusion works with faces always
41 aSelection->setSelectionType("FACE");
43 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
45 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
55 //=================================================================================================
56 void FeaturesPlugin_Extrusion::execute()
58 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
61 double aFromSize = real(FROM_SIZE_ID())->value();
62 double aToSize = real(TO_SIZE_ID())->value();
64 // Getting bounding planes.
65 std::shared_ptr<GeomAPI_Shape> aFromShape;
66 std::shared_ptr<GeomAPI_Shape> aToShape;
68 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
69 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FROM_OBJECT_ID());
70 if(anObjRef.get() != NULL) {
71 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
72 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
73 aFromShape = anObjRef->context()->shape();
76 anObjRef = selection(TO_OBJECT_ID());
77 if(anObjRef.get() != NULL) {
78 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
79 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
80 aToShape = anObjRef->context()->shape();
85 // for each selected face generate a result
86 int anIndex = 0, aResultIndex = 0;
87 for(; anIndex < aFaceRefs->size(); anIndex++) {
88 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
89 ResultPtr aContextRes = aFaceRef->context();
90 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
91 if (!aContext.get()) {
92 static const std::string aContextError = "The selection context is bad";
93 setError(aContextError);
97 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
98 int aFacesNum = -1; // this mean that "aFace" is used
99 ResultConstructionPtr aConstruction =
100 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
101 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
102 if (aConstruction.get()) {
103 aFacesNum = aConstruction->facesNum();
105 static const std::string aFaceError = "Can not find basis for extrusion";
106 setError(aFaceError);
111 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
112 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
113 std::shared_ptr<GeomAPI_Shape> aBaseShape;
114 if (aFacesNum == -1) {
115 aBaseShape = aValueFace;
117 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
120 GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
121 if(!aFeature.isDone()) {
122 static const std::string aFeatureError = "Extrusion algorithm failed";
123 setError(aFeatureError);
127 // Check if shape is valid
128 if(aFeature.shape()->isNull()) {
129 static const std::string aShapeError = "Resulting shape is Null";
130 setError(aShapeError);
133 if(!aFeature.isValid()) {
134 std::string aFeatureError = "Warning: resulting shape is not valid";
135 setError(aFeatureError);
139 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
141 setResult(aResultBody, aResultIndex);
148 // remove the rest results if there were produced in the previous pass
149 removeResults(aResultIndex);
152 //=================================================================================================
153 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
154 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
155 std::shared_ptr<GeomAPI_Shape> theBasis,
156 std::shared_ptr<GeomAPI_Shape> theContext)
159 if(theBasis->isEqual(theContext))
160 theResultBody->store(theFeature.shape());
162 theResultBody->storeGenerated(theBasis, theFeature.shape());
164 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
166 //Insert lateral face : Face from Edge
167 std::string aLatName = "LateralFace";
168 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
171 std::string aBotName = "BottomFace";
172 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
173 if(!aBottomFace->isNull()) {
174 if(aSubShapes->isBound(aBottomFace)) {
175 aBottomFace = aSubShapes->find(aBottomFace);
177 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
181 std::string aTopName = "TopFace";
182 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
183 if (!aTopFace->isNull()) {
184 if (aSubShapes->isBound(aTopFace)) {
185 aTopFace = aSubShapes->find(aTopFace);
187 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);