Salome HOME
af86e815255c9bb88d0788cd453e9f1bec155b94
[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 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
16 {
17 }
18
19 void FeaturesPlugin_Placement::initAttributes()
20 {
21   data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
22   data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
23 }
24
25 void FeaturesPlugin_Placement::execute()
26 {
27   // Verify the base face
28   std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
29     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
30   if (!aFaceRef)
31     return;
32
33   std::shared_ptr<GeomAPI_Shape> aBaseFace = 
34     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
35   if (!aBaseFace)
36     return;
37
38   std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
39   ResultPtr aContextRes = aFaceRef->context();
40   if (aContextRes) {
41     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
42       aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
43     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
44       aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
45   }
46   if (!aBaseFaceContext) {
47     static const std::string aContextError = "The selection context is bad";
48     setError(aContextError);
49     return;
50   }
51
52   // Verify the attractive face
53   aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
54       data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
55
56   std::shared_ptr<GeomAPI_Shape> anAttractiveFace = 
57     std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
58   if (!anAttractiveFace)
59     return;
60
61   std::shared_ptr<GeomAPI_Shape> anAttractiveFaceContext;
62   aContextRes = aFaceRef->context();
63   if (aContextRes) {
64     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
65       anAttractiveFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
66     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
67       anAttractiveFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
68   }
69   if (!anAttractiveFaceContext) {
70     static const std::string aContextError = "The selection context is bad";
71     setError(aContextError);
72     return;
73   }
74
75   // Verify faces planarity
76   std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
77   std::shared_ptr<GeomAPI_Face> anAttractFace1(new GeomAPI_Face(anAttractiveFace));
78   if (!aBaseFace1->isPlanar() || !anAttractFace1->isPlanar()) {
79     static const std::string aPlanarityError = "One of selected face is not planar";
80     setError(aPlanarityError);
81     return;
82   }
83
84   std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace1->getPlane();
85   std::shared_ptr<GeomAPI_Pln> anAttractivePlane = anAttractFace1->getPlane();
86
87   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
88   GeomAlgoAPI_Placement aFeature(anAttractiveFaceContext, anAttractivePlane, aBasePlane);
89   if(!aFeature.isDone()) {
90     static const std::string aFeatureError = "Placement algorithm failed";
91     setError(aFeatureError);
92     return;
93   }
94
95   // Check if shape is valid
96   if (aFeature.shape()->isNull()) {
97     static const std::string aShapeError = "Resulting shape is Null";
98     setError(aShapeError);
99     return;
100   }
101   if(!aFeature.isValid()) {
102     std::string aFeatureError = "Warning: resulting shape is not valid";
103     setError(aFeatureError);
104     return;
105   }  
106   //LoadNamingDS
107   LoadNamingDS(aFeature, aResultBody, anAttractiveFace, anAttractiveFaceContext);
108
109   setResult(aResultBody);
110 }
111
112 //============================================================================
113 void FeaturesPlugin_Placement::LoadNamingDS(
114     GeomAlgoAPI_Placement& theFeature,
115     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
116     std::shared_ptr<GeomAPI_Shape> theBasis,
117     std::shared_ptr<GeomAPI_Shape> theContext)
118 {
119   theResultBody->store(theFeature.shape());
120   /// TODO: SZY
121 /*
122
123   //load result
124   if(theBasis->isEqual(theContext))
125     theResultBody->store(theFeature.shape());
126   else
127     theResultBody->storeGenerated(theContext, theFeature.shape()); 
128
129   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
130   theFeature.mapOfShapes(*aSubShapes);
131
132     //Insert lateral face : Face from Edge
133   theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, *aSubShapes);
134
135   //Insert bottom face
136   std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();  
137   if (!aBottomFace->isNull()) {
138         if (aSubShapes->isBound(aBottomFace)) {  
139                 aBottomFace = aSubShapes->find(aBottomFace);            
140     }    
141     theResultBody->generated(aBottomFace, _FIRST_TAG);
142   }
143
144
145
146   //Insert top face
147   std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
148   if (!aTopFace->isNull()) {
149     if (aSubShapes->isBound(aTopFace)) {         
150       aTopFace = aSubShapes->find(aTopFace);    
151     }
152     theResultBody->generated(aTopFace, _LAST_TAG);
153   }
154
155 */
156 }