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;
79 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
82 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
83 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
84 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
85 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
87 // it could be a construction plane
88 ResultPtr aContext = aToolAttr->context();
90 aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aContext->shape(), aBoundingPoints);
91 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
92 aMkShCustom->addModified(aContext->shape(), aTool);
93 aMakeShapeList.append(aMkShCustom);
94 aTools.push_back(aTool);
95 aToolsForNaming.push_back(aContext->shape());
98 aTools.push_back(aTool);
99 aToolsForNaming.push_back(aTool);
103 // Getting combine flag.
104 bool isCombine = boolean(COMBINE_ID())->value();
106 if(anObjects.empty()/* || aTools.empty()*/) {
107 std::string aFeatureError = "Not enough objects for partition operation";
108 setError(aFeatureError);
112 int aResultIndex = 0;
115 // Create single result.
116 GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
118 // Checking that the algorithm worked properly.
119 if (!aPartitionAlgo.isDone()) {
120 static const std::string aFeatureError = "Partition algorithm failed";
121 setError(aFeatureError);
124 if (aPartitionAlgo.shape()->isNull()) {
125 static const std::string aShapeError = "Resulting shape is Null";
126 setError(aShapeError);
129 if (!aPartitionAlgo.isValid()) {
130 std::string aFeatureError = "Warning: resulting shape is not valid";
131 setError(aFeatureError);
135 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
136 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
137 aMakeShapeList.append(aPartitionAlgo.makeShape());
138 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
139 loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
140 setResult(aResultBody, aResultIndex);
144 // Create result for each object.
145 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
146 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
147 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
148 GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
150 // Checking that the algorithm worked properly.
151 if (!aPartitionAlgo.isDone()) {
152 static const std::string aFeatureError = "Partition algorithm failed";
153 setError(aFeatureError);
156 if (aPartitionAlgo.shape()->isNull()) {
157 static const std::string aShapeError = "Resulting shape is Null";
158 setError(aShapeError);
161 if (!aPartitionAlgo.isValid()) {
162 std::string aFeatureError = "Warning: resulting shape is not valid";
163 setError(aFeatureError);
167 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
168 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
169 aMakeShapeList.append(aPartitionAlgo.makeShape());
170 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
171 loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
172 setResult(aResultBody, aResultIndex);
178 // remove the rest results if there were produced in the previous pass
179 removeResults(aResultIndex);
182 //=================================================================================================
183 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
184 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
185 const ListOfShape& theTools,
186 const std::shared_ptr<GeomAPI_Shape> theResultShape,
187 GeomAlgoAPI_MakeShape& theMakeShape,
188 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
191 if(theBaseShape->isEqual(theResultShape)) {
192 theResultBody->store(theResultShape);
194 const int aModifyTag = 1;
195 const int aDeletedTag = 2;
196 const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
198 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
200 std::string aModName = "Modified";
201 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
202 aModifyTag, aModName, theMapOfShapes);
203 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
205 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
206 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
207 aModifyTag, aModName, theMapOfShapes);
208 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);