Salome HOME
Improve ExchangePlugin code coverage
[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 <GeomAPI_ShapeIterator.h>
35 #include <GeomAlgoAPI_Placement.h>
36 #include <GeomAlgoAPI_Transform.h>
37 #include <GeomAlgoAPI_Tools.h>
38
39 #include <FeaturesPlugin_Tools.h>
40
41 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
42 {
43 }
44
45 void FeaturesPlugin_Placement::initAttributes()
46 {
47
48   AttributeSelectionListPtr aSelection =
49     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
50     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
51
52   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
53   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
54   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
55   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
56 }
57
58 void FeaturesPlugin_Placement::execute()
59 {
60   // Getting objects.
61   ListOfShape anObjects;
62   std::list<ResultPtr> aContextes;
63   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
64   if(anObjectsSelList->size() == 0) {
65     return;
66   }
67   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
68     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
69       anObjectsSelList->value(anObjectsIndex);
70     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
71     if(!anObject.get()) { // may be for not-activated parts
72       return;
73     }
74     anObjects.push_back(anObject);
75     aContextes.push_back(anObjectAttr->context());
76   }
77
78   // Verify the start shape
79   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
80   if(!anObjRef) {
81     return;
82   }
83   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
84   if(!aStartShape) {
85     static const std::string aSelectionError = "Error: The start shape selection is bad.";
86     setError(aSelectionError);
87     return;
88   }
89
90
91   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
92   ResultPtr aContextRes = anObjRef->context();
93   if (aContextRes.get()) {
94     aStartShapeContext = aContextRes->shape();
95   }
96   if(!aStartShapeContext.get()) {
97     static const std::string aContextError = "Error: The start shape selection context is bad.";
98     setError(aContextError);
99     return;
100   }
101
102   // Verify the end shape
103   anObjRef = selection(END_SHAPE_ID());
104   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
105   if(!anEndShape) {
106     static const std::string aSelectionError = "Error: The end shape selection is bad.";
107     setError(aSelectionError);
108     return;
109   }
110
111   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
112   aContextRes = anObjRef->context();
113   if(aContextRes.get()) {
114     anEndShapeContext = aContextRes->shape();
115   }
116   if(!anEndShapeContext.get()) {
117     static const std::string aContextError = "Error: The end shape selection context is bad.";
118     setError(aContextError);
119     return;
120   }
121
122   // Verify planarity of faces and linearity of edges
123   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
124   for (int i = 0; i < 2; i++) {
125     if (!isShapeValid(aShapes[i])) {
126       return;
127     }
128   }
129
130   // Flags of the Placement
131   bool isReverse = boolean(REVERSE_ID())->value();
132   bool isCentering = boolean(CENTERING_ID())->value();
133
134   // Getting transformation.
135   GeomAlgoAPI_Placement aPlacementAlgo(
136     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
137   if(!aPlacementAlgo.isDone()) {
138     static const std::string aFeatureError = "Error: Placement algorithm failed.";
139     setError(aFeatureError);
140     return;
141   }
142   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
143
144   // Applying transformation to each object.
145   int aResultIndex = 0;
146   std::string anError;
147   std::list<ResultPtr>::iterator aContext = aContextes.begin();
148   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
149       anObjectsIt++, aContext++) {
150
151     // for part results just set transformation
152     if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
153       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
154       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
155       aResultPart->setTrsf(aContextRes, aTrsf);
156       setResult(aResultPart, aResultIndex);
157     } else {
158       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
159       std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(new GeomAlgoAPI_Transform(aBaseShape,
160                                                                                       aTrsf));
161
162       // Checking that the algorithm worked properly.
163       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
164         setError(anError);
165         break;
166       }
167
168       //LoadNamingDS
169       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
170       aResultBody->storeModified(aBaseShape, aTransformAlgo->shape());
171       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aTransformAlgo, "Placed");
172       setResult(aResultBody, aResultIndex);
173     }
174     aResultIndex++;
175   }
176
177   // Remove the rest results if there were produced in the previous pass.
178   removeResults(aResultIndex);
179 }
180
181 //==================================================================================================
182 bool FeaturesPlugin_Placement::isShapeValid(GeomShapePtr theShape)
183 {
184   if (theShape->isCompound()) {
185     GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
186     for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
187       GeomShapePtr aCurrentShape = anIt.current();
188       if (aShapeType == GeomAPI_Shape::SHAPE) {
189         aShapeType = aCurrentShape->shapeType();
190       }
191       else if (aShapeType != aCurrentShape->shapeType()) {
192         static const std::string aLinearityError =
193           "Error: Selected compound contains shapes with different types.";
194         setError(aLinearityError);
195         return false;
196       }
197
198       if (!isShapeValid(aCurrentShape)) {
199         return false;
200       }
201     }
202   }
203   else if (theShape->isFace()) {
204     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
205     if (!aFace->isPlanar()) {
206       static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
207       setError(aPlanarityError);
208       return false;
209     }
210   }
211   else if (theShape->isEdge()) {
212     std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
213     if (!anEdge->isLine()) {
214       static const std::string aLinearityError = "Error: One of selected edges is not linear.";
215       setError(aLinearityError);
216       return false;
217     }
218   }
219
220   return true;
221 }