]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Placement.cpp
Salome HOME
Update copyrights
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // Copyright (C) 2014-2019  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
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 <GeomAPI_ShapeIterator.h>
34 #include <GeomAlgoAPI_Placement.h>
35 #include <GeomAlgoAPI_Transform.h>
36 #include <GeomAlgoAPI_Tools.h>
37
38 #include <FeaturesPlugin_Tools.h>
39
40 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
41 {
42 }
43
44 void FeaturesPlugin_Placement::initAttributes()
45 {
46
47   AttributeSelectionListPtr aSelection =
48     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
49     OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
50
51   data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
52   data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
53   data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
54   data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
55 }
56
57 void FeaturesPlugin_Placement::execute()
58 {
59   // Getting objects.
60   ListOfShape anObjects;
61   std::list<ResultPtr> aContextes;
62   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
63   if(anObjectsSelList->size() == 0) {
64     return;
65   }
66   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
67     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
68       anObjectsSelList->value(anObjectsIndex);
69     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
70     if(!anObject.get()) { // may be for not-activated parts
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 (!isShapeValid(aShapes[i])) {
125       return;
126     }
127   }
128
129   // Flags of the Placement
130   bool isReverse = boolean(REVERSE_ID())->value();
131   bool isCentering = boolean(CENTERING_ID())->value();
132
133   // Getting transformation.
134   GeomAlgoAPI_Placement aPlacementAlgo(
135     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
136   if(!aPlacementAlgo.isDone()) {
137     static const std::string aFeatureError = "Error: Placement algorithm failed.";
138     setError(aFeatureError);
139     return;
140   }
141   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
142
143   // Applying transformation to each object.
144   int aResultIndex = 0;
145   std::string anError;
146   std::list<ResultPtr>::iterator aContext = aContextes.begin();
147   for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
148       anObjectsIt++, aContext++) {
149
150     // for part results just set transformation
151     if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
152       ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153       ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154       aResultPart->setTrsf(aContextRes, aTrsf);
155       setResult(aResultPart, aResultIndex);
156     } else {
157       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
158       std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(new GeomAlgoAPI_Transform(aBaseShape,
159                                                                                       aTrsf));
160
161       // Checking that the algorithm worked properly.
162       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
163         setError(anError);
164         break;
165       }
166
167       //LoadNamingDS
168       ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
169       aResultBody->storeModified(aBaseShape, aTransformAlgo->shape());
170       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShape, aTransformAlgo, "Placed");
171       setResult(aResultBody, aResultIndex);
172     }
173     aResultIndex++;
174   }
175
176   // Remove the rest results if there were produced in the previous pass.
177   removeResults(aResultIndex);
178 }
179
180 //==================================================================================================
181 bool FeaturesPlugin_Placement::isShapeValid(GeomShapePtr theShape)
182 {
183   if (theShape->isCompound()) {
184     GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
185     for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
186       GeomShapePtr aCurrentShape = anIt.current();
187       if (aShapeType == GeomAPI_Shape::SHAPE) {
188         aShapeType = aCurrentShape->shapeType();
189       }
190       else if (aShapeType != aCurrentShape->shapeType()) {
191         static const std::string aLinearityError =
192           "Error: Selected compound contains shapes with different types.";
193         setError(aLinearityError);
194         return false;
195       }
196
197       if (!isShapeValid(aCurrentShape)) {
198         return false;
199       }
200     }
201   }
202   else if (theShape->isFace()) {
203     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
204     if (!aFace->isPlanar()) {
205       static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
206       setError(aPlanarityError);
207       return false;
208     }
209   }
210   else if (theShape->isEdge()) {
211     std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
212     if (!anEdge->isLine()) {
213       static const std::string aLinearityError = "Error: One of selected edges is not linear.";
214       setError(aLinearityError);
215       return false;
216     }
217   }
218
219   return true;
220 }