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;
61 std::shared_ptr<GeomAPI_Shape> aToShape;
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> aBasePln = aBaseFace->getPlane();
115 std::shared_ptr<GeomAPI_Dir> aBaseDir = aBasePln->direction();
116 std::shared_ptr<GeomAPI_Pnt> aBaseLoc = aBasePln->location();
117 std::shared_ptr<GeomAPI_Shape> aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
119 std::shared_ptr<GeomAPI_Shape> aBoundingFromShape = aFromShape ? aFromShape : aBasePlane;
120 std::shared_ptr<GeomAPI_Shape> aBoundingToShape = aToShape ? aToShape : aBasePlane;
122 // Moving bounding faces according to "from" and "to" sizes.
123 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aBoundingFromShape));
124 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
125 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
126 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
128 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aBoundingToShape));
129 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
130 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
131 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
133 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
135 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? aFromSize : -aFromSize))));
136 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
138 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(aSign ? -aToSize : aToSize))));
139 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
141 //GeomAlgoAPI_Extrusion aFeature(aFace, aFromSize);
142 GeomAlgoAPI_Prism aFeature(aBaseShape, aBoundingFromShape, aBoundingToShape);
143 if(!aFeature.isDone()) {
144 static const std::string aFeatureError = "Extrusion algorithm failed";
145 setError(aFeatureError);
149 // Check if shape is valid
150 if(aFeature.shape()->isNull()) {
151 static const std::string aShapeError = "Resulting shape is Null";
152 setError(aShapeError);
155 if(!aFeature.isValid()) {
156 std::string aFeatureError = "Warning: resulting shape is not valid";
157 setError(aFeatureError);
161 LoadNamingDS(aFeature, aResultBody, aBaseShape, aContext);
163 setResult(aResultBody, aResultIndex);
170 // remove the rest results if there were produced in the previous pass
171 removeResults(aResultIndex);
174 //============================================================================
175 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
176 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
177 std::shared_ptr<GeomAPI_Shape> theBasis,
178 std::shared_ptr<GeomAPI_Shape> theContext)
181 if(theBasis->isEqual(theContext))
182 theResultBody->store(theFeature.shape());
184 theResultBody->storeGenerated(theBasis, theFeature.shape());
186 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
187 theFeature.mapOfShapes(*aSubShapes);
189 //Insert lateral face : Face from Edge
190 std::string aLatName = "LateralFace";
191 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
194 std::string aBotName = "BottomFace";
195 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
196 if(!aBottomFace->isNull()) {
197 if(aSubShapes->isBound(aBottomFace)) {
198 aBottomFace = aSubShapes->find(aBottomFace);
200 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
204 std::string aTopName = "TopFace";
205 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
206 if (!aTopFace->isNull()) {
207 if (aSubShapes->isBound(aTopFace)) {
208 aTopFace = aSubShapes->find(aTopFace);
210 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);