]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Placement.cpp
Salome HOME
Multiple selection in placement
[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 #include <GeomAlgoAPI_Transform.h>
21
22 #define _MODIFIEDF_TAG 1
23 #define _MODIFIEDE_TAG 2
24 #define _MODIFIEDV_TAG 3
25 #define _FACE 4
26 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
27 {
28 }
29
30 void FeaturesPlugin_Placement::initAttributes()
31 {
32
33   AttributeSelectionListPtr aSelection = 
34     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36   // extrusion works with faces always
37   aSelection->setSelectionType("SOLID");
38
39   data()->addAttribute(START_FACE_ID(), ModelAPI_AttributeSelection::typeId());
40   data()->addAttribute(END_FACE_ID(), ModelAPI_AttributeSelection::typeId());
41   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
42   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
43 }
44
45 void FeaturesPlugin_Placement::execute()
46 {
47   // Getting objects.
48   ListOfShape anObjects;
49   std::list<ResultPtr> aContextes;
50   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
51   if(anObjectsSelList->size() == 0) {
52     return;
53   }
54   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
55     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
56     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
57     if(!anObject.get()) {
58       return;
59     }
60     anObjects.push_back(anObject);
61     aContextes.push_back(anObjectAttr->context());
62   }
63
64   // Verify the start face
65   AttributeSelectionPtr anObjRef = selection(START_FACE_ID());
66   if(!anObjRef) {
67     return;
68   }
69   std::shared_ptr<GeomAPI_Shape> aStartFace = anObjRef->value();
70   if(!aStartFace || !GeomAPI_Face(aStartFace).isPlanar()) {
71     static const std::string aSelectionError = "The start face selection is bad";
72     setError(aSelectionError);
73     return;
74   }
75
76
77   std::shared_ptr<GeomAPI_Shape> aStartShape;
78   ResultPtr aContextRes = anObjRef->context();
79   if (aContextRes.get()) {
80     aStartShape = aContextRes->shape();
81   }
82   if(!aStartShape.get()) {
83     static const std::string aContextError = "The start face selection context is bad";
84     setError(aContextError);
85     return;
86   }
87
88   // Verify the end face
89   anObjRef = selection(END_FACE_ID());
90   std::shared_ptr<GeomAPI_Shape> anEndFace = anObjRef->value();
91   if(!anEndFace || !GeomAPI_Face(anEndFace).isPlanar()) {
92     static const std::string aSelectionError = "The end face selection is bad";
93     setError(aSelectionError);
94     return;
95   }
96
97   std::shared_ptr<GeomAPI_Shape> anEndShape;
98   aContextRes = anObjRef->context();
99   if(aContextRes.get()) {
100     anEndShape = aContextRes->shape();
101   }
102   if(!anEndShape.get()) {
103     static const std::string aContextError = "The end face selection context is bad";
104     setError(aContextError);
105     return;
106   }
107
108   // Flags of the Placement
109   bool isReverse = boolean(REVERSE_ID())->value();
110   bool isCentering = boolean(CENTERING_ID())->value();
111
112   bool isPart = aContextRes->groupName() == ModelAPI_ResultPart::group();
113
114   // Getting transformation.
115   GeomAlgoAPI_Placement aPlacementAlgo(
116     aStartShape, anEndShape, aStartFace, anEndFace, isReverse, isCentering, true);
117   if(!aPlacementAlgo.isDone()) {
118     static const std::string aFeatureError = "Placement algorithm failed";
119     setError(aFeatureError);
120     return;
121   }
122   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
123
124   // Applying transformation to each object.
125   int aResultIndex = 0;
126   std::list<ResultPtr>::iterator aContext = aContextes.begin();
127   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
128       anObjectsIt++, aContext++) {
129
130     if (isPart) { // for part results just set transformation
131       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
132       ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
133       aResultPart->setTrsf(aContextRes, aTrsf);
134       setResult(aResultPart);
135     } else {
136       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
137       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
138
139       // Checking that the algorithm worked properly.
140       if(!aTransformAlgo.isDone()) {
141         static const std::string aFeatureError = "Transform algorithm failed";
142         setError(aFeatureError);
143         break;
144       }
145       if(aTransformAlgo.shape()->isNull()) {
146         static const std::string aShapeError = "Resulting shape is Null";
147         setError(aShapeError);
148         break;
149       }
150       if(!aTransformAlgo.isValid()) {
151         std::string aFeatureError = "Warning: resulting shape is not valid";
152         setError(aFeatureError);
153         break;
154       }
155
156       //LoadNamingDS
157       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
158       LoadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
159       setResult(aResultBody, aResultIndex);
160     }
161     aResultIndex++;
162   }
163
164   // Remove the rest results if there were produced in the previous pass.
165   removeResults(aResultIndex);
166 }
167
168 //============================================================================
169 void FeaturesPlugin_Placement::LoadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
170                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
171                                             std::shared_ptr<GeomAPI_Shape> theSlaveObject)
172 {
173   //load result
174   theResultBody->storeModified(theSlaveObject, theTransformAlgo.shape()); // the initial Slave, the resulting Slave
175
176   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfShapes();
177
178     // put modifed faces in DF
179   std::string aModName = "Modified";
180   theResultBody->loadAndOrientModifiedShapes(theTransformAlgo.makeShape().get(),
181                                              theSlaveObject, _FACE,
182                                              _MODIFIEDF_TAG, aModName, *aSubShapes.get());
183 }