Salome HOME
Adding Naming DS for Placement operation.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // File:        FeaturesPlugin_Placement.cpp
2 // Created:     2 Dec 2014
3 // Author:      Artem ZHIDKOV
4
5 #include "FeaturesPlugin_Placement.h"
6
7 #include <ModelAPI_ResultConstruction.h>
8 #include <ModelAPI_ResultBody.h>
9 #include <ModelAPI_AttributeSelection.h>
10
11 #include <GeomAPI_Face.h>
12 #include <GeomAPI_Pln.h>
13 #include <GeomAlgoAPI_Placement.h>
14
15 #define _MODIFIEDF_TAG 1
16 #define _MODIFIEDE_TAG 2
17 #define _MODIFIEDV_TAG 3
18 #define _FACE 4
19 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
20 {
21 }
22
23 void FeaturesPlugin_Placement::initAttributes()
24 {
25   data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
26   data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
27 }
28
29 void FeaturesPlugin_Placement::execute()
30 {
31   // Verify the base face
32   std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
33     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
34   if (!aFaceRef)
35     return;
36
37   std::shared_ptr<GeomAPI_Shape> aBaseFace = 
38     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
39   if (!aBaseFace)
40     return;
41
42   std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
43   ResultPtr aContextRes = aFaceRef->context();
44   if (aContextRes) {
45     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
46       aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
47     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
48       aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
49   }
50   if (!aBaseFaceContext) {
51     static const std::string aContextError = "The selection context is bad";
52     setError(aContextError);
53     return;
54   }
55
56   // Verify the attractive face
57   aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
58       data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
59
60   std::shared_ptr<GeomAPI_Shape> aSlaveObjectFace = 
61     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
62   if (!aSlaveObjectFace)
63     return;
64
65   std::shared_ptr<GeomAPI_Shape> aSlaveObject;
66   aContextRes = aFaceRef->context();
67   if (aContextRes) {
68     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
69       aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
70     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
71       aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
72   }
73   if (!aSlaveObject) {
74     static const std::string aContextError = "The selection context is bad";
75     setError(aContextError);
76     return;
77   }
78
79   // Verify faces planarity
80   std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
81   std::shared_ptr<GeomAPI_Face> aSlaveFace1(new GeomAPI_Face(aSlaveObjectFace));
82   if (!aBaseFace1->isPlanar() || !aSlaveFace1->isPlanar()) {
83     static const std::string aPlanarityError = "One of selected face is not planar";
84     setError(aPlanarityError);
85     return;
86   }
87
88   std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace1->getPlane();
89   std::shared_ptr<GeomAPI_Pln> aSlavePlane = aSlaveFace1->getPlane();
90
91   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
92   GeomAlgoAPI_Placement aFeature(aSlaveObject, aSlavePlane, aBasePlane);
93   if(!aFeature.isDone()) {
94     static const std::string aFeatureError = "Placement algorithm failed";
95     setError(aFeatureError);
96     return;
97   }
98
99   // Check if shape is valid
100   if (aFeature.shape()->isNull()) {
101     static const std::string aShapeError = "Resulting shape is Null";
102     setError(aShapeError);
103     return;
104   }
105   if(!aFeature.isValid()) {
106     std::string aFeatureError = "Warning: resulting shape is not valid";
107     setError(aFeatureError);
108     return;
109   }  
110   //LoadNamingDS
111   LoadNamingDS(aFeature, aResultBody, aSlaveObject);
112
113   setResult(aResultBody);
114 }
115
116 //============================================================================
117 void FeaturesPlugin_Placement::LoadNamingDS(
118     GeomAlgoAPI_Placement& theFeature,
119     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
120     std::shared_ptr<GeomAPI_Shape> theSlaveObject)
121 {
122   //load result
123   theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
124
125   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
126   theFeature.mapOfShapes(*aSubShapes);
127
128     // put modifed faces in DF
129   theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, *aSubShapes); 
130
131 }