Salome HOME
Merge remote-tracking branch 'origin/ngo/macros'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.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_Recover.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeString.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 #include <GeomAlgoAPI_Tools.h>
35
36 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
37   : myClearListOnTypeChange(true)
38 {
39 }
40
41 void FeaturesPlugin_Recover::initAttributes()
42 {
43   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
44   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
45
46   data()->addAttribute(METHOD(), ModelAPI_AttributeString::typeId());
47   if (!string(METHOD())->isInitialized()) {
48     myClearListOnTypeChange = false;
49     data()->blockSendAttributeUpdated(true, false);
50     string(METHOD())->setValue(METHOD_DEFAULT());
51     data()->blockSendAttributeUpdated(false, false);
52     myClearListOnTypeChange = true;
53   }
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), METHOD());
55   // set default method for recovering
56   string(METHOD())->setValue(METHOD_DEFAULT());
57 }
58
59 void FeaturesPlugin_Recover::execute()
60 {
61   std::string anError;
62   int aResultIndex = 0;
63   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
64   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
65     ObjectPtr anObj = aRecovered->object(anIndex);
66     if (!anObj.get())
67       continue;
68     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
69     if (!aResult.get())
70       continue;
71     GeomShapePtr aShape = aResult->shape();
72     if (!aShape.get())
73       continue;
74
75     // Copy shape.
76     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
77     // Check that algo is done.
78     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
79       setError(anError);
80       return;
81     }
82
83     // Store result.
84     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
85     aResultBody->store(aCopyAlgo->shape());//, aCopyAlgo.shape());
86
87     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX);
88     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::EDGE);
89     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::FACE);
90
91     setResult(aResultBody, aResultIndex);
92     ++aResultIndex;
93   }
94
95   removeResults(aResultIndex);
96 }
97
98 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
99 {
100   if (theID == METHOD() && myClearListOnTypeChange)
101     reflist(RECOVERED_ENTITIES())->clear();
102 }