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_ResultCompSolid.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19 #include <ModelAPI_AttributeBoolean.h>
20 #include <ModelAPI_AttributeString.h>
21 #include <ModelAPI_AttributeReference.h>
23 #include <GeomAlgoAPI_Prism.h>
25 #define _LATERAL_TAG 1
30 #define DEBUG_COMPSOLID
32 //=================================================================================================
33 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
37 //=================================================================================================
38 void FeaturesPlugin_Extrusion::initAttributes()
40 AttributeSelectionListPtr aSelection =
41 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
43 // extrusion works with faces always
44 aSelection->setSelectionType("FACE");
46 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
48 data()->addAttribute(TO_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(FROM_SIZE_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
54 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
55 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
61 //=================================================================================================
62 void FeaturesPlugin_Extrusion::execute()
64 AttributeSelectionListPtr aFaceRefs = selectionList(LIST_ID());
68 double aFromSize = 0.0;
70 if(string(CREATION_METHOD())->value() == "BySizes") {
71 aToSize = real(TO_SIZE_ID())->value();
72 aFromSize = real(FROM_SIZE_ID())->value();
74 aToSize = real(TO_OFFSET_ID())->value();
75 aFromSize = real(FROM_OFFSET_ID())->value();
78 // Getting bounding planes.
79 std::shared_ptr<GeomAPI_Shape> aToShape;
80 std::shared_ptr<GeomAPI_Shape> aFromShape;
82 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
83 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(TO_OBJECT_ID());
84 if(anObjRef.get() != NULL) {
85 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
86 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
87 aToShape = anObjRef->context()->shape();
90 anObjRef = selection(FROM_OBJECT_ID());
91 if(anObjRef.get() != NULL) {
92 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
93 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
94 aFromShape = anObjRef->context()->shape();
99 // for each selected face generate a result
100 int anIndex = 0, aResultIndex = 0;
101 #ifdef DEBUG_COMPSOLID
102 ResultCompSolidPtr aCompSolidResult = document()->createCompSolid(data(), aResultIndex);
103 setResult(aCompSolidResult, aResultIndex);
106 for(; anIndex < aFaceRefs->size(); anIndex++) {
107 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
108 ResultPtr aContextRes = aFaceRef->context();
109 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
110 if (!aContext.get()) {
111 static const std::string aContextError = "The selection context is bad";
112 setError(aContextError);
116 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
117 int aFacesNum = -1; // this mean that "aFace" is used
118 ResultConstructionPtr aConstruction =
119 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
120 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
121 if (aConstruction.get()) {
122 aFacesNum = aConstruction->facesNum();
124 static const std::string aFaceError = "Can not find basis for extrusion";
125 setError(aFaceError);
129 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
130 #ifdef DEBUG_COMPSOLID
131 ResultBodyPtr aResultBody = aCompSolidResult->addResult(aResultIndex);
133 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
135 std::shared_ptr<GeomAPI_Shape> aBaseShape;
136 if (aFacesNum == -1) {
137 aBaseShape = aValueFace;
139 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
142 GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
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().get() || 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);
162 setResult(aResultBody, aResultIndex);
169 // remove the rest results if there were produced in the previous pass
170 removeResults(aResultIndex);
173 //=================================================================================================
174 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Prism& theFeature,
175 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
176 std::shared_ptr<GeomAPI_Shape> theBasis,
177 std::shared_ptr<GeomAPI_Shape> theContext)
180 if(theBasis->isEqual(theContext))
181 theResultBody->store(theFeature.shape());
183 theResultBody->storeGenerated(theBasis, theFeature.shape());
185 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theFeature.mapOfShapes();
187 //Insert lateral face : Face from Edge
188 std::string aLatName = "LateralFace";
189 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape().get(), theBasis, EDGE,_LATERAL_TAG, aLatName, *aSubShapes);
192 std::string aBotName = "BottomFace";
193 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
194 if(!aBottomFace->isNull()) {
195 if(aSubShapes->isBound(aBottomFace)) {
196 aBottomFace = aSubShapes->find(aBottomFace);
198 theResultBody->generated(aBottomFace, aBotName, _FIRST_TAG);
202 std::string aTopName = "TopFace";
203 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
204 if (!aTopFace->isNull()) {
205 if (aSubShapes->isBound(aTopFace)) {
206 aTopFace = aSubShapes->find(aTopFace);
208 theResultBody->generated(aTopFace, aTopName, _LAST_TAG);