1 // File: FeaturesPlugin_Boolean.cpp
2 // Created: 02 Sept 2014
3 // Author: Vitaly SMETANNIKOV
5 #include "FeaturesPlugin_Boolean.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Document.h>
9 #include <ModelAPI_AttributeReference.h>
10 #include <ModelAPI_AttributeInteger.h>
11 #include <ModelAPI_ResultBody.h>
12 #include <GeomAlgoAPI_Boolean.h>
13 #include <Events_Error.h>
22 #define _DELETED_TAG 2
23 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
27 void FeaturesPlugin_Boolean::initAttributes()
29 data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::type());
30 data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type());
31 data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type());
34 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
36 std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
37 ModelAPI_AttributeReference>(data()->attribute(theAttrName));
39 std::shared_ptr<ModelAPI_ResultBody> aConstr = std::dynamic_pointer_cast<
40 ModelAPI_ResultBody>(aObjRef->value());
42 return aConstr->shape();
44 return std::shared_ptr<GeomAPI_Shape>();
48 void FeaturesPlugin_Boolean::execute()
50 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
51 ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
54 int aType = aTypeAttr->value();
56 std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
60 std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
64 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
66 GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
67 if(aFeature && !aFeature->isDone()) {
68 std::string aFeatureError = "Boolean feature: algorithm failed";
69 Events_Error::send(aFeatureError, this);
72 // Check if shape is valid
73 if (aFeature->shape()->isNull()) {
74 std::string aShapeError = "Boolean feature: resulting shape is Null";
75 Events_Error::send(aShapeError, this);
77 std::cerr << aShapeError << std::endl;
81 if(!aFeature->isValid()) {
82 std::string aFeatureError = "Boolean feature: resulting shape is not valid";
83 Events_Error::send(aFeatureError, this);
86 // if result of Boolean operation is same as was before it means that Boolean operation has no sence
87 // and naming provides no result, so, generate an error in this case
88 if (anObject->isEqual(aFeature->shape())) {
89 std::string aFeatureError = "Boolean feature: operation was not performed";
90 Events_Error::send(aFeatureError, this);
94 LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
96 setResult(aResultBody);
99 //============================================================================
100 void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature,
101 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
102 std::shared_ptr<GeomAPI_Shape> theObject,
103 std::shared_ptr<GeomAPI_Shape> theTool,
108 theResultBody->storeModified(theObject, theFeature->shape());
110 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
111 theFeature->mapOfShapes(*aSubShapes);
113 // Put in DF modified faces
114 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes);
115 theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool, FACE, _MODIFY_TAG, *aSubShapes);
117 //Put in DF deleted faces
118 theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
119 theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool, FACE, _DELETED_TAG);