1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Intersection.cpp
4 // Created: 15 Feb 2016
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_Intersection.h"
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>
15 #include <GeomAlgoAPI_Intersection.h>
16 #include <GeomAPI_ShapeExplorer.h>
20 //=================================================================================================
21 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
25 //=================================================================================================
26 void FeaturesPlugin_Intersection::initAttributes()
28 data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
29 ModelAPI_AttributeSelectionList::typeId());
30 data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
31 ModelAPI_AttributeSelectionList::typeId());
34 //=================================================================================================
35 void FeaturesPlugin_Intersection::execute()
37 ListOfShape anObjects, aTools;
40 AttributeSelectionListPtr anObjectsSelList =
41 selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
42 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
43 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
44 anObjectsSelList->value(anObjectsIndex);
45 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
46 if (!anObject.get()) {
49 anObjects.push_back(anObject);
53 AttributeSelectionListPtr aToolsSelList =
54 selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
55 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
56 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
57 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
61 aTools.push_back(aTool);
64 if(anObjects.empty() || aTools.empty()) {
65 setError("Error: Objects or tools are empty.");
71 // Create result for each object.
72 for (ListOfShape::iterator
73 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
74 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
75 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
76 GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
78 // Checking that the algorithm worked properly.
79 if (!anIntersectionAlgo.isDone()) {
80 static const std::string aFeatureError = "Error: Intersection algorithm failed.";
81 setError(aFeatureError);
84 if (anIntersectionAlgo.shape()->isNull()) {
85 static const std::string aShapeError = "Error: Resulting shape is Null.";
86 setError(aShapeError);
89 if (!anIntersectionAlgo.isValid()) {
90 std::string aFeatureError = "Error: Resulting shape is not valid.";
91 setError(aFeatureError);
95 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
96 loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
97 setResult(aResultBody, aResultIndex);
101 // remove the rest results if there were produced in the previous pass
102 removeResults(aResultIndex);
105 //=================================================================================================
106 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
107 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
108 const ListOfShape& theTools,
109 GeomAlgoAPI_MakeShape& theMakeShape)
111 std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
112 theResultBody->storeModified(theBaseShape, aResultShape);
114 const int aDeletedVertexTag = 1;
115 const int aDeletedEdgeTag = 2;
116 const int aDeletedFaceTag = 3;
118 theResultBody->loadDeletedShapes(&theMakeShape,
120 GeomAPI_Shape::VERTEX,
122 theResultBody->loadDeletedShapes(&theMakeShape,
126 theResultBody->loadDeletedShapes(&theMakeShape,
131 ListOfShape aShapes = theTools;
132 aShapes.push_back(theBaseShape);
133 GeomAPI_DataMapOfShapeShape aShapesMap; // Map to store {result_shape, original_shape}
134 const int aShapeTypesNb = 2;
135 const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] =
136 {GeomAPI_Shape::VERTEX, GeomAPI_Shape::EDGE};
137 for(ListOfShape::const_iterator anIt = aShapes.cbegin(); anIt != aShapes.cend(); ++anIt) {
138 const GeomShapePtr aShape = *anIt;
139 for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
140 for(GeomAPI_ShapeExplorer anOrigShapeExp(aShape, aShapeTypes[anIndex]);
141 anOrigShapeExp.more();
142 anOrigShapeExp.next()) {
143 ListOfShape aHistory;
144 const GeomShapePtr aSubShape = anOrigShapeExp.current();
145 theMakeShape.modified(aSubShape, aHistory);
146 for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
147 aHistoryIt != aHistory.cend();
149 aShapesMap.bind(*aHistoryIt, aSubShape);
155 int aModifiedVertexIndex(1),
156 aGeneratedVertexIndex(1),
157 aModifiedEdgeIndex(1),
158 aGeneratedEdgeIndex(1);
160 GeomAPI_DataMapOfShapeShape aStoredShapes;
161 for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
162 for(GeomAPI_ShapeExplorer aShapeExp(aResultShape, aShapeTypes[anIndex]);
165 const GeomShapePtr aSubShape = aShapeExp.current();
166 if(aStoredShapes.isBound(aSubShape)) {
169 if(aShapesMap.isBound(aSubShape)) {
170 theResultBody->modified(aShapesMap.find(aSubShape),
172 std::string("Modified_")
173 + (anIndex == 0 ? "Vertex_" : "Edge_")
174 + std::to_string((long long)(anIndex == 0 ? aModifiedVertexIndex++
175 : aModifiedEdgeIndex++)),
178 theResultBody->generated(aSubShape,
179 std::string("Generated_")
180 + (anIndex == 0 ? "Vertex_" : "Edge_")
181 + std::to_string((long long)(anIndex == 0 ? aGeneratedVertexIndex++
182 : aGeneratedEdgeIndex++)),
185 aStoredShapes.bind(aSubShape, aSubShape);