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