]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Placement.cpp
Salome HOME
Make the movement, placement and rotation 3D features may be applied to the Part...
[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(aSlaveObject, aBaseObject, aSlaveShape, aBaseShape, isReverse, isCentering);
123   if(!aFeature.isDone()) {
124     static const std::string aFeatureError = "Placement algorithm failed";
125     setError(aFeatureError);
126     return;
127   }
128
129   // Check if shape is valid
130   if (aFeature.shape()->isNull()) {
131     static const std::string aShapeError = "Resulting shape is Null";
132     setError(aShapeError);
133     return;
134   }
135   if(!aFeature.isValid()) {
136     std::string aFeatureError = "Warning: resulting shape is not valid";
137     setError(aFeatureError);
138     return;
139   }  
140
141   if (isPart) { // for part results just set transformation
142     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
143     ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
144     aResultPart->setShape(aContextRes, aFeature.shape());
145     setResult(aResultPart);
146   } else {
147     //LoadNamingDS
148     LoadNamingDS(aFeature, aResultBody, aSlaveObject);
149
150     setResult(aResultBody);
151   }
152 }
153
154 //============================================================================
155 void FeaturesPlugin_Placement::LoadNamingDS(
156     GeomAlgoAPI_Placement& theFeature,
157     std::shared_ptr<ModelAPI_ResultBody> theResultBody,
158     std::shared_ptr<GeomAPI_Shape> theSlaveObject)
159 {
160   //load result
161   theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
162
163   GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
164   theFeature.mapOfShapes(*aSubShapes);
165   
166     // put modifed faces in DF
167   std::string aModName = "Modified";
168   theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, aModName, *aSubShapes); 
169
170 }