Salome HOME
bd87cd0e40ac30231357e3966ce5ba932d4b6f11
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Interface.cpp
1 // Copyright (C) 2014-2021  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 "ModelHighAPI_Interface.h"
21 //--------------------------------------------------------------------------------------
22 #include <Events_InfoMessage.h>
23
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Result.h>
30
31 #include "ModelHighAPI_Selection.h"
32 //--------------------------------------------------------------------------------------
33 ModelHighAPI_Interface::ModelHighAPI_Interface(
34   const std::shared_ptr<ModelAPI_Feature> & theFeature)
35 : myFeature(theFeature)
36 {
37
38 }
39
40 ModelHighAPI_Interface::~ModelHighAPI_Interface()
41 {
42
43 }
44
45 //--------------------------------------------------------------------------------------
46 std::shared_ptr<ModelAPI_Feature> ModelHighAPI_Interface::feature() const
47 {
48   return myFeature;
49 }
50
51 std::shared_ptr<ModelHighAPI_Interface> ModelHighAPI_Interface::subFeature(const int theIndex) const
52 {
53   CompositeFeaturePtr aCompositeFeature =
54     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
55   if(!aCompositeFeature.get()) {
56     return InterfacePtr();
57   }
58
59   FeaturePtr aSubFeature = aCompositeFeature->subFeature(theIndex);
60   if(!aSubFeature.get()) {
61     return InterfacePtr();
62   }
63
64   return InterfacePtr(new ModelHighAPI_Interface(aSubFeature));
65 }
66
67 const std::string& ModelHighAPI_Interface::getKind() const
68 {
69   return feature()->getKind();
70 }
71
72 void ModelHighAPI_Interface::execute(bool isForce)
73 {
74   if (isForce) {
75     SessionPtr aMgr = ModelAPI_Session::get();
76     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
77     FeaturePtr aFeature = feature();
78     if(aFactory->validate(aFeature))
79       aFeature->execute();
80   }
81
82   Events_Loop* aLoop = Events_Loop::loop();
83   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
84   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
85   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
86   //aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
87 }
88
89 void ModelHighAPI_Interface::setName(const std::wstring& theName)
90 {
91   if (feature().get() && feature()->data()->isValid())
92     feature()->data()->setName(theName);
93   else {
94     std::cout<<"Error: set name "<<theName.c_str()<<" for an invalid feature"<<std::endl;
95   }
96 }
97
98 std::wstring ModelHighAPI_Interface::name() const
99 {
100   return feature()->data()->name();
101 }
102
103 ModelHighAPI_Selection ModelHighAPI_Interface::result() const
104 {
105   std::list<ModelHighAPI_Selection> aResults = results();
106   if (aResults.empty())
107     return ModelHighAPI_Selection(std::shared_ptr<ModelAPI_Result>());
108   return aResults.front();
109 }
110
111 std::list<ModelHighAPI_Selection> ModelHighAPI_Interface::results() const
112 {
113   const_cast<ModelHighAPI_Interface*>(this)->execute();
114
115   std::list<ModelHighAPI_Selection> aSelectionList;
116
117   if (feature().get())  {
118     std::list<std::shared_ptr<ModelAPI_Result> > aResults = feature()->results();
119     for (auto it = aResults.begin(), end = aResults.end(); it != end; ++it) {
120       if (!(*it)->isDisabled())
121         aSelectionList.push_back(ModelHighAPI_Selection(*it));
122     }
123   }
124
125   return aSelectionList;
126 }
127
128 std::shared_ptr<ModelAPI_Result> ModelHighAPI_Interface::defaultResult() const
129 {
130   const_cast<ModelHighAPI_Interface*>(this)->execute();
131
132   return feature()->lastResult();
133 }
134
135 void ModelHighAPI_Interface::throwException(const std::string & theDescription)
136 {
137   Events_InfoMessage("ModelHighAPI_Interface", theDescription).send();
138 }
139
140 const std::string& ModelHighAPI_Interface::attributeGetter(const std::string& theAttrName)
141 {
142   return myAttrGetter[theAttrName];
143 }