1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Partition.cpp
4 // Created: 31 Jul 2015
5 // Author: Natalia ERMOLAEVA
7 #include "FeaturesPlugin_Partition.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeInteger.h>
13 #include <ModelAPI_BodyBuilder.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_Session.h>
17 #include <ModelAPI_Validator.h>
19 #include <GeomAlgoAPI_Partition.h>
20 #include <GeomAlgoAPI_MakeShapeList.h>
21 #include <GeomAlgoAPI_ShapeTools.h>
23 //=================================================================================================
24 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
28 //=================================================================================================
29 void FeaturesPlugin_Partition::initAttributes()
31 AttributeSelectionListPtr aSelection =
32 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
33 FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
34 aSelection->setSelectionType("SOLID");
37 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38 FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39 aSelection->setSelectionType("SOLID");
41 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
45 //=================================================================================================
46 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::string& theAttrName)
48 std::shared_ptr<ModelAPI_AttributeReference> aObjRef =
49 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(data()->attribute(theAttrName));
51 std::shared_ptr<ModelAPI_ResultBody> aConstr =
52 std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObjRef->value());
54 return aConstr->shape();
56 return std::shared_ptr<GeomAPI_Shape>();
59 //=================================================================================================
60 void FeaturesPlugin_Partition::execute()
62 ListOfShape anObjects, aTools;
65 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
66 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
67 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
68 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
69 if (!anObject.get()) {
72 anObjects.push_back(anObject);
76 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
77 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
78 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
79 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
81 // it could be a construction plane
82 ResultPtr aContext = aToolAttr->context();
84 aTool = GeomAlgoAPI_ShapeTools::faceToInfinitePlane(aContext->shape());
90 aTools.push_back(aTool);
95 if (anObjects.empty() || aTools.empty()) {
96 std::string aFeatureError = "Not enough objects for partition operation";
97 setError(aFeatureError);
101 // Cut each object with all tools
102 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
103 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
104 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
105 GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
107 // Checking that the algorithm worked properly.
108 if (!aPartitionAlgo.isDone()) {
109 static const std::string aFeatureError = "Partition algorithm failed";
110 setError(aFeatureError);
113 if (aPartitionAlgo.shape()->isNull()) {
114 static const std::string aShapeError = "Resulting shape is Null";
115 setError(aShapeError);
118 if (!aPartitionAlgo.isValid()) {
119 std::string aFeatureError = "Warning: resulting shape is not valid";
120 setError(aFeatureError);
124 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
125 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
126 aResultBody->store(aPartitionAlgo.shape());
127 loadNamingDS(aResultBody, anObject, aTools, aPartitionAlgo);
128 setResult(aResultBody, aResultIndex);
133 // remove the rest results if there were produced in the previous pass
134 removeResults(aResultIndex);
137 //=================================================================================================
138 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
139 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
140 const ListOfShape& theTools,
141 const GeomAlgoAPI_Partition& thePartitionAlgo)
144 if(theBaseShape->isEqual(thePartitionAlgo.shape())) {
145 theResultBody->store(thePartitionAlgo.shape());
147 const int aModifyTag = 1;
148 const int aDeletedTag = 2;
149 const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
151 theResultBody->storeModified(theBaseShape, thePartitionAlgo.shape(), aSubsolidsTag);
153 std::shared_ptr<GeomAlgoAPI_MakeShape> aMkShape = thePartitionAlgo.makeShape();
154 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = thePartitionAlgo.mapOfShapes();
156 std::string aModName = "Modified";
157 theResultBody->loadAndOrientModifiedShapes(aMkShape.get(), theBaseShape, GeomAPI_Shape::FACE,
158 aModifyTag, aModName, *aMapOfShapes.get());
159 theResultBody->loadDeletedShapes(aMkShape.get(), theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
161 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
162 theResultBody->loadAndOrientModifiedShapes(aMkShape.get(), *anIter, GeomAPI_Shape::FACE,
163 aModifyTag, aModName, *aMapOfShapes.get());
164 theResultBody->loadDeletedShapes(aMkShape.get(), *anIter, GeomAPI_Shape::FACE, aDeletedTag);