Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Recover.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_AttributeBoolean.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_Tools.h>
33 #include <GeomAlgoAPI_Copy.h>
34
35 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
36 {
37 }
38
39 void FeaturesPlugin_Recover::initAttributes()
40 {
41   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
42   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
43
44 }
45
46 void FeaturesPlugin_Recover::execute()
47 {
48   int aResultIndex = 0;
49   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
50   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
51     ObjectPtr anObj = aRecovered->object(anIndex);
52     if (!anObj.get())
53       continue;
54     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
55     if (!aResult.get())
56       continue;
57     GeomShapePtr aShape = aResult->shape();
58     if (!aShape.get())
59       continue;
60
61     // Copy shape.
62     GeomAlgoAPI_Copy aCopyAlgo(aShape);
63     // Check that algo is done.
64     if(!aCopyAlgo.isDone()) {
65       setError("Error: recover algorithm failed.");
66       return;
67     }
68     // Check if shape is not null.
69     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
70       setError("Error: resulting shape is null.");
71       return;
72     }
73     // Check that resulting shape is valid.
74     if(!aCopyAlgo.isValid()) {
75       setError("Error: resulting shape is not valid.");
76       return;
77     }
78
79     // Store result.
80     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
81     aResultBody->store(aCopyAlgo.shape());//, aCopyAlgo.shape());
82     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aCopyAlgo.mapOfSubShapes();
83     // like in import: forget any history
84     int aTag(1);
85     std::string aNameMS = "Shape";
86     aResultBody->loadFirstLevel(aCopyAlgo.shape(), aNameMS, aTag);
87
88     setResult(aResultBody, aResultIndex);
89     ++aResultIndex;
90   }
91
92   removeResults(aResultIndex);
93 }