Salome HOME
GeomAlgoAPI_Prism now derived from GeomAlgoAPI_MakeSweep
[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 <sstream>
27
28 //=================================================================================================
29 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
30 {
31 }
32
33 //=================================================================================================
34 void FeaturesPlugin_Partition::initAttributes()
35 {
36   AttributeSelectionListPtr aSelection =
37     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38     FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39   aSelection->setSelectionType("SOLID");
40
41   aSelection =
42     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
43     FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
44   aSelection->setSelectionType("SOLID");
45
46   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
47
48   data()->addAttribute(COMBINE_ID(), ModelAPI_AttributeBoolean::typeId());
49 }
50
51 //=================================================================================================
52 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::string& theAttrName)
53 {
54   std::shared_ptr<ModelAPI_AttributeReference> aObjRef =
55       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(data()->attribute(theAttrName));
56   if (aObjRef) {
57     std::shared_ptr<ModelAPI_ResultBody> aConstr =
58         std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObjRef->value());
59     if (aConstr)
60       return aConstr->shape();
61   }
62   return std::shared_ptr<GeomAPI_Shape>();
63 }
64
65 //=================================================================================================
66 void FeaturesPlugin_Partition::execute()
67 {
68   ListOfShape anObjects, aTools, aToolsForNaming;
69
70   // Getting objects.
71   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
72   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
73     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
74     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
75     if (!anObject.get()) {
76       return;
77     }
78     anObjects.push_back(anObject);
79   }
80
81   GeomAlgoAPI_MakeShapeList aMakeShapeList;
82   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
83
84   // Getting tools.
85   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
86   for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
87     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
88     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
89     if(!aTool.get()) {
90       // it could be a construction plane
91       ResultPtr aContext = aToolAttr->context();
92       if(aContext.get()) {
93         aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aContext->shape(), aBoundingPoints);
94         std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
95         aMkShCustom->addModified(aContext->shape(), aTool);
96         aMakeShapeList.appendAlgo(aMkShCustom);
97         aTools.push_back(aTool);
98         aToolsForNaming.push_back(aContext->shape());
99       }
100     } else {
101       aTools.push_back(aTool);
102       aToolsForNaming.push_back(aTool);
103     }
104   }
105
106   // Getting combine flag.
107   bool isCombine = boolean(COMBINE_ID())->value();
108
109   if(anObjects.empty()/* || aTools.empty()*/) {
110     std::string aFeatureError = "Not enough objects for partition operation";
111     setError(aFeatureError);
112     return;
113   }
114
115   int aResultIndex = 0;
116
117   if(isCombine) {
118     // Create single result.
119     if(!aTools.empty()) {
120       // This is a workaround for naming. Passing compound of objects as argument instead each object separately.
121       std::shared_ptr<GeomAPI_Shape> aCompoud = GeomAlgoAPI_CompoundBuilder::compound(anObjects);
122       anObjects.clear();
123       anObjects.push_back(aCompoud);
124     }
125     GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
126
127     // Checking that the algorithm worked properly.
128     if (!aPartitionAlgo.isDone()) {
129       static const std::string aFeatureError = "Partition algorithm failed";
130       setError(aFeatureError);
131       return;
132     }
133     if (aPartitionAlgo.shape()->isNull()) {
134       static const std::string aShapeError = "Resulting shape is Null";
135       setError(aShapeError);
136       return;
137     }
138     if (!aPartitionAlgo.isValid()) {
139       std::string aFeatureError = "Warning: resulting shape is not valid";
140       setError(aFeatureError);
141       return;
142     }
143
144     if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
145       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
146       aMakeShapeList.appendAlgo(aPartitionAlgo.makeShape());
147       GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
148       loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
149       setResult(aResultBody, aResultIndex);
150       aResultIndex++;
151     }
152   } else {
153     // Create result for each object.
154     for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
155       std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
156       ListOfShape aListWithObject; aListWithObject.push_back(anObject);
157       GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
158
159       // Checking that the algorithm worked properly.
160       if (!aPartitionAlgo.isDone()) {
161         static const std::string aFeatureError = "Partition algorithm failed";
162         setError(aFeatureError);
163         return;
164       }
165       if (aPartitionAlgo.shape()->isNull()) {
166         static const std::string aShapeError = "Resulting shape is Null";
167         setError(aShapeError);
168         return;
169       }
170       if (!aPartitionAlgo.isValid()) {
171         std::string aFeatureError = "Warning: resulting shape is not valid";
172         setError(aFeatureError);
173         return;
174       }
175
176       if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
177         std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
178         aMakeShapeList.appendAlgo(aPartitionAlgo.makeShape());
179         GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
180         loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
181         setResult(aResultBody, aResultIndex);
182         aResultIndex++;
183       }
184     }
185   }
186
187   // remove the rest results if there were produced in the previous pass
188   removeResults(aResultIndex);
189 }
190
191 //=================================================================================================
192 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
193                                             const std::shared_ptr<GeomAPI_Shape> theBaseShape,
194                                             const ListOfShape& theTools,
195                                             const std::shared_ptr<GeomAPI_Shape> theResultShape,
196                                             GeomAlgoAPI_MakeShape& theMakeShape,
197                                             GeomAPI_DataMapOfShapeShape& theMapOfShapes)
198 {
199   //load result
200   if(theBaseShape->isEqual(theResultShape)) {
201     theResultBody->store(theResultShape);
202   } else {
203     const int aDeletedTag = 1;
204     const int aSubsolidsTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
205     const int aModifyTag = 100000;
206     int aModifyToolsTag = 200000;
207     std::ostringstream aStream;
208
209     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
210
211     std::string aModName = "Modified";
212     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
213                                                aModifyTag, aModName, theMapOfShapes, true);
214     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
215
216     int anIndex = 1;
217     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
218       aStream.str(std::string());
219       aStream.clear();
220       aStream << aModName << "_" << anIndex++;
221       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
222                                                  aModifyToolsTag, aStream.str(), theMapOfShapes, true);
223       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
224       aModifyToolsTag += 10000;
225     }
226   }
227 }