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