Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 }
56
57 void FeaturesPlugin_Recover::execute()
58 {
59   std::string anError;
60   int aResultIndex = 0;
61   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
62   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
63     ObjectPtr anObj = aRecovered->object(anIndex);
64     if (!anObj.get())
65       continue;
66     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
67     if (!aResult.get())
68       continue;
69     GeomShapePtr aShape = aResult->shape();
70     if (!aShape.get())
71       continue;
72
73     // Copy shape.
74     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
75     // Check that algo is done.
76     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
77       setError(anError);
78       return;
79     }
80
81     // Store result.
82     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
83     aResultBody->store(aCopyAlgo->shape());//, aCopyAlgo.shape());
84
85     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX);
86     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::EDGE);
87     aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::FACE);
88
89     setResult(aResultBody, aResultIndex);
90     ++aResultIndex;
91   }
92
93   removeResults(aResultIndex);
94 }
95
96 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
97 {
98   if (theID == METHOD() && myClearListOnTypeChange)
99     reflist(RECOVERED_ENTITIES())->clear();
100 }