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_MakeShapeCustom.h>
21 #include <GeomAlgoAPI_MakeShapeList.h>
22 #include <GeomAlgoAPI_ShapeTools.h>
24 //=================================================================================================
25 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
29 //=================================================================================================
30 void FeaturesPlugin_Partition::initAttributes()
32 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35 aSelection->setSelectionType("SOLID");
38 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
39 FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
40 aSelection->setSelectionType("SOLID");
42 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
43 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
46 //=================================================================================================
47 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::string& theAttrName)
49 std::shared_ptr<ModelAPI_AttributeReference> aObjRef =
50 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(data()->attribute(theAttrName));
52 std::shared_ptr<ModelAPI_ResultBody> aConstr =
53 std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObjRef->value());
55 return aConstr->shape();
57 return std::shared_ptr<GeomAPI_Shape>();
60 //=================================================================================================
61 void FeaturesPlugin_Partition::execute()
63 ListOfShape anObjects, aTools, aToolsForNaming;
66 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
67 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
68 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
69 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
70 if (!anObject.get()) {
73 anObjects.push_back(anObject);
76 GeomAlgoAPI_MakeShapeList aMakeShapeList;
79 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
80 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
81 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
82 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
84 // it could be a construction plane
85 ResultPtr aContext = aToolAttr->context();
87 aTool = GeomAlgoAPI_ShapeTools::faceToInfinitePlane(aContext->shape());
88 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
89 aMkShCustom->addModified(aContext->shape(), aTool);
90 aMakeShapeList.append(aMkShCustom);
91 aTools.push_back(aTool);
92 aToolsForNaming.push_back(aContext->shape());
95 aTools.push_back(aTool);
96 aToolsForNaming.push_back(aTool);
100 int aResultIndex = 0;
102 if (anObjects.empty() || aTools.empty()) {
103 std::string aFeatureError = "Not enough objects for partition operation";
104 setError(aFeatureError);
108 // Cut each object with all tools
109 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
110 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
111 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
112 GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
114 // Checking that the algorithm worked properly.
115 if (!aPartitionAlgo.isDone()) {
116 static const std::string aFeatureError = "Partition algorithm failed";
117 setError(aFeatureError);
120 if (aPartitionAlgo.shape()->isNull()) {
121 static const std::string aShapeError = "Resulting shape is Null";
122 setError(aShapeError);
125 if (!aPartitionAlgo.isValid()) {
126 std::string aFeatureError = "Warning: resulting shape is not valid";
127 setError(aFeatureError);
131 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
132 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
133 aMakeShapeList.append(aPartitionAlgo.makeShape());
134 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
135 loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
136 setResult(aResultBody, aResultIndex);
141 // remove the rest results if there were produced in the previous pass
142 removeResults(aResultIndex);
145 //=================================================================================================
146 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
147 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
148 const ListOfShape& theTools,
149 const std::shared_ptr<GeomAPI_Shape> theResultShape,
150 GeomAlgoAPI_MakeShape& theMakeShape,
151 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
154 if(theBaseShape->isEqual(theResultShape)) {
155 theResultBody->store(theResultShape);
157 const int aModifyTag = 1;
158 const int aDeletedTag = 2;
159 const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
161 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
163 std::string aModName = "Modified";
164 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
165 aModifyTag, aModName, theMapOfShapes);
166 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
168 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
169 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
170 aModifyTag, aModName, theMapOfShapes);
171 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);