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