Salome HOME
Issue 1299 Angle presentation: complementary angle.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Intersection.cpp
4 // Created:     15 Feb 2016
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_Intersection.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14
15 #include <GeomAlgoAPI_Intersection.h>
16
17 #include <sstream>
18
19 //=================================================================================================
20 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
21 {
22 }
23
24 //=================================================================================================
25 void FeaturesPlugin_Intersection::initAttributes()
26 {
27   data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
28   data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
29 }
30
31 //=================================================================================================
32 void FeaturesPlugin_Intersection::execute()
33 {
34   ListOfShape anObjects, aTools;
35
36   // Getting objects.
37   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
38   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
39     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
40     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
41     if (!anObject.get()) {
42       return;
43     }
44     anObjects.push_back(anObject);
45   }
46
47   // Getting tools.
48   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
49   for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
50     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
51     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
52     if (!aTool.get()) {
53       return;
54     }
55     aTools.push_back(aTool);
56   }
57
58   if(anObjects.empty() || aTools.empty()) {
59     setError("Error: Objects or tools are empty.");
60     return;
61   }
62
63   int aResultIndex = 0;
64
65   // Create result for each object.
66   for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
67     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
68     ListOfShape aListWithObject; aListWithObject.push_back(anObject);
69     GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
70
71     // Checking that the algorithm worked properly.
72     if (!anIntersectionAlgo.isDone()) {
73       static const std::string aFeatureError = "Error: Intersection algorithm failed.";
74       setError(aFeatureError);
75       return;
76     }
77     if (anIntersectionAlgo.shape()->isNull()) {
78       static const std::string aShapeError = "Error: Resulting shape is Null.";
79       setError(aShapeError);
80       return;
81     }
82     if (!anIntersectionAlgo.isValid()) {
83       std::string aFeatureError = "Error: Resulting shape is not valid.";
84       setError(aFeatureError);
85       return;
86     }
87
88     std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
89     loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
90     setResult(aResultBody, aResultIndex);
91     aResultIndex++;
92   }
93
94   // remove the rest results if there were produced in the previous pass
95   removeResults(aResultIndex);
96 }
97
98 //=================================================================================================
99 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
100                                                const std::shared_ptr<GeomAPI_Shape> theBaseShape,
101                                                const ListOfShape& theTools,
102                                                GeomAlgoAPI_MakeShape& theMakeShape)
103 {
104   //load result
105   std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
106   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = theMakeShape.mapOfSubShapes();
107   const int aDeletedTag = 1;
108   const int aSubsolidsTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
109   const int aModifyTag = 100000;
110   int aModifyToolsTag = 200000;
111   std::ostringstream aStream;
112
113   theResultBody->storeModified(theBaseShape, aResultShape, aSubsolidsTag);
114
115   std::string aModName = "Modified";
116   theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
117                                              aModifyTag, aModName, *aMapOfShapes.get(), true);
118   theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
119                                              aModifyTag, aModName, *aMapOfShapes.get(), true);
120   theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
121
122   int anIndex = 1;
123   for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
124     aStream.str(std::string());
125     aStream.clear();
126     aStream << aModName << "_" << anIndex++;
127     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
128                                                aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
129     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
130                                                aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
131     theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
132     aModifyToolsTag += 10000;
133   }
134 }