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>
52 //=================================================================================================
53 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
57 //=================================================================================================
58 void FeaturesPlugin_Partition::initAttributes()
60 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
61 initVersion(THE_VERSION_1, selectionList(BASE_OBJECTS_ID()));
64 //=================================================================================================
65 void FeaturesPlugin_Partition::execute()
67 ObjectHierarchy anObjects;
71 processAttribute(BASE_OBJECTS_ID(), anObjects, aPlanes);
72 if(anObjects.IsEmpty()) {
73 static const std::string aFeatureError = "Error: No objects for partition.";
74 setError(aFeatureError);
78 ListOfShape aBaseObjects = anObjects.Objects();
79 aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
81 // resize planes to the bounding box of operated shapes
82 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
83 resizePlanes(anObjects.Objects(), aPlanes, aMakeShapeList);
85 // cut unused solids of composolids from the objects of partition
86 ListOfShape aTargetObjects, anUnusedSubs;
88 if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aMakeShapeList, aError)) {
92 aBaseObjects.insert(aBaseObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
94 // perform partition first time to split target solids by planes
95 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
96 new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
98 // Checking that the algorithm worked properly.
99 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
104 aMakeShapeList->appendAlgo(aPartitionAlgo);
105 GeomShapePtr aResultShape = aPartitionAlgo->shape();
107 if (!anUnusedSubs.empty()) {
108 // second pass of a partition to split shared faces of compsolids
109 aTargetObjects.clear();
110 aTargetObjects.push_back(aResultShape);
111 aTargetObjects.insert(aTargetObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
113 aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
115 // Checking that the algorithm worked properly.
116 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
121 aMakeShapeList->appendAlgo(aPartitionAlgo);
122 aResultShape = aPartitionAlgo->shape();
125 int aResultIndex = 0;
127 int aPartitionVersion = version();
128 if (aPartitionVersion < THE_VERSION_1) {
129 // default behaviors of Partition
130 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
131 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
132 storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
136 storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
141 // merge hierarchies of compounds containing objects and tools
142 GeomShapePtr aFirstShape = aResultShape;
143 GeomAPI_ShapeIterator anIt;
144 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
145 anIt = GeomAPI_ShapeIterator(aResultShape);
146 aFirstShape = anIt.current();
150 GeomShapePtr aResultCompound =
151 keepUnusedSubsOfCompound(aFirstShape, anObjects, ObjectHierarchy(), aMakeShapeList);
154 if (aResultCompound->shapeType() != GeomAPI_Shape::COMPOUND) {
155 // put the shape into compound
157 aShapes.push_back(aResultCompound);
158 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
160 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
161 for (; anIt.more(); anIt.next())
162 aBuilder->add(aResultCompound, anIt.current());
163 aMakeShapeList->appendAlgo(aBuilder);
166 storeResult(aBaseObjects, aPlanes, aResultCompound, aMakeShapeList, aResultIndex);
170 // Remove the rest results if there were produced in the previous pass.
171 removeResults(aResultIndex);
174 //=================================================================================================
175 void FeaturesPlugin_Partition::storeResult(
176 ListOfShape& theObjects, ListOfShape& thePlanes,
177 const GeomShapePtr theResultShape,
178 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
181 // Create result body.
182 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
184 // if result is same as one of the base object, no modification was performed
185 for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
187 if (anObj->get() && (*anObj)->isSame(theResultShape)) {
188 aResultBody->store(theResultShape, false);
189 setResult(aResultBody, theIndex);
194 aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
196 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
197 theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
198 for (ListOfShape::const_iterator anIt = theObjects.cbegin();
199 anIt != theObjects.cend();
202 GeomShapePtr aShape = *anIt;
203 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
204 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
205 aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
208 setResult(aResultBody, theIndex);
212 //=================================================================================================
214 static bool cutSubs(ListOfShape& theSubsToCut,
215 const ListOfShape& theTools,
216 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
217 std::string& theError)
219 if (theSubsToCut.empty() || theTools.empty())
222 // cut from current list of solids
223 std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo(
224 new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
225 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
227 theMakeShapeList->appendAlgo(aCutAlgo);
229 // update list of un-selected objects of the partition
230 GeomAPI_Shape::ShapeType aType = theSubsToCut.front()->shapeType();
231 theSubsToCut.clear();
232 for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
233 theSubsToCut.push_back(anExp.current());
237 bool FeaturesPlugin_Partition::cutSubs(
238 FeaturesPlugin_Partition::ObjectHierarchy& theHierarchy,
239 ListOfShape& theUsed,
240 ListOfShape& theNotUsed,
241 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
242 std::string& theError)
247 ObjectHierarchy::Iterator anIt = theHierarchy.Begin();
249 // compose a set of tools for the CUT operation:
250 // find the list of unused subs of the first argument or use itself
251 ListOfShape aToolsForUsed, aToolsForUnused;
252 GeomShapePtr aFirstArgument = theHierarchy.Parent(*anIt, false);
253 if (aFirstArgument && aFirstArgument->shapeType() == GeomAPI_Shape::COMPSOLID) {
254 theHierarchy.SplitCompound(aFirstArgument, theUsed, aToolsForUsed);
255 theNotUsed = aToolsForUsed;
258 aFirstArgument = *anIt;
259 theUsed.push_back(aFirstArgument);
261 aToolsForUnused.push_back(aFirstArgument);
265 for (++anIt; anIt != theHierarchy.End() && isOk; ++anIt) {
266 ListOfShape aUsed, aNotUsed;
268 GeomShapePtr aParent = theHierarchy.Parent(*anIt, false);
269 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
270 aParent = theHierarchy.Parent(*anIt); // get parent once again to mark its subs as processed
271 theHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
274 aUsed.push_back(*anIt);
276 isOk = ::cutSubs(aUsed, aToolsForUsed, theMakeShapeList, theError)
277 && ::cutSubs(aNotUsed, aToolsForUnused, theMakeShapeList, theError);
279 theUsed.insert(theUsed.end(), aUsed.begin(), aUsed.end());
280 theNotUsed.insert(theNotUsed.end(), aNotUsed.begin(), aNotUsed.end());