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>
15 #include <GeomAlgoAPI_Boolean.h>
18 // to use multi-selection, please comment the next define and uncomment multi_selector in boolean_widget.xml
19 #define DEBUG_ONE_OBJECT
23 #define _DELETED_TAG 2
24 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
28 void FeaturesPlugin_Boolean::initAttributes()
30 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
32 #ifdef DEBUG_ONE_OBJECT
33 data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::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");
42 #ifdef DEBUG_ONE_OBJECT
43 data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::typeId());
45 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
46 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
47 // extrusion works with faces always
48 aSelection->setSelectionType("SOLID");
52 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
54 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
55 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
57 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
58 ModelAPI_ResultBody>(aObjRef->value());
60 return aConstr->shape();
62 return std::shared_ptr<GeomAPI_Shape>();
66 void FeaturesPlugin_Boolean::execute()
68 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
69 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
72 int aType = aTypeAttr->value();
73 #ifdef DEBUG_ONE_OBJECT
74 std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
76 std::shared_ptr<GeomAPI_Shape> anObject;
78 AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
79 if (anObjects->size() == 0)
82 // Getting bounding planes.
83 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
86 anObject = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
92 #ifdef DEBUG_ONE_OBJECT
93 std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
95 std::shared_ptr<GeomAPI_Shape> aTool;
97 AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
98 if (anObjects->size() == 0)
101 // Getting bounding planes.
102 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
105 aTool = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
111 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
113 GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
114 if(aFeature && !aFeature->isDone()) {
115 static const std::string aFeatureError = "Boolean feature: algorithm failed";
116 setError(aFeatureError);
119 // Check if shape is valid
120 if (aFeature->shape()->isNull()) {
121 static const std::string aShapeError = "Boolean feature: resulting shape is Null";
122 setError(aShapeError);
125 if(!aFeature->isValid()) {
126 static const std::string aFeatureError = "Boolean feature: resulting shape is not valid";
127 setError(aFeatureError);
130 // if result of Boolean operation is same as was before it means that Boolean operation has no sence
131 // and naming provides no result, so, generate an error in this case
132 if (anObject->isEqual(aFeature->shape())) {
133 static const std::string aFeatureError = "Boolean feature: operation was not performed";
134 setError(aFeatureError);
138 LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
140 setResult(aResultBody);
143 //============================================================================
144 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature,
145 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
146 std::shared_ptr<GeomAPI_Shape> theObject,
147 std::shared_ptr<GeomAPI_Shape> theTool,
152 theResultBody->storeModified(theObject, theFeature->shape());
154 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
155 theFeature->mapOfShapes(*aSubShapes);
157 // Put in DF modified faces
158 std::string aModName = "Modified";
159 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, aModName, *aSubShapes);
160 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool, FACE, _MODIFY_TAG, aModName, *aSubShapes);
162 //Put in DF deleted faces
163 theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
164 theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool, FACE, _DELETED_TAG);