1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Partition.h"
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Validator.h>
34 #include <GeomAlgoAPI_Boolean.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_MakeShapeCustom.h>
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_Partition.h>
39 #include <GeomAlgoAPI_ShapeBuilder.h>
40 #include <GeomAlgoAPI_ShapeTools.h>
41 #include <GeomAlgoAPI_Tools.h>
43 #include <GeomAPI_Face.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAPI_ShapeIterator.h>
51 static const int THE_PARTITION_VERSION_1 = 20190506;
54 //=================================================================================================
55 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
59 //=================================================================================================
60 void FeaturesPlugin_Partition::initAttributes()
62 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
63 initVersion(THE_PARTITION_VERSION_1, selectionList(BASE_OBJECTS_ID()));
66 //=================================================================================================
67 void FeaturesPlugin_Partition::execute()
69 ObjectHierarchy anObjects;
73 processAttribute(BASE_OBJECTS_ID(), anObjects, aPlanes);
74 if(anObjects.IsEmpty()) {
75 static const std::string aFeatureError = "Error: No objects for partition.";
76 setError(aFeatureError);
80 ListOfShape aBaseObjects = anObjects.Objects();
81 aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
83 // resize planes to the bounding box of operated shapes
84 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
85 resizePlanes(anObjects.Objects(), aPlanes, aMakeShapeList);
87 // cut unused solids of composolids from the objects of partition
88 ListOfShape aTargetObjects, anUnusedSubs;
90 if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aMakeShapeList, aError)) {
94 aBaseObjects.insert(aBaseObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
96 // perform partition first time to split target solids by planes
97 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
98 new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
100 // Checking that the algorithm worked properly.
101 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
106 aMakeShapeList->appendAlgo(aPartitionAlgo);
107 GeomShapePtr aResultShape = aPartitionAlgo->shape();
109 if (!anUnusedSubs.empty()) {
110 // second pass of a partition to split shared faces of compsolids
111 aTargetObjects.clear();
112 aTargetObjects.push_back(aResultShape);
113 aTargetObjects.insert(aTargetObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
115 aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
117 // Checking that the algorithm worked properly.
118 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
123 aMakeShapeList->appendAlgo(aPartitionAlgo);
124 aResultShape = aPartitionAlgo->shape();
127 int aResultIndex = 0;
129 int aPartitionVersion = version();
130 if (aPartitionVersion < THE_PARTITION_VERSION_1) {
131 // default behaviours of Partition
132 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
133 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
134 storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
138 storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
143 // merge hierarchies of compounds containing objects and tools
144 GeomShapePtr aFirstShape = aResultShape;
145 GeomAPI_ShapeIterator anIt;
146 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
147 anIt = GeomAPI_ShapeIterator(aResultShape);
148 aFirstShape = anIt.current();
152 GeomShapePtr aResultCompound =
153 keepUnusedSubsOfCompound(aFirstShape, anObjects, ObjectHierarchy(), aMakeShapeList);
156 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
157 for (; anIt.more(); anIt.next())
158 aBuilder->add(aResultCompound, anIt.current());
159 aMakeShapeList->appendAlgo(aBuilder);
162 storeResult(aBaseObjects, aPlanes, aResultCompound, aMakeShapeList, aResultIndex);
166 // Remove the rest results if there were produced in the previous pass.
167 removeResults(aResultIndex);
170 //=================================================================================================
171 void FeaturesPlugin_Partition::storeResult(
172 ListOfShape& theObjects, ListOfShape& thePlanes,
173 const GeomShapePtr theResultShape,
174 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
177 // Create result body.
178 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
180 // if result is same as one of the base object, no modification was performed
181 for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
183 if (anObj->get() && (*anObj)->isSame(theResultShape)) {
184 aResultBody->store(theResultShape, false);
185 setResult(aResultBody, theIndex);
190 aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
192 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
193 theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
194 for (ListOfShape::const_iterator anIt = theObjects.cbegin();
195 anIt != theObjects.cend();
198 GeomShapePtr aShape = *anIt;
199 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
200 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
201 aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
204 setResult(aResultBody, theIndex);
208 //=================================================================================================
210 static bool cutSubs(ListOfShape& theSubsToCut,
211 const ListOfShape& theTools,
212 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
213 std::string& theError)
215 if (theSubsToCut.empty() || theTools.empty())
218 // cut from current list of solids
219 std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo(
220 new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
221 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
223 theMakeShapeList->appendAlgo(aCutAlgo);
225 // update list of un-selected objects of the partition
226 GeomAPI_Shape::ShapeType aType = theSubsToCut.front()->shapeType();
227 theSubsToCut.clear();
228 for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
229 theSubsToCut.push_back(anExp.current());
233 bool FeaturesPlugin_Partition::cutSubs(
234 FeaturesPlugin_Partition::ObjectHierarchy& theHierarchy,
235 ListOfShape& theUsed,
236 ListOfShape& theNotUsed,
237 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
238 std::string& theError)
243 ObjectHierarchy::Iterator anIt = theHierarchy.Begin();
245 // compose a set of tools for the CUT operation:
246 // find the list of unused subs of the first argument or use itself
247 ListOfShape aToolsForUsed, aToolsForUnused;
248 GeomShapePtr aFirstArgument = theHierarchy.Parent(*anIt, false);
249 if (aFirstArgument && aFirstArgument->shapeType() == GeomAPI_Shape::COMPSOLID) {
250 theHierarchy.SplitCompound(aFirstArgument, theUsed, aToolsForUsed);
251 theNotUsed = aToolsForUsed;
254 aFirstArgument = *anIt;
255 theUsed.push_back(aFirstArgument);
257 aToolsForUnused.push_back(aFirstArgument);
261 for (++anIt; anIt != theHierarchy.End() && isOk; ++anIt) {
262 ListOfShape aUsed, aNotUsed;
264 GeomShapePtr aParent = theHierarchy.Parent(*anIt, false);
265 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
266 aParent = theHierarchy.Parent(*anIt); // get parent once again to mark its subs as processed
267 theHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
270 aUsed.push_back(*anIt);
272 isOk = ::cutSubs(aUsed, aToolsForUsed, theMakeShapeList, theError)
273 && ::cutSubs(aNotUsed, aToolsForUnused, theMakeShapeList, theError);
275 theUsed.insert(theUsed.end(), aUsed.begin(), aUsed.end());
276 theNotUsed.insert(theNotUsed.end(), aNotUsed.begin(), aNotUsed.end());