Salome HOME
Issue #1860: fix end lines with spaces
[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(),
28                        ModelAPI_AttributeSelectionList::typeId());
29   data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
30                        ModelAPI_AttributeSelectionList::typeId());
31 }
32
33 //=================================================================================================
34 void FeaturesPlugin_Intersection::execute()
35 {
36   ListOfShape anObjects, aTools;
37
38   // Getting objects.
39   AttributeSelectionListPtr anObjectsSelList =
40     selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
41   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
42     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
43       anObjectsSelList->value(anObjectsIndex);
44     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
45     if (!anObject.get()) {
46       return;
47     }
48     anObjects.push_back(anObject);
49   }
50
51   // Getting tools.
52   AttributeSelectionListPtr aToolsSelList =
53     selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
54   for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
55     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
56     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
57     if (!aTool.get()) {
58       return;
59     }
60     aTools.push_back(aTool);
61   }
62
63   if(anObjects.empty() || aTools.empty()) {
64     setError("Error: Objects or tools are empty.");
65     return;
66   }
67
68   int aResultIndex = 0;
69
70   // Create result for each object.
71   for (ListOfShape::iterator
72        anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
73     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
74     ListOfShape aListWithObject; aListWithObject.push_back(anObject);
75     GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
76
77     // Checking that the algorithm worked properly.
78     if (!anIntersectionAlgo.isDone()) {
79       static const std::string aFeatureError = "Error: Intersection algorithm failed.";
80       setError(aFeatureError);
81       return;
82     }
83     if (anIntersectionAlgo.shape()->isNull()) {
84       static const std::string aShapeError = "Error: Resulting shape is Null.";
85       setError(aShapeError);
86       return;
87     }
88     if (!anIntersectionAlgo.isValid()) {
89       std::string aFeatureError = "Error: Resulting shape is not valid.";
90       setError(aFeatureError);
91       return;
92     }
93
94     std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
95     loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
96     setResult(aResultBody, aResultIndex);
97     aResultIndex++;
98   }
99
100   // remove the rest results if there were produced in the previous pass
101   removeResults(aResultIndex);
102 }
103
104 //=================================================================================================
105 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
106                                                const std::shared_ptr<GeomAPI_Shape> theBaseShape,
107                                                const ListOfShape& theTools,
108                                                GeomAlgoAPI_MakeShape& theMakeShape)
109 {
110   //load result
111   std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
112   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = theMakeShape.mapOfSubShapes();
113   const int aDeletedTag = 1;
114   /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
115   const int aSubsolidsTag = 2;
116   const int aModifyTag = 100000;
117   int aModifyToolsTag = 200000;
118   std::ostringstream aStream;
119
120   theResultBody->storeModified(theBaseShape, aResultShape, aSubsolidsTag);
121
122   std::string aModName = "Modified";
123   theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
124                                              aModifyTag, aModName, *aMapOfShapes.get(), true);
125   theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
126                                              aModifyTag, aModName, *aMapOfShapes.get(), true);
127   theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
128
129   int anIndex = 1;
130   for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
131     aStream.str(std::string());
132     aStream.clear();
133     aStream << aModName << "_" << anIndex++;
134     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
135                                    aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
136     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
137                                  aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
138     theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
139     aModifyToolsTag += 10000;
140   }
141 }