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