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