Salome HOME
Issue #1837: make all files with UNIX end lines
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Partition.cpp
4 // Created:     31 Jul 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include "FeaturesPlugin_Partition.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeBoolean.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_BodyBuilder.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
19
20 #include <GeomAlgoAPI_CompoundBuilder.h>
21 #include <GeomAlgoAPI_Partition.h>
22 #include <GeomAlgoAPI_MakeShapeCustom.h>
23 #include <GeomAlgoAPI_MakeShapeList.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
29
30 #include <iostream>
31 #include <sstream>
32
33 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
34                              const GeomShapePtr theResultShape,
35                              const GeomAPI_Shape::ShapeType theShapeType,
36                              const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
37
38 //=================================================================================================
39 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
40 {
41 }
42
43 //=================================================================================================
44 void FeaturesPlugin_Partition::initAttributes()
45 {
46   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
47 }
48
49 //=================================================================================================
50 void FeaturesPlugin_Partition::execute()
51 {
52   ListOfShape anObjects, aPlanes;
53
54   // Getting objects.
55   AttributeSelectionListPtr anObjectsSelList = selectionList(BASE_OBJECTS_ID());
56   for(int anIndex = 0; anIndex < anObjectsSelList->size(); ++anIndex) {
57     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anIndex);
58     GeomShapePtr anObject = anObjectAttr->value();
59     if(!anObject.get()) {
60       // It could be a construction plane.
61       ResultPtr aContext = anObjectAttr->context();
62       aPlanes.push_back(anObjectAttr->context()->shape());
63     } else {
64       anObjects.push_back(anObject);
65     }
66   }
67
68   if(anObjects.empty()) {
69     static const std::string aFeatureError = "Error: No objects for partition.";
70     setError(aFeatureError);
71     return;
72   }
73
74   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
75
76   // Resize planes.
77   ListOfShape aTools;
78   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
79   for(ListOfShape::const_iterator anIt = aPlanes.cbegin(); anIt != aPlanes.cend(); ++anIt) {
80     GeomShapePtr aPlane = *anIt;
81     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
82     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
83     aMkShCustom->addModified(aPlane, aTool);
84     aMakeShapeList->appendAlgo(aMkShCustom);
85     aTools.push_back(aTool);
86   }
87
88   // Create single result.
89   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools));
90
91   // Checking that the algorithm worked properly.
92   if (!aPartitionAlgo->isDone()) {
93     static const std::string aFeatureError = "Error: Partition algorithm failed.";
94     setError(aFeatureError);
95     return;
96   }
97   if (aPartitionAlgo->shape()->isNull()) {
98     static const std::string aShapeError = "Error: Resulting shape is Null.";
99     setError(aShapeError);
100     return;
101   }
102   if (!aPartitionAlgo->isValid()) {
103     std::string aFeatureError = "Error: Resulting shape is not valid.";
104     setError(aFeatureError);
105     return;
106   }
107   aMakeShapeList->appendAlgo(aPartitionAlgo);
108   GeomShapePtr aResultShape = aPartitionAlgo->shape();
109
110   int aResultIndex = 0;
111   anObjects.insert(anObjects.end(), aPlanes.begin(), aPlanes.end());
112   if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
113     for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
114       storeResult(anObjects, anIt.current(), aMakeShapeList, aResultIndex);
115       ++aResultIndex;
116     }
117   } else {
118     storeResult(anObjects, aResultShape, aMakeShapeList, aResultIndex);
119     ++aResultIndex;
120   }
121
122   // Remove the rest results if there were produced in the previous pass.
123   removeResults(aResultIndex);
124 }
125
126 //=================================================================================================
127 void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects,
128                                            const GeomShapePtr theResultShape,
129                                            const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
130                                            const int theIndex)
131 {
132   // Find base.
133   GeomShapePtr aBaseShape;
134   for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
135     GeomShapePtr anObjectShape = *anIt;
136     aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
137     if(!aBaseShape.get()) {
138       aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
139     }
140     if(!aBaseShape.get()) {
141       aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
142     }
143     if(aBaseShape.get()) {
144       break;
145     }
146   }
147
148   // Create result body.
149   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
150
151   // Store modified shape.
152   if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
153     aResultBody->store(theResultShape);
154     setResult(aResultBody, theIndex);
155     return;
156   }
157
158   const int aDelTag = 1;
159   const int aSubTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
160   int aModTag = aSubTag + 10000;
161   const std::string aModName = "Modified";
162
163   aResultBody->storeModified(aBaseShape, theResultShape, aSubTag);
164
165   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
166   int anIndex = 1;
167   for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
168     GeomShapePtr aShape = *anIt;
169     std::string aModEdgeName = aModName + "_Edge_" + std::to_string((long long)anIndex);
170     std::string aModFaceName = aModName + "_Face_" + std::to_string((long long)anIndex++);
171     aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE,
172                                              aModTag, aModEdgeName, *aMapOfSubShapes.get(), true);
173     aModTag += 10000;
174     aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE,
175                                              aModTag, aModFaceName, *aMapOfSubShapes.get(), true);
176     aModTag += 10000;
177     aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE, aDelTag);
178     aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, aDelTag);
179   }
180
181   setResult(aResultBody, theIndex);
182 }
183
184
185 //=================================================================================================
186 GeomShapePtr findBase(const GeomShapePtr theObjectShape,
187                       const GeomShapePtr theResultShape,
188                       const GeomAPI_Shape::ShapeType theShapeType,
189                       const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
190 {
191   GeomShapePtr aBaseShape;
192   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
193   for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
194       anObjectSubShapesExp.more();
195       anObjectSubShapesExp.next()) {
196     GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
197     ListOfShape aModifiedShapes;
198     theMakeShape->modified(anObjectSubShape, aModifiedShapes);
199     for(ListOfShape::const_iterator aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
200       GeomShapePtr aModShape = *aModIt;
201       if(aMapOfSubShapes->isBound(aModShape)) {
202         aModShape = aMapOfSubShapes->find(aModShape);
203       }
204       if(theResultShape->isSubShape(aModShape)) {
205         aBaseShape = theObjectShape;
206         break;
207       }
208     }
209     if(aBaseShape.get()) {
210       break;
211     }
212   }
213
214   return aBaseShape;
215 }