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(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
48 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
58 //=================================================================================================
59 void FeaturesPlugin_Extrusion::execute()
61 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
65 double aFromSize = 0.0;
67 if(string(CREATION_METHOD())->value() == "BySizes") {
68 aToSize = real(TO_SIZE_ID())->value();
69 aFromSize = real(FROM_SIZE_ID())->value();
71 aToSize = real(TO_OFFSET_ID())->value();
72 aFromSize = real(FROM_OFFSET_ID())->value();
75 // Getting bounding planes.
76 std::shared_ptr<GeomAPI_Shape> aToShape;
77 std::shared_ptr<GeomAPI_Shape> aFromShape;
79 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
80 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
81 if(anObjRef.get() != NULL) {
82 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
83 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
84 aToShape = anObjRef->context()->shape();
87 anObjRef = selection(FROM_OBJECT_ID());
88 if(anObjRef.get() != NULL) {
89 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
90 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
91 aFromShape = anObjRef->context()->shape();
96 // for each selected face generate a result
97 int anIndex = 0, aResultIndex = 0;
98 for(; anIndex < aFaceRefs->size(); anIndex++) {
99 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
100 ResultPtr aContextRes = aFaceRef->context();
101 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
102 if (!aContext.get()) {
103 static const std::string aContextError = "The selection context is bad";
104 setError(aContextError);
108 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
109 int aFacesNum = -1; // this mean that "aFace" is used
110 ResultConstructionPtr aConstruction =
111 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
112 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
113 if (aConstruction.get()) {
114 aFacesNum = aConstruction->facesNum();
116 static const std::string aFaceError = "Can not find basis for extrusion";
117 setError(aFaceError);
122 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
123 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
124 std::shared_ptr<GeomAPI_Shape> aBaseShape;
125 if (aFacesNum == -1) {
126 aBaseShape = aValueFace;
128 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
131 GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
132 if(!aFeature.isDone()) {
133 static const std::string aFeatureError = "Extrusion algorithm failed";
134 setError(aFeatureError);
138 // Check if shape is valid
139 if(!aFeature.shape().get() || aFeature.shape()->isNull()) {
140 static const std::string aShapeError = "Resulting shape is Null";
141 setError(aShapeError);
144 if(!aFeature.isValid()) {
145 std::string aFeatureError = "Warning: resulting shape is not valid";
146 setError(aFeatureError);
150 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
152 setResult(aResultBody, aResultIndex);
159 // remove the rest results if there were produced in the previous pass
160 removeResults(aResultIndex);
163 //=================================================================================================
164 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
165 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
166 std::shared_ptr<GeomAPI_Shape> theBasis,
167 std::shared_ptr<GeomAPI_Shape> theContext)
170 if(theBasis->isEqual(theContext))
171 theResultBody->store(theFeature.shape());
173 theResultBody->storeGenerated(theBasis, theFeature.shape());
175 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
177 //Insert lateral face : Face from Edge
178 std::string aLatName = "LateralFace";
179 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
182 std::string aBotName = "BottomFace";
183 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
184 if(!aBottomFace->isNull()) {
185 if(aSubShapes->isBound(aBottomFace)) {
186 aBottomFace = aSubShapes->find(aBottomFace);
188 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
192 std::string aTopName = "TopFace";
193 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
194 if (!aTopFace->isNull()) {
195 if (aSubShapes->isBound(aTopFace)) {
196 aTopFace = aSubShapes->find(aTopFace);
198 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);