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 //#define DEBUG_ONE_OBJECT
22 #define _DELETED_TAG 2
23 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
27 void FeaturesPlugin_Boolean::initAttributes()
29 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
31 #ifdef DEBUG_ONE_OBJECT
32 data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::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");
41 #ifdef DEBUG_ONE_OBJECT
42 data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::typeId());
44 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
45 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
46 // extrusion works with faces always
47 aSelection->setSelectionType("SOLID");
51 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
53 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
54 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
56 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
57 ModelAPI_ResultBody>(aObjRef->value());
59 return aConstr->shape();
61 return std::shared_ptr<GeomAPI_Shape>();
65 void FeaturesPlugin_Boolean::execute()
67 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
68 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
71 int aType = aTypeAttr->value();
72 #ifdef DEBUG_ONE_OBJECT
73 std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
75 std::shared_ptr<GeomAPI_Shape> anObject;
77 AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
78 if (anObjects->size() == 0)
81 // Getting bounding planes.
82 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
85 anObject = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
91 #ifdef DEBUG_ONE_OBJECT
92 std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
94 std::shared_ptr<GeomAPI_Shape> aTool;
96 AttributeSelectionListPtr anObjects = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
97 if (anObjects->size() == 0)
100 // Getting bounding planes.
101 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = anObjects->value(0);
104 aTool = std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
110 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
112 GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
113 if(aFeature && !aFeature->isDone()) {
114 static const std::string aFeatureError = "Boolean feature: algorithm failed";
115 setError(aFeatureError);
118 // Check if shape is valid
119 if (aFeature->shape()->isNull()) {
120 static const std::string aShapeError = "Boolean feature: resulting shape is Null";
121 setError(aShapeError);
124 if(!aFeature->isValid()) {
125 static const std::string aFeatureError = "Boolean feature: resulting shape is not valid";
126 setError(aFeatureError);
129 // if result of Boolean operation is same as was before it means that Boolean operation has no sence
130 // and naming provides no result, so, generate an error in this case
131 if (anObject->isEqual(aFeature->shape())) {
132 static const std::string aFeatureError = "Boolean feature: operation was not performed";
133 setError(aFeatureError);
137 LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
139 setResult(aResultBody);
142 //============================================================================
143 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature,
144 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
145 std::shared_ptr<GeomAPI_Shape> theObject,
146 std::shared_ptr<GeomAPI_Shape> theTool,
151 theResultBody->storeModified(theObject, theFeature->shape());
153 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
154 theFeature->mapOfShapes(*aSubShapes);
156 // Put in DF modified faces
157 std::string aModName = "Modified";
158 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, aModName, *aSubShapes);
159 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool, FACE, _MODIFY_TAG, aModName, *aSubShapes);
161 //Put in DF deleted faces
162 theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
163 theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool, FACE, _DELETED_TAG);