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_CompoundBuilder.h>
21 #include <GeomAlgoAPI_Partition.h>
22 #include <GeomAlgoAPI_MakeShapeCustom.h>
23 #include <GeomAlgoAPI_MakeShapeList.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
28 //=================================================================================================
29 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
33 //=================================================================================================
34 void FeaturesPlugin_Partition::initAttributes()
36 AttributeSelectionListPtr aSelection =
37 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
38 FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
39 aSelection->setSelectionType("SOLID");
42 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
43 FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
44 aSelection->setSelectionType("SOLID");
46 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
48 data()->addAttribute(COMBINE_ID(), ModelAPI_AttributeBoolean::typeId());
51 //=================================================================================================
52 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::string& theAttrName)
54 std::shared_ptr<ModelAPI_AttributeReference> aObjRef =
55 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(data()->attribute(theAttrName));
57 std::shared_ptr<ModelAPI_ResultBody> aConstr =
58 std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObjRef->value());
60 return aConstr->shape();
62 return std::shared_ptr<GeomAPI_Shape>();
65 //=================================================================================================
66 void FeaturesPlugin_Partition::execute()
68 ListOfShape anObjects, aTools, aToolsForNaming;
71 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
72 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
73 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
74 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
75 if (!anObject.get()) {
78 anObjects.push_back(anObject);
81 GeomAlgoAPI_MakeShapeList aMakeShapeList;
82 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
85 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::TOOL_LIST_ID());
86 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
87 std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
88 std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
90 // it could be a construction plane
91 ResultPtr aContext = aToolAttr->context();
93 aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aContext->shape(), aBoundingPoints);
94 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
95 aMkShCustom->addModified(aContext->shape(), aTool);
96 aMakeShapeList.append(aMkShCustom);
97 aTools.push_back(aTool);
98 aToolsForNaming.push_back(aContext->shape());
101 aTools.push_back(aTool);
102 aToolsForNaming.push_back(aTool);
106 // Getting combine flag.
107 bool isCombine = boolean(COMBINE_ID())->value();
109 if(anObjects.empty()/* || aTools.empty()*/) {
110 std::string aFeatureError = "Not enough objects for partition operation";
111 setError(aFeatureError);
115 int aResultIndex = 0;
118 // Create single result.
119 std::shared_ptr<GeomAPI_Shape> aCompoud = GeomAlgoAPI_CompoundBuilder::compound(anObjects);
121 anObjects.push_back(aCompoud);
122 GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
124 // Checking that the algorithm worked properly.
125 if (!aPartitionAlgo.isDone()) {
126 static const std::string aFeatureError = "Partition algorithm failed";
127 setError(aFeatureError);
130 if (aPartitionAlgo.shape()->isNull()) {
131 static const std::string aShapeError = "Resulting shape is Null";
132 setError(aShapeError);
135 if (!aPartitionAlgo.isValid()) {
136 std::string aFeatureError = "Warning: resulting shape is not valid";
137 setError(aFeatureError);
141 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
142 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
143 aMakeShapeList.append(aPartitionAlgo.makeShape());
144 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
145 loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
146 setResult(aResultBody, aResultIndex);
150 // Create result for each object.
151 for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
152 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
153 ListOfShape aListWithObject; aListWithObject.push_back(anObject);
154 GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
156 // Checking that the algorithm worked properly.
157 if (!aPartitionAlgo.isDone()) {
158 static const std::string aFeatureError = "Partition algorithm failed";
159 setError(aFeatureError);
162 if (aPartitionAlgo.shape()->isNull()) {
163 static const std::string aShapeError = "Resulting shape is Null";
164 setError(aShapeError);
167 if (!aPartitionAlgo.isValid()) {
168 std::string aFeatureError = "Warning: resulting shape is not valid";
169 setError(aFeatureError);
173 if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
174 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
175 aMakeShapeList.append(aPartitionAlgo.makeShape());
176 GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
177 loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
178 setResult(aResultBody, aResultIndex);
184 // remove the rest results if there were produced in the previous pass
185 removeResults(aResultIndex);
188 //=================================================================================================
189 void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
190 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
191 const ListOfShape& theTools,
192 const std::shared_ptr<GeomAPI_Shape> theResultShape,
193 GeomAlgoAPI_MakeShape& theMakeShape,
194 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
197 if(theBaseShape->isEqual(theResultShape)) {
198 theResultBody->store(theResultShape);
200 const int aDeletedTag = 1;
201 const int aSubsolidsTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
202 const int aModifyTag = 100000;
203 int aModifyToolsTag = 200000;
204 std::ostringstream aStream;
206 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
208 std::string aModName = "Modified";
209 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
210 aModifyTag, aModName, theMapOfShapes, true);
211 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
214 for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
215 aStream.str(std::string());
217 aStream << aModName << "_" << anIndex++;
218 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
219 aModifyToolsTag, aStream.str(), theMapOfShapes, true);
220 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
221 aModifyToolsTag += 10000;