1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Revolution.cpp
4 // Created: 12 May 2015
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_Revolution.h"
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_AttributeString.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_BodyBuilder.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_ResultBody.h>
19 #include <GeomAlgoAPI_CompoundBuilder.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
21 #include <GeomAPI_Edge.h>
22 #include <GeomAPI_Lin.h>
26 //=================================================================================================
27 FeaturesPlugin_Revolution::FeaturesPlugin_Revolution()
31 //=================================================================================================
32 void FeaturesPlugin_Revolution::initAttributes()
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37 // revolution works with faces always
38 aSelection->setSelectionType("FACE");
40 data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
42 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
44 data()->addAttribute(TO_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(FROM_ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(TO_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
48 data()->addAttribute(TO_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(FROM_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(FROM_OFFSET_ID(), ModelAPI_AttributeDouble::typeId());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TO_OBJECT_ID());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), FROM_OBJECT_ID());
56 // Composite Sketch attribute
57 data()->addAttribute(FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID(),
58 ModelAPI_AttributeReference::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
60 FeaturesPlugin_CompositeSketch::SKETCH_OBJECT_ID());
63 //=================================================================================================
64 void FeaturesPlugin_Revolution::execute()
66 /// sub feature of the composite should be set in the base list
67 AttributeSelectionListPtr aFacesSelectionList = selectionList(LIST_ID());
68 if (aFacesSelectionList.get() && !aFacesSelectionList->isInitialized()) {
69 AttributeReferencePtr aSketchAttr = reference(SKETCH_OBJECT_ID());
70 if (aSketchAttr.get() && aSketchAttr->isInitialized())
71 setSketchObjectToList();
75 ListOfShape aFacesList;
76 aFacesSelectionList = selectionList(LIST_ID());
77 for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
78 AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
79 std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
80 if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
81 aFacesList.push_back(aFaceShape);
82 } else { // This may be the whole sketch result selected, check and get faces.
83 ResultPtr aContext = aFaceSel->context();
84 std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
85 if(!aContextShape.get()) {
86 static const std::string aContextError = "Error: The selection context is bad.";
87 setError(aContextError);
90 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
91 if(!aConstruction.get()) {
92 static const std::string aFaceError = "Error: Can not find basis for revolution.";
96 int aFacesNum = aConstruction->facesNum();
97 for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
98 aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
99 aFacesList.push_back(aFaceShape);
105 std::shared_ptr<GeomAPI_Ax1> anAxis;
106 std::shared_ptr<GeomAPI_Edge> anEdge;
107 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(AXIS_OBJECT_ID());
108 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
109 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
110 } else if(anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
111 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
114 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
118 double aToAngle = real(TO_ANGLE_ID())->value();
119 double aFromAngle = real(FROM_ANGLE_ID())->value();
121 if(string(CREATION_METHOD())->value() == "ByAngles") {
122 aToAngle = real(TO_ANGLE_ID())->value();
123 aFromAngle = real(FROM_ANGLE_ID())->value();
125 aToAngle = real(TO_OFFSET_ID())->value();
126 aFromAngle = real(FROM_OFFSET_ID())->value();
129 // Getting bounding planes.
130 std::shared_ptr<GeomAPI_Shape> aToShape;
131 std::shared_ptr<GeomAPI_Shape> aFromShape;
133 if(string(CREATION_METHOD())->value() == "ByPlanesAndOffsets") {
134 anObjRef = selection(TO_OBJECT_ID());
135 if(anObjRef.get() != NULL) {
136 aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
137 if(aToShape.get() == NULL && anObjRef->context().get() != NULL) {
138 aToShape = anObjRef->context()->shape();
141 anObjRef = selection(FROM_OBJECT_ID());
142 if(anObjRef.get() != NULL) {
143 aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
144 if(aFromShape.get() == NULL && anObjRef->context().get() != NULL) {
145 aFromShape = anObjRef->context()->shape();
150 // Searching faces with common edges.
152 ListOfShape aFreeFaces;
153 std::shared_ptr<GeomAPI_Shape> aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList);
154 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces);
155 aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end());
157 // Generating result for each shell and face.
158 int aResultIndex = 0;
159 for(ListOfShape::const_iterator anIter = aShells.cbegin(); anIter != aShells.cend(); anIter++) {
160 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
162 GeomAlgoAPI_Revolution aRevolAlgo(aBaseShape, anAxis, aToShape, aToAngle, aFromShape, aFromAngle);
163 if(!aRevolAlgo.isDone()) {
164 static const std::string aPrismAlgoError = "Error: Revolution algorithm failed.";
165 setError(aPrismAlgoError);
170 // Check if shape is valid
171 if(!aRevolAlgo.shape().get() || aRevolAlgo.shape()->isNull()) {
172 static const std::string aShapeError = "Error: Resulting shape is Null.";
173 setError(aShapeError);
177 if(!aRevolAlgo.isValid()) {
178 std::string aPrismAlgoError = "Error: Resulting shape is not valid.";
179 setError(aPrismAlgoError);
184 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
185 loadNamingDS(aRevolAlgo, aResultBody, aBaseShape);
186 setResult(aResultBody, aResultIndex);
190 removeResults(aResultIndex);
193 //=================================================================================================
194 void FeaturesPlugin_Revolution::loadNamingDS(GeomAlgoAPI_Revolution& theRevolAlgo,
195 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
196 std::shared_ptr<GeomAPI_Shape> theBasis)
199 theResultBody->storeGenerated(theBasis, theRevolAlgo.shape());
201 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theRevolAlgo.mapOfSubShapes();
203 //Insert lateral face : Face from Edge
204 const std::string aLatName = "LateralFace";
205 const int aLatTag = 1;
206 theResultBody->loadAndOrientGeneratedShapes(&theRevolAlgo, theBasis, GeomAPI_Shape::EDGE, aLatTag, aLatName, *aSubShapes);
209 int aToFaceIndex = 1;
210 const std::string aToName = "ToFace";
212 const ListOfShape& aToFaces = theRevolAlgo.toShapes();
213 for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) {
214 std::shared_ptr<GeomAPI_Shape> aToFace = *anIt;
215 if(aSubShapes->isBound(aToFace)) {
216 aToFace = aSubShapes->find(aToFace);
218 std::ostringstream aStr;
219 aStr << aToName << "_" << aToFaceIndex++;
220 theResultBody->generated(aToFace, aStr.str(), aToTag++);
224 int aFromFaceIndex = 1;
225 const std::string aFromName = "FromFace";
226 int aFromTag = aToTag > 10000 ? aToTag : 10000;
227 const ListOfShape& aFromFaces = theRevolAlgo.fromShapes();
228 for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) {
229 std::shared_ptr<GeomAPI_Shape> aFromFace = *anIt;
230 if(aSubShapes->isBound(aFromFace)) {
231 aFromFace = aSubShapes->find(aFromFace);
233 std::ostringstream aStr;
234 aStr << aFromName << "_" << aFromFaceIndex++;
235 theResultBody->generated(aFromFace, aStr.str(), aFromTag++);