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 =
75 GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
79 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
80 for(ListOfShape::const_iterator anIt = aPlanes.cbegin(); anIt != aPlanes.cend(); ++anIt) {
81 GeomShapePtr aPlane = *anIt;
82 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
83 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
84 aMkShCustom->addModified(aPlane, aTool);
85 aMakeShapeList->appendAlgo(aMkShCustom);
86 aTools.push_back(aTool);
89 // Create single result.
90 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools));
92 // Checking that the algorithm worked properly.
93 if (!aPartitionAlgo->isDone()) {
94 static const std::string aFeatureError = "Error: Partition algorithm failed.";
95 setError(aFeatureError);
98 if (aPartitionAlgo->shape()->isNull()) {
99 static const std::string aShapeError = "Error: Resulting shape is Null.";
100 setError(aShapeError);
103 if (!aPartitionAlgo->isValid()) {
104 std::string aFeatureError = "Error: Resulting shape is not valid.";
105 setError(aFeatureError);
108 aMakeShapeList->appendAlgo(aPartitionAlgo);
109 GeomShapePtr aResultShape = aPartitionAlgo->shape();
111 int aResultIndex = 0;
112 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
113 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
114 storeResult(anObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
118 storeResult(anObjects, aPlanes, 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(
128 ListOfShape& theObjects, ListOfShape& thePlanes,
129 const GeomShapePtr theResultShape,
130 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
133 // Find base. The most complicated is the real modified object (#1799 if box is partitioned by
134 // two planes the box is the base, not planes, independently on the order in the list).
135 GeomShapePtr aBaseShape;
136 for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
137 GeomShapePtr anObjectShape = *anIt;
138 GeomShapePtr aCandidate =
139 findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
140 if(!aCandidate.get()) {
141 aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
143 if (!aCandidate.get())
144 aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
146 if(aCandidate.get()) {
147 if (!aBaseShape.get() || aBaseShape->shapeType() > aCandidate->shapeType()) {
148 aBaseShape = aCandidate;
153 // Create result body.
154 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
156 // Store modified shape.
157 if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
158 aResultBody->store(theResultShape);
159 setResult(aResultBody, theIndex);
163 const int aDelTag = 1;
164 const int aSubTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
165 int aModTag = aSubTag + 10000;
166 const std::string aModName = "Modified";
168 aResultBody->storeModified(aBaseShape, theResultShape, aSubTag);
170 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
171 theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
173 for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
174 GeomShapePtr aShape = *anIt;
175 std::string aModEdgeName = aModName + "_Edge_" + std::to_string((long long)anIndex);
176 std::string aModFaceName = aModName + "_Face_" + std::to_string((long long)anIndex++);
177 aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE,
178 aModTag, aModEdgeName, *aMapOfSubShapes.get(), true);
180 aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE,
181 aModTag, aModFaceName, *aMapOfSubShapes.get(), true);
183 aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE, aDelTag);
184 aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, aDelTag);
187 setResult(aResultBody, theIndex);
191 //=================================================================================================
192 GeomShapePtr findBase(const GeomShapePtr theObjectShape,
193 const GeomShapePtr theResultShape,
194 const GeomAPI_Shape::ShapeType theShapeType,
195 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
197 GeomShapePtr aBaseShape;
198 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
199 for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
200 anObjectSubShapesExp.more();
201 anObjectSubShapesExp.next()) {
202 GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
203 ListOfShape aModifiedShapes;
204 theMakeShape->modified(anObjectSubShape, aModifiedShapes);
205 for(ListOfShape::const_iterator aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
206 GeomShapePtr aModShape = *aModIt;
207 if(aMapOfSubShapes->isBound(aModShape)) {
208 aModShape = aMapOfSubShapes->find(aModShape);
210 if(theResultShape->isSubShape(aModShape)) {
211 aBaseShape = theObjectShape;
215 if(aBaseShape.get()) {