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 int aType = 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;
101 ListOfMakeShape aListOfMakeShape;
102 std::shared_ptr<GeomAPI_Shape> aResShape;
103 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aDataMapOfShapes;
106 case GeomAlgoAPI_Boolean::BOOL_CUT: {
107 // Cut each object with all tools
108 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
109 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
110 aResShape = anObject;
111 ListOfShape aSingleObjectList; aSingleObjectList.push_back(anObject);
112 for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
113 std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
114 GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aResShape, aTool, aType);
115 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
116 static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
117 setError(aBoolAlgoError);
120 aListOfMakeShape.push_back(aBoolAlgo->makeShape());
121 aResShape = aBoolAlgo->shape();
122 aBoolAlgo->mapOfShapes(aDataMapOfShapes);
125 if(GeomAlgoAPI_ShapeProps::volume(aResShape) > 10.e-5) {
126 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
127 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
128 new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
130 LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, aSingleObjectList, aTools);
131 setResult(aResultBody, aResultIndex);
137 case GeomAlgoAPI_Boolean::BOOL_FUSE: {
139 std::shared_ptr<GeomAPI_Shape> aTempRes = anObjects.front();
140 for(ListOfShape::iterator anObjectsIt = ++anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
141 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
142 GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aTempRes, anObject, aType);
143 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
144 static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
145 setError(aBoolAlgoError);
148 aListOfMakeShape.push_back(aBoolAlgo->makeShape());
149 aTempRes = aBoolAlgo->shape();
152 // Fuse all tools with result of objects fuse.
153 for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
154 std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
155 GeomAlgoAPI_Boolean* aBoolAlgo = new GeomAlgoAPI_Boolean(aTempRes, aTool, aType);
156 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
157 static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
158 setError(aBoolAlgoError);
161 aListOfMakeShape.push_back(aBoolAlgo->makeShape());
162 aTempRes = aBoolAlgo->shape();
163 aBoolAlgo->mapOfShapes(aDataMapOfShapes);
165 aResShape = aTempRes;
167 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
168 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
169 new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
171 LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, anObjects, aTools);
172 setResult(aResultBody);
176 case GeomAlgoAPI_Boolean::BOOL_COMMON: {
177 // Search common between object and tool and fuse them.
178 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
179 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
180 std::shared_ptr<GeomAPI_Shape> aTempRes;
181 ListOfShape aSingleObjectList; aSingleObjectList.push_back(anObject);
182 for(ListOfShape::iterator aToolsIt = aTools.begin(); aToolsIt != aTools.end(); aToolsIt++) {
183 std::shared_ptr<GeomAPI_Shape> aTool = *aToolsIt;
184 GeomAlgoAPI_Boolean* aBoolAlgo1 = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
185 if(!aBoolAlgo1->isDone() || aBoolAlgo1->shape()->isNull() || !aBoolAlgo1->isValid()) {
186 static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
187 setError(aBoolAlgoError);
190 aListOfMakeShape.push_back(aBoolAlgo1->makeShape());
193 aTempRes = aBoolAlgo1->shape();
194 aBoolAlgo1->mapOfShapes(aDataMapOfShapes);
196 // Fuse common result with previous common results.
197 GeomAlgoAPI_Boolean* aBoolAlgo2 = new GeomAlgoAPI_Boolean(aTempRes,
199 GeomAlgoAPI_Boolean::BOOL_FUSE);
200 if(!aBoolAlgo1->isDone() || aBoolAlgo1->shape()->isNull() || !aBoolAlgo1->isValid()) {
201 static const std::string aBoolAlgoError = "Boolean feature: algorithm failed";
202 setError(aBoolAlgoError);
205 aListOfMakeShape.push_back(aBoolAlgo2->makeShape());
206 aTempRes = aBoolAlgo2->shape();
207 aBoolAlgo2->mapOfShapes(aDataMapOfShapes);
210 aResShape = aTempRes;
212 if(GeomAlgoAPI_ShapeProps::volume(aResShape) > 10.e-5) {
213 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
214 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(
215 new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
217 LoadNamingDS(aMakeShapeList, aResultBody, aResShape, aDataMapOfShapes, aSingleObjectList, aTools);
218 setResult(aResultBody, aResultIndex);
225 // remove the rest results if there were produced in the previous pass
226 removeResults(aResultIndex);
229 //=================================================================================================
230 void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
231 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
232 std::shared_ptr<GeomAPI_Shape> theResult,
233 std::shared_ptr<GeomAPI_DataMapOfShapeShape> theDataMapOfShapes,
234 const ListOfShape& theObjects,
235 const ListOfShape& theTools)
238 if(theObjects.front()->isEqual(theResult)) {
239 theResultBody->store(theObjects.front());
241 theResultBody->storeModified(theObjects.front(), theResult);
243 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
245 std::string aModName = "Modified";
246 for(ListOfShape::const_iterator anIter = theObjects.begin(); anIter != theObjects.end(); anIter++) {
247 theResultBody->loadAndOrientModifiedShapes(theMakeShapeList.get(), *anIter, FACE, _MODIFY_TAG, aModName, *theDataMapOfShapes.get());
248 theResultBody->loadDeletedShapes(theMakeShapeList.get(), *anIter, FACE, _DELETED_TAG);
251 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
252 theResultBody->loadAndOrientModifiedShapes(theMakeShapeList.get(), *anIter, FACE, _MODIFY_TAG, aModName, *theDataMapOfShapes.get());
253 theResultBody->loadDeletedShapes(theMakeShapeList.get(), *anIter, FACE, _DELETED_TAG);