]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Placement.cpp
Salome HOME
Working with folders which can not be shown empty
[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_ResultPart.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Face.h>
18 #include <GeomAPI_Pln.h>
19 #include <GeomAlgoAPI_Placement.h>
20
21 #define _MODIFIEDF_TAG 1
22 #define _MODIFIEDE_TAG 2
23 #define _MODIFIEDV_TAG 3
24 #define _FACE 4
25 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
26 {
27 }
28
29 void FeaturesPlugin_Placement::initAttributes()
30 {
31   /* Modification for specification of 1.3.0
32   AttributeSelectionListPtr aSelection = 
33     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34     FeaturesPlugin_Placement::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35   // extrusion works with faces always
36   aSelection->setSelectionType("SOLID");
37   */
38   data()->addAttribute(FeaturesPlugin_Placement::BASE_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
39   data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
40   data()->addAttribute(FeaturesPlugin_Placement::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
41   data()->addAttribute(FeaturesPlugin_Placement::CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
42 }
43
44 void FeaturesPlugin_Placement::execute()
45 {
46   // Verify the base face
47   std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = std::dynamic_pointer_cast<
48     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_OBJECT_ID()));
49   if (!anObjRef)
50     return;
51
52   std::shared_ptr<GeomAPI_Shape> aBaseShape = 
53     std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
54   if (!aBaseShape)
55     return;
56
57   std::shared_ptr<GeomAPI_Shape> aBaseObject;
58   ResultPtr aContextRes = anObjRef->context();
59   if (aContextRes.get()) {
60     aBaseObject = aContextRes->shape();
61   }
62   if (!aBaseObject.get()) {
63     static const std::string aContextError = "The base selection context is bad";
64     setError(aContextError);
65     return;
66   }
67
68   // Verify the attractive face
69   anObjRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
70       data()->attribute(FeaturesPlugin_Placement::ATTRACT_OBJECT_ID()));
71
72   std::shared_ptr<GeomAPI_Shape> aSlaveShape = 
73     std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
74   if (!aSlaveShape)
75     return;
76
77   std::shared_ptr<GeomAPI_Shape> aSlaveObject;
78   aContextRes = anObjRef->context();
79   if (aContextRes.get()) {
80     aSlaveObject = aContextRes->shape();
81   }
82   if (!aSlaveObject.get()) {
83     static const std::string aContextError = "The tool selection context is bad";
84     setError(aContextError);
85     return;
86   }
87
88   // Verify planarity of faces and linearity of edges
89   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aBaseShape, aSlaveShape};
90   for (int i = 0; i < 2; i++) {
91     if (aShapes[i]->isFace()) {
92       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
93       if (!aFace->isPlanar()) {
94         static const std::string aPlanarityError = "One of selected faces is not planar";
95         setError(aPlanarityError);
96         return;
97       }
98     }
99     else if (aShapes[i]->isEdge()) {
100       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
101       if (!anEdge->isLine()) {
102         static const std::string aLinearityError = "One of selected endges is not linear";
103         setError(aLinearityError);
104         return;
105       }
106     }
107   }
108
109   // Flags of the Placement
110   AttributeBooleanPtr aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
111       data()->attribute(FeaturesPlugin_Placement::REVERSE_ID()));
112   bool isReverse = aBoolAttr->value();
113   aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
114       data()->attribute(FeaturesPlugin_Placement::CENTERING_ID()));
115   bool isCentering = aBoolAttr->value();
116
117   bool isPart = aContextRes->groupName() == ModelAPI_ResultPart::group();
118
119   std::shared_ptr<ModelAPI_ResultBody> aResultBody;
120   if (!isPart) 
121     aResultBody = document()->createBody(data());
122   GeomAlgoAPI_Placement aFeature(
123     aSlaveObject, aBaseObject, aSlaveShape, aBaseShape, isReverse, isCentering, isPart);
124   if(!aFeature.isDone()) {
125     static const std::string aFeatureError = "Placement algorithm failed";
126     setError(aFeatureError);
127     return;
128   }
129
130   // Check if shape is valid
131   if (aFeature.shape()->isNull()) {
132     static const std::string aShapeError = "Resulting shape is Null";
133     setError(aShapeError);
134     return;
135   }
136   if(!aFeature.isValid()) {
137     std::string aFeatureError = "Warning: resulting shape is not valid";
138     setError(aFeatureError);
139     return;
140   }  
141
142   if (isPart) { // for part results just set transformation
143     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
144     ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
145     aResultPart->setTrsf(aContextRes, aFeature.transformation());
146     setResult(aResultPart);
147   } else {
148     //LoadNamingDS
149     LoadNamingDS(aFeature, aResultBody, aSlaveObject);
150
151     setResult(aResultBody);
152   }
153 }
154
155 //============================================================================
156 void FeaturesPlugin_Placement::LoadNamingDS(
157     GeomAlgoAPI_Placement& theFeature,
158     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
159     std::shared_ptr<GeomAPI_Shape> theSlaveObject)
160 {
161   //load result
162   theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
163
164   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
165   theFeature.mapOfShapes(*aSubShapes);
166   
167     // put modifed faces in DF
168   std::string aModName = "Modified";
169   theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, aModName, *aSubShapes); 
170
171 }