]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Intersection.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "FeaturesPlugin_Intersection.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_BodyBuilder.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27
28 #include <GeomAlgoAPI_Intersection.h>
29 #include <GeomAPI_ShapeExplorer.h>
30
31 #include <sstream>
32
33 //=================================================================================================
34 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
35 {
36 }
37
38 //=================================================================================================
39 void FeaturesPlugin_Intersection::initAttributes()
40 {
41   data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
42                        ModelAPI_AttributeSelectionList::typeId());
43   data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
44                        ModelAPI_AttributeSelectionList::typeId());
45 }
46
47 //=================================================================================================
48 void FeaturesPlugin_Intersection::execute()
49 {
50   ListOfShape anObjects, aTools;
51
52   // Getting objects.
53   AttributeSelectionListPtr anObjectsSelList =
54     selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
55   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
56     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
57       anObjectsSelList->value(anObjectsIndex);
58     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
59     if (!anObject.get()) {
60       return;
61     }
62     anObjects.push_back(anObject);
63   }
64
65   // Getting tools.
66   AttributeSelectionListPtr aToolsSelList =
67     selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
68   for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
69     std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
70     std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
71     if (!aTool.get()) {
72       return;
73     }
74     aTools.push_back(aTool);
75   }
76
77   if(anObjects.empty() || aTools.empty()) {
78     setError("Error: Objects or tools are empty.");
79     return;
80   }
81
82   int aResultIndex = 0;
83
84   // Create result for each object.
85   for (ListOfShape::iterator
86        anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
87     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
88     ListOfShape aListWithObject; aListWithObject.push_back(anObject);
89     GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
90
91     // Checking that the algorithm worked properly.
92     if (!anIntersectionAlgo.isDone()) {
93       static const std::string aFeatureError = "Error: Intersection algorithm failed.";
94       setError(aFeatureError);
95       return;
96     }
97     if (anIntersectionAlgo.shape()->isNull()) {
98       static const std::string aShapeError = "Error: Resulting shape is Null.";
99       setError(aShapeError);
100       return;
101     }
102     if (!anIntersectionAlgo.isValid()) {
103       std::string aFeatureError = "Error: Resulting shape is not valid.";
104       setError(aFeatureError);
105       return;
106     }
107
108     std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
109     loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
110     setResult(aResultBody, aResultIndex);
111     aResultIndex++;
112   }
113
114   // remove the rest results if there were produced in the previous pass
115   removeResults(aResultIndex);
116 }
117
118 //=================================================================================================
119 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
120                                                const std::shared_ptr<GeomAPI_Shape> theBaseShape,
121                                                const ListOfShape& theTools,
122                                                GeomAlgoAPI_MakeShape& theMakeShape)
123 {
124   std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
125   theResultBody->storeModified(theBaseShape, aResultShape);
126
127   const int aDeletedVertexTag = 1;
128   const int aDeletedEdgeTag   = 2;
129   const int aDeletedFaceTag   = 3;
130
131   theResultBody->loadDeletedShapes(&theMakeShape,
132                                    theBaseShape,
133                                    GeomAPI_Shape::VERTEX,
134                                    aDeletedVertexTag);
135   theResultBody->loadDeletedShapes(&theMakeShape,
136                                    theBaseShape,
137                                    GeomAPI_Shape::EDGE,
138                                    aDeletedEdgeTag);
139   theResultBody->loadDeletedShapes(&theMakeShape,
140                                    theBaseShape,
141                                    GeomAPI_Shape::FACE,
142                                    aDeletedFaceTag);
143
144   ListOfShape aShapes = theTools;
145   aShapes.push_back(theBaseShape);
146   GeomAPI_DataMapOfShapeShape aShapesMap; // Map to store {result_shape, original_shape}
147   const int aShapeTypesNb = 2;
148   const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] =
149     {GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE};
150   for(ListOfShape::const_iterator anIt = aShapes.cbegin(); anIt != aShapes.cend(); ++anIt) {
151     const GeomShapePtr aShape = *anIt;
152     for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
153       for(GeomAPI_ShapeExplorer anOrigShapeExp(aShape, aShapeTypes[anIndex]);
154           anOrigShapeExp.more();
155           anOrigShapeExp.next()) {
156         ListOfShape aHistory;
157         const GeomShapePtr aSubShape = anOrigShapeExp.current();
158         theMakeShape.modified(aSubShape, aHistory);
159         for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
160             aHistoryIt != aHistory.cend();
161             ++aHistoryIt) {
162           aShapesMap.bind(*aHistoryIt, aSubShape);
163         }
164       }
165     }
166   }
167
168   int aModifiedVertexIndex(1),
169       aGeneratedVertexIndex(1),
170       aModifiedEdgeIndex(1),
171       aGeneratedEdgeIndex(1);
172   int aTag = 4;
173   GeomAPI_DataMapOfShapeShape aStoredShapes;
174   for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
175     for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
176         aShapeExp.more();
177         aShapeExp.next()) {
178       const GeomShapePtr aSubShape = aShapeExp.current();
179       if(aStoredShapes.isBound(aSubShape)) {
180         continue;
181       }
182       if(aShapesMap.isBound(aSubShape)) {
183         theResultBody->modified(aShapesMap.find(aSubShape),
184           aSubShape,
185           std::string("Modified_")
186             + (anIndex == 0 ? "Vertex_" : "Edge_")
187             + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
188                                                       : aModifiedEdgeIndex++)),
189           aTag++);
190       } else {
191         theResultBody->generated(aSubShape,
192           std::string("Generated_")
193             + (anIndex == 0 ? "Vertex_" : "Edge_")
194             + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
195                                                       : aGeneratedEdgeIndex++)),
196           aTag++);
197       }
198       aStoredShapes.bind(aSubShape, aSubShape);
199     }
200   }
201 }