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