Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "FeaturesPlugin_Placement.h"
21
22 #include <ModelAPI_ResultConstruction.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_BodyBuilder.h>
29
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAlgoAPI_Placement.h>
34 #include <GeomAlgoAPI_Transform.h>
35
36 #include <FeaturesPlugin_Tools.h>
37
38 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
39 {
40 }
41
42 void FeaturesPlugin_Placement::initAttributes()
43 {
44
45   AttributeSelectionListPtr aSelection =
46     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
47     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
48
49   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
50   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
52   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
53 }
54
55 void FeaturesPlugin_Placement::execute()
56 {
57   // Getting objects.
58   ListOfShape anObjects;
59   std::list<ResultPtr> aContextes;
60   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
61   if(anObjectsSelList->size() == 0) {
62     return;
63   }
64   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
65     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
66       anObjectsSelList->value(anObjectsIndex);
67     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
68     if(!anObject.get()) { // may be for not-activated parts
69       eraseResults();
70       return;
71     }
72     anObjects.push_back(anObject);
73     aContextes.push_back(anObjectAttr->context());
74   }
75
76   // Verify the start shape
77   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
78   if(!anObjRef) {
79     return;
80   }
81   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
82   if(!aStartShape) {
83     static const std::string aSelectionError = "Error: The start shape selection is bad.";
84     setError(aSelectionError);
85     return;
86   }
87
88
89   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
90   ResultPtr aContextRes = anObjRef->context();
91   if (aContextRes.get()) {
92     aStartShapeContext = aContextRes->shape();
93   }
94   if(!aStartShapeContext.get()) {
95     static const std::string aContextError = "Error: The start shape selection context is bad.";
96     setError(aContextError);
97     return;
98   }
99
100   // Verify the end shape
101   anObjRef = selection(END_SHAPE_ID());
102   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
103   if(!anEndShape) {
104     static const std::string aSelectionError = "Error: The end shape selection is bad.";
105     setError(aSelectionError);
106     return;
107   }
108
109   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
110   aContextRes = anObjRef->context();
111   if(aContextRes.get()) {
112     anEndShapeContext = aContextRes->shape();
113   }
114   if(!anEndShapeContext.get()) {
115     static const std::string aContextError = "Error: The end shape selection context is bad.";
116     setError(aContextError);
117     return;
118   }
119
120   // Verify planarity of faces and linearity of edges
121   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
122   for (int i = 0; i < 2; i++) {
123     if (aShapes[i]->isFace()) {
124       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
125       if (!aFace->isPlanar()) {
126         static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
127         setError(aPlanarityError);
128         return;
129       }
130     }
131     else if (aShapes[i]->isEdge()) {
132       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
133       if (!anEdge->isLine()) {
134         static const std::string aLinearityError = "Error: One of selected endges is not linear.";
135         setError(aLinearityError);
136         return;
137       }
138     }
139   }
140
141   // Flags of the Placement
142   bool isReverse = boolean(REVERSE_ID())->value();
143   bool isCentering = boolean(CENTERING_ID())->value();
144
145   // Getting transformation.
146   GeomAlgoAPI_Placement aPlacementAlgo(
147     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
148   if(!aPlacementAlgo.isDone()) {
149     static const std::string aFeatureError = "Error: Placement algorithm failed.";
150     setError(aFeatureError);
151     return;
152   }
153   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
154
155   // Applying transformation to each object.
156   int aResultIndex = 0;
157   std::list<ResultPtr>::iterator aContext = aContextes.begin();
158   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
159       anObjectsIt++, aContext++) {
160
161     // for part results just set transformation
162     if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) {
163       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
164       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
165       aResultPart->setTrsf(aContextRes, aTrsf);
166       setResult(aResultPart, aResultIndex);
167     } else {
168       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
169       GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
170
171       // Checking that the algorithm worked properly.
172       if(!aTransformAlgo.isDone()) {
173         static const std::string aFeatureError = "Error: Transform algorithm failed.";
174         setError(aFeatureError);
175         break;
176       }
177       if(aTransformAlgo.shape()->isNull()) {
178         static const std::string aShapeError = "Error: Resulting shape is Null.";
179         setError(aShapeError);
180         break;
181       }
182       if(!aTransformAlgo.isValid()) {
183         std::string aFeatureError = "Error: Resulting shape is not valid.";
184         setError(aFeatureError);
185         break;
186       }
187
188       //LoadNamingDS
189       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
190         document()->createBody(data(), aResultIndex);
191       loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
192       setResult(aResultBody, aResultIndex);
193     }
194     aResultIndex++;
195   }
196
197   // Remove the rest results if there were produced in the previous pass.
198   removeResults(aResultIndex);
199 }
200
201 //============================================================================
202 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
203                                             std::shared_ptr<ModelAPI_ResultBody> theResultBody,
204                                             std::shared_ptr<GeomAPI_Shape> theBaseShape)
205 {
206   //load result
207   theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
208
209   std::string aPlacedName = "Placed";
210   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
211
212   FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
213                                             theBaseShape, 1, 2, 3, aPlacedName,
214                                             *aSubShapes.get());
215 }