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>
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
33 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
34 const GeomShapePtr theResultShape,
35 const GeomAPI_Shape::ShapeType theShapeType,
36 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
38 //=================================================================================================
39 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
43 //=================================================================================================
44 void FeaturesPlugin_Partition::initAttributes()
46 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
49 //=================================================================================================
50 void FeaturesPlugin_Partition::execute()
52 ListOfShape anObjects, aPlanes;
55 AttributeSelectionListPtr anObjectsSelList = selectionList(BASE_OBJECTS_ID());
56 for(int anIndex = 0; anIndex < anObjectsSelList->size(); ++anIndex) {
57 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anIndex);
58 GeomShapePtr anObject = anObjectAttr->value();
60 // It could be a construction plane.
61 ResultPtr aContext = anObjectAttr->context();
62 aPlanes.push_back(anObjectAttr->context()->shape());
64 anObjects.push_back(anObject);
68 if(anObjects.empty()) {
69 static const std::string aFeatureError = "Error: No objects for partition.";
70 setError(aFeatureError);
74 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
78 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
79 for(ListOfShape::const_iterator anIt = aPlanes.cbegin(); anIt != aPlanes.cend(); ++anIt) {
80 GeomShapePtr aPlane = *anIt;
81 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
82 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
83 aMkShCustom->addModified(aPlane, aTool);
84 aMakeShapeList->appendAlgo(aMkShCustom);
85 aTools.push_back(aTool);
88 // Create single result.
89 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools));
91 // Checking that the algorithm worked properly.
92 if (!aPartitionAlgo->isDone()) {
93 static const std::string aFeatureError = "Error: Partition algorithm failed.";
94 setError(aFeatureError);
97 if (aPartitionAlgo->shape()->isNull()) {
98 static const std::string aShapeError = "Error: Resulting shape is Null.";
99 setError(aShapeError);
102 if (!aPartitionAlgo->isValid()) {
103 std::string aFeatureError = "Error: Resulting shape is not valid.";
104 setError(aFeatureError);
107 aMakeShapeList->appendAlgo(aPartitionAlgo);
108 GeomShapePtr aResultShape = aPartitionAlgo->shape();
110 int aResultIndex = 0;
111 anObjects.insert(anObjects.end(), aPlanes.begin(), aPlanes.end());
112 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
113 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
114 storeResult(anObjects, anIt.current(), aMakeShapeList, aResultIndex);
118 storeResult(anObjects, aResultShape, aMakeShapeList, aResultIndex);
122 // Remove the rest results if there were produced in the previous pass.
123 removeResults(aResultIndex);
126 //=================================================================================================
127 void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects,
128 const GeomShapePtr theResultShape,
129 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
133 GeomShapePtr aBaseShape;
134 for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
135 GeomShapePtr anObjectShape = *anIt;
136 aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
137 if(!aBaseShape.get()) {
138 aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
140 if(!aBaseShape.get()) {
141 aBaseShape = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
143 if(aBaseShape.get()) {
148 // Create result body.
149 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
151 // Store modified shape.
152 if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
153 aResultBody->store(theResultShape);
154 setResult(aResultBody, theIndex);
158 const int aDelTag = 1;
159 const int aSubTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
160 int aModTag = aSubTag + 10000;
161 const std::string aModName = "Modified";
163 aResultBody->storeModified(aBaseShape, theResultShape, aSubTag);
165 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
167 for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
168 GeomShapePtr aShape = *anIt;
169 std::string aModEdgeName = aModName + "_Edge_" + std::to_string((long long)anIndex);
170 std::string aModFaceName = aModName + "_Face_" + std::to_string((long long)anIndex++);
171 aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE,
172 aModTag, aModEdgeName, *aMapOfSubShapes.get(), true);
174 aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE,
175 aModTag, aModFaceName, *aMapOfSubShapes.get(), true);
177 aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE, aDelTag);
178 aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, aDelTag);
181 setResult(aResultBody, theIndex);
185 //=================================================================================================
186 GeomShapePtr findBase(const GeomShapePtr theObjectShape,
187 const GeomShapePtr theResultShape,
188 const GeomAPI_Shape::ShapeType theShapeType,
189 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
191 GeomShapePtr aBaseShape;
192 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
193 for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
194 anObjectSubShapesExp.more();
195 anObjectSubShapesExp.next()) {
196 GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
197 ListOfShape aModifiedShapes;
198 theMakeShape->modified(anObjectSubShape, aModifiedShapes);
199 for(ListOfShape::const_iterator aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
200 GeomShapePtr aModShape = *aModIt;
201 if(aMapOfSubShapes->isBound(aModShape)) {
202 aModShape = aMapOfSubShapes->find(aModShape);
204 if(theResultShape->isSubShape(aModShape)) {
205 aBaseShape = theObjectShape;
209 if(aBaseShape.get()) {