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
23 #define _SUBSOLIDS_TAG 3 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
25 //=================================================================================================
26 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
30 //=================================================================================================
31 void FeaturesPlugin_Boolean::initAttributes()
33 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
35 AttributeSelectionListPtr aSelection =
36 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
37 FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
38 // extrusion works with faces always
39 aSelection->setSelectionType("SOLID");
41 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
42 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
43 // extrusion works with faces always
44 aSelection->setSelectionType("SOLID");
47 //=================================================================================================
48 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
50 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
51 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
53 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
54 ModelAPI_ResultBody>(aObjRef->value());
56 return aConstr->shape();
58 return std::shared_ptr<GeomAPI_Shape>();
61 //=================================================================================================
62 void FeaturesPlugin_Boolean::execute()
64 // Getting operation type.
65 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
66 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
69 GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
71 ListOfShape anObjects, aTools;
74 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
75 if (anObjectsSelList->size() == 0) {
78 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
79 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
80 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
84 anObjects.push_back(anObject);
88 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
89 if (aToolsSelList->size() == 0) {
92 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
93 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
94 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
98 aTools.push_back(aTool);
101 int aResultIndex = 0;
104 case GeomAlgoAPI_Boolean::BOOL_CUT:
105 case GeomAlgoAPI_Boolean::BOOL_COMMON:{
106 // Cut each object with all tools
107 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
108 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
109 ListOfShape aListWithObject;
110 aListWithObject.push_back(anObject);
111 GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
113 // Checking that the algorithm worked properly.
114 if(!aBoolAlgo.isDone()) {
115 static const std::string aFeatureError = "Boolean algorithm failed";
116 setError(aFeatureError);
119 if(aBoolAlgo.shape()->isNull()) {
120 static const std::string aShapeError = "Resulting shape is Null";
121 setError(aShapeError);
124 if(!aBoolAlgo.isValid()) {
125 std::string aFeatureError = "Warning: resulting shape is not valid";
126 setError(aFeatureError);
130 if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
131 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
132 LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo);
133 setResult(aResultBody, aResultIndex);
139 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
140 // Fuse all objects and all tools.
141 GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType);
143 // Checking that the algorithm worked properly.
144 if(!aBoolAlgo.isDone()) {
145 static const std::string aFeatureError = "Boolean algorithm failed";
146 setError(aFeatureError);
149 if(aBoolAlgo.shape()->isNull()) {
150 static const std::string aShapeError = "Resulting shape is Null";
151 setError(aShapeError);
154 if(!aBoolAlgo.isValid()) {
155 std::string aFeatureError = "Warning: resulting shape is not valid";
156 setError(aFeatureError);
160 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
161 LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo);
162 setResult(aResultBody, aResultIndex);
167 std::string anOperationError = "Error: wrong type of operation";
168 setError(anOperationError);
172 // remove the rest results if there were produced in the previous pass
173 removeResults(aResultIndex);
176 //=================================================================================================
177 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
178 const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
179 const ListOfShape& theTools,
180 const GeomAlgoAPI_Boolean& theAlgo)
183 if(theBaseShape->isEqual(theAlgo.shape())) {
184 theResultBody->store(theAlgo.shape());
186 theResultBody->storeModified(theBaseShape, theAlgo.shape(), _SUBSOLIDS_TAG);
188 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
190 std::string aModName = "Modified";
191 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE,
192 _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
193 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG);
195 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
196 theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE,
197 _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
198 theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG);