Salome HOME
Partition namig fix
[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   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
80
81   // Getting tools.
82   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
83   for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
84     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
85     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
86     if(!aTool.get()) {
87       // it could be a construction plane
88       ResultPtr aContext = aToolAttr->context();
89       if(aContext.get()) {
90         aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aContext->shape(), aBoundingPoints);
91         std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
92         aMkShCustom->addModified(aContext->shape(), aTool);
93         aMakeShapeList.append(aMkShCustom);
94         aTools.push_back(aTool);
95         aToolsForNaming.push_back(aContext->shape());
96       }
97     } else {
98       aTools.push_back(aTool);
99       aToolsForNaming.push_back(aTool);
100     }
101   }
102
103   // Getting combine flag.
104   bool isCombine = boolean(COMBINE_ID())->value();
105
106   if(anObjects.empty()/* || aTools.empty()*/) {
107     std::string aFeatureError = "Not enough objects for partition operation";
108     setError(aFeatureError);
109     return;
110   }
111
112   int aResultIndex = 0;
113
114   if(isCombine) {
115     // Create single result.
116     GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
117
118     // Checking that the algorithm worked properly.
119     if (!aPartitionAlgo.isDone()) {
120       static const std::string aFeatureError = "Partition algorithm failed";
121       setError(aFeatureError);
122       return;
123     }
124     if (aPartitionAlgo.shape()->isNull()) {
125       static const std::string aShapeError = "Resulting shape is Null";
126       setError(aShapeError);
127       return;
128     }
129     if (!aPartitionAlgo.isValid()) {
130       std::string aFeatureError = "Warning: resulting shape is not valid";
131       setError(aFeatureError);
132       return;
133     }
134
135     if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
136       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
137       aMakeShapeList.append(aPartitionAlgo.makeShape());
138       GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
139       loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
140       setResult(aResultBody, aResultIndex);
141       aResultIndex++;
142     }
143   } else {
144     // Create result for each object.
145     for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
146       std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
147       ListOfShape aListWithObject; aListWithObject.push_back(anObject);
148       GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
149
150       // Checking that the algorithm worked properly.
151       if (!aPartitionAlgo.isDone()) {
152         static const std::string aFeatureError = "Partition algorithm failed";
153         setError(aFeatureError);
154         return;
155       }
156       if (aPartitionAlgo.shape()->isNull()) {
157         static const std::string aShapeError = "Resulting shape is Null";
158         setError(aShapeError);
159         return;
160       }
161       if (!aPartitionAlgo.isValid()) {
162         std::string aFeatureError = "Warning: resulting shape is not valid";
163         setError(aFeatureError);
164         return;
165       }
166
167       if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
168         std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
169         aMakeShapeList.append(aPartitionAlgo.makeShape());
170         GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
171         loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
172         setResult(aResultBody, aResultIndex);
173         aResultIndex++;
174       }
175     }
176   }
177
178   // remove the rest results if there were produced in the previous pass
179   removeResults(aResultIndex);
180 }
181
182 //=================================================================================================
183 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
184                                             const std::shared_ptr<GeomAPI_Shape> theBaseShape,
185                                             const ListOfShape& theTools,
186                                             const std::shared_ptr<GeomAPI_Shape> theResultShape,
187                                             GeomAlgoAPI_MakeShape& theMakeShape,
188                                             GeomAPI_DataMapOfShapeShape& theMapOfShapes)
189 {
190   //load result
191   if(theBaseShape->isEqual(theResultShape)) {
192     theResultBody->store(theResultShape);
193   } else {
194     const int aModifyTag = 1;
195     const int aDeletedTag = 2;
196     const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
197
198     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
199
200     std::string aModName = "Modified";
201     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
202                                                aModifyTag, aModName, theMapOfShapes);
203     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
204
205     for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
206       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
207                                                  aModifyTag, aModName, theMapOfShapes);
208       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
209     }
210   }
211 }