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 <GeomAPI_Pln.h>
21 #include <GeomAPI_XYZ.h>
22 #include <GeomAlgoAPI_Extrusion.h>
23 #include <GeomAlgoAPI_FaceBuilder.h>
24 #include <GeomAlgoAPI_Prism.h>
27 #define _LATERAL_TAG 1
32 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
36 void FeaturesPlugin_Extrusion::initAttributes()
38 AttributeSelectionListPtr aSelection =
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 FeaturesPlugin_Extrusion::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41 // extrusion works with faces always
42 aSelection->setSelectionType("FACE");
44 data()->addAttribute(FeaturesPlugin_Extrusion::TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(FeaturesPlugin_Extrusion::FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
47 //data()->addAttribute(FeaturesPlugin_Extrusion::AXIS_OBJECT_ID(), ModelAPI_AttributeReference::typeId());
49 data()->addAttribute(FeaturesPlugin_Extrusion::FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
50 data()->addAttribute(FeaturesPlugin_Extrusion::TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FeaturesPlugin_Extrusion::TO_OBJECT_ID());
56 void FeaturesPlugin_Extrusion::execute()
58 AttributeSelectionListPtr aFaceRefs = selectionList(FeaturesPlugin_Extrusion::LIST_ID());
60 std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape);
61 std::shared_ptr<GeomAPI_Shape> aToShape(new GeomAPI_Shape);
63 // Getting bounding planes.
64 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Extrusion::FROM_OBJECT_ID());
66 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
68 anObjRef = selection(FeaturesPlugin_Extrusion::TO_OBJECT_ID());
70 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
74 double aFromSize = real(FeaturesPlugin_Extrusion::FROM_SIZE_ID())->value();
75 double aToSize = real(FeaturesPlugin_Extrusion::TO_SIZE_ID())->value();
77 // for each selected face generate a result
78 int anIndex = 0, aResultIndex = 0;
79 for(; anIndex < aFaceRefs->size(); anIndex++) {
80 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
81 ResultPtr aContextRes = aFaceRef->context();
82 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
83 if (!aContext.get()) {
84 static const std::string aContextError = "The selection context is bad";
85 setError(aContextError);
89 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
90 int aFacesNum = -1; // this mean that "aFace" is used
91 ResultConstructionPtr aConstruction =
92 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
93 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
94 if (aConstruction.get()) {
95 aFacesNum = aConstruction->facesNum();
97 static const std::string aFaceError = "Can not find basis for extrusion";
103 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
104 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
105 std::shared_ptr<GeomAPI_Shape> aBaseShape;
106 if (aFacesNum == -1) {
107 aBaseShape = aValueFace;
109 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
112 // If bounding faces was not set creating them.
113 std::shared_ptr<GeomAPI_Face> aBaseFace(new GeomAPI_Face(aBaseShape));
114 std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace->getPlane();
115 std::shared_ptr<GeomAPI_Dir> aBaseDir = aBasePlane->direction();
116 std::shared_ptr<GeomAPI_Pnt> aBaseLoc = aBasePlane->location();
119 aFromShape = GeomAlgoAPI_FaceBuilder::plane(aBaseLoc, aBaseDir);
122 aToShape = GeomAlgoAPI_FaceBuilder::plane(aBaseLoc, aBaseDir);
125 // Moving bounding faces according to "from" and "to" sizes.
126 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
127 std::shared_ptr<GeomAPI_Pln> aFromPlane = aFromFace->getPlane();
128 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPlane->location();
129 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPlane->direction();
131 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
132 std::shared_ptr<GeomAPI_Pln> aToPlane = aToFace->getPlane();
133 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPlane->location();
134 std::shared_ptr<GeomAPI_Dir> aToDir = aToPlane->direction();
136 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
138 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? aFromSize : -aFromSize))));
139 aFromShape = GeomAlgoAPI_FaceBuilder::plane(aFromPnt, aFromDir);
141 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? -aToSize : aToSize))));
142 aToShape = GeomAlgoAPI_FaceBuilder::plane(aToPnt, aToDir);
144 //GeomAlgoAPI_Extrusion aFeature(aFace, aFromSize);
145 GeomAlgoAPI_Prism aFeature(aBaseShape, aFromShape, aToShape);
146 if(!aFeature.isDone()) {
147 static const std::string aFeatureError = "Extrusion algorithm failed";
148 setError(aFeatureError);
152 // Check if shape is valid
153 if(aFeature.shape()->isNull()) {
154 static const std::string aShapeError = "Resulting shape is Null";
155 setError(aShapeError);
158 if(!aFeature.isValid()) {
159 std::string aFeatureError = "Warning: resulting shape is not valid";
160 setError(aFeatureError);
164 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
166 setResult(aResultBody, aResultIndex);
173 // remove the rest results if there were produced in the previous pass
174 removeResults(aResultIndex);
177 //============================================================================
178 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
179 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
180 std::shared_ptr<GeomAPI_Shape> theBasis,
181 std::shared_ptr<GeomAPI_Shape> theContext)
184 if(theBasis->isEqual(theContext))
185 theResultBody->store(theFeature.shape());
187 theResultBody->storeGenerated(theContext, theFeature.shape());
189 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
190 theFeature.mapOfShapes(*aSubShapes);
192 //Insert lateral face : Face from Edge
193 std::string aLatName = "LateralFace";
194 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
197 std::string aBotName = "BottomFace";
198 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
199 if(!aBottomFace->isNull()) {
200 if(aSubShapes->isBound(aBottomFace)) {
201 aBottomFace = aSubShapes->find(aBottomFace);
203 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
207 std::string aTopName = "TopFace";
208 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
209 if (!aTopFace->isNull()) {
210 if (aSubShapes->isBound(aTopFace)) {
211 aTopFace = aSubShapes->find(aTopFace);
213 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);
217 //============================================================================
218 void FeaturesPlugin_Extrusion::clearResult()
220 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
221 std::shared_ptr<GeomAPI_Shape> anEmptyShape(new GeomAPI_Shape);
222 aResultBody->store(anEmptyShape);
223 setResult(aResultBody);