1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Intersection.h"
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>
29 #include <GeomAlgoAPI_Intersection.h>
30 #include <GeomAPI_ShapeExplorer.h>
34 //=================================================================================================
35 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
39 //=================================================================================================
40 void FeaturesPlugin_Intersection::initAttributes()
42 data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
43 ModelAPI_AttributeSelectionList::typeId());
44 data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
45 ModelAPI_AttributeSelectionList::typeId());
48 //=================================================================================================
49 void FeaturesPlugin_Intersection::execute()
51 ListOfShape anObjects, aTools;
54 AttributeSelectionListPtr anObjectsSelList =
55 selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
56 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
57 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
58 anObjectsSelList->value(anObjectsIndex);
59 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
60 if (!anObject.get()) {
63 anObjects.push_back(anObject);
67 AttributeSelectionListPtr aToolsSelList =
68 selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
69 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
70 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
71 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
75 aTools.push_back(aTool);
78 if(anObjects.empty() || aTools.empty()) {
79 setError("Error: Objects or tools are empty.");
85 // Create result for each object.
86 for (ListOfShape::iterator
87 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
88 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
89 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
90 GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
92 // Checking that the algorithm worked properly.
93 if (!anIntersectionAlgo.isDone()) {
94 static const std::string aFeatureError = "Error: Intersection algorithm failed.";
95 setError(aFeatureError);
98 if (anIntersectionAlgo.shape()->isNull()) {
99 static const std::string aShapeError = "Error: Resulting shape is Null.";
100 setError(aShapeError);
103 if (!anIntersectionAlgo.isValid()) {
104 std::string aFeatureError = "Error: Resulting shape is not valid.";
105 setError(aFeatureError);
109 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
110 loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
111 setResult(aResultBody, aResultIndex);
115 // remove the rest results if there were produced in the previous pass
116 removeResults(aResultIndex);
119 //=================================================================================================
120 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
121 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
122 const ListOfShape& theTools,
123 GeomAlgoAPI_MakeShape& theMakeShape)
125 std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
126 theResultBody->storeModified(theBaseShape, aResultShape);
128 const int aDeletedVertexTag = 1;
129 const int aDeletedEdgeTag = 2;
130 const int aDeletedFaceTag = 3;
132 theResultBody->loadDeletedShapes(&theMakeShape,
134 GeomAPI_Shape::VERTEX,
136 theResultBody->loadDeletedShapes(&theMakeShape,
140 theResultBody->loadDeletedShapes(&theMakeShape,
145 ListOfShape aShapes = theTools;
146 aShapes.push_back(theBaseShape);
147 GeomAPI_DataMapOfShapeShape aShapesMap; // Map to store {result_shape, original_shape}
148 const int aShapeTypesNb = 2;
149 const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] =
150 {GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE};
151 for(ListOfShape::const_iterator anIt = aShapes.cbegin(); anIt != aShapes.cend(); ++anIt) {
152 const GeomShapePtr aShape = *anIt;
153 for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
154 for(GeomAPI_ShapeExplorer anOrigShapeExp(aShape, aShapeTypes[anIndex]);
155 anOrigShapeExp.more();
156 anOrigShapeExp.next()) {
157 ListOfShape aHistory;
158 const GeomShapePtr aSubShape = anOrigShapeExp.current();
159 theMakeShape.modified(aSubShape, aHistory);
160 for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
161 aHistoryIt != aHistory.cend();
163 aShapesMap.bind(*aHistoryIt, aSubShape);
169 int aModifiedVertexIndex(1),
170 aGeneratedVertexIndex(1),
171 aModifiedEdgeIndex(1),
172 aGeneratedEdgeIndex(1);
174 GeomAPI_DataMapOfShapeShape aStoredShapes;
175 for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
176 for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
179 const GeomShapePtr aSubShape = aShapeExp.current();
180 if(aStoredShapes.isBound(aSubShape)) {
183 if(aShapesMap.isBound(aSubShape)) {
184 theResultBody->modified(aShapesMap.find(aSubShape),
186 std::string("Modified_")
187 + (anIndex == 0 ? "Vertex_" : "Edge_")
188 + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
189 : aModifiedEdgeIndex++)),
192 theResultBody->generated(aSubShape,
193 std::string("Generated_")
194 + (anIndex == 0 ? "Vertex_" : "Edge_")
195 + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
196 : aGeneratedEdgeIndex++)),
199 aStoredShapes.bind(aSubShape, aSubShape);