Salome HOME
Initialize the default method of Recover feature.
[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 {
38 }
39
40 void FeaturesPlugin_Recover::initAttributes()
41 {
42   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
43   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
44
45   data()->addAttribute(METHOD(), ModelAPI_AttributeString::typeId());
46   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), METHOD());
47   // set default method for recovering
48   string(METHOD())->setValue(METHOD_DEFAULT());
49 }
50
51 void FeaturesPlugin_Recover::execute()
52 {
53   std::string anError;
54   int aResultIndex = 0;
55   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
56   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
57     ObjectPtr anObj = aRecovered->object(anIndex);
58     if (!anObj.get())
59       continue;
60     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
61     if (!aResult.get())
62       continue;
63     GeomShapePtr aShape = aResult->shape();
64     if (!aShape.get())
65       continue;
66
67     // Copy shape.
68     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
69     // Check that algo is done.
70     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
71       setError(anError);
72       return;
73     }
74
75     // Store result.
76     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
77     aResultBody->store(aCopyAlgo->shape());//, aCopyAlgo.shape());
78
79     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX);
80     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::EDGE);
81     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::FACE);
82
83     setResult(aResultBody, aResultIndex);
84     ++aResultIndex;
85   }
86
87   removeResults(aResultIndex);
88 }
89
90 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
91 {
92   if (theID == METHOD())
93     reflist(RECOVERED_ENTITIES())->clear();
94 }