Salome HOME
Merge remote-tracking branch 'remotes/origin/occ/compounds_processing'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_Intersection.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_BodyBuilder.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27
28 #include <GeomAlgoAPI_Intersection.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_Tools.h>
31 #include <GeomAPI_ShapeExplorer.h>
32
33 #include <sstream>
34
35 static const std::string INTERSECTION_VERSION_1("v9.5");
36
37 //=================================================================================================
38 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
39 {
40 }
41
42 //=================================================================================================
43 void FeaturesPlugin_Intersection::initAttributes()
44 {
45   AttributePtr anObjectsAttr = data()->addAttribute(OBJECT_LIST_ID(),
46                        ModelAPI_AttributeSelectionList::typeId());
47
48   initVersion(INTERSECTION_VERSION_1, anObjectsAttr, AttributePtr());
49 }
50
51 //=================================================================================================
52 void FeaturesPlugin_Intersection::execute()
53 {
54   GeomAPI_ShapeHierarchy anObjectsHierarchy;
55   ListOfShape aPlanes;
56   // Getting objects.
57   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes)) {
58     setError("Error: Objects or tools are empty.");
59     return;
60   }
61
62   int aResultIndex = 0;
63
64   // Create result.
65   const ListOfShape& anObjects = anObjectsHierarchy.objects();
66   GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects));
67
68   // Checking that the algorithm worked properly.
69   std::string anError;
70   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anIntersectionAlgo, getKind(), anError)) {
71     setError(anError);
72     return;
73   }
74
75   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
76   aMakeShapeList->appendAlgo(anIntersectionAlgo);
77
78   GeomShapePtr aResShape = anIntersectionAlgo->shape();
79   if (data()->version() == INTERSECTION_VERSION_1) {
80     // merge hierarchies of compounds containing objects and tools
81     // and append the result of the FUSE operation
82     aResShape = keepUnusedSubsOfCompound(aResShape, anObjectsHierarchy,
83                                          GeomAPI_ShapeHierarchy(), aMakeShapeList);
84   }
85
86   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
87   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
88                                            anObjects,
89                                            ListOfShape(),
90                                            aMakeShapeList,
91                                            aResShape);
92   setResult(aResultBody, aResultIndex);
93   aResultIndex++;
94
95   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
96                                           GeomShapePtr(),
97                                           anObjects,
98                                           aMakeShapeList,
99                                           aResShape);
100
101   // remove the rest results if there were produced in the previous pass
102   removeResults(aResultIndex);
103 }