Salome HOME
Update copyrights
[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_Tools.h>
30 #include <GeomAPI_ShapeExplorer.h>
31
32 #include <sstream>
33
34 //=================================================================================================
35 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
36 {
37 }
38
39 //=================================================================================================
40 void FeaturesPlugin_Intersection::initAttributes()
41 {
42   data()->addAttribute(OBJECT_LIST_ID(),
43                        ModelAPI_AttributeSelectionList::typeId());
44 }
45
46 //=================================================================================================
47 void FeaturesPlugin_Intersection::execute()
48 {
49   ListOfShape anObjects;
50
51   // Getting objects.
52   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
53   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54     std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
55       anObjectsSelList->value(anObjectsIndex);
56     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
57     if (!anObject.get()) {
58       return;
59     }
60     anObjects.push_back(anObject);
61   }
62
63   if(anObjects.empty()) {
64     setError("Error: Objects or tools are empty.");
65     return;
66   }
67
68   int aResultIndex = 0;
69
70   // Create result.
71   GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects));
72
73   // Checking that the algorithm worked properly.
74   std::string anError;
75   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anIntersectionAlgo, getKind(), anError)) {
76     setError(anError);
77     return;
78   }
79
80   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
81   loadNamingDS(aResultBody, anObjects, anIntersectionAlgo);
82   setResult(aResultBody, aResultIndex);
83   aResultIndex++;
84
85
86   // remove the rest results if there were produced in the previous pass
87   removeResults(aResultIndex);
88 }
89
90 //=================================================================================================
91 void FeaturesPlugin_Intersection::loadNamingDS(ResultBodyPtr theResultBody,
92                                                const ListOfShape& theObjects,
93                                                const GeomMakeShapePtr& theMakeShape)
94 {
95   std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape->shape();
96
97   if(theObjects.front()->isEqual(aResultShape)) {
98     theResultBody->store(aResultShape, false);
99     return;
100   }
101
102   theResultBody->storeModified(theObjects.front(), aResultShape);
103
104   const int aShapeTypesNb = 3;
105   const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] = {GeomAPI_Shape::VERTEX,
106                                                                GeomAPI_Shape::EDGE,
107                                                                GeomAPI_Shape::FACE };
108   for (ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
109     const GeomShapePtr aShape = *anIt;
110     for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
111       theResultBody->loadModifiedShapes(theMakeShape, aShape, aShapeTypes[anIndex]);
112       theResultBody->loadGeneratedShapes(theMakeShape, aShape, aShapeTypes[anIndex]);
113     }
114   }
115 }