Salome HOME
ab932f01d6c21ac5993a434a9846b0253f926bda
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Placement.h"
22
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeBoolean.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_BodyBuilder.h>
30
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAlgoAPI_Placement.h>
35 #include <GeomAlgoAPI_Transform.h>
36
37 #include <FeaturesPlugin_Tools.h>
38
39 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
40 {
41 }
42
43 void FeaturesPlugin_Placement::initAttributes()
44 {
45
46   AttributeSelectionListPtr aSelection =
47     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
48     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
49
50   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
52   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
53   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
54 }
55
56 void FeaturesPlugin_Placement::execute()
57 {
58   // Getting objects.
59   ListOfShape anObjects;
60   std::list<ResultPtr> aContextes;
61   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
62   if(anObjectsSelList->size() == 0) {
63     return;
64   }
65   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
66     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
67       anObjectsSelList->value(anObjectsIndex);
68     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
69     if(!anObject.get()) { // may be for not-activated parts
70       eraseResults();
71       return;
72     }
73     anObjects.push_back(anObject);
74     aContextes.push_back(anObjectAttr->context());
75   }
76
77   // Verify the start shape
78   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
79   if(!anObjRef) {
80     return;
81   }
82   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
83   if(!aStartShape) {
84     static const std::string aSelectionError = "Error: The start shape selection is bad.";
85     setError(aSelectionError);
86     return;
87   }
88
89
90   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
91   ResultPtr aContextRes = anObjRef->context();
92   if (aContextRes.get()) {
93     aStartShapeContext = aContextRes->shape();
94   }
95   if(!aStartShapeContext.get()) {
96     static const std::string aContextError = "Error: The start shape selection context is bad.";
97     setError(aContextError);
98     return;
99   }
100
101   // Verify the end shape
102   anObjRef = selection(END_SHAPE_ID());
103   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
104   if(!anEndShape) {
105     static const std::string aSelectionError = "Error: The end shape selection is bad.";
106     setError(aSelectionError);
107     return;
108   }
109
110   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
111   aContextRes = anObjRef->context();
112   if(aContextRes.get()) {
113     anEndShapeContext = aContextRes->shape();
114   }
115   if(!anEndShapeContext.get()) {
116     static const std::string aContextError = "Error: The end shape selection context is bad.";
117     setError(aContextError);
118     return;
119   }
120
121   // Verify planarity of faces and linearity of edges
122   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
123   for (int i = 0; i < 2; i++) {
124     if (aShapes[i]->isFace()) {
125       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
126       if (!aFace->isPlanar()) {
127         static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
128         setError(aPlanarityError);
129         return;
130       }
131     }
132     else if (aShapes[i]->isEdge()) {
133       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
134       if (!anEdge->isLine()) {
135         static const std::string aLinearityError = "Error: One of selected endges is not linear.";
136         setError(aLinearityError);
137         return;
138       }
139     }
140   }
141
142   // Flags of the Placement
143   bool isReverse = boolean(REVERSE_ID())->value();
144   bool isCentering = boolean(CENTERING_ID())->value();
145
146   // Getting transformation.
147   GeomAlgoAPI_Placement aPlacementAlgo(
148     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
149   if(!aPlacementAlgo.isDone()) {
150     static const std::string aFeatureError = "Error: Placement algorithm failed.";
151     setError(aFeatureError);
152     return;
153   }
154   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
155
156   // Applying transformation to each object.
157   int aResultIndex = 0;
158   std::list<ResultPtr>::iterator aContext = aContextes.begin();
159   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
160       anObjectsIt++, aContext++) {
161
162     // for part results just set transformation
163     if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
164       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
165       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
166       aResultPart->setTrsf(aContextRes, aTrsf);
167       setResult(aResultPart, aResultIndex);
168     } else {
169       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
170       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
171
172       // Checking that the algorithm worked properly.
173       if(!aTransformAlgo.isDone()) {
174         static const std::string aFeatureError = "Error: Transform algorithm failed.";
175         setError(aFeatureError);
176         break;
177       }
178       if(aTransformAlgo.shape()->isNull()) {
179         static const std::string aShapeError = "Error: Resulting shape is Null.";
180         setError(aShapeError);
181         break;
182       }
183       if(!aTransformAlgo.isValid()) {
184         std::string aFeatureError = "Error: Resulting shape is not valid.";
185         setError(aFeatureError);
186         break;
187       }
188
189       //LoadNamingDS
190       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
191         document()->createBody(data(), aResultIndex);
192       loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
193       setResult(aResultBody, aResultIndex);
194     }
195     aResultIndex++;
196   }
197
198   // Remove the rest results if there were produced in the previous pass.
199   removeResults(aResultIndex);
200 }
201
202 //============================================================================
203 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
204                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
205                                             std::shared_ptr<GeomAPI_Shape> theBaseShape)
206 {
207   //load result
208   theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
209
210   std::string aPlacedName = "Placed";
211   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
212
213   FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
214                                             theBaseShape, 1, 2, 3, aPlacedName,
215                                             *aSubShapes.get());
216 }