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_AttributeBoolean.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_BodyBuilder.h>
15 #include <ModelAPI_ResultBody.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_Validator.h>
20 #include <GeomAlgoAPI_Partition.h>
21 #include <GeomAlgoAPI_MakeShapeCustom.h>
22 #include <GeomAlgoAPI_MakeShapeList.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
25 //=================================================================================================
26 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
30 //=================================================================================================
31 void FeaturesPlugin_Partition::initAttributes()
33 AttributeSelectionListPtr aSelection =
34 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35 FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36 aSelection->setSelectionType("SOLID");
39 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
40 FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
41 aSelection->setSelectionType("SOLID");
43 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
45 data()->addAttribute(COMBINE_ID(), ModelAPI_AttributeBoolean::typeId());
48 //=================================================================================================
49 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::string& theAttrName)
51 std::shared_ptr<ModelAPI_AttributeReference> aObjRef =
52 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(data()->attribute(theAttrName));
54 std::shared_ptr<ModelAPI_ResultBody> aConstr =
55 std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObjRef->value());
57 return aConstr->shape();
59 return std::shared_ptr<GeomAPI_Shape>();
62 //=================================================================================================
63 void FeaturesPlugin_Partition::execute()
65 ListOfShape anObjects, aTools, aToolsForNaming;
68 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
69 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
70 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
71 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
72 if (!anObject.get()) {
75 anObjects.push_back(anObject);
78 GeomAlgoAPI_MakeShapeList aMakeShapeList;
81 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
82 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
83 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
84 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
86 // it could be a construction plane
87 ResultPtr aContext = aToolAttr->context();
89 aTool = GeomAlgoAPI_ShapeTools::faceToInfinitePlane(aContext->shape());
90 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
91 aMkShCustom->addModified(aContext->shape(), aTool);
92 aMakeShapeList.append(aMkShCustom);
93 aTools.push_back(aTool);
94 aToolsForNaming.push_back(aContext->shape());
97 aTools.push_back(aTool);
98 aToolsForNaming.push_back(aTool);
102 // Getting combine flag.
103 bool isCombine = boolean(COMBINE_ID())->value();
105 if(anObjects.empty()/* || aTools.empty()*/) {
106 std::string aFeatureError = "Not enough objects for partition operation";
107 setError(aFeatureError);
111 int aResultIndex = 0;
114 // Create single result.
115 GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
117 // Checking that the algorithm worked properly.
118 if (!aPartitionAlgo.isDone()) {
119 static const std::string aFeatureError = "Partition algorithm failed";
120 setError(aFeatureError);
123 if (aPartitionAlgo.shape()->isNull()) {
124 static const std::string aShapeError = "Resulting shape is Null";
125 setError(aShapeError);
128 if (!aPartitionAlgo.isValid()) {
129 std::string aFeatureError = "Warning: resulting shape is not valid";
130 setError(aFeatureError);
134 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
135 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
136 aMakeShapeList.append(aPartitionAlgo.makeShape());
137 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
138 loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
139 setResult(aResultBody, aResultIndex);
143 // Create result for each object.
144 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
145 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
146 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
147 GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
149 // Checking that the algorithm worked properly.
150 if (!aPartitionAlgo.isDone()) {
151 static const std::string aFeatureError = "Partition algorithm failed";
152 setError(aFeatureError);
155 if (aPartitionAlgo.shape()->isNull()) {
156 static const std::string aShapeError = "Resulting shape is Null";
157 setError(aShapeError);
160 if (!aPartitionAlgo.isValid()) {
161 std::string aFeatureError = "Warning: resulting shape is not valid";
162 setError(aFeatureError);
166 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
167 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
168 aMakeShapeList.append(aPartitionAlgo.makeShape());
169 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
170 loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
171 setResult(aResultBody, aResultIndex);
177 // remove the rest results if there were produced in the previous pass
178 removeResults(aResultIndex);
181 //=================================================================================================
182 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
183 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
184 const ListOfShape& theTools,
185 const std::shared_ptr<GeomAPI_Shape> theResultShape,
186 GeomAlgoAPI_MakeShape& theMakeShape,
187 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
190 if(theBaseShape->isEqual(theResultShape)) {
191 theResultBody->store(theResultShape);
193 const int aModifyTag = 1;
194 const int aDeletedTag = 2;
195 const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
197 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
199 std::string aModName = "Modified";
200 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
201 aModifyTag, aModName, theMapOfShapes);
202 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
204 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
205 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
206 aModifyTag, aModName, theMapOfShapes);
207 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);