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>
19 //=================================================================================================
20 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
24 //=================================================================================================
25 void FeaturesPlugin_Intersection::initAttributes()
27 data()->addAttribute(FeaturesPlugin_Intersection::OBJECT_LIST_ID(),
28 ModelAPI_AttributeSelectionList::typeId());
29 data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(),
30 ModelAPI_AttributeSelectionList::typeId());
33 //=================================================================================================
34 void FeaturesPlugin_Intersection::execute()
36 ListOfShape anObjects, aTools;
39 AttributeSelectionListPtr anObjectsSelList =
40 selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
41 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
42 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
43 anObjectsSelList->value(anObjectsIndex);
44 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
45 if (!anObject.get()) {
48 anObjects.push_back(anObject);
52 AttributeSelectionListPtr aToolsSelList =
53 selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
54 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
55 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
56 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
60 aTools.push_back(aTool);
63 if(anObjects.empty() || aTools.empty()) {
64 setError("Error: Objects or tools are empty.");
70 // Create result for each object.
71 for (ListOfShape::iterator
72 anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
73 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
74 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
75 GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
77 // Checking that the algorithm worked properly.
78 if (!anIntersectionAlgo.isDone()) {
79 static const std::string aFeatureError = "Error: Intersection algorithm failed.";
80 setError(aFeatureError);
83 if (anIntersectionAlgo.shape()->isNull()) {
84 static const std::string aShapeError = "Error: Resulting shape is Null.";
85 setError(aShapeError);
88 if (!anIntersectionAlgo.isValid()) {
89 std::string aFeatureError = "Error: Resulting shape is not valid.";
90 setError(aFeatureError);
94 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
95 loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
96 setResult(aResultBody, aResultIndex);
100 // remove the rest results if there were produced in the previous pass
101 removeResults(aResultIndex);
104 //=================================================================================================
105 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
106 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
107 const ListOfShape& theTools,
108 GeomAlgoAPI_MakeShape& theMakeShape)
111 std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
112 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = theMakeShape.mapOfSubShapes();
113 const int aDeletedTag = 1;
114 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
115 const int aSubsolidsTag = 2;
116 const int aModifyTag = 100000;
117 int aModifyToolsTag = 200000;
118 std::ostringstream aStream;
120 theResultBody->storeModified(theBaseShape, aResultShape, aSubsolidsTag);
122 std::string aModName = "Modified";
123 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
124 aModifyTag, aModName, *aMapOfShapes.get(), true);
125 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
126 aModifyTag, aModName, *aMapOfShapes.get(), true);
127 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
130 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
131 aStream.str(std::string());
133 aStream << aModName << "_" << anIndex++;
134 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
135 aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
136 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
137 aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
138 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
139 aModifyToolsTag += 10000;