Salome HOME
e421c55f42cee3d451f69cbfcc70cd91d117679a
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Intersection.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28
29 #include <GeomAlgoAPI_Intersection.h>
30 #include <GeomAPI_ShapeExplorer.h>
31
32 #include <sstream>
33
34 //=================================================================================================
35 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
36 {
37 }
38
39 //=================================================================================================
40 void FeaturesPlugin_Intersection::initAttributes()
41 {
42   data()->addAttribute(OBJECT_LIST_ID(),
43                        ModelAPI_AttributeSelectionList::typeId());
44 }
45
46 //=================================================================================================
47 void FeaturesPlugin_Intersection::execute()
48 {
49   ListOfShape anObjects;
50
51   // Getting objects.
52   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
53   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
55       anObjectsSelList->value(anObjectsIndex);
56     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
57     if (!anObject.get()) {
58       return;
59     }
60     anObjects.push_back(anObject);
61   }
62
63   if(anObjects.empty()) {
64     setError("Error: Objects or tools are empty.");
65     return;
66   }
67
68   int aResultIndex = 0;
69
70   // Create result.
71   GeomAlgoAPI_Intersection anIntersectionAlgo(anObjects);
72
73   // Checking that the algorithm worked properly.
74   if (!anIntersectionAlgo.isDone()) {
75     static const std::string aFeatureError = "Error: Intersection algorithm failed.";
76     setError(aFeatureError);
77     return;
78   }
79   if (anIntersectionAlgo.shape()->isNull()) {
80     static const std::string aShapeError = "Error: Resulting shape is Null.";
81     setError(aShapeError);
82     return;
83   }
84   if (!anIntersectionAlgo.isValid()) {
85     std::string aFeatureError = "Error: Resulting shape is not valid.";
86     setError(aFeatureError);
87     return;
88   }
89
90   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
91   loadNamingDS(aResultBody, anObjects, anIntersectionAlgo);
92   setResult(aResultBody, aResultIndex);
93   aResultIndex++;
94
95
96   // remove the rest results if there were produced in the previous pass
97   removeResults(aResultIndex);
98 }
99
100 //=================================================================================================
101 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
102                                                const ListOfShape& theObjects,
103                                                GeomAlgoAPI_MakeShape& theMakeShape)
104 {
105   std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
106   theResultBody->storeModified(theObjects.front(), aResultShape);
107
108   GeomAPI_DataMapOfShapeShape aShapesMap; // Map to store {result_shape, original_shape}
109   const int aShapeTypesNb = 2;
110   const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] =
111     {GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE};
112   for (ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
113     const GeomShapePtr aShape = *anIt;
114     for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
115       for(GeomAPI_ShapeExplorer anOrigShapeExp(aShape, aShapeTypes[anIndex]);
116           anOrigShapeExp.more();
117           anOrigShapeExp.next()) {
118         ListOfShape aHistory;
119         const GeomShapePtr aSubShape = anOrigShapeExp.current();
120         theMakeShape.modified(aSubShape, aHistory);
121         for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
122             aHistoryIt != aHistory.cend();
123             ++aHistoryIt) {
124           aShapesMap.bind(*aHistoryIt, aSubShape);
125         }
126       }
127     }
128   }
129
130   int aModifiedVertexIndex(1),
131       aGeneratedVertexIndex(1),
132       aModifiedEdgeIndex(1),
133       aGeneratedEdgeIndex(1);
134   int aTag = 4;
135   GeomAPI_DataMapOfShapeShape aStoredShapes;
136   for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
137     for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
138         aShapeExp.more();
139         aShapeExp.next()) {
140       const GeomShapePtr aSubShape = aShapeExp.current();
141       if(aStoredShapes.isBound(aSubShape)) {
142         continue;
143       }
144       if(aShapesMap.isBound(aSubShape)) {
145         theResultBody->modified(aShapesMap.find(aSubShape),
146           aSubShape,
147           std::string("Modified_")
148             + (anIndex == 0 ? "Vertex_" : "Edge_")
149             + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
150                                                       : aModifiedEdgeIndex++)),
151           aTag++);
152       } else {
153         theResultBody->generated(
154           aSubShape,
155           std::string("Generated_")
156             + (anIndex == 0 ? "Vertex_" : "Edge_")
157             + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
158                                                       : aGeneratedEdgeIndex++)));
159       }
160       aStoredShapes.bind(aSubShape, aSubShape);
161     }
162   }
163 }