1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Partition.h"
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_BodyBuilder.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Validator.h>
35 #include <GeomAlgoAPI_Boolean.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
37 #include <GeomAlgoAPI_MakeShapeCustom.h>
38 #include <GeomAlgoAPI_MakeShapeList.h>
39 #include <GeomAlgoAPI_Partition.h>
40 #include <GeomAlgoAPI_ShapeTools.h>
42 #include <GeomAPI_Face.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_ShapeIterator.h>
50 typedef std::list<std::pair<GeomShapePtr, ListOfShape> > CompsolidSubs;
52 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
53 const GeomShapePtr theResultShape,
54 const GeomAPI_Shape::ShapeType theShapeType,
55 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
57 static void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
58 CompsolidSubs& theObjects, ListOfShape& thePlanes);
60 static void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
61 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
63 static void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed);
65 static bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
66 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
67 std::string& theError);
69 static bool isAlgoFailed(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
70 std::string& theError);
73 //=================================================================================================
74 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
78 //=================================================================================================
79 void FeaturesPlugin_Partition::initAttributes()
81 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
84 //=================================================================================================
85 void FeaturesPlugin_Partition::execute()
87 CompsolidSubs anObjects;
91 pullObjectsAndPlanes(selectionList(BASE_OBJECTS_ID()), anObjects, aPlanes);
92 if(anObjects.empty()) {
93 static const std::string aFeatureError = "Error: No objects for partition.";
94 setError(aFeatureError);
98 ListOfShape aBaseObjects;
99 for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
100 aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
101 aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
103 // resize planes to the bounding box of operated shapes
104 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
105 resizePlanes(anObjects, aPlanes, aMakeShapeList);
107 // cut unused solids of composolids from the objects of partition
108 CompsolidSubs anUnusedSubs;
109 unusedSubsOfComposolid(anObjects, anUnusedSubs);
110 for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
111 aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
114 if (!cutUnusedSubs(anObjects, anUnusedSubs, aMakeShapeList, aError)) {
119 // perform partition first time to split target solids
120 ListOfShape aTargetObjects;
121 for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
122 aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
124 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
125 new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
127 // Checking that the algorithm worked properly.
128 if (isAlgoFailed(aPartitionAlgo, aError)) {
133 aMakeShapeList->appendAlgo(aPartitionAlgo);
134 GeomShapePtr aResultShape = aPartitionAlgo->shape();
136 if (!anUnusedSubs.empty()) {
137 // second pass of a partition to split shared faces of compsolids
138 aTargetObjects.clear();
139 aTargetObjects.push_back(aResultShape);
140 for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
141 aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
143 aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
145 // Checking that the algorithm worked properly.
146 if (isAlgoFailed(aPartitionAlgo, aError)) {
151 aMakeShapeList->appendAlgo(aPartitionAlgo);
152 aResultShape = aPartitionAlgo->shape();
155 int aResultIndex = 0;
156 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
157 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
158 storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
162 storeResult(aBaseObjects, aPlanes, aResultShape, 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 // Find base. The most complicated is the real modified object (#1799 if box is partitioned by
178 // two planes the box is the base, not planes, independently on the order in the list).
179 GeomShapePtr aBaseShape;
180 for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
181 GeomShapePtr anObjectShape = *anIt;
182 GeomShapePtr aCandidate =
183 findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
184 if(!aCandidate.get()) {
185 aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
187 if (!aCandidate.get())
188 aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
190 if(aCandidate.get()) {
191 if (!aBaseShape.get() || aBaseShape->shapeType() > aCandidate->shapeType()) {
192 aBaseShape = aCandidate;
197 // Create result body.
198 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
200 // Store modified shape.
201 if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
202 aResultBody->store(theResultShape, false);
203 setResult(aResultBody, theIndex);
207 aResultBody->storeModified(aBaseShape, theResultShape);
209 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
210 theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
211 for (ListOfShape::const_iterator anIt = theObjects.cbegin();
212 anIt != theObjects.cend();
215 GeomShapePtr aShape = *anIt;
216 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE, "Modified_Edge");
217 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE, "Modified_Face");
218 aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
221 setResult(aResultBody, theIndex);
225 //================= Auxiliary functions ===================================================
227 GeomShapePtr findBase(const GeomShapePtr theObjectShape,
228 const GeomShapePtr theResultShape,
229 const GeomAPI_Shape::ShapeType theShapeType,
230 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
232 GeomShapePtr aBaseShape;
233 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
234 for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
235 anObjectSubShapesExp.more();
236 anObjectSubShapesExp.next()) {
237 GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
238 ListOfShape aModifiedShapes;
239 theMakeShape->modified(anObjectSubShape, aModifiedShapes);
240 for(ListOfShape::const_iterator
241 aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
242 GeomShapePtr aModShape = *aModIt;
243 if(aMapOfSubShapes->isBound(aModShape)) {
244 aModShape = aMapOfSubShapes->find(aModShape);
246 if(theResultShape->isSubShape(aModShape)) {
247 aBaseShape = theObjectShape;
251 if(aBaseShape.get()) {
259 static CompsolidSubs::iterator findOrAdd(CompsolidSubs& theList, const GeomShapePtr& theCompsolid)
261 CompsolidSubs::iterator aFound = theList.begin();
262 for (; aFound != theList.end(); ++aFound)
263 if (aFound->first == theCompsolid)
265 if (aFound == theList.end()) {
266 theList.push_back(std::pair<GeomShapePtr, ListOfShape>(theCompsolid, ListOfShape()));
267 aFound = --theList.end();
272 void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
273 CompsolidSubs& theObjects, ListOfShape& thePlanes)
275 std::map<ResultBodyPtr, GeomShapePtr> aMapCompsolidShape;
277 int aSize = theSelectedList->size();
278 for (int anIndex = 0; anIndex < aSize; ++anIndex) {
279 AttributeSelectionPtr anObjectAttr = theSelectedList->value(anIndex);
280 ResultPtr aContext = anObjectAttr->context();
281 GeomShapePtr anObject = anObjectAttr->value();
283 GeomShapePtr anOwnerShape = anObject;
284 // check the result is a compsolid and store all used subs in a single list
285 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
286 if (aResCompSolidPtr && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
287 std::map<ResultBodyPtr, GeomShapePtr>::const_iterator
288 aFound = aMapCompsolidShape.find(aResCompSolidPtr);
289 if (aFound != aMapCompsolidShape.end())
290 anOwnerShape = aFound->second;
292 anOwnerShape = aResCompSolidPtr->shape();
293 aMapCompsolidShape[aResCompSolidPtr] = anOwnerShape;
297 CompsolidSubs::iterator aFound = findOrAdd(theObjects, anOwnerShape);
298 aFound->second.push_back(anObject);
301 // It could be a construction plane.
302 thePlanes.push_back(anObjectAttr->context()->shape());
307 void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
308 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
310 ListOfShape aSolidsInOperation;
311 for (CompsolidSubs::const_iterator anIt = theObjects.begin(); anIt != theObjects.end(); ++anIt)
312 aSolidsInOperation.insert(aSolidsInOperation.end(), anIt->second.begin(), anIt->second.end());
314 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
315 GeomAlgoAPI_ShapeTools::getBoundingBox(aSolidsInOperation, 1.0);
317 ListOfShape aPlanesCopy = thePlanes;
320 // Resize planes to fit in bounding box
321 for (ListOfShape::const_iterator anIt = aPlanesCopy.begin(); anIt != aPlanesCopy.end(); ++anIt) {
322 GeomShapePtr aPlane = *anIt;
323 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
324 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
325 aMkShCustom->addModified(aPlane, aTool);
326 theMakeShapeList->appendAlgo(aMkShCustom);
327 thePlanes.push_back(aTool);
331 void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed)
333 for (CompsolidSubs::const_iterator aCSIt = theObjects.begin();
334 aCSIt != theObjects.end(); ++aCSIt) {
335 if (aCSIt->first->shapeType() != GeomAPI_Shape::COMPSOLID)
338 // check the compsolid is selected
339 if (aCSIt->second.size() == 1 && aCSIt->first->isEqual(aCSIt->second.front()))
342 // process all sub-solids of compsolid
343 ListOfShape aNotUsedSolids;
344 for (GeomAPI_ShapeExplorer anExp(aCSIt->first, GeomAPI_Shape::SOLID);
345 anExp.more(); anExp.next()) {
346 GeomShapePtr aSolidInCompSolid = anExp.current();
347 ListOfShape::const_iterator anIt = aCSIt->second.begin();
348 for (; anIt != aCSIt->second.end(); ++anIt)
349 if (aSolidInCompSolid->isEqual(*anIt))
352 if (anIt == aCSIt->second.end())
353 aNotUsedSolids.push_back(aSolidInCompSolid);
356 if (!aNotUsedSolids.empty())
357 theNotUsed.push_back(std::pair<GeomShapePtr, ListOfShape>(aCSIt->first, aNotUsedSolids));
361 static bool cutSubs(const GeomShapePtr& theFirstArgument,
362 CompsolidSubs& theSubsToCut,
363 const ListOfShape& theTools,
364 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
365 std::string& theError)
367 if (theTools.empty())
370 std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo;
372 for (CompsolidSubs::iterator aUIt= theSubsToCut.begin(); aUIt != theSubsToCut.end(); ++aUIt) {
373 if (aUIt->first == theFirstArgument)
374 continue; // no need to split unused subs of the first compsolid
376 // cut from current list of solids
378 new GeomAlgoAPI_Boolean(aUIt->second, theTools, GeomAlgoAPI_Boolean::BOOL_CUT));
379 if (isAlgoFailed(aCutAlgo, theError))
381 theMakeShapeList->appendAlgo(aCutAlgo);
383 // update list of un-selected objects of the partition
384 GeomAPI_Shape::ShapeType aType = aUIt->second.front()->shapeType();
385 aUIt->second.clear();
386 for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
387 aUIt->second.push_back(anExp.current());
392 bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
393 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
394 std::string& theError)
396 GeomShapePtr aFirstArgument = theObjects.front().first;
398 // compose a set of tools for the CUT operation:
399 // find the list of unused subs of the first argument or use itself
400 ListOfShape aToolsForUsed;
401 CompsolidSubs::iterator aUIt = theNotUsed.begin();
402 for (; aUIt != theNotUsed.end(); ++aUIt)
403 if (aUIt->first == aFirstArgument) {
404 aToolsForUsed.insert(aToolsForUsed.end(), aUIt->second.begin(), aUIt->second.end());
407 ListOfShape aToolsForUnused;
408 aToolsForUnused.push_back(aFirstArgument);
411 return cutSubs(aFirstArgument, theObjects, aToolsForUsed, theMakeShapeList, theError)
412 && cutSubs(aFirstArgument, theNotUsed, aToolsForUnused, theMakeShapeList, theError);
415 bool isAlgoFailed(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo, std::string& theError)
417 if (!theAlgo->isDone()) {
418 theError = "Error: Partition algorithm failed.";
421 if (theAlgo->shape()->isNull()) {
422 theError = "Error: Resulting shape is Null.";
425 if (!theAlgo->isValid()) {
426 theError = "Error: Resulting shape is not valid.";