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 for(; anIndex < aFaceRefs->size(); anIndex++) {
102 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = aFaceRefs->value(anIndex);
103 ResultPtr aContextRes = aFaceRef->context();
104 std::shared_ptr<GeomAPI_Shape> aContext = aContextRes->shape();
105 if (!aContext.get()) {
106 static const std::string aContextError = "The selection context is bad";
107 setError(aContextError);
111 std::shared_ptr<GeomAPI_Shape> aValueFace = aFaceRef->value();
112 int aFacesNum = -1; // this mean that "aFace" is used
113 ResultConstructionPtr aConstruction =
114 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes);
115 if (!aValueFace.get()) { // this may be the whole sketch result selected, check and get faces
116 if (aConstruction.get()) {
117 aFacesNum = aConstruction->facesNum();
119 static const std::string aFaceError = "Can not find basis for extrusion";
120 setError(aFaceError);
125 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
126 //ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
127 #ifdef DEBUG_COMPSOLID
128 ResultCompSolidPtr aResultBody = document()->createCompSolid(data(), aResultIndex);
130 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
132 std::shared_ptr<GeomAPI_Shape> aBaseShape;
133 if (aFacesNum == -1) {
134 aBaseShape = aValueFace;
136 aBaseShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
139 GeomAlgoAPI_Prism aFeature(aBaseShape, aToShape, aToSize, aFromShape, aFromSize);
140 if(!aFeature.isDone()) {
141 static const std::string aFeatureError = "Extrusion algorithm failed";
142 setError(aFeatureError);
146 // Check if shape is valid
147 if(!aFeature.shape().get() || aFeature.shape()->isNull()) {
148 static const std::string aShapeError = "Resulting shape is Null";
149 setError(aShapeError);
152 if(!aFeature.isValid()) {
153 std::string aFeatureError = "Warning: resulting shape is not valid";
154 setError(aFeatureError);
158 #ifdef DEBUG_COMPSOLID
160 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);