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(), ModelAPI_AttributeSelectionList::typeId());
28 data()->addAttribute(FeaturesPlugin_Intersection::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
31 //=================================================================================================
32 void FeaturesPlugin_Intersection::execute()
34 ListOfShape anObjects, aTools;
37 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Intersection::OBJECT_LIST_ID());
38 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
39 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
40 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
41 if (!anObject.get()) {
44 anObjects.push_back(anObject);
48 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Intersection::TOOL_LIST_ID());
49 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
50 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
51 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
55 aTools.push_back(aTool);
58 if(anObjects.empty() || aTools.empty()) {
59 setError("Error: Objects or tools are empty.");
65 // Create result for each object.
66 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
67 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
68 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
69 GeomAlgoAPI_Intersection anIntersectionAlgo(aListWithObject, aTools);
71 // Checking that the algorithm worked properly.
72 if (!anIntersectionAlgo.isDone()) {
73 static const std::string aFeatureError = "Error: Intersection algorithm failed.";
74 setError(aFeatureError);
77 if (anIntersectionAlgo.shape()->isNull()) {
78 static const std::string aShapeError = "Error: Resulting shape is Null.";
79 setError(aShapeError);
82 if (!anIntersectionAlgo.isValid()) {
83 std::string aFeatureError = "Error: resulting shape is not valid";
84 setError(aFeatureError);
88 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
89 loadNamingDS(aResultBody, anObject, aTools, anIntersectionAlgo);
90 setResult(aResultBody, aResultIndex);
94 // remove the rest results if there were produced in the previous pass
95 removeResults(aResultIndex);
98 //=================================================================================================
99 void FeaturesPlugin_Intersection::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
100 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
101 const ListOfShape& theTools,
102 GeomAlgoAPI_MakeShape& theMakeShape)
105 std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape.shape();
106 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = theMakeShape.mapOfSubShapes();
107 const int aDeletedTag = 1;
108 const int aSubsolidsTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
109 const int aModifyTag = 100000;
110 int aModifyToolsTag = 200000;
111 std::ostringstream aStream;
113 theResultBody->storeModified(theBaseShape, aResultShape, aSubsolidsTag);
115 std::string aModName = "Modified";
116 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
117 aModifyTag, aModName, *aMapOfShapes.get(), true);
118 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
119 aModifyTag, aModName, *aMapOfShapes.get(), true);
120 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
123 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
124 aStream.str(std::string());
126 aStream << aModName << "_" << anIndex++;
127 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
128 aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
129 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
130 aModifyToolsTag, aStream.str(), *aMapOfShapes.get(), true);
131 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
132 aModifyToolsTag += 10000;