Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Interface.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 email : webmaster.salome@opencascade.com<mailto: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
30 #include "ModelHighAPI_Selection.h"
31 //--------------------------------------------------------------------------------------
32 ModelHighAPI_Interface::ModelHighAPI_Interface(
33   const std::shared_ptr<ModelAPI_Feature> & theFeature)
34 : myFeature(theFeature)
35 {
36
37 }
38
39 ModelHighAPI_Interface::~ModelHighAPI_Interface()
40 {
41
42 }
43
44 //--------------------------------------------------------------------------------------
45 std::shared_ptr<ModelAPI_Feature> ModelHighAPI_Interface::feature() const
46 {
47   return myFeature;
48 }
49
50 std::shared_ptr<ModelHighAPI_Interface> ModelHighAPI_Interface::subFeature(const int theIndex) const
51 {
52   CompositeFeaturePtr aCompositeFeature =
53     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
54   if(!aCompositeFeature.get()) {
55     return InterfacePtr();
56   }
57
58   FeaturePtr aSubFeature = aCompositeFeature->subFeature(theIndex);
59   if(!aSubFeature.get()) {
60     return InterfacePtr();
61   }
62
63   return InterfacePtr(new ModelHighAPI_Interface(aSubFeature));
64 }
65
66 const std::string& ModelHighAPI_Interface::getKind() const
67 {
68   return feature()->getKind();
69 }
70
71 void ModelHighAPI_Interface::execute(bool isForce)
72 {
73   if (isForce) {
74     SessionPtr aMgr = ModelAPI_Session::get();
75     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
76     FeaturePtr aFeature = feature();
77     if(aFactory->validate(aFeature))
78       aFeature->execute();
79   }
80
81   Events_Loop* aLoop = Events_Loop::loop();
82   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
83   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
84   //aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
85   //aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
86 }
87
88 void ModelHighAPI_Interface::setName(const std::string& theName)
89 {
90   feature()->data()->setName(theName);
91 }
92
93 ModelHighAPI_Selection ModelHighAPI_Interface::result() const
94 {
95   const_cast<ModelHighAPI_Interface*>(this)->execute();
96
97   return ModelHighAPI_Selection(feature()->firstResult());
98 }
99
100 std::list<ModelHighAPI_Selection> ModelHighAPI_Interface::results() const
101 {
102   const_cast<ModelHighAPI_Interface*>(this)->execute();
103
104   std::list<ModelHighAPI_Selection> aSelectionList;
105
106   std::list<std::shared_ptr<ModelAPI_Result> > aResults = feature()->results();
107   for (auto it = aResults.begin(), end = aResults.end(); it != end; ++it) {
108     aSelectionList.push_back(ModelHighAPI_Selection(*it));
109   }
110
111   return aSelectionList;
112 }
113
114 std::shared_ptr<ModelAPI_Result> ModelHighAPI_Interface::defaultResult() const
115 {
116   const_cast<ModelHighAPI_Interface*>(this)->execute();
117
118   return feature()->lastResult();
119 }
120
121 void ModelHighAPI_Interface::throwException(const std::string & theDescription)
122 {
123   Events_InfoMessage("ModelHighAPI_Interface", theDescription).send();
124 }
125
126 const std::string& ModelHighAPI_Interface::attributeGetter(const std::string& theAttrName)
127 {
128   return myAttrGetter[theAttrName];
129 }