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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "FeaturesPlugin_Intersection.h"
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>
28 #include <GeomAlgoAPI_Intersection.h>
29 #include <GeomAPI_ShapeExplorer.h>
33 //=================================================================================================
34 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
38 //=================================================================================================
39 void FeaturesPlugin_Intersection::initAttributes()
41 data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
42 ModelAPI_AttributeSelectionList::typeId());
43 data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
44 ModelAPI_AttributeSelectionList::typeId());
47 //=================================================================================================
48 void FeaturesPlugin_Intersection::execute()
50 ListOfShape anObjects, aTools;
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()) {
62 anObjects.push_back(anObject);
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();
74 aTools.push_back(aTool);
77 if(anObjects.empty() || aTools.empty()) {
78 setError("Error: Objects or tools are empty.");
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);
91 // Checking that the algorithm worked properly.
92 if (!anIntersectionAlgo.isDone()) {
93 static const std::string aFeatureError = "Error: Intersection algorithm failed.";
94 setError(aFeatureError);
97 if (anIntersectionAlgo.shape()->isNull()) {
98 static const std::string aShapeError = "Error: Resulting shape is Null.";
99 setError(aShapeError);
102 if (!anIntersectionAlgo.isValid()) {
103 std::string aFeatureError = "Error: Resulting shape is not valid.";
104 setError(aFeatureError);
108 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
109 loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
110 setResult(aResultBody, aResultIndex);
114 // remove the rest results if there were produced in the previous pass
115 removeResults(aResultIndex);
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)
124 std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
125 theResultBody->storeModified(theBaseShape, aResultShape);
127 const int aDeletedVertexTag = 1;
128 const int aDeletedEdgeTag = 2;
129 const int aDeletedFaceTag = 3;
131 theResultBody->loadDeletedShapes(&theMakeShape,
133 GeomAPI_Shape::VERTEX,
135 theResultBody->loadDeletedShapes(&theMakeShape,
139 theResultBody->loadDeletedShapes(&theMakeShape,
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();
162 aShapesMap.bind(*aHistoryIt, aSubShape);
168 int aModifiedVertexIndex(1),
169 aGeneratedVertexIndex(1),
170 aModifiedEdgeIndex(1),
171 aGeneratedEdgeIndex(1);
173 GeomAPI_DataMapOfShapeShape aStoredShapes;
174 for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
175 for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
178 const GeomShapePtr aSubShape = aShapeExp.current();
179 if(aStoredShapes.isBound(aSubShape)) {
182 if(aShapesMap.isBound(aSubShape)) {
183 theResultBody->modified(aShapesMap.find(aSubShape),
185 std::string("Modified_")
186 + (anIndex == 0 ? "Vertex_" : "Edge_")
187 + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
188 : aModifiedEdgeIndex++)),
191 theResultBody->generated(aSubShape,
192 std::string("Generated_")
193 + (anIndex == 0 ? "Vertex_" : "Edge_")
194 + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
195 : aGeneratedEdgeIndex++)),
198 aStoredShapes.bind(aSubShape, aSubShape);