Salome HOME
bos #26449: SHAPER: save imported images
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Placement.cpp
1 // Copyright (C) 2014-2021  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   ResultPtr aTextureSource;
75   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
76   if (!FeaturesPlugin_Tools::shapesFromSelectionList
77       (anObjectsSelList, isKeepSubShapes, anObjects, aParts, aTextureSource))
78     return;
79
80   // Verify the start shape
81   AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
82   if(!anObjRef) {
83     return;
84   }
85   std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
86   if(!aStartShape) {
87     static const std::string aSelectionError = "Error: The start shape selection is bad.";
88     setError(aSelectionError);
89     return;
90   }
91
92
93   std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
94   ResultPtr aContextRes = anObjRef->context();
95   if (aContextRes.get()) {
96     aStartShapeContext = aContextRes->shape();
97   }
98   if(!aStartShapeContext.get()) {
99     static const std::string aContextError = "Error: The start shape selection context is bad.";
100     setError(aContextError);
101     return;
102   }
103
104   // Verify the end shape
105   anObjRef = selection(END_SHAPE_ID());
106   std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
107   if(!anEndShape) {
108     static const std::string aSelectionError = "Error: The end shape selection is bad.";
109     setError(aSelectionError);
110     return;
111   }
112
113   std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
114   aContextRes = anObjRef->context();
115   if(aContextRes.get()) {
116     anEndShapeContext = aContextRes->shape();
117   }
118   if(!anEndShapeContext.get()) {
119     static const std::string aContextError = "Error: The end shape selection context is bad.";
120     setError(aContextError);
121     return;
122   }
123
124   // Verify planarity of faces and linearity of edges
125   std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
126   for (int i = 0; i < 2; i++) {
127     if (!isShapeValid(aShapes[i])) {
128       return;
129     }
130   }
131
132   // Flags of the Placement
133   bool isReverse = boolean(REVERSE_ID())->value();
134   bool isCentering = boolean(CENTERING_ID())->value();
135
136   // Getting transformation.
137   GeomAlgoAPI_Placement aPlacementAlgo(
138     aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
139   if(!aPlacementAlgo.isDone()) {
140     static const std::string aFeatureError = "Error: Placement algorithm failed.";
141     setError(aFeatureError);
142     return;
143   }
144   std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
145
146   int aResultIndex = 0;
147   std::string anError;
148   for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
149     // Applying transformation to each part.
150     ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
151     ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
152     aResultPart->setTrsf(aContextRes, aTrsf);
153     setResult(aResultPart, aResultIndex++);
154   }
155
156   // Collect transformations for each object.
157   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
158
159   for(GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
160       anObjectsIt != anObjects.end(); anObjectsIt++) {
161     std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
162     std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(
163         new GeomAlgoAPI_Transform(aBaseShape, aTrsf));
164
165     // Checking that the algorithm worked properly.
166     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
167       setError(anError);
168       break;
169     }
170
171     anObjects.markModified(aBaseShape, aTransformAlgo->shape());
172     aMakeShapeList->appendAlgo(aTransformAlgo);
173   }
174
175   // Build results of the operation.
176   const ListOfShape& anOriginalShapes = anObjects.objects();
177   ListOfShape aTopLevel;
178   anObjects.topLevelObjects(aTopLevel);
179   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
180     //LoadNamingDS
181     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
182     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
183                                              aMakeShapeList, *anIt, "Placed");
184     // Copy image data, if any
185     ModelAPI_Tools::copyImageAttribute(aTextureSource, aResultBody);
186     setResult(aResultBody, aResultIndex++);
187   }
188
189   // Remove the rest results if there were produced in the previous pass.
190   removeResults(aResultIndex);
191 }
192
193 //==================================================================================================
194 bool FeaturesPlugin_Placement::isShapeValid(GeomShapePtr theShape)
195 {
196   if (theShape->isCompound()) {
197     GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
198     for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
199       GeomShapePtr aCurrentShape = anIt.current();
200       if (aShapeType == GeomAPI_Shape::SHAPE) {
201         aShapeType = aCurrentShape->shapeType();
202       }
203       else if (aShapeType != aCurrentShape->shapeType()) {
204         static const std::string aLinearityError =
205           "Error: Selected compound contains shapes with different types.";
206         setError(aLinearityError);
207         return false;
208       }
209
210       if (!isShapeValid(aCurrentShape)) {
211         return false;
212       }
213     }
214   }
215   else if (theShape->isFace()) {
216     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
217     if (!aFace->isPlanar()) {
218       static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
219       setError(aPlanarityError);
220       return false;
221     }
222   }
223   else if (theShape->isEdge()) {
224     std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
225     if (!anEdge->isLine()) {
226       static const std::string aLinearityError = "Error: One of selected edges is not linear.";
227       setError(aLinearityError);
228       return false;
229     }
230   }
231
232   return true;
233 }