1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Boolean.cpp
4 // Created: 02 Sept 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "FeaturesPlugin_Boolean.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeSelectionList.h>
16 #include <GeomAlgoAPI_Boolean.h>
17 #include <GeomAlgoAPI_MakeShapeList.h>
18 #include <GeomAlgoAPI_ShapeProps.h>
22 #define _DELETED_TAG 2
24 //=================================================================================================
25 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
29 //=================================================================================================
30 void FeaturesPlugin_Boolean::initAttributes()
32 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37 // extrusion works with faces always
38 aSelection->setSelectionType("SOLID");
40 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
41 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
42 // extrusion works with faces always
43 aSelection->setSelectionType("SOLID");
46 //=================================================================================================
47 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
49 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
50 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
52 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
53 ModelAPI_ResultBody>(aObjRef->value());
55 return aConstr->shape();
57 return std::shared_ptr<GeomAPI_Shape>();
60 //=================================================================================================
61 void FeaturesPlugin_Boolean::execute()
63 // Getting operation type.
64 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
65 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
68 GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
70 ListOfShape anObjects, aTools;
73 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
74 if (anObjectsSelList->size() == 0) {
77 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
78 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
79 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
83 anObjects.push_back(anObject);
87 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
88 if (aToolsSelList->size() == 0) {
91 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
92 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
93 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
97 aTools.push_back(aTool);
100 int aResultIndex = 0;
103 case GeomAlgoAPI_Boolean::BOOL_CUT:
104 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
105 // Cut each object with all tools
106 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
107 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
108 ListOfShape aListWithObject;
109 aListWithObject.push_back(anObject);
110 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
112 // Checking that the algorithm worked properly.
113 if(!aBoolAlgo.isDone()) {
114 static const std::string aFeatureError = "Boolean algorithm failed";
115 setError(aFeatureError);
118 if(aBoolAlgo.shape()->isNull()) {
119 static const std::string aShapeError = "Resulting shape is Null";
120 setError(aShapeError);
123 if(!aBoolAlgo.isValid()) {
124 std::string aFeatureError = "Warning: resulting shape is not valid";
125 setError(aFeatureError);
129 if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
130 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
131 LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo);
132 setResult(aResultBody, aResultIndex);
138 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
139 // Fuse all objects and all tools.
140 GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType);
142 // Checking that the algorithm worked properly.
143 if(!aBoolAlgo.isDone()) {
144 static const std::string aFeatureError = "Boolean algorithm failed";
145 setError(aFeatureError);
148 if(aBoolAlgo.shape()->isNull()) {
149 static const std::string aShapeError = "Resulting shape is Null";
150 setError(aShapeError);
153 if(!aBoolAlgo.isValid()) {
154 std::string aFeatureError = "Warning: resulting shape is not valid";
155 setError(aFeatureError);
159 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
160 LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo);
161 setResult(aResultBody, aResultIndex);
166 std::string anOperationError = "Error: wrong type of operation";
167 setError(anOperationError);
171 // remove the rest results if there were produced in the previous pass
172 removeResults(aResultIndex);
175 //=================================================================================================
176 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
177 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
178 const ListOfShape& theTools,
179 const GeomAlgoAPI_Boolean& theAlgo)
182 if(theBaseShape->isEqual(theAlgo.shape())) {
183 theResultBody->store(theAlgo.shape());
185 theResultBody->storeModified(theBaseShape, theAlgo.shape());
187 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
189 std::string aModName = "Modified";
190 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE,
191 _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
192 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG);
194 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
195 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE,
196 _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
197 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG);