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>
41 #include <GeomAlgoAPI_Tools.h>
43 #include <GeomAPI_Face.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAPI_ShapeIterator.h>
51 typedef std::list<std::pair<GeomShapePtr, ListOfShape> > CompsolidSubs;
53 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
54 const GeomShapePtr theResultShape,
55 const GeomAPI_Shape::ShapeType theShapeType,
56 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
58 static void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
59 CompsolidSubs& theObjects, ListOfShape& thePlanes);
61 static void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
62 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
64 static void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed);
66 static bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
67 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
68 std::string& theError);
71 //=================================================================================================
72 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
76 //=================================================================================================
77 void FeaturesPlugin_Partition::initAttributes()
79 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
82 //=================================================================================================
83 void FeaturesPlugin_Partition::execute()
85 CompsolidSubs anObjects;
89 pullObjectsAndPlanes(selectionList(BASE_OBJECTS_ID()), anObjects, aPlanes);
90 if(anObjects.empty()) {
91 static const std::string aFeatureError = "Error: No objects for partition.";
92 setError(aFeatureError);
96 ListOfShape aBaseObjects;
97 for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
98 aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
99 aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
101 // resize planes to the bounding box of operated shapes
102 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
103 resizePlanes(anObjects, aPlanes, aMakeShapeList);
105 // cut unused solids of composolids from the objects of partition
106 CompsolidSubs anUnusedSubs;
107 unusedSubsOfComposolid(anObjects, anUnusedSubs);
108 for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
109 aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
112 if (!cutUnusedSubs(anObjects, anUnusedSubs, aMakeShapeList, aError)) {
117 // perform partition first time to split target solids
118 ListOfShape aTargetObjects;
119 for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
120 aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
122 std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
123 new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
125 // Checking that the algorithm worked properly.
126 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
131 aMakeShapeList->appendAlgo(aPartitionAlgo);
132 GeomShapePtr aResultShape = aPartitionAlgo->shape();
134 if (!anUnusedSubs.empty()) {
135 // second pass of a partition to split shared faces of compsolids
136 aTargetObjects.clear();
137 aTargetObjects.push_back(aResultShape);
138 for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
139 aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
141 aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
143 // Checking that the algorithm worked properly.
144 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
149 aMakeShapeList->appendAlgo(aPartitionAlgo);
150 aResultShape = aPartitionAlgo->shape();
153 int aResultIndex = 0;
154 if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
155 for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
156 storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
160 storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
164 // Remove the rest results if there were produced in the previous pass.
165 removeResults(aResultIndex);
168 //=================================================================================================
169 void FeaturesPlugin_Partition::storeResult(
170 ListOfShape& theObjects, ListOfShape& thePlanes,
171 const GeomShapePtr theResultShape,
172 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
175 // Create result body.
176 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
178 // if result is same as one of the base object, no modification was performed
179 for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
181 if (anObj->get() && (*anObj)->isSame(theResultShape)) {
182 aResultBody->store(theResultShape, false);
183 setResult(aResultBody, theIndex);
188 aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
190 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
191 theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
192 for (ListOfShape::const_iterator anIt = theObjects.cbegin();
193 anIt != theObjects.cend();
196 GeomShapePtr aShape = *anIt;
197 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
198 aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
199 aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
202 setResult(aResultBody, theIndex);
206 //================= Auxiliary functions ===================================================
208 static CompsolidSubs::iterator findOrAdd(CompsolidSubs& theList, const GeomShapePtr& theCompsolid)
210 CompsolidSubs::iterator aFound = theList.begin();
211 for (; aFound != theList.end(); ++aFound)
212 if (aFound->first == theCompsolid)
214 if (aFound == theList.end()) {
215 theList.push_back(std::pair<GeomShapePtr, ListOfShape>(theCompsolid, ListOfShape()));
216 aFound = --theList.end();
221 void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
222 CompsolidSubs& theObjects, ListOfShape& thePlanes)
224 std::map<ResultBodyPtr, GeomShapePtr> aMapCompsolidShape;
226 int aSize = theSelectedList->size();
227 for (int anIndex = 0; anIndex < aSize; ++anIndex) {
228 AttributeSelectionPtr anObjectAttr = theSelectedList->value(anIndex);
229 ResultPtr aContext = anObjectAttr->context();
230 GeomShapePtr anObject = anObjectAttr->value();
232 GeomShapePtr anOwnerShape = anObject;
233 // check the result is a compsolid and store all used subs in a single list
234 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
235 if (aResCompSolidPtr && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
236 std::map<ResultBodyPtr, GeomShapePtr>::const_iterator
237 aFound = aMapCompsolidShape.find(aResCompSolidPtr);
238 if (aFound != aMapCompsolidShape.end())
239 anOwnerShape = aFound->second;
241 anOwnerShape = aResCompSolidPtr->shape();
242 aMapCompsolidShape[aResCompSolidPtr] = anOwnerShape;
246 CompsolidSubs::iterator aFound = findOrAdd(theObjects, anOwnerShape);
247 aFound->second.push_back(anObject);
250 // It could be a construction plane.
251 thePlanes.push_back(anObjectAttr->context()->shape());
256 void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
257 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
259 ListOfShape aSolidsInOperation;
260 for (CompsolidSubs::const_iterator anIt = theObjects.begin(); anIt != theObjects.end(); ++anIt)
261 aSolidsInOperation.insert(aSolidsInOperation.end(), anIt->second.begin(), anIt->second.end());
263 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
264 GeomAlgoAPI_ShapeTools::getBoundingBox(aSolidsInOperation, 1.0);
266 ListOfShape aPlanesCopy = thePlanes;
269 // Resize planes to fit in bounding box
270 for (ListOfShape::const_iterator anIt = aPlanesCopy.begin(); anIt != aPlanesCopy.end(); ++anIt) {
271 GeomShapePtr aPlane = *anIt;
272 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
273 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
274 aMkShCustom->addModified(aPlane, aTool);
275 theMakeShapeList->appendAlgo(aMkShCustom);
276 thePlanes.push_back(aTool);
280 void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed)
282 for (CompsolidSubs::const_iterator aCSIt = theObjects.begin();
283 aCSIt != theObjects.end(); ++aCSIt) {
284 if (aCSIt->first->shapeType() != GeomAPI_Shape::COMPSOLID)
287 // check the compsolid is selected
288 if (aCSIt->second.size() == 1 && aCSIt->first->isEqual(aCSIt->second.front()))
291 // process all sub-solids of compsolid
292 ListOfShape aNotUsedSolids;
293 for (GeomAPI_ShapeExplorer anExp(aCSIt->first, GeomAPI_Shape::SOLID);
294 anExp.more(); anExp.next()) {
295 GeomShapePtr aSolidInCompSolid = anExp.current();
296 ListOfShape::const_iterator anIt = aCSIt->second.begin();
297 for (; anIt != aCSIt->second.end(); ++anIt)
298 if (aSolidInCompSolid->isEqual(*anIt))
301 if (anIt == aCSIt->second.end())
302 aNotUsedSolids.push_back(aSolidInCompSolid);
305 if (!aNotUsedSolids.empty())
306 theNotUsed.push_back(std::pair<GeomShapePtr, ListOfShape>(aCSIt->first, aNotUsedSolids));
310 static bool cutSubs(const GeomShapePtr& theFirstArgument,
311 CompsolidSubs& theSubsToCut,
312 const ListOfShape& theTools,
313 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
314 std::string& theError)
316 if (theTools.empty())
319 std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo;
321 for (CompsolidSubs::iterator aUIt= theSubsToCut.begin(); aUIt != theSubsToCut.end(); ++aUIt) {
322 if (aUIt->first == theFirstArgument)
323 continue; // no need to split unused subs of the first compsolid
325 // cut from current list of solids
327 new GeomAlgoAPI_Boolean(aUIt->second, theTools, GeomAlgoAPI_Boolean::BOOL_CUT));
328 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
330 theMakeShapeList->appendAlgo(aCutAlgo);
332 // update list of un-selected objects of the partition
333 GeomAPI_Shape::ShapeType aType = aUIt->second.front()->shapeType();
334 aUIt->second.clear();
335 for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
336 aUIt->second.push_back(anExp.current());
341 bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
342 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
343 std::string& theError)
345 GeomShapePtr aFirstArgument = theObjects.front().first;
347 // compose a set of tools for the CUT operation:
348 // find the list of unused subs of the first argument or use itself
349 ListOfShape aToolsForUsed;
350 CompsolidSubs::iterator aUIt = theNotUsed.begin();
351 for (; aUIt != theNotUsed.end(); ++aUIt)
352 if (aUIt->first == aFirstArgument) {
353 aToolsForUsed.insert(aToolsForUsed.end(), aUIt->second.begin(), aUIt->second.end());
356 ListOfShape aToolsForUnused;
357 aToolsForUnused.push_back(aFirstArgument);
360 return cutSubs(aFirstArgument, theObjects, aToolsForUsed, theMakeShapeList, theError)
361 && cutSubs(aFirstArgument, theNotUsed, aToolsForUnused, theMakeShapeList, theError);